#include #include #include "ntp.h" #include "config.h" #include "my_wifi.h" #include "ota.h" #include "my_fastled.h" #include "my_mqtt.h" #include "functions.h" #include "effects.h" #include "http_server.h" #include "settings.h" uint8_t starting_up = OTA_STARTUP_DELAY; int loop_timeouts = 0; long loop_started_at = 0; uint8_t baseHue = 0; // defined as extern in prototypes.h char hostname[30]; // defined as extern in prototypes.h uint16_t frame = 0; // defined as extern in prototypes.h unsigned long _last_effect_loop_finished_at = 0; unsigned long timer = 0; #ifdef RECORDER_ENABLE Recorder* recorder; #endif #ifdef MQTT_REPORT_METRICS uint16_t metrics_frame_count = 0; unsigned long metrics_frame_time = 0; #endif void setup() { Serial.begin(74880); LOGln("Core * Starting"); int chipid; #if defined( ESP8266 ) chipid = ESP.getChipId(); #elif defined( ESP32 ) chipid = ESP.getEfuseMac() & 0xFFFFFF; #endif snprintf(hostname, 30, HOSTNAME, chipid); setup_effects(); wifi_setup(); ntp_setup(); ota_setup(); fastled_setup(); #ifdef HTTP_SERVER_ENABLE http_server_setup(); #endif #ifdef MQTT_ENABLE mqtt_setup(); #endif 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."); } load_settings(); LOGln("Core * Setup complete"); } void loop() { loop_started_at = millis(); ota_loop(); if (starting_up > 0) { EVERY_N_SECONDS(1) { Serial.print("Core * Waiting for OTA... "); Serial.println(starting_up); starting_up--; Window* w = &Window::window_full; CRGB color(0xFF0000); w->clear(); for (int i=0; isetPixel(i, 0, &color); } FastLED.show(); } return; } #ifdef MQTT_ENABLE mqtt_loop(); #endif EVERY_N_MILLISECONDS(100) { baseHue++; } EVERY_N_MILLISECONDS(1000 / FPS) { frame++; // 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; } #ifdef MQTT_REPORT_METRICS unsigned long effect_loop_started = millis(); #endif current_effect->loop(last_loop_ago); #ifdef MQTT_REPORT_METRICS metrics_frame_count++; metrics_frame_time += (millis() - effect_loop_started); #endif // Save the time for the next run. _last_effect_loop_finished_at = now; if (current_effect->can_be_shown_with_clock()) { effect_clock.loop_with_invert(current_effect->clock_as_mask()); } effect_timer.loop(last_loop_ago); FastLED.show(); http_server_send_framedata(); } #if defined(MQTT_ENABLE) && defined(MQTT_REPORT_METRICS) EVERY_N_SECONDS(15) { char json[120]; 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); mqtt_publish("metrics", json); metrics_frame_count = 0; metrics_frame_time = 0; } #endif // MQTT_REPORT_METRICS if (MONITOR_LOOP_TIMES && millis()-loop_started_at>=MONITOR_LOOP_TIME_THRESHOLD) { //LOG("Core * Loop took "); LOG(millis()-loop_started_at); LOGln("ms."); loop_timeouts++; //LOG("Core * Timeout counter is now "); LOGln(loop_timeouts); if (loop_timeouts >= MONITOR_LOOP_TIME_COUNT_MAX) { ESP.restart(); } } else if (loop_timeouts > 0) { loop_timeouts--; } }