Skip to main content

Code Logic

The system monitors engine health by Calculating the vibration , temperature , RPM of Fuel Pump
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "ThingSpeak.h"

// -------- DISPLAY SETUP --------
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// -------- WIFI CREDENTIALS --------
char ssid[] = "A";              // Your network SSID
char pass[] = "12345678";       // Your network password
WiFiClient client;

// -------- THINGSPEAK CONFIG --------
unsigned long myChannelNumber = 3209793;
const char * myWriteAPIKey = "9PC614ZGGO6X53YY";

// -------- DIGITAL BUTTON PINS --------
#define BTN_PAGE1 15  // GPIO15 - Digital input
#define BTN_PAGE2 4   // GPIO4 - Digital input

// -------- BUZZER --------
#define BUZZER_PIN 2
#define BUZZER_DURATION 60000UL // 1 minute

int currentPage = 0;  // 0 = Home, 1 = Page1, 2 = Page2

// -------- SYSTEM PARAMETERS --------
int engineTemp = 3000;      
int pumpRPM = 22000;     
bool fuelFlowStable = true;
int vibrationLevel = 50;  // New parameter (0-100)

// -------- TIMING --------
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 3000;
unsigned long lastThingSpeakUpdate = 0;
const unsigned long thingSpeakInterval = 20000; // 20 seconds

// -------- BUZZER STATE --------
bool buzzerActive = false;
unsigned long buzzerStartTime = 0;

// -------- WIFI STATUS --------
bool wifiConnected = false;

void setup() {
  Serial.begin(115200);
  randomSeed(millis()); // Use millis for random seed

  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  
  // Configure button pins with pull-up resistors
  pinMode(BTN_PAGE1, INPUT_PULLUP);
  pinMode(BTN_PAGE2, INPUT_PULLUP);

  // Initialize Display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 allocation failed");
    while (1);
  }

  display.ssd1306_command(SSD1306_SETCONTRAST);
  display.ssd1306_command(0xFF);

  // Show connecting message
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("Connecting WiFi...");
  display.display();

  // Connect to WiFi
  WiFi.mode(WIFI_STA);
  connectWiFi();
  
  // Initialize ThingSpeak
  ThingSpeak.begin(client);

  showHome();
}

void loop() {
  // -------- WIFI CONNECTION CHECK --------
  if (WiFi.status() != WL_CONNECTED) {
    wifiConnected = false;
    connectWiFi();
  } else {
    wifiConnected = true;
  }

  // -------- PARAMETER UPDATE --------
  if (millis() - lastUpdate >= updateInterval) {
    lastUpdate = millis();
    updateParameters();

    // Refresh page display
    if (currentPage == 1) showPage1();
    else if (currentPage == 2) showPage2();
  }

  // -------- THINGSPEAK UPDATE --------
  if (wifiConnected && (millis() - lastThingSpeakUpdate >= thingSpeakInterval)) {
    lastThingSpeakUpdate = millis();
    sendToThingSpeak();
  }

  // -------- BUZZER TIMER --------
  if (buzzerActive) {
    if (millis() - buzzerStartTime >= BUZZER_DURATION) {
      analogWrite(BUZZER_PIN, 0); 
      buzzerActive = false;
    }
  }

  // -------- BUTTON HANDLING --------
  // Buttons are active LOW (pressed = LOW, released = HIGH)
  bool btn1 = !digitalRead(BTN_PAGE1);
  bool btn2 = !digitalRead(BTN_PAGE2);

  if (btn1 && btn2) {
    delay(150);
    return;
  }

  if (btn1 && currentPage != 1) {
    currentPage = 1;
    showPage1();
    delay(400);
  }
  else if (btn2 && currentPage != 2) {
    currentPage = 2;
    showPage2();
    delay(400);
  }
  else if (btn2 && currentPage == 2) {
    currentPage = 0;
    showHome();
    delay(400);
  }

  delay(150);
}

