Disaster Management System — ESP32 simulation

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

Components

Sketch code

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

/* =====================================================
   CHANGE ONLY THIS VALUE FOR EACH ESP32
   1 = FLOOD NODE
   2 = FIRE NODE
   3 = AIR POLLUTION NODE
   4 = GATEWAY
   ===================================================== */

#define NODE_TYPE 1


/* ================= WIFI ================= */

const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";


/* ================= MQTT ================= */

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


/* ================= DHT22 ================= */

#define DHT_PIN 4
#define DHT_TYPE DHT22

DHT dht(DHT_PIN, DHT_TYPE);


/* ================= FLOOD ================= */

#define TRIG_PIN 5
#define ECHO_PIN 18
#define RAIN_PIN 34


/* ================= FIRE ================= */

#define MQ2_PIN 34
#define FLAME_PIN 35


/* ================= AIR ================= */

#define MQ135_PIN 34
#define PM25_PIN 35


/* ================= MQTT OBJECTS ================= */

WiFiClient espClient;
PubSubClient mqtt(espClient);


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

void connectWiFi()
{
  Serial.println();
  Serial.print("Connecting to WiFi");

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

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

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


/* =====================================================
   MQTT CALLBACK
   Used by Gateway
   ===================================================== */

void mqttCallback(char* topic, byte* payload, unsigned int length)
{
  Serial.println();
  Serial.println("========== MQTT DATA ==========");

  Serial.print("Topic: ");
  Serial.println(topic);

  Serial.print("Message: ");

  for (unsigned int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }

  Serial.println();
  Serial.println("===============================");
}


/* =====================================================
   MQTT CONNECTION
   ===================================================== */

void connectMQTT()
{
  while (!mqtt.connected())
  {
    Serial.print("Connecting to MQTT...");

    String clientID;

    if (NODE_TYPE == 1)
      clientID = "SIH26178_Flood_Node";

    else if (NODE_TYPE == 2)
      clientID = "SIH26178_Fire_Node";

    else if (NODE_TYPE == 3)
      clientID = "SIH26178_Air_Node";

    else
      clientID = "SIH26178_Gateway";

    if (mqtt.connect(clientID.c_str()))
    {
      Serial.println("Connected!");

      /* Gateway subscribes to all nodes */

      if (NODE_TYPE == 4)
      {
        mqtt.subscribe("sih26178/flood");
        mqtt.subscribe("sih26178/fire");
        mqtt.subscribe("sih26178/air");

        Serial.println("Gateway subscribed to all topics");
      }
    }

    else
    {
      Serial.p

More projects by ividhya528