OpenLab_Slave_master_perfect (copy) — ESP32 simulation

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

Components

Sketch code

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

// ==========================================
// WIFI AND MQTT CONFIGURATION
// ==========================================

const char* WIFI_SSID = "Velxio-GUEST";
const char* MQTT_BROKER = "broker.hivemq.com";
const int MQTT_PORT = 1883;

WiFiClient espClient;
PubSubClient mqttClient(espClient);

// ==========================================
// FAN DATA STORAGE
// ==========================================

String fanData[3][2];

bool fanReceived[3][2] = {
  {false, false},
  {false, false},
  {false, false}
};

// ==========================================
// SLAVE ON/OFF STATUS
// ==========================================

bool slaveEnabled[3] = {
  true, true, true
};

// ==========================================
// DISPLAY TIMING
// ==========================================

unsigned long lastDisplayTime = 0;

const unsigned long DISPLAY_INTERVAL = 5000;

int currentSlave = 0;

// ==========================================
// SERIAL COMMAND INPUT
// ==========================================

String commandBuffer = "";

// ==========================================
// MQTT TOPIC GENERATION
// ==========================================

String getTopic(int slave, int fan) {

  return "fanmonitor/slave" + String(slave) +
         "/fan" + String(fan);
}

String getControlTopic(int slave) {

  return "fanmonitor/slave" + String(slave) +
         "/control";
}

// ==========================================
// WIFI CONNECTION
// ==========================================

void connectWiFi() {

  Serial.println("Connecting to WiFi...");

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi Connected!");

  Serial.print("Master IP Address: ");
  Serial.println(WiFi.localIP());
}

// ==========================================
// 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);

  int slave = 0;
  int fan = 0;

  // Identify Slave and Fan

  for (int s = 1; s <= 3; s++) {

    for (int f = 1; f <= 2; f++) {

      if (receivedTopic == getTopic(s, f)) {

        slave = s;
        fan = f;
        break;
      }
    }

    if (slave != 0) {
      break;
    }
  }

  if (slave == 0 || fan == 0) {
    return;
  }

  // Store latest fan reading

  fanData[slave - 1][fan - 1] = message;

  fanReceived[slave - 1][fan - 1] = true;
}

// ==========================================
// SEND SLAVE ON/OFF COMMAND
// ==========================================

void sendSlaveCommand(int slave, bool turnOn) {

  if (!mqttClient.connected()) {

    Serial.println("MQTT not connected. Command not sent.");
    return;
  }

  String topic = getControlTopic(slave);

More projects by sravanalakshmigangisetty