prueba exitosa matrix

This commit is contained in:
2021-04-13 22:52:45 -05:00
parent 6935387d06
commit 3ccbd309b2
4 changed files with 35 additions and 108 deletions

View File

@@ -1,17 +1,16 @@
#include <PubSubClient.h>
// MQTT: ID, server IP, port, username and password
const char *MQTT_CLIENT_ID = "matrix_client";
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_MENSAJE = "matrix/mensaje";
const char *MQTT_TOPIC_RGB = "matrix/rgb";
//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);
String getValue(String data, char separator, int index);
void callback(char *p_topic, byte *p_payload, unsigned int p_length)
{
@@ -22,44 +21,41 @@ void callback(char *p_topic, byte *p_payload, unsigned int p_length)
payload.concat((char)p_payload[i]);
}
if (String(MQTT_TOPIC_MENSAJE).equals(p_topic))
if (String(MQTT_TOPIC_FULL).equals(p_topic))
{
mensaje = String(payload);
DeserializationError err = deserializeJson(doc, String(payload));
if (err)
{
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err.c_str());
}
mensaje = doc["msg"].as<String>();
R = doc["R"];
G = doc["G"];
B = doc["B"];
texto = mensaje + " " + hora;
x = matrix.width();
longitud = texto.length() * 5 + 64;
}
if (String(MQTT_TOPIC_RGB).equals(p_topic))
{
String rgb = String(payload);
R = getValue(rgb,',',0).toInt();
G = getValue(rgb,',',1).toInt();
B = getValue(rgb,',',2).toInt();
texto = mensaje + " " + hora;
x = matrix.width();
longitud = texto.length() * 5 + 64;
}
}
void reconnect()
{
while (!client.connected())
{
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD))
String clientId = "matrix_client-" + String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD))
{
Serial.println("INFO: connected");
client.subscribe(MQTT_TOPIC_MENSAJE);
client.subscribe(MQTT_TOPIC_RGB);
client.subscribe(MQTT_TOPIC_FULL);
}
else
{
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
delay(5000);
}
}
}
@@ -70,19 +66,3 @@ void mqtt_init()
client.setCallback(callback);
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}