2019-05-27 19:17:05 +00:00
|
|
|
#include <Arduino.h>
|
2019-10-23 03:41:39 +00:00
|
|
|
#include <FS.h>
|
2019-05-21 03:52:57 +00:00
|
|
|
|
2019-05-29 22:49:54 +00:00
|
|
|
#include "ntp.h"
|
2019-05-21 03:52:57 +00:00
|
|
|
#include "config.h"
|
2019-06-13 04:01:44 +00:00
|
|
|
#include "my_wifi.h"
|
2019-05-28 19:19:35 +00:00
|
|
|
#include "ota.h"
|
|
|
|
#include "my_fastled.h"
|
2019-05-29 22:49:54 +00:00
|
|
|
#include "my_mqtt.h"
|
2019-05-21 03:52:57 +00:00
|
|
|
#include "functions.h"
|
2019-05-22 04:52:41 +00:00
|
|
|
#include "effects.h"
|
2019-06-12 18:57:31 +00:00
|
|
|
#include "http_server.h"
|
2019-10-09 03:57:20 +00:00
|
|
|
#include "settings.h"
|
2019-05-21 03:52:57 +00:00
|
|
|
|
2019-05-29 22:49:54 +00:00
|
|
|
uint8_t starting_up = OTA_STARTUP_DELAY;
|
|
|
|
int loop_timeouts = 0;
|
|
|
|
long loop_started_at = 0;
|
2019-05-30 11:08:24 +00:00
|
|
|
uint8_t baseHue = 0; // defined as extern in prototypes.h
|
2019-06-14 03:35:17 +00:00
|
|
|
char hostname[30]; // defined as extern in prototypes.h
|
2019-10-23 10:36:08 +00:00
|
|
|
uint16_t frame = 0; // defined as extern in prototypes.h
|
2019-10-01 04:29:32 +00:00
|
|
|
unsigned long _last_effect_loop_finished_at = 0;
|
2019-12-21 13:21:31 +00:00
|
|
|
unsigned long timer = 0;
|
2019-09-04 04:05:45 +00:00
|
|
|
#ifdef RECORDER_ENABLE
|
|
|
|
Recorder* recorder;
|
|
|
|
#endif
|
2019-09-25 04:40:24 +00:00
|
|
|
#ifdef MQTT_REPORT_METRICS
|
|
|
|
uint16_t metrics_frame_count = 0;
|
|
|
|
unsigned long metrics_frame_time = 0;
|
|
|
|
#endif
|
2019-05-23 19:18:15 +00:00
|
|
|
|
2019-05-21 03:52:57 +00:00
|
|
|
void setup() {
|
2019-06-07 04:24:16 +00:00
|
|
|
Serial.begin(74880);
|
|
|
|
LOGln("Core * Starting");
|
2019-06-19 20:28:38 +00:00
|
|
|
|
2019-06-14 03:35:17 +00:00
|
|
|
int chipid;
|
|
|
|
#if defined( ESP8266 )
|
|
|
|
chipid = ESP.getChipId();
|
|
|
|
#elif defined( ESP32 )
|
|
|
|
chipid = ESP.getEfuseMac() & 0xFFFFFF;
|
|
|
|
#endif
|
|
|
|
snprintf(hostname, 30, HOSTNAME, chipid);
|
2019-06-19 20:28:38 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
setup_effects();
|
|
|
|
wifi_setup();
|
|
|
|
ntp_setup();
|
|
|
|
ota_setup();
|
|
|
|
fastled_setup();
|
2019-06-12 18:57:31 +00:00
|
|
|
#ifdef HTTP_SERVER_ENABLE
|
|
|
|
http_server_setup();
|
|
|
|
#endif
|
2019-06-07 04:24:16 +00:00
|
|
|
#ifdef MQTT_ENABLE
|
2019-06-06 04:43:50 +00:00
|
|
|
mqtt_setup();
|
2019-06-07 04:24:16 +00:00
|
|
|
#endif
|
2020-04-29 18:28:10 +00:00
|
|
|
if (!SPIFFS.begin()) {
|
|
|
|
LOGln("Core * Could not open SPIFFS filesystem");
|
|
|
|
} else {
|
|
|
|
LOGln("Core * Files in SPIFFS filesystem:");
|
|
|
|
Dir d = SPIFFS.openDir("/");
|
|
|
|
while(d.next()) {
|
|
|
|
LOGln("Core * %s", d.fileName().c_str());
|
|
|
|
}
|
|
|
|
LOGln("Core * End of SPIFFS file listing.");
|
|
|
|
}
|
2019-10-09 03:57:20 +00:00
|
|
|
load_settings();
|
2019-06-07 04:24:16 +00:00
|
|
|
LOGln("Core * Setup complete");
|
2019-05-23 19:18:15 +00:00
|
|
|
}
|
2019-05-21 03:52:57 +00:00
|
|
|
|
|
|
|
void loop() {
|
2019-06-07 04:24:16 +00:00
|
|
|
loop_started_at = millis();
|
|
|
|
ota_loop();
|
2019-05-21 03:52:57 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
if (starting_up > 0) {
|
|
|
|
EVERY_N_SECONDS(1) {
|
|
|
|
Serial.print("Core * Waiting for OTA... "); Serial.println(starting_up);
|
|
|
|
starting_up--;
|
2020-04-30 04:41:30 +00:00
|
|
|
Window* w = &Window::window_full;
|
2019-06-11 17:48:09 +00:00
|
|
|
CRGB color(0xFF0000);
|
|
|
|
w->clear();
|
2019-06-07 04:24:16 +00:00
|
|
|
for (int i=0; i<starting_up; i++) {
|
2019-06-11 17:48:09 +00:00
|
|
|
w->setPixel(i, 0, &color);
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
|
|
|
FastLED.show();
|
|
|
|
}
|
|
|
|
return;
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|
2019-05-27 19:17:05 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
#ifdef MQTT_ENABLE
|
|
|
|
mqtt_loop();
|
|
|
|
#endif
|
2019-05-27 19:17:05 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
EVERY_N_MILLISECONDS(100) {
|
|
|
|
baseHue++;
|
|
|
|
}
|
2019-05-30 11:08:24 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
EVERY_N_MILLISECONDS(1000 / FPS) {
|
2019-10-23 10:36:08 +00:00
|
|
|
frame++;
|
2019-10-01 04:29:32 +00:00
|
|
|
// Calculate the delay since the last time loop() was called.
|
|
|
|
// This way, the effect can handle varying frame rates.
|
|
|
|
uint16_t last_loop_ago;
|
|
|
|
unsigned long now = millis();
|
|
|
|
if (now > _last_effect_loop_finished_at && _last_effect_loop_finished_at) {
|
|
|
|
last_loop_ago = now - _last_effect_loop_finished_at;
|
|
|
|
} else {
|
|
|
|
last_loop_ago = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:03:48 +00:00
|
|
|
#ifdef MQTT_REPORT_METRICS
|
|
|
|
unsigned long effect_loop_started = millis();
|
|
|
|
#endif
|
|
|
|
|
2019-10-01 04:29:32 +00:00
|
|
|
current_effect->loop(last_loop_ago);
|
|
|
|
|
2019-10-18 15:03:48 +00:00
|
|
|
#ifdef MQTT_REPORT_METRICS
|
|
|
|
metrics_frame_count++;
|
|
|
|
metrics_frame_time += (millis() - effect_loop_started);
|
|
|
|
#endif
|
|
|
|
|
2019-10-01 04:29:32 +00:00
|
|
|
// Save the time for the next run.
|
|
|
|
_last_effect_loop_finished_at = now;
|
2019-05-23 20:14:15 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
if (current_effect->can_be_shown_with_clock()) {
|
2019-09-25 16:11:52 +00:00
|
|
|
effect_clock.loop_with_invert(current_effect->clock_as_mask());
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
2019-12-21 13:21:31 +00:00
|
|
|
|
|
|
|
effect_timer.loop(last_loop_ago);
|
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
FastLED.show();
|
2019-10-18 15:03:48 +00:00
|
|
|
|
2019-10-23 04:22:53 +00:00
|
|
|
http_server_send_framedata();
|
2019-05-23 20:14:15 +00:00
|
|
|
}
|
2019-05-29 23:36:30 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
#if defined(MQTT_ENABLE) && defined(MQTT_REPORT_METRICS)
|
|
|
|
EVERY_N_SECONDS(15) {
|
2019-09-25 04:40:24 +00:00
|
|
|
char json[120];
|
2019-09-25 16:11:03 +00:00
|
|
|
snprintf(json, 120, "{\"effect\":\"%s\",\"heap\":%u, \"up\":%lu, \"fps\":%d, \"frametime\":%lu}", current_effect->get_name().c_str(), ESP.getFreeHeap(), millis()/1000, FastLED.getFPS(), metrics_frame_time / metrics_frame_count);
|
2019-09-25 04:40:24 +00:00
|
|
|
mqtt_publish("metrics", json);
|
|
|
|
metrics_frame_count = 0;
|
|
|
|
metrics_frame_time = 0;
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
|
|
|
#endif // MQTT_REPORT_METRICS
|
2019-05-30 11:09:38 +00:00
|
|
|
|
2019-05-23 19:18:15 +00:00
|
|
|
|
2019-06-07 04:24:16 +00:00
|
|
|
if (MONITOR_LOOP_TIMES && millis()-loop_started_at>=MONITOR_LOOP_TIME_THRESHOLD) {
|
2019-06-18 16:18:07 +00:00
|
|
|
//LOG("Core * Loop took "); LOG(millis()-loop_started_at); LOGln("ms.");
|
2019-06-07 04:24:16 +00:00
|
|
|
loop_timeouts++;
|
2019-06-18 16:18:07 +00:00
|
|
|
//LOG("Core * Timeout counter is now "); LOGln(loop_timeouts);
|
2019-06-07 04:24:16 +00:00
|
|
|
if (loop_timeouts >= MONITOR_LOOP_TIME_COUNT_MAX) {
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
} else if (loop_timeouts > 0) {
|
|
|
|
loop_timeouts--;
|
2019-05-23 19:18:15 +00:00
|
|
|
}
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|