Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Respetando TODOS los conceptos vistos en clase, realizar un código en Arduino utilizando el ESP32 o ESP8266.
- Requerimientos MÍNIMOS:
- Blink que realice un destello de 100ms cada 2000ms.
- Mensajes por el puerto serie
- Cuando inicia el sistema: Apellido y nombre del programador.
- Cuando se conecta al WiFi, IP que se le fué asignado.
- Conectarse al Wifi que tenga salida a internet (puede ser Alumnos)
- Conectarse a un Broker MQTT según las indicaciones del docente
- IP: broker.hivemq.com
- Puerto: 1883
- Publicar en el tópico ites/`apellido`/ini
- Mensaje con el Nombre y Apellido
- MAC del dispositivo
- Suscribirse al tópico ites/‘apellido’/msg
- Mostrar en el puerto serie, los mensajes provenientes del tópico ites/‘apellido’/msg*/
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define APELLIDO "Soberon"
- #define NOMBRE "Joaquin"
- // === WIFI / MQTT ===
- #define WIFI_SSID "Alumnos"
- #define WIFI_PASS ""
- #define MQTT_HOST "broker.hivemq.com"
- #define MQTT_PORT 1883
- // === TOPICS REQUERIDOS ===
- #define TOPIC_PUB_INI "ites/soberon/ini"
- #define TOPIC_SUB_MSG "ites/soberon/msg"
- // (opcionales útiles)
- #define TOPIC_LWT "ites/soberon/esp8266/lwt"
- // === TIEMPOS (ms) ===
- #define REINTENTO_WIFI_MS 3000
- #define REINTENTO_MQTT_MS 3000
- // === BLINK: 100 ms ON cada 2000 ms ===
- #define BLINK_PERIOD_MS 2000UL
- #define BLINK_ON_MS 100UL
- // === LED integrado (ESP8266 activo LOW) ===
- #define PIN_LED LED_BUILTIN
- #define LED_ACTIVO_LOW 1
- #define CONFIG_LED pinMode(PIN_LED, OUTPUT)
- #define _LED_ON() digitalWrite(PIN_LED, (LED_ACTIVO_LOW ? LOW : HIGH))
- #define _LED_OFF() digitalWrite(PIN_LED, (LED_ACTIVO_LOW ? HIGH : LOW))
- #define AJUSTAR_LED(x) do { if (x) _LED_ON(); else _LED_OFF(); } while (0)
- // === Objetos del sistema ===
- WiFiClient NET;
- PubSubClient MQTT(NET);
- static bool wifiReported = false;
- static bool blinkOn = false;
- static unsigned long tBlink = 0;
- // === Declaraciones ===
- void TaskWiFi(void);
- void TaskMQTT(void);
- void TaskBlink(void);
- void OnMqttMessage(char* topic, byte* payload, unsigned int length);
- // Callback MQTT
- void OnMqttMessage(char* topic, byte* payload, unsigned int length) {
- String t = String(topic);
- String msg; msg.reserve(length);
- for (unsigned int i = 0; i < length; i++) msg += (char)payload[i];
- if (t == String(TOPIC_SUB_MSG)) {
- Serial.print("[MQTT] Mensaje en ");
- Serial.print(TOPIC_SUB_MSG);
- Serial.print(": ");
- Serial.println(msg);
- }
- }
- void TaskWiFi(void) {
- static unsigned long t_last = 0;
- if (WiFi.status() == WL_CONNECTED) {
- if (!wifiReported) {
- wifiReported = true;
- Serial.print("[WiFi] Conectado. IP asignada: ");
- Serial.println(WiFi.localIP());
- }
- return;
- }
- wifiReported = false;
- if (millis() - t_last < REINTENTO_WIFI_MS) return;
- t_last = millis();
- Serial.print("[WiFi] Conectando a SSID: ");
- Serial.println(WIFI_SSID);
- WiFi.mode(WIFI_STA);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- }
- void TaskMQTT(void) {
- static unsigned long t_last = 0;
- if (WiFi.status() != WL_CONNECTED) return;
- if (!MQTT.connected()) {
- if (millis() - t_last < REINTENTO_MQTT_MS) return;
- t_last = millis();
- String clientId = "esp8266-" + String(ESP.getChipId(), HEX);
- Serial.print("[MQTT] Conectando a ");
- Serial.print(MQTT_HOST);
- Serial.print(":");
- Serial.print(MQTT_PORT);
- Serial.print(" como ");
- Serial.println(clientId);
- if (MQTT.connect(clientId.c_str(),
- TOPIC_LWT, 1, true,
- "offline")) {
- MQTT.publish(TOPIC_LWT, "online", true);
- Serial.println("[MQTT] Conectado. LWT=online");
- MQTT.subscribe(TOPIC_SUB_MSG, 1);
- Serial.print("[MQTT] Suscripto a: ");
- Serial.println(TOPIC_SUB_MSG);
- String mac = WiFi.macAddress();
- String payload = String("Nombre y Apellido: ") + NOMBRE + " " + APELLIDO + " | MAC: " + mac;
- MQTT.publish(TOPIC_PUB_INI, payload.c_str(), false);
- Serial.print("[MQTT] Publicado en ");
- Serial.print(TOPIC_PUB_INI);
- Serial.print(": ");
- Serial.println(payload);
- } else {
- Serial.print("[MQTT] Falló conexión, rc=");
- Serial.println(MQTT.state());
- }
- return;
- }
- MQTT.loop();
- }
- void TaskBlink(void) {
- unsigned long now = millis();
- if (!blinkOn) {
- if (now - tBlink >= BLINK_PERIOD_MS) {
- blinkOn = true;
- tBlink = now;
- _LED_ON();
- }
- } else {
- if (now - tBlink >= BLINK_ON_MS) {
- blinkOn = false;
- _LED_OFF();
- }
- }
- }
- void setup() {
- CONFIG_LED;
- _LED_OFF();
- Serial.begin(115200);
- delay(50);
- Serial.println("=======================================");
- Serial.print("Programador: ");
- Serial.print(APELLIDO);
- Serial.print(", ");
- Serial.println(NOMBRE);
- Serial.println("Dispositivo: ESP8266");
- Serial.println("=======================================");
- // MQTT
- MQTT.setServer(MQTT_HOST, MQTT_PORT);
- MQTT.setCallback(OnMqttMessage);
- MQTT.setKeepAlive(30);
- MQTT.setBufferSize(1024);
- MQTT.setSocketTimeout(10);
- tBlink = millis();
- blinkOn = false;
- }
- void loop() {
- TaskWiFi();
- TaskMQTT();
- TaskBlink();
- }
Advertisement
Add Comment
Please, Sign In to add comment