interacao — ESP32 simulation

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

Components

Sketch code

// ESP32 + WiFi + MQTT (PubSubClient) + LCD I2C
// Joins the emulator AP "Velxio-GUEST", connects to a public MQTT broker,
// subscribes to a topic, controls GPIO4 (LIGAR/DESLIGAR LAMPADA) and
// exibe no LCD I2C a mensagem recebida.
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const char* WIFI_SSID   = "Velxio-GUEST";   // open AP the emulator advertises
const char* MQTT_BROKER = "broker.hivemq.com";
const int   MQTT_PORT   = 1883;
const int   LED         = 4;

// Ajuste o endereço I2C (0x27 ou 0x3F são os mais comuns) e as colunas/linhas do seu LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

WiFiClient net;
PubSubClient mqtt(net);
String topic;  // unique per board so two ESP32s don't collide

void showOnLCD(const String& msg) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("MQTT recebido:");
  lcd.setCursor(0, 1);
  // Trunca para caber na segunda linha do LCD (16 chars)
  lcd.print(msg.substring(0, 16));
}

void onMessage(char* t, byte* payload, unsigned int len) {
  String m;
  for (unsigned int i = 0; i < len; i++) m += (char)payload[i];
  Serial.printf("RX [%s]: %s\n", t, m.c_str());

  showOnLCD(m);  // sempre exibe no LCD o texto recebido do tópico inscrito

  if (m == "LIGAR LAMPADA") {
    digitalWrite(LED, HIGH);
    Serial.println("GPIO4 -> HIGH (lampada ligada)");
  } else if (m == "DESLIGAR LAMPADA") {
    digitalWrite(LED, LOW);
    Serial.println("GPIO4 -> LOW (lampada desligada)");
  } else {
    Serial.println("Mensagem nao reconhecida");
  }
}

void connectWiFi() {
  Serial.printf("WiFi: joining %s ...\n", WIFI_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED) { delay(300); Serial.print("."); }
  Serial.printf("\nWiFi connected, IP %s\n", WiFi.localIP().toString().c_str());
}

void connectMQTT() {
  mqtt.setServer(MQTT_BROKER, MQTT_PORT);
  mqtt.setCallback(onMessage);
  while (!mqtt.connected()) {
    String cid = "velxio-" + String((uint32_t)ESP.getEfuseMac(), HEX);
    Serial.printf("MQTT: connecting to %s as %s ...\n", MQTT_BROKER, cid.c_str());
    if (mqtt.connect(cid.c_str())) {
      Serial.println("MQTT connected");
      mqtt.subscribe(topic.c_str());
      Serial.printf("Subscribed to %s\n", topic.c_str());
    } else {
      Serial.printf("MQTT failed, rc=%d, retry in 2s\n", mqtt.state());
      delay(2000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);

  Wire.begin();          // usa os pinos I2C padrão do ESP32 (SDA=21, SCL=22)
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Iniciando...");

  delay(500);
  topic = "velxio/demo/";
  connectWiFi();
  connectMQTT();

  lcd.clear();
  lcd.print("Aguardando MQTT");
}

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

More projects by arquiteturadecomputadores1