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>

// ============================================================
// WIFI
// ============================================================

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

// ============================================================
// MQTT
// ============================================================

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

const char* GATEWAY_TOPIC =
  "sih26178/vidhya/gateway/telemetry";

// ============================================================
// WIFI + MQTT
// ============================================================

WiFiClient espClient;
PubSubClient mqttClient(espClient);

unsigned long lastPublish = 0;


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

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

  WiFi.mode(WIFI_STA);

  WiFi.begin(
    WIFI_SSID,
    WIFI_PASSWORD
  );

  int count = 0;

  while (
    WiFi.status() != WL_CONNECTED &&
    count < 30
  )
  {
    delay(500);
    Serial.print(".");
    count++;
  }

  Serial.println();

  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("WiFi CONNECTED");

    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
  }
  else
  {
    Serial.println("WiFi CONNECTION FAILED");
  }
}


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

void connectMQTT()
{
  while (!mqttClient.connected())
  {
    Serial.println();
    Serial.println("Connecting to HiveMQ...");

    String clientID = "SIH26178_GATEWAY_";
    clientID += String(random(1000, 9999));

    Serial.print("Client ID: ");
    Serial.println(clientID);

    if (
      mqttClient.connect(
        clientID.c_str()
      )
    )
    {
      Serial.println("MQTT CONNECTED!");

      Serial.print("Broker: ");
      Serial.println(MQTT_SERVER);

      Serial.print("Port: ");
      Serial.println(MQTT_PORT);
    }
    else
    {
      Serial.print(
        "MQTT FAILED, state = "
      );

      Serial.println(
        mqttClient.state()
      );

      delay(3000);
    }
  }
}


// ============================================================
// SETUP
// ============================================================

void setup()
{
  Serial.begin(115200);

  delay(1000);

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

  Serial.println(
    "     SIH26178 MQTT GATEWAY"
  );

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

  // WiFi

  connectWiFi();

  // MQTT server

  mqttClient.setServer(
    MQTT_SERVER,
    MQTT_PORT
  );

  // Connect MQTT

  connectMQTT();

  Serial.println();
  Serial.println(
    "Gateway ready."
  );
}


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

More projects by ividhya528