OpenLab_Slave1_2Fans_Working (master failure detection) (copy) — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by sravanalakshmigangisetty.

Components

Sketch code

/************************************************************
   FAN MONITORING SYSTEM - MASTER ESP32
   Wi-Fi + MQTT (HiveMQ)
   
   Serial commands:
     MASTER OFF
     MASTER ON
     SLAVE1 OFF
     SLAVE1 ON
     SLAVE2 OFF
     SLAVE2 ON
     SLAVE3 OFF
     SLAVE3 ON
************************************************************/

#include <WiFi.h>
#include <PubSubClient.h>

// ---------------- WIFI / MQTT ----------------
const char* WIFI_SSID = "Velxio-GUEST";
const char* WIFI_PASSWORD = "";

const char* MQTT_SERVER = "broker.hivemq.com";
const int MQTT_PORT = 1883;

WiFiClient espClient;
PubSubClient mqtt(espClient);

// ---------------- TOPICS ----------------
const char* masterCommandTopic = "fanmonitor/master/command";
const char* masterHeartbeatTopic = "fanmonitor/master/heartbeat";
const char* systemReportTopic = "fanmonitor/system/report";

const char* slaveHeartbeatTopics[3] = {
  "fanmonitor/slave1/heartbeat",
  "fanmonitor/slave2/heartbeat",
  "fanmonitor/slave3/heartbeat"
};

const char* slaveFan1Topics[3] = {
  "fanmonitor/slave1/fan1",
  "fanmonitor/slave2/fan1",
  "fanmonitor/slave3/fan1"
};

const char* slaveFan2Topics[3] = {
  "fanmonitor/slave1/fan2",
  "fanmonitor/slave2/fan2",
  "fanmonitor/slave3/fan2"
};

const char* slaveControlTopics[3] = {
  "fanmonitor/slave1/control",
  "fanmonitor/slave2/control",
  "fanmonitor/slave3/control"
};

// ---------------- TIMING ----------------
const unsigned long HEARTBEAT_INTERVAL = 1500;
const unsigned long DISPLAY_INTERVAL = 5000;
const unsigned long MQTT_RETRY_INTERVAL = 3000;

// ---------------- SLAVE DATA ----------------
struct SlaveData {
  bool online = false;
  bool commandedOff = false;
  unsigned long lastHeartbeat = 0;

  float fan1RPM = 0;
  float fan1Vibration = 0;
  bool fan1SensorOK = false;

  float fan2RPM = 0;
  float fan2Vibration = 0;
  bool fan2SensorOK = false;
};

SlaveData slaves[3];

bool masterDisabled = false;

unsigned long lastHeartbeatPublish = 0;
unsigned long lastDisplay = 0;
unsigned long lastMQTTRetry = 0;

// ---------------- MQTT CALLBACK ----------------
void mqttCallback(char* topic, byte* payload, unsigned int length) {
  String message;
  for (unsigned int i = 0; i < length; i++) {
    message += (char)payload[i];
  }

  String receivedTopic = String(topic);

  for (int i = 0; i < 3; i++) {

    if (receivedTopic == slaveHeartbeatTopics[i]) {
      if (message == "1" || message == "ONLINE") {
        slaves[i].online = true;
        slaves[i].commandedOff = false;
        slaves[i].lastHeartbeat = millis();
      }
      else if (message == "OFFLINE") {
        slaves[i].online = false;
        slaves[i].commandedOff = true;
      }
    }

    if (receivedTopic == slaveFan1Topics[i]) {
      int firstComma = message.indexOf(',');
      int secondComma = message.indexOf(',', firstComma + 1);

      if (firstComma > 0 && secondComma > firstComma) {
        slaves[i].fan1RPM = message.substring(0, firstComma).toFloat();
        slaves[i].fan1V

More projects by sravanalakshmigangisetty