/****************************************************************************************
Programa: Este programa permite una conexión por wifi a un servidor(broker) con protocilo MQTT
Permitiendo enviar y recibir indormación desde cualquier parte del mundo, el
ejemplo simula la temperatura con valores aleatorios, recomiendo descargar la aplicación
MQTT explorer para verificar el envio y recepción de datos por este medio, encuende un led remoro
Adicionalmente presenta la información en pantalla OLED
Realizado: Javier Saldarriaga Cano ([email protected])
Fecha: Mayo 11 de 2024 4:00 pm
Versión: 1.0
************************************************************************************/
#include <WiFi.h>
//#include <PubSubClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Inicializamos la pantalla OLED con la dirección I2C predeterminada (0x3C para la mayoría de los módulos SSD1306).
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const uint8_t wifi[] = {0x00, 0x0f, 0xf0, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x01, 0x8f, 0xf1, 0x80, 0x03, 0x3c, 0x3c, 0xc0, 0x02, 0x70, 0x0e, 0x40, 0x03, 0xcf, 0xf3, 0xc0, 0x01, 0x9c, 0x39, 0x80, 0x00, 0x31, 0x8c, 0x00, 0x00, 0x37, 0xec, 0x00, 0x00, 0x3c, 0x3c, 0x00, 0x00, 0x19, 0x98, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x1f, 0xf8, 0x7f, 0xfd, 0xbf, 0xfe, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc1, 0xb5, 0xbd, 0x83, 0xc1, 0xbd, 0xb1, 0x83, 0xc0, 0xfd, 0xbd, 0x83, 0xc0, 0xfd, 0xb9, 0x83, 0xc0, 0xdd, 0xb1, 0x83, 0xc0, 0xc8, 0xb1, 0x83, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xfe};
const uint8_t NoWifi[] = {0x00, 0x18, 0x18, 0x00, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x07, 0x1e, 0x78, 0xe0, 0x1f, 0x1c, 0x38, 0xf8, 0x3e, 0x18, 0x18, 0x7c, 0x7c, 0x00, 0x00, 0x3e, 0xf0, 0x00, 0x00, 0x0f, 0xe0, 0x1f, 0xf8, 0x07, 0xc0, 0x7f, 0xfe, 0x03, 0x01, 0xff, 0xff, 0x80, 0x03, 0xf0, 0x0f, 0xc0, 0x07, 0xc0, 0x03, 0xe0, 0x0f, 0x00, 0x00, 0xf0, 0x06, 0x00, 0x00, 0x60, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x7c, 0x3e, 0x00, 0x00, 0x70, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00};
#define FRAME_DELAY (42)
#define FRAME_WIDTH (32)
#define FRAME_HEIGHT (32)
#define FRAME_COUNT (sizeof(frames) / sizeof(frames[0]))
const byte PROGMEM frames[][128] = {
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,128,0,0,15,224,0,0,24,32,0,0,48,48,0,0,100,120,0,0,220,108,0,0,156,103,128,1,172,112,128,1,40,223,192,1,104,195,128,1,200,192,0,1,152,192,0,0,28,96,0,0,22,48,0,0,19,16,0,0,51,152,0,0,102,216,0,0,68,200,0,0,204,72,0,1,152,76,0,1,48,108,0,1,224,124,0,0,192,16,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,128,0,0,15,224,0,0,24,32,0,0,48,48,0,0,100,120,0,0,204,108,0,0,156,111,128,1,188,113,128,1,40,222,128,1,104,199,128,1,200,192,0,1,216,192,0,0,28,96,0,0,22,48,0,0,19,48,0,0,51,144,0,0,38,152,0,0,102,200,0,0,204,72,0,0,152,76,0,1,176,108,0,0,224,124,0,0,64,16,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,192,0,0,15,224,0,0,24,32,0,0,48,48,0,0,100,112,0,0,76,104,0,0,220,111,0,0,156,113,128,0,184,253,128,1,168,207,0,1,248,192,0,0,216,192,0,0,28,96,0,0,22,32,0,0,19,48,0,0,19,144,0,0,50,152,0,0,102,216,0,0,204,200,0,0,152,76,0,0,144,108,0,0,224,124,0,0,0,16,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,3,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,192,0,0,15,224,0,0,24,32,0,0,48,48,0,0,100,112,0,0,76,120,0,0,92,110,0,0,220,103,0,0,152,249,0,0,184,223,0,0,184,192,0,0,248,192,0,0,28,96,0,0,30,96,0,0,27,48,0,0,19,16,0,0,51,144,0,0,102,152,0,0,204,200,0,0,136,76,0,0,176,108,0,0,240,124,0,0,0,16,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,3,24,0,0,3,16,0,0,1,240,0,0,0,96,0,0,3,192,0,0,14,224,0,0,24,32,0,0,48,48,0,0,100,112,0,0,108,112,0,0,92,124,0,0,92,102,0,0,216,243,0,0,216,255,0,0,248,198,0,0,248,192,0,0,28,64,0,0,30,96,0,0,30,32,0,0,27,48,0,0,51,144,0,0,99,152,0,0,198,200,0,0,204,76,0,0,216,108,0,0,112,60,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,3,240,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,64,0,0,7,192,0,0,14,96,0,0,24,32,0,0,16,32,0,0,54,96,0,0,108,112,0,0,108,120,0,0,108,76,0,0,120,228,0,0,88,252,0,0,120,128,0,0,120,192,0,0,12,64,0,0,12,64,0,0,14,96,0,0,27,32,0,0,51,48,0,0,103,176,0,0,205,144,0,0,248,152,0,0,112,216,0,0,0,112,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,0,0,0,7,192,0,0,12,96,0,0,8,32,0,0,24,32,0,0,54,96,0,0,36,96,0,0,44,112,0,0,44,88,0,0,40,248,0,0,40,240,0,0,56,128,0,0,56,192,0,0,12,192,0,0,12,64,0,0,14,64,0,0,58,96,0,0,99,96,0,0,207,32,0,0,221,32,0,0,113,160,0,0,1,160,0,0,0,224,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,48,0,0,1,240,0,0,0,0,0,0,7,192,0,0,12,96,0,0,12,32,0,0,26,32,0,0,22,96,0,0,54,96,0,0,60,96,0,0,52,96,0,0,52,224,0,0,52,224,0,0,60,128,0,0,24,128,0,0,12,192,0,0,12,192,0,0,28,64,0,0,126,64,0,0,198,64,0,0,222,64,0,0,122,96,0,0,3,96,0,0,3,96,0,0,1,192,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,48,0,0,3,24,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,12,96,0,0,12,32,0,0,26,32,0,0,26,96,0,0,22,96,0,0,20,64,0,0,20,64,0,0,20,192,0,0,22,192,0,0,30,128,0,0,12,128,0,0,8,128,0,0,12,128,0,0,124,128,0,0,228,192,0,0,142,192,0,0,254,192,0,0,6,192,0,0,6,192,0,0,6,192,0,0,3,128,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,48,0,0,3,24,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,12,96,0,0,12,32,0,0,8,32,0,0,26,96,0,0,22,96,0,0,22,64,0,0,22,64,0,0,26,192,0,0,10,192,0,0,14,128,0,0,12,128,0,0,8,128,0,0,8,128,0,0,28,128,0,0,60,128,0,0,36,128,0,0,60,128,0,0,60,128,0,0,4,128,0,0,5,128,0,0,7,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,48,0,0,3,24,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,6,96,0,0,12,32,0,0,9,32,0,0,27,96,0,0,26,96,0,0,26,64,0,0,26,64,0,0,11,192,0,0,13,192,0,0,15,128,0,0,14,128,0,0,8,128,0,0,9,128,0,0,9,128,0,0,9,128,0,0,25,128,0,0,25,0,0,0,9,0,0,0,9,0,0,0,9,0,0,0,15,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,3,192,0,0,6,96,0,0,12,32,0,0,13,32,0,0,27,96,0,0,27,96,0,0,26,64,0,0,27,64,0,0,29,192,0,0,29,192,0,0,31,128,0,0,27,128,0,0,25,128,0,0,25,128,0,0,25,128,0,0,25,128,0,0,27,128,0,0,27,0,0,0,27,0,0,0,19,0,0,0,18,0,0,0,30,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,48,0,0,1,240,0,0,0,0,0,0,3,192,0,0,6,224,0,0,12,32,0,0,29,32,0,0,29,96,0,0,59,96,0,0,59,96,0,0,45,64,0,0,61,192,0,0,30,192,0,0,26,192,0,0,27,128,0,0,25,128,0,0,25,192,0,0,19,64,0,0,19,64,0,0,18,192,0,0,50,128,0,0,55,128,0,0,55,0,0,0,54,0,0,0,60,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,0,0,0,3,192,0,0,6,224,0,0,12,32,0,0,29,32,0,0,61,96,0,0,45,96,0,0,109,96,0,0,77,192,0,0,126,192,0,0,63,64,0,0,27,96,0,0,25,192,0,0,25,192,0,0,25,192,0,0,27,96,0,0,19,96,0,0,51,96,0,0,55,64,0,0,55,64,0,0,37,128,0,0,44,0,0,0,60,0,0,0,16,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,1,240,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,96,0,0,3,192,0,0,6,224,0,0,28,32,0,0,61,32,0,0,45,96,0,0,109,96,0,0,77,224,0,0,93,192,0,0,126,96,0,0,107,32,0,0,25,224,0,0,25,192,0,0,25,192,0,0,25,96,0,0,27,32,0,0,19,32,0,0,51,160,0,0,55,160,0,0,103,160,0,0,108,224,0,0,76,224,0,0,120,0,0,0,48,0,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,3,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,128,0,0,7,224,0,0,28,32,0,0,61,32,0,0,109,96,0,0,77,96,0,0,93,224,0,0,220,224,0,0,255,96,0,0,107,176,0,0,24,240,0,0,25,224,0,0,25,192,0,0,25,96,0,0,27,48,0,0,19,176,0,0,51,144,0,0,38,144,0,0,100,152,0,0,76,216,0,0,216,112,0,0,248,32,0,0,96,0,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,128,0,0,7,224,0,0,28,32,0,0,61,32,0,0,45,96,0,0,109,224,0,0,77,224,0,0,220,224,0,0,255,112,0,0,105,144,0,0,24,240,0,0,25,224,0,0,25,224,0,0,25,160,0,0,27,48,0,0,19,144,0,0,51,144,0,0,38,152,0,0,100,216,0,0,204,200,0,0,216,120,0,0,240,48,0,0,96,0,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,192,0,0,7,224,0,0,12,32,0,0,29,32,0,0,61,96,0,0,109,96,0,0,77,224,0,0,222,224,0,0,255,96,0,0,105,176,0,0,24,240,0,0,24,224,0,0,25,192,0,0,25,224,0,0,25,48,0,0,27,176,0,0,51,144,0,0,38,144,0,0,100,152,0,0,204,200,0,0,216,120,0,0,240,112,0,0,32,0,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,192,0,0,6,224,0,0,12,32,0,0,13,32,0,0,13,96,0,0,29,96,0,0,29,224,0,0,62,192,0,0,63,96,0,0,59,32,0,0,25,224,0,0,24,192,0,0,25,192,0,0,25,224,0,0,25,32,0,0,25,48,0,0,51,176,0,0,55,144,0,0,102,152,0,0,204,216,0,0,216,216,0,0,112,112,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,2,24,0,0,3,16,0,0,1,240,0,0,0,224,0,0,3,192,0,0,6,224,0,0,12,32,0,0,13,32,0,0,13,96,0,0,13,96,0,0,13,224,0,0,12,192,0,0,14,192,0,0,11,192,0,0,25,192,0,0,24,128,0,0,9,192,0,0,9,192,0,0,9,224,0,0,25,32,0,0,19,48,0,0,51,48,0,0,103,144,0,0,76,144,0,0,120,208,0,0,0,112,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,1,240,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,96,0,0,3,192,0,0,4,96,0,0,12,32,0,0,13,32,0,0,13,96,0,0,11,96,0,0,11,64,0,0,13,192,0,0,13,192,0,0,14,192,0,0,27,128,0,0,8,128,0,0,8,192,0,0,8,192,0,0,9,192,0,0,9,224,0,0,19,32,0,0,35,32,0,0,101,48,0,0,121,144,0,0,48,144,0,0,0,224,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,0,0,0,7,192,0,0,6,96,0,0,12,32,0,0,13,32,0,0,11,96,0,0,27,96,0,0,11,96,0,0,11,96,0,0,13,224,0,0,13,224,0,0,15,224,0,0,8,192,0,0,8,128,0,0,8,128,0,0,12,192,0,0,12,192,0,0,9,192,0,0,27,64,0,0,18,64,0,0,23,96,0,0,31,96,0,0,1,192,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,14,96,0,0,12,32,0,0,9,32,0,0,27,96,0,0,26,96,0,0,18,112,0,0,26,112,0,0,11,240,0,0,11,240,0,0,15,240,0,0,12,128,0,0,8,192,0,0,12,192,0,0,4,192,0,0,6,192,0,0,4,128,0,0,13,128,0,0,13,128,0,0,15,128,0,0,14,128,0,0,3,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,48,0,0,2,24,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,12,96,0,0,12,32,0,0,25,32,0,0,27,96,0,0,22,112,0,0,22,112,0,0,22,88,0,0,18,248,0,0,18,248,0,0,30,152,0,0,12,192,0,0,12,64,0,0,12,64,0,0,14,64,0,0,14,64,0,0,14,64,0,0,14,192,0,0,14,192,0,0,14,128,0,0,11,128,0,0,14,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,48,0,0,2,24,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,12,96,0,0,8,32,0,0,24,32,0,0,50,112,0,0,54,112,0,0,44,80,0,0,44,92,0,0,44,228,0,0,44,252,0,0,60,204,0,0,24,192,0,0,12,64,0,0,14,96,0,0,11,96,0,0,11,32,0,0,27,96,0,0,27,96,0,0,19,96,0,0,23,96,0,0,23,192,0,0,28,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,240,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,48,0,0,1,224,0,0,0,0,0,0,7,192,0,0,14,96,0,0,24,32,0,0,16,48,0,0,54,112,0,0,108,112,0,0,108,92,0,0,108,102,0,0,88,243,0,0,88,223,0,0,120,198,0,0,56,192,0,0,28,96,0,0,30,96,0,0,27,32,0,0,27,32,0,0,19,160,0,0,55,160,0,0,39,160,0,0,108,160,0,0,108,224,0,0,56,224,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,224,0,0,3,176,0,0,3,16,0,0,3,16,0,0,3,16,0,0,1,240,0,0,0,0,0,0,3,192,0,0,14,224,0,0,24,32,0,0,48,48,0,0,102,112,0,0,76,120,0,0,92,110,0,0,220,99,0,0,152,249,0,0,184,207,0,0,248,192,0,0,88,192,0,0,28,96,0,0,30,32,0,0,19,48,0,0,19,144,0,0,54,144,0,0,102,144,0,0,108,144,0,0,200,216,0,0,216,216,0,0,240,112,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,224,0,0,1,240,0,0,3,16,0,0,3,24,0,0,3,16,0,0,1,240,0,0,0,96,0,0,3,192,0,0,15,224,0,0,24,32,0,0,48,48,0,0,100,120,0,0,204,108,0,0,156,111,128,0,188,113,128,1,168,223,128,1,104,199,128,1,200,192,0,0,216,192,0,0,28,96,0,0,22,48,0,0,19,48,0,0,51,144,0,0,38,152,0,0,100,200,0,0,204,72,0,0,152,72,0,1,144,72,0,0,240,120,0,0,96,0,0,0,0,0,0}
};
int logo=0 ;
// Configuración de la red WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Configuración del servidor MQTT https://www.hivemq.com/mqtt/public-mqtt-broker/
//const char* mqtt_server = "broker.hivemq.com";
const char* mqtt_server = "test.mosquitto.org";
const int mqtt_port = 1883;
const char* mqtt_user = "";
const char* mqtt_password = "";
// ID único del cliente MQTT
const char* client_id = "esp32_temperatura_led2023";
// Tópico MQTT para enviar la temperatura simulada
//const char* temperatura_topic = "temperatura";
const char* temperatura_topic = "home/sensor/temperatura";
// Tópico MQTT para controlar el LED
const char* led_control_topic = "temperatura/led";
//const char* conta_topic = "temperatura/conta";
const char* conta_topic = "home/sensor/contador";
// Pin para la generación de valores de temperatura simulados
const int temperatura_pin = 34;
const int PinLED = 4;// Pin del LED
long lastMsg2 = 0;
int conta=0;
String Recibe="";
char mensaje[10];
WiFiClient espClient;
PubSubClient client(espClient); // instancia del cliente MQTT
void recuadro(String texto){
oled.clearDisplay(); // limpia pantalla
// ---- creamos recuadros ---
oled.drawRect(0, 0, 128, 15, 1);
oled.drawRect(0, 16, 128, 46, WHITE); // dibuja rectangulo
oled.drawRect(80, 16, 128, 46, WHITE);
// ---- Organizamos los textos en pantalla ---
oled.setTextSize(1);
oled.setCursor(5, 3);
oled.setTextColor(WHITE);
oled.print(texto); // titulo superior
}
void FUNC_cuadro(String texto,int conta,float temp,int hume , int logo, String Recibe){
recuadro(texto);
if ( logo==0){
int frame = 0;
for(int i=1;i<60;i++){
recuadro(texto);
oled.drawBitmap(90, 20, NoWifi, 32, 32, 1);
oled.drawBitmap(20, 20, frames[frame], FRAME_WIDTH, FRAME_HEIGHT, 1);
oled.display();
frame = (frame + 1) % FRAME_COUNT;
delay(FRAME_DELAY);
}
}else{
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo)
oled.setCursor (5, 20); // ubica cursor en coordenas 10,30
oled.setTextSize(1);
oled.print("Conta:");
oled.print(conta); // escribe valor de millis() dividido por 1000
oled.println("");
oled.print(" Temp:");
oled.print(temp);
oled.print(" C"); // escribe texto
oled.println("");
//oled.setCursor (5, 30);
oled.print(" Hume:");
oled.print(hume);
oled.println("");
oled.print("-------------");
oled.println("");
oled.print(" Recibe-> ");
oled.print(Recibe);
oled.drawBitmap(90, 20, wifi, 32, 32, 1);
}
oled.display();
}
void setup_wifi() {
delay(10);
Serial.println("Conectando a WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
FUNC_cuadro(" CONECTANDO WIFI",0,0,0,0,"");
Serial.println("Conectando a WiFi...");
}
Serial.println("Conectado a WiFi");
}
void reconnect() {
while (!client.connected()) {
FUNC_cuadro(" CONECTANDO MQTT",0,0,0,0,"");
Serial.println("Intentando conexión MQTT...");
if (client.connect(client_id, mqtt_user, mqtt_password)) {
Serial.println("Conectado al servidor MQTT");
// Suscribirse al tópico de control del LED al conectarse
client.subscribe(led_control_topic);
} else {
Serial.print("Error de conexión, rc=");
Serial.print(client.state());
Serial.println(" Intentando de nuevo en 5 segundos");
delay(5000);
}
}
}
void setup() {
Serial.begin(9600);
Wire.begin(); // inicializa bus I2C
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // inicializa pantalla con direccion 0x3C
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
pinMode(PinLED, OUTPUT);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Generar valor de temperatura simulada (entre 20 y 30 grados Celsius)
long now = millis();
if (now - lastMsg2 > 2000) {
lastMsg2 = now;
float temperatura_simulada = random(1000, 5000) / 100.0;
int Humedad = random(30, 80) ;
// Enviar valor de temperatura al servidor MQTT
char mensaje[10];
dtostrf(temperatura_simulada, 4, 2, mensaje);
client.publish(temperatura_topic, mensaje);
Serial.print("Temperatura enviada: ");
Serial.println(temperatura_simulada);
conta++;
dtostrf(conta, 4, 0, mensaje);
client.publish(conta_topic, mensaje);
FUNC_cuadro("INFORMACION SISTEMA",conta,temperatura_simulada,Humedad,1,Recibe);
}
//delay(5000); // Esperar 5 segundos antes de enviar el siguiente valor
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Mensaje recibido en el tópico: ");
Serial.println(topic);
String msg="" ;
for (int i = 0; i < length; i++) {
//Serial.print((char)payload[i]);
msg += (char)payload[i];
}
//********************* control de LED ********************
if (msg.substring(0,3) == "on1") { Serial.print("LAMPARA1 ON "); digitalWrite(PinLED, HIGH);Recibe="ON";}
if (msg.substring(0,3) == "of1") { Serial.print("LAMPARA1 Off "); digitalWrite(PinLED, LOW); Recibe="OF";}
Serial.print(msg.substring(0,3));
Serial.print("-----------------------");
Serial.print(msg.length());
}