remove clock dependency

This commit is contained in:
2020-11-19 10:55:37 +01:00
parent 233a07ce5a
commit 2fbbaf7972
4 changed files with 239 additions and 405 deletions

View File

@@ -1,4 +1,4 @@
;PlatformIO Project Configuration File ; PlatformIO Project Configuration File
; ;
; Build options: build flags, source filter ; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags ; Upload options: custom upload port, speed and extra flags
@@ -8,9 +8,22 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:esp32doit-devkit-v1] [env:ttgo-t1]
platform = espressif32 platform = espressif32
board = esp32doit-devkit-v1 board = ttgo-t1
framework = arduino framework = arduino
monitor_port = COM[13] monitor_port = COM[1367]
monitor_speed = 115200 monitor_speed = 115200
lib_deps =
knolleary/PubSubClient@^2.8
adafruit/Adafruit GFX Library@^1.10.1
adafruit/Adafruit BusIO@^1.5.0
fastled/FastLED@^3.3.3
marcmerlin/FastLED NeoMatrix@^1.2
bertmelis/Ticker-esp32@^1.1.0
bblanchon/ArduinoJson@^6.16.1
prampec/IotWebConf@^2.3.1
khoih-prog/ESP32TimerInterrupt@^1.0.3
aharshac/StringSplitter@^1.0.0
sstaub/NTP@^1.4
aster94/Utilities@^0.4.6

View File

@@ -5,19 +5,20 @@ Scroller::Scroller(int max_loops)
_max_loops = max_loops; _max_loops = max_loops;
} }
void Scroller::SetMatrix(Adafruit_NeoMatrix *matrix) void Scroller::SetMatrix(FastLED_NeoMatrix *matrix)
{ {
this->_matrix = matrix; _matrix = matrix;
} }
int Scroller::GetNumberOfLines() int Scroller::GetNumberOfLines()
{ {
return this->_nlines; return _nlines;
} }
int Scroller::AddLine(String line) int Scroller::AddLine(String line)
{ {
this->_Lines[this->_nlines] = line; _Lines[_nlines] = line;
SetParameters(_nlines);
return _nlines++; return _nlines++;
} }
@@ -25,6 +26,7 @@ int Scroller::AddLines(String* lines,int nlines)
{ {
for (int i=0; i < nlines; i++) { for (int i=0; i < nlines; i++) {
_Lines[i] = lines[i]; _Lines[i] = lines[i];
SetParameters(i);
} }
_nlines = nlines; _nlines = nlines;
@@ -42,7 +44,7 @@ void Scroller::DeleteLines()
_nlines = 0; _nlines = 0;
} }
int Scroller::Scroll(int tick) int Scroller::Scroll()
{ {
int mx = 32; int mx = 32;
String line; String line;
@@ -55,29 +57,30 @@ int Scroller::Scroll(int tick)
{ {
_matrix->fillScreen(0); _matrix->fillScreen(0);
_matrix->setCursor(-_x, _y); _matrix->setCursor(-_x, _y);
_matrix->setTextColor(_colors[1]); _matrix->setTextColor(_colors[_Settings[_lp].color]);
_matrix->setBrightness(_Settings[_lp].brightness);
_matrix->print(line); _matrix->print(line);
_matrix->show(); _matrix->show();
if (_x++ == 0) { if (_x++ == 0) {
return 3; return _Settings[_lp].timeout_on/SCROLLER_TICK_TIME;
} }
else { else {
return 1; return _Settings[_lp].speed/SCROLLER_TICK_TIME;
} }
} }
_lp++; _lp++;
_x = 0; _x = 0;
return 10; return _Settings[_lp].timeout_off/SCROLLER_TICK_TIME;
} }
else else
{ {
_matrix->fillScreen(0); _matrix->fillScreen(0);
_matrix->setCursor(_x, _y); _matrix->setCursor(_x, _y);
_matrix->setTextColor(_colors[1]); _matrix->setTextColor(_colors[_Settings[_lp].color]);
_matrix->setBrightness(_Settings[_lp].brightness);
_matrix->print(line); _matrix->print(line);
_matrix->show(); _matrix->show();
_lp++; return _Settings[_lp++].timeout_off/SCROLLER_TICK_TIME;
return 30;
} }
} }
@@ -91,6 +94,7 @@ void Scroller::PrintSerialLines()
bool Scroller::TurnOn() bool Scroller::TurnOn()
{ {
Serial.println("Turn on Scroller");
_off = false; _off = false;
_lp = 0; _lp = 0;
_x = 0; _x = 0;
@@ -103,6 +107,7 @@ bool Scroller::TurnOn()
void Scroller::TurnOff() void Scroller::TurnOff()
{ {
Serial.println("TURN OFF");
_matrix->fillScreen(0); _matrix->fillScreen(0);
_matrix->print(""); _matrix->print("");
_matrix->show(); _matrix->show();
@@ -114,7 +119,7 @@ void Scroller::SetColors(uint16_t *colors)
_colors = colors; _colors = colors;
} }
void Scroller::loop(int tick) void Scroller::loop(ulong tick)
{ {
if (_off) return; if (_off) return;
@@ -131,10 +136,13 @@ void Scroller::loop(int tick)
return; return;
} }
if (_delayTicks - delta <= 0) _delayTicks = Scroll(tick); if (_delayTicks - delta <= 0) _delayTicks = Scroll();
else _delayTicks -= delta; else _delayTicks -= delta;
if (_delayTicks < 0) _delayTicks = 0; if (_delayTicks < 0) _delayTicks = 0;
_tick = tick;
} }
int Scroller::TimesScrolled() int Scroller::TimesScrolled()
@@ -142,6 +150,15 @@ int Scroller::TimesScrolled()
return _nscrolled; return _nscrolled;
} }
void Scroller::SetParameters(int line,uint8_t color,uint8_t brightness,int timeout_off,int timeout_on,int speed)
{
_Settings[line].color = color;
_Settings[line].brightness = brightness;
_Settings[line].timeout_on = timeout_on;
_Settings[line].timeout_off = timeout_off;
_Settings[line].speed = speed;
}
Scroller::~Scroller() Scroller::~Scroller()
{ {
} }

