7.- App on/off LED12 and LED14 of ESP32. App checks status LED12 and LED14.
p117B_mqtt_Extension_Btn_LED.aia (74.8 KB)
- App Publish boton12 and boton14, and on/off LED12 and LED14
- ESP32 Subscripted to boton12 and boton14 Publish status LEDs
Broker: broker.hivemq.com
Port: 1883
App Inventor Publish: juan/boton12
App Inventor Publish: juan/boton14
App Inventor Subscribe: juan/led12_status
App Inventor Subscribe: juan/led14_status
App Inventor Subscribe: juan/led12_led14_status
ESP32 Publish: juan/led12_status
ESP32 Publish: juan/led14_status
ESP32 Publish: juan/led12_led14_status
ESP32 Subscribe: juan/boton12
ESP32 Subscribe: juan/boton14
// Juan A. Villalpando.
// http://kio4.com/arduino/117_Wemos_MQTT.htm
//#include <ESP8266WiFi.h> // Para el ESP8266
#include <WiFi.h> // Para el ESP32
WiFiClient WIFI_CLIENT;
#include <PubSubClient.h>
PubSubClient MQTT_CLIENT;
const char* ssid = "Nombre_Red_WiFi";
const char* password = "Clave_Wifi";
#define LED12 12 // LED12 ESP32
#define LED14 14 // LED14 ESP32
String LED12_status = "- ? -";
String LED14_status = "- ? -";
String LED12_LED14_status = "- ? -";
void setup() {
pinMode(LED12, OUTPUT);
pinMode(LED14, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting with ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi conected. IP: ");
Serial.println(WiFi.localIP());
// Setting Callback.
MQTT_CLIENT.setCallback(callback);
}
// What to do when it receives the data.
void callback(char* recibido, byte* payload, unsigned int length) {
Serial.print("Message received: ");
Serial.print(recibido);
Serial.print(" ");
for (int i=0;i<length;i++) {
char receivedChar = (char)payload[i];
Serial.println(receivedChar);
if (receivedChar == '1') {digitalWrite(LED12, HIGH);}
if (receivedChar == '2') {digitalWrite(LED12, LOW);}
if (receivedChar == '3') {digitalWrite(LED14, HIGH);}
if (receivedChar == '4') {digitalWrite(LED14, LOW);}
if (digitalRead(LED12) == HIGH) {LED12_status = "LED12 ON";} else {LED12_status = "LED12 OFF";}
if (digitalRead(LED14) == HIGH) {LED14_status = "LED14 ON";} else {LED14_status = "LED14 OFF";}
LED12_LED14_status = LED12_status + " and " + LED14_status;
}
}
void loop() {
if (!MQTT_CLIENT.connected()) {
reconnect();
}
// PUBLISH topic.
// Convierte el entero a char. Debe ser char.
char led12_st[10];
char led14_st[10];
char led12_led14_st[25];
LED12_status.toCharArray(led12_st, 10);
LED14_status.toCharArray(led14_st, 10);
LED12_LED14_status.toCharArray(led12_led14_st, 25);
MQTT_CLIENT.publish("juan/led12_status", led12_st);
delay(1000);
MQTT_CLIENT.publish("juan/led14_status", led14_st);
delay(1000);
MQTT_CLIENT.publish("juan/led12_led14_status", led12_led14_st);
delay(1000);
MQTT_CLIENT.loop(); // Check Subscription.
}
// Reconecta con MQTT broker
void reconnect() {
MQTT_CLIENT.setServer("broker.hivemq.com", 1883);
//MQTT_CLIENT.setServer("mqtt.eclipse.org", 1883);
MQTT_CLIENT.setClient(WIFI_CLIENT);
// Trying connect with broker.
while (!MQTT_CLIENT.connected()) {
Serial.println("Trying to connect with Broker MQTT.");
MQTT_CLIENT.connect("JuanAntonio"); // it isn't necessary..
MQTT_CLIENT.subscribe("juan/boton12"); // HERE SUBSCRIBE.
MQTT_CLIENT.subscribe("juan/boton14"); // HERE SUBSCRIBE.
// Wait to try to reconnect again...
delay(3000);
}
Serial.println("Conectado a MQTT.");
}