CALL — ESP32 simulation

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

Components

Sketch code

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

// ESP2 - BOTTOM BOARD
#define DESK_ID 2

const int BUTTON_PIN = 18;
const int LED_PIN = 21;

const char* WIFI_SSID = "Velxio-GUEST";

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

// MUST MATCH ESP1
const char* TOPIC_ROOT =
  "deskcaller/demo/office-test-853172";

// Unique ID for ESP2
const char* CLIENT_ID =
  "velxio-office-853172-ESP2";

const unsigned long BLINK_TIME = 5000;
const unsigned long BLINK_INTERVAL = 250;
const unsigned long DEBOUNCE_TIME = 30;

WiFiClient wifi;
PubSubClient mqtt(wifi);

String receiveTopic;
String sendTopic;

bool blinking = false;
unsigned long blinkStart = 0;

bool lastRaw = HIGH;
bool stableButton = HIGH;
bool armed = false;
unsigned long lastChange = 0;

void onMessage(char* topic, byte* payload,
               unsigned int length) {
  String msg;

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

  Serial.print("ESP2 received: ");
  Serial.println(msg);

  if (String(topic) == receiveTopic &&
      msg == "CALL") {
    blinking = true;
    blinkStart = millis();
    Serial.println("ESP2 INCOMING CALL - LED BLINKING");
  }
}

void connectWiFi() {
  if (WiFi.status() == WL_CONNECTED) return;

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

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

  Serial.println("\nESP2 WiFi connected");
}

void connectMQTT() {
  mqtt.setServer(MQTT_SERVER, MQTT_PORT);
  mqtt.setCallback(onMessage);
  mqtt.setKeepAlive(30);

  while (!mqtt.connected()) {
    Serial.println("ESP2 connecting MQTT");

    if (mqtt.connect(CLIENT_ID)) {
      Serial.println("ESP2 MQTT CONNECTED");

      bool ok = mqtt.subscribe(receiveTopic.c_str());
      Serial.println(ok ? "ESP2 SUBSCRIBED"
                        : "ESP2 SUBSCRIBE FAILED");
    } else {
      Serial.print("ESP2 MQTT error: ");
      Serial.println(mqtt.state());
      delay(2000);
    }
  }
}

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  receiveTopic = String(TOPIC_ROOT) + "/to2";
  sendTopic = String(TOPIC_ROOT) + "/to1";

  lastRaw = digitalRead(BUTTON_PIN);
  stableButton = lastRaw;
  lastChange = millis();

  connectWiFi();
  connectMQTT();

  Serial.println("ESP2 READY");
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();
  }

  if (!mqtt.connected()) {
    connectMQTT();
  }

  mqtt.loop();

  unsigned long now = millis();
  bool raw = digitalRead(BUTTON_PIN);

  if (raw != lastRaw) {
    lastChange = now;
  }

  if (now - lastChange >= DEBOUNCE_TIME &&
      raw != stableButton) {

    stableButton = raw;

    if (stableButton == HIGH) {
      armed = true;
    }

    if (stableButton == LOW && armed) {
      armed = false;

      Serial.println("ESP2 BUTTON PRESSED");

      if (mqtt.publish(sendTopic.c_str(), "CALL")) {
        Se