first_commit
This commit is contained in:
111
ESP8266/MatrixLedsRGB/src/main.cpp
Normal file
111
ESP8266/MatrixLedsRGB/src/main.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
//#include <TaskScheduler.h>
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "0.south-america.pool.ntp.org", -18000, 6000);
|
||||
|
||||
#include "matrix.hpp"
|
||||
#include "mqtt.hpp"
|
||||
#include "ota.hpp"
|
||||
|
||||
const char *ssid = "mdchaparror";
|
||||
const char *password = "un260874";
|
||||
long timeAnterior;
|
||||
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()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.waitForConnectResult() != WL_CONNECTED)
|
||||
{
|
||||
delay(5000);
|
||||
ESP.restart();
|
||||
}
|
||||
Serial.println(WiFi.localIP());
|
||||
mensaje += WiFi.localIP().toString();
|
||||
|
||||
ota_init();
|
||||
mqtt_init();
|
||||
|
||||
timeClient.begin();
|
||||
matrix_init();
|
||||
|
||||
|
||||
// runner.init();
|
||||
// runner.addTask(t1);
|
||||
// runner.addTask(t2);
|
||||
// delay(1000);
|
||||
// t1.enable();
|
||||
// t2.enable();
|
||||
}
|
||||
// void loop()
|
||||
// {
|
||||
// runner.execute();
|
||||
// }
|
||||
|
||||
// void t1Callback()
|
||||
// {
|
||||
// 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();
|
||||
if((millis() - matrix_time) > 100){
|
||||
matrix_time = millis();
|
||||
show_matrix();
|
||||
}
|
||||
|
||||
|
||||
if ((millis() - timeAnterior) > 1000)
|
||||
{
|
||||
timeAnterior = millis();
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
timeClient.update(); //sincronizamos con el server NTP
|
||||
hora = timeClient.getFormattedTime();
|
||||
texto = mensaje + " " + hora;
|
||||
longitud = texto.length() * 5 + 64;
|
||||
}
|
||||
|
||||
client.loop();
|
||||
}
|
||||
46
ESP8266/MatrixLedsRGB/src/matrix.hpp
Normal file
46
ESP8266/MatrixLedsRGB/src/matrix.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_NeoMatrix.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#define PIN D7 //DIN MATRIZ
|
||||
String texto = "Puerto Ota: ";
|
||||
String mensaje = "Puerto Ota: ";
|
||||
String hora = "00:00:00";
|
||||
int R = 255;
|
||||
int G = 0;
|
||||
int B = 0;
|
||||
|
||||
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
|
||||
NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
|
||||
NEO_GRB + NEO_KHZ800);
|
||||
|
||||
int x = matrix.width();
|
||||
int longitud = 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()
|
||||
{
|
||||
matrix.begin();
|
||||
matrix.setTextWrap(false);
|
||||
matrix.setBrightness(20);
|
||||
matrix.setTextColor(matrix.Color(R, G, B));
|
||||
}
|
||||
|
||||
void show_matrix()
|
||||
{
|
||||
matrix.fillScreen(0);
|
||||
matrix.setCursor(x, 1);
|
||||
matrix.print(texto);
|
||||
matrix.setTextColor(matrix.Color(R, G, B));
|
||||
if (--x < -1 * longitud)
|
||||
{
|
||||
x = matrix.width();
|
||||
|
||||
}
|
||||
matrix.show();
|
||||
}
|
||||
88
ESP8266/MatrixLedsRGB/src/mqtt.hpp
Normal file
88
ESP8266/MatrixLedsRGB/src/mqtt.hpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#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";
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
// 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_MENSAJE).equals(p_topic))
|
||||
{
|
||||
mensaje = String(payload);
|
||||
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))
|
||||
{
|
||||
Serial.println("INFO: connected");
|
||||
client.subscribe(MQTT_TOPIC_MENSAJE);
|
||||
client.subscribe(MQTT_TOPIC_RGB);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("ERROR: failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println("DEBUG: try again in 5 seconds");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_init()
|
||||
{
|
||||
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
|
||||
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]) : "";
|
||||
}
|
||||
10
ESP8266/MatrixLedsRGB/src/ota.hpp
Normal file
10
ESP8266/MatrixLedsRGB/src/ota.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
|
||||
void ota_init()
|
||||
{
|
||||
ArduinoOTA.setHostname("OtaMatrix");
|
||||
ArduinoOTA.setPassword((const char *)"un260874");
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
Reference in New Issue
Block a user