// -------- WIFI CONNECTION --------
void connectWiFi() {
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, pass);
  
  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 20) {
    Serial.print(".");
    delay(500);
    attempts++;
  }
  
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi Connected!");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nWiFi Connection Failed!");
  }
}

// -------- THINGSPEAK UPLOAD --------
void sendToThingSpeak() {
  Serial.println("Sending data to ThingSpeak...");
  
  // Set the fields with data
  ThingSpeak.setField(1, engineTemp);
  ThingSpeak.setField(2, pumpRPM);
  ThingSpeak.setField(3, fuelFlowStable ? 1 : 0);  // 1 = Stable, 0 = Unstable
  ThingSpeak.setField(4, vibrationLevel);
  
  // Write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  
  if (x == 200) {
    Serial.println("ThingSpeak update successful.");
  } else {
    Serial.println("Problem updating ThingSpeak. HTTP error code " + String(x));
  }
}

// -------- PARAMETER GENERATION --------
void updateParameters() {
  engineTemp = random(2800, 3401);
  pumpRPM = random(20000, 26001);
  vibrationLevel = random(30, 101);  // 30-100 range

  fuelFlowStable = !(engineTemp > 3200 || pumpRPM > 24000);

  Serial.print("TEMP: "); Serial.print(engineTemp);
  Serial.print(" | RPM: "); Serial.print(pumpRPM);
  Serial.print(" | FUEL: "); Serial.print(fuelFlowStable ? "STABLE" : "UNSTABLE");
  Serial.print(" | VIB: "); Serial.println(vibrationLevel);

  checkCriticalAndTriggerBuzzer();
}

// -------- CRITICAL CHECK --------
void checkCriticalAndTriggerBuzzer() {
  bool tempCritical = (engineTemp >= 3273);
  bool rpmCritical = (pumpRPM >= 25000);
  bool fuelCritical = (!fuelFlowStable);
  bool vibCritical = (vibrationLevel >= 85);

  bool systemCritical = tempCritical || rpmCritical || fuelCritical || vibCritical;

  if (systemCritical) {
    if (!buzzerActive) {
      analogWrite(BUZZER_PIN, 128);
      buzzerActive = true;
      buzzerStartTime = millis();
    }
  } else {
    analogWrite(BUZZER_PIN, 0);
    buzzerActive = false;
  }
}

// -------- DISPLAY PAGES --------
void showHome() {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("BUILD");
  display.println("CLUB");
  
  // WiFi indicator
  display.setTextSize(1);
  display.setCursor(90, 24);
  display.print(wifiConnected ? "WiFi" : "----");
  
  display.display();
}

void showPage1() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);

  display.println("ENGINE STATUS");
  display.print("TEMP: "); display.print(engineTemp); display.println(" C");
  display.print("RPM : "); display.println(pumpRPM);
  display.print("FUEL: "); display.println(fuelFlowStable ? "STABLE" : "UNSTBL");
  display.print("VIB : "); display.print(vibrationLevel); display.println("%");

  display.display();
}

void showPage2() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);

  bool tempCritical = (engineTemp >= 3273);
  bool rpmCritical = (pumpRPM >= 25000);
  bool fuelCritical = (!fuelFlowStable);
  bool vibCritical = (vibrationLevel >= 85);

  bool systemCritical = tempCritical || rpmCritical || fuelCritical || vibCritical;

  display.println("CREW ADVISORY");

  if (!systemCritical) {
    display.println("STATUS: NORMAL");
    display.println("- Monitor systems");
    display.println("- Maintain RPM");
  } else {
    display.println("STATUS: CRITICAL");
    if (tempCritical) display.println("- Reduce eng load");
    if (rpmCritical) display.println("- Throttle RPM");
    if (fuelCritical) display.println("- Check fuel");
    if (vibCritical) display.println("- High vibration");
  }

  display.display();
}