Disaster monitoring system — ESP32 simulation

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

Components

Sketch code

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

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

// =====================================================
// MQTT
// =====================================================
const char* MQTT_BROKER = "broker.hivemq.com";
const int MQTT_PORT = 1883;

WiFiClient espClient;
PubSubClient mqtt(espClient);

// =====================================================
// INPUT TOPICS FROM NODES
// =====================================================
const char* NODE1_INPUT = "enviromesh/node1";
const char* NODE2_INPUT = "enviromesh/node2";
const char* NODE3_INPUT = "enviromesh/node3";

// =====================================================
// OUTPUT TOPICS FOR NODE-RED
// =====================================================
const char* NODE1_OUTPUT = "enviromesh/dashboard/node1";
const char* NODE2_OUTPUT = "enviromesh/dashboard/node2";
const char* NODE3_OUTPUT = "enviromesh/dashboard/node3";
const char* SYSTEM_TOPIC = "enviromesh/dashboard/system";

// =====================================================
// 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];
  }

  Serial.println();
  Serial.println("================================");
  Serial.print("MQTT RECEIVED: ");
  Serial.println(topic);
  Serial.print("DATA: ");
  Serial.println(message);
  Serial.println("================================");

  // -----------------------------------------------
  // NODE 1
  // -----------------------------------------------
  if (strcmp(topic, NODE1_INPUT) == 0)
  {
    mqtt.publish(NODE1_OUTPUT, message.c_str());
    Serial.println("Forwarded -> Dashboard Node 1");
  }

  // -----------------------------------------------
  // NODE 2
  // -----------------------------------------------
  else if (strcmp(topic, NODE2_INPUT) == 0)
  {
    mqtt.publish(NODE2_OUTPUT, message.c_str());
    Serial.println("Forwarded -> Dashboard Node 2");
  }

  // -----------------------------------------------
  // NODE 3
  // -----------------------------------------------
  else if (strcmp(topic, NODE3_INPUT) == 0)
  {
    mqtt.publish(NODE3_OUTPUT, message.c_str());
    Serial.println("Forwarded -> Dashboard Node 3");
  }
}

// =====================================================
// WIFI CONNECTION
// =====================================================
void connectWiFi()
{
  Serial.println();
  Serial.print("Connecting to WiFi: ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

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

// =================================

More projects by sabharish3434