View File

@@ -1,45 +1,58 @@
#ifndef SCROLLER_H #ifndef SCROLLER_H
#define SCROLLER_H #define SCROLLER_H
#include <Adafruit_GFX.h> #include <FastLED.h>
#include <Adafruit_NeoMatrix.h> #include <FastLED_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <Arduino.h> #include <Arduino.h>
#include <ESP32Ticker.h> #include <ESP32Ticker.h>
#include "ESP32TimerInterrupt.h"
#define SCROLLER_TICK_TIME 100 //ms #define SCROLLER_TICK_TIME 100 //ms
struct Setting
{
uint8_t color;
uint8_t brightness;
int timeout_off;
int timeout_on;
int speed;
};
class Scroller class Scroller
{ {
private: private:
/* data */ /* data */
const static int MAX_LINES = 10; const static int MAX_LINES = 10;
String _Lines[MAX_LINES]; String _Lines[MAX_LINES];
Setting _Settings[MAX_LINES];
int _nlines = 0; int _nlines = 0;
int _lp = 0; int _lp = 0;
int _tick=0; int _tick=0;
int _delayTicks = 0; int _delayTicks = 0;
int _nscrolled = 0; int _nscrolled = 0;
int _x = 0, _y = 0; int _x = 0, _y = 0;
Adafruit_NeoMatrix *_matrix; FastLED_NeoMatrix *_matrix;
uint16_t *_colors; uint16_t *_colors;
int _max_loops; int _max_loops;
bool _off = true; bool _off = true;
public: public:
Scroller(int max_loops); Scroller(int max_loops);
bool TurnOn(); bool TurnOn();
void SetColors(uint16_t *colors); void SetColors(uint16_t *colors);
void SetMatrix(Adafruit_NeoMatrix *matrix); void SetMatrix(FastLED_NeoMatrix *matrix);
int AddLine(String line); int AddLine(String line);
void SetParameters(int line,uint8_t color=1,uint8_t brightness=10,int timeout_off=2000,int timeout_on=2000,int speed=200);
int AddLines(String* lines,int nlines); int AddLines(String* lines,int nlines);
void DeleteLines(); void DeleteLines();
bool ReplaceLine(int number,String line); bool ReplaceLine(int number,String line);
int GetNumberOfLines(); int GetNumberOfLines();
int Scroll(int tick); int Scroll();
int TimesScrolled(); int TimesScrolled();
void PrintSerialLines(); void PrintSerialLines();
void TurnOff(); void TurnOff();
void loop(int tick); void loop(ulong tick);
~Scroller(); ~Scroller();
}; };
#endif #endif

