#include <WiFi.h>
#include "ThingSpeak.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ================= WIFI =================
char ssid[] = "A";
char pass[] = "12345678";
WiFiClient client;
unsigned long myChannelNumber = 3209793;
const char * myWriteAPIKey = "9PC614ZGGO6X53YY";
// ================= OLED =================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// ================= BUTTONS =================
#define BTN_PAGE 32
#define BTN_HOME 33
// ================= BUZZER =================
#define BUZZER_PIN 25
#define BUZZER_DURATION 60000UL
// ================= PARAMETERS =================
int engineTemp = 3000;
int pumpRPM = 22000;
bool fuelFlowStable = true;
int vibration = 0;
// ================= STATE =================
int currentPage = 0; // 0=Home,1=Status,2=Advisory
bool buzzerActive = false;
unsigned long buzzerStartTime = 0;
// ================= TIMING =================
unsigned long lastUpdate = 0;
unsigned long lastThingSpeakUpdate = 0;
const unsigned long updateInterval = 3000;
const unsigned long thingSpeakInterval = 20000;
// =====================================================
void setup() {
Serial.begin(115200);
randomSeed(analogRead(34));
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
pinMode(BTN_PAGE, INPUT_PULLUP);
pinMode(BTN_HOME, INPUT_PULLUP);
// OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED failed");
while (1);
}
display.clearDisplay();
display.display();
// WiFi
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
showHome();
}
// =====================================================
void loop() {
// -------- SENSOR UPDATE --------
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
updateParameters();
if (currentPage == 1) showStatus();
else if (currentPage == 2) showAdvisory();
}
// -------- THINGSPEAK --------
if (millis() - lastThingSpeakUpdate >= thingSpeakInterval) {
lastThingSpeakUpdate = millis();
sendToThingSpeak();
}
// -------- BUZZER TIMER --------
if (buzzerActive && millis() - buzzerStartTime >= BUZZER_DURATION) {
digitalWrite(BUZZER_PIN, LOW);
buzzerActive = false;
}
// -------- BUTTONS --------
if (digitalRead(BTN_PAGE) == LOW) {
currentPage++;
if (currentPage > 2) currentPage = 1;
delay(300);
}
if (digitalRead(BTN_HOME) == LOW) {
currentPage = 0;
showHome();
delay(300);
}
}
// =====================================================
void updateParameters() {
engineTemp = random(2800, 3401);
pumpRPM = random(20000, 26001);
vibration = random(0, 101);
fuelFlowStable = !(engineTemp > 3200 || pumpRPM > 24000);
Serial.print("TEMP: "); Serial.print(engineTemp);
Serial.print(" | RPM: "); Serial.print(pumpRPM);
Serial.print(" | FUEL: "); Serial.print(fuelFlowStable);
Serial.print(" | VIB: "); Serial.println(vibration);
checkCritical();
}
// =====================================================
void checkCritical() {
bool tempCritical = (engineTemp >= 3273);
bool rpmCritical = (pumpRPM >= 25000);
bool fuelCritical = (!fuelFlowStable);
if (tempCritical || rpmCritical || fuelCritical) {
if (!buzzerActive) {
digitalWrite(BUZZER_PIN, HIGH);
buzzerActive = true;
buzzerStartTime = millis();
}
} else {
digitalWrite(BUZZER_PIN, LOW);
buzzerActive = false;
}
}
// =====================================================
void sendToThingSpeak() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.println("Connecting WiFi...");
return;
}
ThingSpeak.setField(1, engineTemp);
ThingSpeak.setField(2, pumpRPM);
ThingSpeak.setField(3, fuelFlowStable ? 1 : 0);
ThingSpeak.setField(4, vibration);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200)
Serial.println("ThingSpeak Updated");
else
Serial.println("ThingSpeak Error: " + String(x));
}
// =====================================================
// OLED PAGES
void showHome() {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 8);
display.println("BUILD CLUB");
display.display();
}
void showStatus() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("ENGINE STATUS");
display.print("TEMP: "); display.println(engineTemp);
display.print("RPM : "); display.println(pumpRPM);
display.print("FUEL: ");
display.println(fuelFlowStable ? "STABLE" : "UNSTABLE");
display.display();
}
void showAdvisory() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("CREW ADVISORY");
if (fuelFlowStable) {
display.println("STATUS: NORMAL");
display.println("- Monitor systems");
} else {
display.println("STATUS: CRITICAL");
display.println("- Reduce load");
display.println("- Check fuel");
}
display.display();
}