pitrix/src/pitrix.cpp

120 lines
2.9 KiB
C++
Raw Normal View History

2019-05-27 19:17:05 +00:00
#include <Arduino.h>
2019-05-21 03:52:57 +00:00
#include "ntp.h"
2019-05-21 03:52:57 +00:00
#include "config.h"
#include "my_wifi.h"
2019-05-28 19:19:35 +00:00
#include "ota.h"
#include "my_fastled.h"
#include "my_mqtt.h"
2019-05-21 03:52:57 +00:00
#include "functions.h"
#include "effects.h"
2019-06-12 18:57:31 +00:00
#include "http_server.h"
#include "recorder.h"
2019-05-21 03:52:57 +00:00
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
#ifdef RECORDER_ENABLE
Recorder* recorder;
#endif
2019-05-21 03:52:57 +00:00
void setup() {
2019-06-07 04:24:16 +00:00
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);
2019-06-07 04:24:16 +00:00
setup_effects();
wifi_setup();
ntp_setup();
ota_setup();
fastled_setup();
ntpClient.begin();
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
mqtt_setup();
2019-06-07 04:24:16 +00:00
#endif
#ifdef RECORDER_ENABLE
recorder = new Recorder();
#endif
2019-06-18 16:18:07 +00:00
SPIFFS.begin();
2019-06-07 04:24:16 +00:00
LOGln("Core * Setup complete");
}
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--;
Window* w = Window::getFullWindow();
CRGB color(0xFF0000);
w->clear();
2019-06-07 04:24:16 +00:00
for (int i=0; i<starting_up; i++) {
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
ntpClient.update();
#ifdef MQTT_ENABLE
mqtt_loop();
#endif
2019-06-12 18:57:31 +00:00
#ifdef HTTP_SERVER_ENABLE
http_server_loop();
#endif
2019-05-27 19:17:05 +00:00
2019-06-07 04:24:16 +00:00
EVERY_N_MILLISECONDS(100) {
baseHue++;
}
2019-06-07 04:24:16 +00:00
EVERY_N_MILLISECONDS(1000 / FPS) {
//LOGln("Core * loop running");
current_effect->loop();
//LOGln("Core * loop ran");
2019-06-07 04:24:16 +00:00
if (current_effect->can_be_shown_with_clock()) {
effect_clock.loop(current_effect->clock_as_mask(), CRGB(0xFFFFFF), CRGB(0x000000));
}
FastLED.show();
#ifdef RECORDER_ENABLE
recorder->loop();
#endif
}
2019-06-07 04:24:16 +00:00
#if defined(MQTT_ENABLE) && defined(MQTT_REPORT_METRICS)
EVERY_N_SECONDS(15) {
mqtt_publish("free_heap", ESP.getFreeHeap());
mqtt_publish("uptime", millis()/1000);
2019-06-12 18:49:37 +00:00
mqtt_publish("fps", FastLED.getFPS());
2019-06-07 04:24:16 +00:00
}
#endif // MQTT_REPORT_METRICS
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-21 03:52:57 +00:00
}