View File

@@ -1,8 +1,8 @@
#include <WiFi.h> #include <WiFi.h>
#include <SPI.h> #include <SPI.h>
#include <Adafruit_GFX.h> #include <FastLED.h>
#include <Adafruit_NeoMatrix.h> #include <FastLED_NeoMatrix.h>
#include <Adafruit_NeoPixel.h> #include <PubSubClient.h>
#include <WiFiMulti.h> #include <WiFiMulti.h>
@@ -27,9 +27,11 @@
#define COMMAND_PARAMETER_LENGTH 30 #define COMMAND_PARAMETER_LENGTH 30
#define MAX_TIMES_SCROLLED 5 #define MAX_TIMES_SCROLLED 5
#define PIN 15 #define PIN 27
#include <WiFiUdp.h> WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMqttConnectionAttempt=0;
void handleRoot(); void handleRoot();
void processCommand(); void processCommand();
@@ -39,23 +41,24 @@ const char thingName[] = "BADThing";
// -- Initial password to connect to the Thing, when it creates an own Access Point. // -- Initial password to connect to the Thing, when it creates an own Access Point.
const char wifiInitialApPassword[] = "BADesp32"; const char wifiInitialApPassword[] = "BADesp32";
#define STRING_LEN 128
char mqttServerValue[STRING_LEN];
DNSServer dnsServer; DNSServer dnsServer;
WebServer server(80); WebServer server(80);
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword); IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword);
IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN,"text","BADnet-gw.lan","BADnet-gw.lan",NULL);
enum Lines_names
{
TIME_LINE,
DATE_LINE,
WEATHER_LINE,
NAMES_LINE,
LAST_LINE
};
String Lines[LAST_LINE];
Adafruit_NeoMatrix *matrix; #define lcdw 32
#define lcdh 8
#define NUMMATRIX (lcdw*lcdh)
CRGB matrixleds[NUMMATRIX];
FastLED_NeoMatrix *matrix;
uint16_t colors[] = { uint16_t colors[] = {
Adafruit_NeoMatrix::Color(255, 0, 0), Adafruit_NeoMatrix::Color(0, 255, 0), Adafruit_NeoMatrix::Color(0, 0, 255)}; Framebuffer_GFX::Color(255, 0, 0), Framebuffer_GFX::Color(0, 255, 0), Framebuffer_GFX::Color(0, 0, 255)};
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
@@ -64,63 +67,30 @@ WiFiUDP ntpUDP;
// update interval (in milliseconds, can be changed using setUpdateInterval() ). // update interval (in milliseconds, can be changed using setUpdateInterval() ).
bool initialized = false; bool initialized = false;
NTP ntp(ntpUDP); NTP ntp(ntpUDP);
HTTPClient client;
DynamicJsonDocument doc(1024);
String settings;
enum when_names {
WORK_DAYS,
WEEKENDS,
TOMOROW,
ONCE
};
struct timer_def {
int when; int h; int m; int d; int mh; int y;
};
int ntimers,nextTimer = -1;
int nts;
TimeZone DateTime; TimeZone DateTime;
TimeZone nextAlarmDateTime;
#define MAX_TIMERS 10
timer_def Timers[MAX_TIMERS];
Scroller scroller(MAX_TIMES_SCROLLED); Scroller scroller(MAX_TIMES_SCROLLED);
unsigned long last_millis; unsigned long last_millis;
bool showText = false; bool showTextMqtt = false;
int scrolledTicker = 0; int scrolledTicker = 0;
bool updateNtpTime = false; bool updateNtpTime = false;
bool needMqttConnect = false;
ESP32Timer ITimer0(0); ESP32Timer ITimer0(0);
ESP32Timer ITimer1(1); ESP32Timer ITimer1(1);
ESP32Timer ITimer2(2); ESP32Timer ITimer2(2);
ulong tick;
void IRAM_ATTR TimerScrollTime1(void);
#define TIMER0_INTERVAL_MS 1000 #define TIMER0_INTERVAL_MS 1000
#define TIMER1_INTERVAL_MS 100
#define TIMER2_INTERVAL_MS 60000 #define TIMER2_INTERVAL_MS 60000
void IRAM_ATTR TimerCheckTime0(void)
{
unsigned long m = millis(), delta;
delta = (m - last_millis)/1000;
DateTime += delta;
if (nextAlarmDateTime.epoch() <= DateTime.epoch() && nextTimer != -1) {
showText = true;
scroller.TurnOn();
}
last_millis = m;
}
void IRAM_ATTR TimerScrollTime1(void) void IRAM_ATTR TimerScrollTime1(void)
{ {
scrolledTicker++; tick++;
} }
void IRAM_ATTR TimerUpdateNTP2(void) void IRAM_ATTR TimerUpdateNTP2(void)
@@ -128,262 +98,33 @@ void IRAM_ATTR TimerUpdateNTP2(void)
updateNtpTime = true; updateNtpTime = true;
} }
String get_name_of_day(int m, int d)
{
for (int i = 0; i < sizeof(cal_names) / sizeof(cal_names[0]); i++)
{
if (cal_names[i].m == m && cal_names[i].d == d)
return String(cal_names[i].name);
}
return String("Sviatok");
}
String get_weather_line()
{
client.begin("http://api.openweathermap.org/data/2.5/weather?q=Trnava,sk&lang=sk&APPID=d15c61931a053026e98769dd9fc46ba3");
int code = client.GET();
if (code > 0)
{
if (code == HTTP_CODE_OK)
{
float temp;
String w;
String desc;
String payload = client.getString();
deserializeJson(doc, payload);
JsonObject obj = doc.as<JsonObject>();
temp = obj["main"]["temp"].as<float>();
w = obj["weather"][0]["main"].as<String>();
desc = obj["weather"][0]["description"].as<String>();
temp -= 273.15;
return String(temp) + String("C ") + Ascifii::ToAscii(desc);
}
}
return "Error";
}
String get_time_line()
{
return String(ntp.formattedTime("%H:%M"));
}
String get_date_line()
{
return String(ntp.formattedTime("%d.%m"));
}
String get_names_line()
{
int d = DateTime.month();
int m = DateTime.day();
m++;
return get_name_of_day(m, d);
}
void calculate_timers()
{
timer_def *t;
int timer = -1;
struct tm tm;
time_t alarm;
TimeZone TZ,NextAlarm;
Serial.println("---CALC TIMERS---");
TZ = DateTime;
for (int i=0; i<ntimers ; i++)
{
t = &Timers[i];
tm.tm_sec = 0;
tm.tm_min = t->m;
tm.tm_hour = t->h;
if (t->d !=0) {
tm.tm_mday = t->d;
tm.tm_mon = t->mh - 1;
tm.tm_year = t->y - 1900;
} else {
tm.tm_mday = 0;
tm.tm_mon = 0;
tm.tm_year = 0;
}
if (t->d == 0) {
if (t->when == WORK_DAYS) {
int offset=24*3600;
int wday;
tm.tm_mday = DateTime.day();
tm.tm_mon = DateTime.month() - 1;
tm.tm_year = DateTime.year() - 1900;
wday = DateTime.weekDay();
if (wday == 0) offset = 3600*24;
if (wday == 6) offset = 2*3600*24;
alarm = mktime(&tm);
TZ.setLocalTime(alarm);
if (TZ.epoch() < DateTime.epoch() && wday == 5 ) offset = 3*3600*24;
if (TZ.epoch() > DateTime.epoch() && (wday >=1 && wday <=5)) offset=0;
TZ += offset;
} else if (t->when == WEEKENDS) {
int offset=24*3600;
int wday;
tm.tm_mday = DateTime.day();
tm.tm_mon = DateTime.month() - 1;
tm.tm_year = DateTime.year() - 1900;
wday = DateTime.weekDay();
if (wday >= 1 && wday <= 5) offset = (7-wday) *3600*24;
alarm = mktime(&tm);
TZ.setLocalTime(alarm);
if (TZ.epoch() > DateTime.epoch() && (wday == 6 || wday == 0 )) offset = 0;
TZ += offset;
} else if (t->when == ONCE ) {
alarm = mktime(&tm);
TZ.setLocalTime(alarm);
}
if (TZ.epoch() > DateTime.epoch() && (TZ.epoch() < NextAlarm.epoch() || timer == -1)) {
Serial.println("TZ:" + TZ.toString());
timer = i;
NextAlarm = TZ;
}
}
}
if (timer != -1) {
nextTimer = timer;
nextAlarmDateTime = NextAlarm;
Serial.println(DateTime.toString());
Serial.println(nextAlarmDateTime.toString());
}
}
void read_config()
{
File file = SPIFFS.open("/settings.json", "r");
if (!file)
{
Serial.println("Failed to open settings file for reading");
return;
}
Serial.println("====File Content SETTINGS====");
String s;
while (file.available())
{
s += (char)file.read();
}
Serial.println(s);
file.close();
ntimers = 0;
settings = s;
Serial.println("SETTINGS: " + settings);
deserializeJson(doc, settings);
JsonObject obj = doc.as<JsonObject>();
for (int i=0;i < obj["data"].size(); i++) {
int t = obj["data"][i]["kedy"].as<int>();
String c = obj["data"][i]["cas"].as<String>();
String datum = obj["data"][i]["datum"].as<String>();
StringSplitter *splitter = new StringSplitter(c, ':', 2);
int h = splitter->getItemAtIndex(0).toInt();
int m = splitter->getItemAtIndex(1).toInt();
int d = 0;
int mh = 0;
int y = 0;
Serial.println(t);
Serial.println(c);
Serial.println(datum);
if (datum != NULL) {
StringSplitter *splitter = new StringSplitter(datum, '.', 3);
d = splitter->getItemAtIndex(0).toInt();
mh = splitter->getItemAtIndex(1).toInt();
y = splitter->getItemAtIndex(2).toInt();
}
Timers[ntimers].when = t;
Timers[ntimers].h = h;
Timers[ntimers].m = m;
Timers[ntimers].d = d;
Timers[ntimers].mh = mh;
Timers[ntimers].y = y;
ntimers++;
}
}
void setup_matrix() void setup_matrix()
{ {
matrix = new Adafruit_NeoMatrix(32, 8, PIN, matrix = new FastLED_NeoMatrix(matrixleds, lcdw, lcdh, 1, 1,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG );
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GBR + NEO_KHZ800);
FastLED.addLeds<NEOPIXEL,PIN>(matrixleds, NUMMATRIX);
matrix->begin(); matrix->begin();
matrix->setTextWrap(false); matrix->setTextWrap(false);
matrix->setBrightness(1); matrix->setBrightness(20);
matrix->setTextColor(colors[1]); matrix->setTextColor(colors[1]);
matrix->print("Start");
matrix->show();
delay(1000);
matrix->fillScreen(0);
matrix->print("");
matrix->show();
} }
void returnOK() { void returnOK() {
server.send(200, "text/plain", ""); server.send(200, "text/plain", "");
} }
void handleSave() { void wifiConnected()
String data; {
needMqttConnect = true;
Serial.println(server.uri());
Serial.println(server.method());
Serial.println(server.argName(0));
Serial.println(server.arg(0));
if (server.uri() != "/settings" && server.method() != HTTP_POST) {
return;
}
data = server.arg(0);
Serial.println("Data:" + data);
File file = SPIFFS.open("/settings.json", "w");
if (!file)
{
Serial.println("Failed to open file for writing");
return;
}
settings = data;
file.print(data.c_str());
file.close();
server.send(200, "text/plain", "");
read_config();
calculate_timers();
} }
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -392,26 +133,16 @@ void setup()
//iotWebConf.setStatusPin(PIN); //iotWebConf.setStatusPin(PIN);
iotWebConf.addParameter(&mqttServerParam);
iotWebConf.setWifiConnectionCallback(&wifiConnected);
iotWebConf.init(); iotWebConf.init();
if (!SPIFFS.begin(true)) if (ITimer1.attachInterruptInterval(SCROLLER_TICK_TIME * 1000, TimerScrollTime1))
{
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerCheckTime0))
Serial.println("Starting ITimer0 OK, millis() = " + String(millis()));
else
Serial.println("Can't set ITimer0. Select another freq. or timer");
// Interval in microsecs
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerScrollTime1))
Serial.println("Starting ITimer1 OK, millis() = " + String(millis())); Serial.println("Starting ITimer1 OK, millis() = " + String(millis()));
else else
Serial.println("Can't set ITimer1. Select another freq. or timer"); Serial.println("Can't set ITimer1. Select another freq. or timer");
if (ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS * 1000, TimerUpdateNTP2)) if (ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS * 1000, TimerUpdateNTP2))
Serial.println("Starting ITimer2 OK, millis() = " + String(millis())); Serial.println("Starting ITimer2 OK, millis() = " + String(millis()));
else else
@@ -421,14 +152,13 @@ void setup()
// -- Set up required URL handlers on the web server. // -- Set up required URL handlers on the web server.
server.on("/", handleRoot); server.on("/", handleRoot);
server.on("/config", [] { iotWebConf.handleConfig(); }); server.on("/config", [] { iotWebConf.handleConfig(); });
server.on("/settings", HTTP_POST, handleSave);
server.onNotFound([]() { iotWebConf.handleNotFound(); }); server.onNotFound([]() { iotWebConf.handleNotFound(); });
setup_matrix(); setup_matrix();
read_config();
scroller.SetColors(colors); scroller.SetColors(colors);
scroller.SetMatrix(matrix); scroller.SetMatrix(matrix);
} }
void handleRoot() void handleRoot()
@@ -440,36 +170,97 @@ void handleRoot()
return; return;
} }
File file = SPIFFS.open("/index.html", "r"); server.send(200, "text/html", "OK");
if (!file) }
boolean reconnect() {
unsigned long now = millis();
if (1000 > now - lastMqttConnectionAttempt)
{ {
Serial.println("Failed to open file for reading"); // Do not repeat within 1 sec.
return; return false;
}
// Loop until we're reconnected
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-SCROLLER";
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("home/scroller");
return true;
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 1 seconds");
// Wait 5 seconds before retrying
lastMqttConnectionAttempt = now;
return false;
}
}
void decode_payload(String msg)
{
StringSplitter *splitter = new StringSplitter(msg, ';', 2);
int n = 0;
int i = 0;
String s,m;
int color=1,brightness=10,speed=200, off=2000,on=2000;
if (splitter->getItemCount() == 2)
s = splitter->getItemAtIndex(n++);
m = splitter->getItemAtIndex(n++);
if (n == 2) {
StringSplitter *splitter = new StringSplitter(s, ':', 10);
String cmd,param;
while (i < splitter->getItemCount()) {
cmd = splitter->getItemAtIndex(i++);
param = splitter->getItemAtIndex(i++);
Serial.printf("cmd=%s,param=%s\n",cmd,param);
if (cmd == "c") color = param.toInt();
if (cmd == "b") brightness = param.toInt();
if (cmd == "s") speed = param.toInt();
if (cmd == "off") off = param.toInt();
if (cmd == "on") on = param.toInt();
}
} }
Serial.println("====File Content===="); i = scroller.AddLine(m);
String s; if (n == 2) {
while (file.available()) Serial.printf("color=%d,brightness=%d,off=%d,on=%d,speed=%d\n",color,brightness,off,on,speed);
{ scroller.SetParameters(i,color,brightness,off,on,speed);
s += (char)file.read();
} }
s.replace("%%SETTINGS%%",settings); }
Serial.print(s);
file.close(); void callback(char* topic, byte* payload, unsigned int length) {
char msg[128];
server.send(200, "text/html", s); Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
strncpy(msg,(char *)payload,length);
msg[length] = '\0';
// Switch on the LED if an 1 was received as first character
scroller.TurnOff();
scroller.DeleteLines();
decode_payload(msg);
showTextMqtt = true;
} }
void loop() void loop()
{ {
unsigned long n = 0; iotWebConf.doLoop();
// -- doLoop should be called as frequently as possible. if (needMqttConnect)
if (WiFi.status() == WL_CONNECTED)
{
if (initialized == false)
{ {
Serial.println("---INIT---"); Serial.println("---INIT---");
@@ -478,11 +269,24 @@ void loop()
ntp.begin(); ntp.begin();
unsigned long sec = ntp.epoch(); unsigned long sec = ntp.epoch();
DateTime.setUTCTime(sec); DateTime.setUTCTime(sec);
nextAlarmDateTime = DateTime; client.setServer(mqttServerValue, 1883);
client.setCallback(callback);
initialized = true; if (reconnect())
{
needMqttConnect = false;
} }
}
else if ((iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) && (!client.connected()))
{
Serial.println("MQTT reconnect");
client.setServer(mqttServerValue, 1883);
client.setCallback(callback);
reconnect();
}
if (iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) {
if (updateNtpTime) { if (updateNtpTime) {
Serial.println("---NTP UPDATE---"); Serial.println("---NTP UPDATE---");
@@ -490,29 +294,16 @@ void loop()
unsigned long sec = ntp.epoch(); unsigned long sec = ntp.epoch();
DateTime.setUTCTime(sec); DateTime.setUTCTime(sec);
last_millis = millis(); last_millis = millis();
calculate_timers();
updateNtpTime = false; updateNtpTime = false;
if (n++ % 10 == 0)
Lines[WEATHER_LINE] = get_weather_line();
Lines[TIME_LINE] = get_time_line();
Lines[DATE_LINE] = get_date_line();
Lines[NAMES_LINE] = get_names_line();
scroller.AddLines(Lines,LAST_LINE);
scroller.PrintSerialLines();
} }
if (showText) { if (showTextMqtt) {
Serial.println("---SHOW TEXT----"); scroller.TurnOn();
scroller.loop(scrolledTicker); showTextMqtt = false;
if (scroller.TimesScrolled() > MAX_TIMES_SCROLLED) showText = false;
}
} }
iotWebConf.doLoop(); scroller.loop(tick);
client.loop();
}
} }