prueba exitosa matrix
This commit is contained in:
@@ -19,9 +19,8 @@ lib_deps =
|
|||||||
arduino-libraries/NTPClient@^3.1.0
|
arduino-libraries/NTPClient@^3.1.0
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
upload_speed = 256000
|
upload_speed = 256000
|
||||||
monitor_filters = colorize
|
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 192.168.1.104
|
upload_port = 192.168.1.108
|
||||||
upload_flags =
|
upload_flags =
|
||||||
--port=8266
|
--port=8266
|
||||||
--auth=un260874
|
--auth=un260874
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
#include <WiFiUdp.h>
|
#include <WiFiUdp.h>
|
||||||
//#include <TaskScheduler.h>
|
|
||||||
|
|
||||||
WiFiUDP ntpUDP;
|
WiFiUDP ntpUDP;
|
||||||
NTPClient timeClient(ntpUDP, "0.south-america.pool.ntp.org", -18000, 6000);
|
NTPClient timeClient(ntpUDP, "0.south-america.pool.ntp.org", -18000, 6000);
|
||||||
@@ -18,16 +16,6 @@ const char *password = "un260874";
|
|||||||
long timeAnterior;
|
long timeAnterior;
|
||||||
long matrix_time;
|
long matrix_time;
|
||||||
|
|
||||||
|
|
||||||
// void t1Callback();
|
|
||||||
// void t2Callback();
|
|
||||||
// void serialEvent();
|
|
||||||
|
|
||||||
// Task t1(120, TASK_FOREVER, &t1Callback);
|
|
||||||
// Task t2(100, TASK_FOREVER, &t2Callback);
|
|
||||||
|
|
||||||
// Scheduler runner;
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -46,54 +34,17 @@ void setup()
|
|||||||
|
|
||||||
timeClient.begin();
|
timeClient.begin();
|
||||||
matrix_init();
|
matrix_init();
|
||||||
|
|
||||||
|
|
||||||
// runner.init();
|
|
||||||
// runner.addTask(t1);
|
|
||||||
// runner.addTask(t2);
|
|
||||||
// delay(1000);
|
|
||||||
// t1.enable();
|
|
||||||
// t2.enable();
|
|
||||||
}
|
}
|
||||||
// void loop()
|
|
||||||
// {
|
|
||||||
// runner.execute();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// void t1Callback()
|
void loop()
|
||||||
// {
|
{
|
||||||
// ArduinoOTA.handle();
|
|
||||||
// show_matrix();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// void t2Callback()
|
|
||||||
// {
|
|
||||||
|
|
||||||
// if ((millis() - timeAnterior) > 1000)
|
|
||||||
// {
|
|
||||||
// timeAnterior = millis();
|
|
||||||
// if (!client.connected())
|
|
||||||
// {
|
|
||||||
// reconnect();
|
|
||||||
// }
|
|
||||||
// timeClient.update(); //sincronizamos con el server NTP
|
|
||||||
// hora = timeClient.getFormattedTime();
|
|
||||||
// texto = mensaje + " " + hora;
|
|
||||||
// longitud = longitud = texto.length() * 5 + 64;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// client.loop();
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
void loop(){
|
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
if((millis() - matrix_time) > 100){
|
if ((millis() - matrix_time) > 150)
|
||||||
|
{
|
||||||
matrix_time = millis();
|
matrix_time = millis();
|
||||||
show_matrix();
|
show_matrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ((millis() - timeAnterior) > 1000)
|
if ((millis() - timeAnterior) > 1000)
|
||||||
{
|
{
|
||||||
timeAnterior = millis();
|
timeAnterior = millis();
|
||||||
@@ -104,8 +55,9 @@ void loop(){
|
|||||||
timeClient.update(); //sincronizamos con el server NTP
|
timeClient.update(); //sincronizamos con el server NTP
|
||||||
hora = timeClient.getFormattedTime();
|
hora = timeClient.getFormattedTime();
|
||||||
texto = mensaje + " " + hora;
|
texto = mensaje + " " + hora;
|
||||||
longitud = texto.length() * 5 + 64;
|
longitud = texto.length() * 5 + 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.loop();
|
client.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <Adafruit_I2CDevice.h>
|
||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <Adafruit_NeoMatrix.h>
|
#include <Adafruit_NeoMatrix.h>
|
||||||
#include <Adafruit_NeoPixel.h>
|
#include <Adafruit_NeoPixel.h>
|
||||||
@@ -17,11 +18,6 @@ int x = matrix.width();
|
|||||||
int longitud = 0;
|
int longitud = 0;
|
||||||
int pass = 0;
|
int pass = 0;
|
||||||
|
|
||||||
const uint16_t colors[] = {
|
|
||||||
matrix.Color(255, 102, 0),
|
|
||||||
matrix.Color(255, 204, 0),
|
|
||||||
matrix.Color(128, 0, 128),
|
|
||||||
matrix.Color(255, 0, 255)};
|
|
||||||
|
|
||||||
void matrix_init()
|
void matrix_init()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
// MQTT: ID, server IP, port, username and password
|
// 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;
|
WiFiClient wifiClient;
|
||||||
PubSubClient client(wifiClient);
|
PubSubClient client(wifiClient);
|
||||||
String getValue(String data, char separator, int index);
|
|
||||||
|
|
||||||
void callback(char *p_topic, byte *p_payload, unsigned int p_length)
|
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]);
|
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;
|
texto = mensaje + " " + hora;
|
||||||
x = matrix.width();
|
x = matrix.width();
|
||||||
longitud = texto.length() * 5 + 64;
|
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()
|
void reconnect()
|
||||||
{
|
{
|
||||||
while (!client.connected())
|
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");
|
Serial.println("INFO: connected");
|
||||||
client.subscribe(MQTT_TOPIC_MENSAJE);
|
client.subscribe(MQTT_TOPIC_FULL);
|
||||||
client.subscribe(MQTT_TOPIC_RGB);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.print("ERROR: failed, rc=");
|
Serial.print("ERROR: failed, rc=");
|
||||||
Serial.print(client.state());
|
Serial.print(client.state());
|
||||||
Serial.println("DEBUG: try again in 5 seconds");
|
Serial.println("DEBUG: try again in 5 seconds");
|
||||||
|
delay(5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,19 +66,3 @@ void mqtt_init()
|
|||||||
client.setCallback(callback);
|
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]) : "";
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user