Files
microcontroladores/ESP8266/MatrixLedsRGB/src/mqtt.hpp
2021-09-19 18:39:08 -05:00

76 lines
1.7 KiB
C++

#include <PubSubClient.h>
// MQTT: ID, server IP, port, username and password
//const String MQTT_CLIENT_ID = "matrix_client_" + String(random(0xffff), HEX);
const char *MQTT_SERVER_IP = "192.168.1.105";
const uint16_t MQTT_SERVER_PORT = 1883;
const char *MQTT_USER = "mdchaparror";
const char *MQTT_PASSWORD = "un80187452";
const char *MQTT_TOPIC_FULL = "matrix/full";
DynamicJsonDocument doc(300);
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void callback(char *p_topic, byte *p_payload, unsigned int p_length)
{
// concat the payload into a string
String payload;
for (uint8_t i = 0; i < p_length; i++)
{
payload.concat((char)p_payload[i]);
}
if (String(MQTT_TOPIC_FULL).equals(p_topic))
{
yield();
deserializeJson(doc, String(payload));
yield();
mensaje = doc["msg"].as<String>();
R = doc["R"];
G = doc["G"];
B = doc["B"];
matrix_speed = doc["speed"].as<int>();
if(matrix_speed<50)
matrix_speed=50;
texto = mensaje + " " + hora;
x = matrix.width();
longitud = texto.length() * 5 + 64;
}
}
void reconnect()
{
while (!client.connected())
{
String clientId = "matrix_client-" + String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD))
{
client.subscribe(MQTT_TOPIC_FULL);
}
else
{
mensaje = "ERROR AL CONECTAR SERVIDOR MQTT";
texto = mensaje + " " + hora;
x = matrix.width();
longitud = texto.length() * 5 + 64;
debug("Error conexion MQTT");
yield();
delay(5000);
yield();
}
}
}
void mqtt_init()
{
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}