Browse Source

automatically restart tally if websocket connection was disconnected for more than 24 hours

main
Franzi 4 weeks ago
parent
commit
f92662b34d
Signed by: kunsi GPG Key ID: 12E3D2136B818350
  1. 46
      src/main.cpp

46
src/main.cpp

@ -26,6 +26,8 @@ static_assert(sizeof(OBS_PASS) > 1, "OBS_PASS must be non-empty if defined");
SHA256 sha256;
#endif
int disconnect_restart_counter = 0;
bool is_currently_live = false;
bool is_currently_preview = false;
bool is_currently_connected = false;
@ -57,7 +59,10 @@ void set_error() {
void handleWebSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.println("[WS] disconnected");
disconnect_restart_counter = disconnect_restart_counter + 1;
Serial.print("[WS] disconnected, restart counter is at ");
Serial.println(disconnect_restart_counter);
/*
We do NOT set the LEDs to off in here on purpose. Maybe
just the wifi connection was interrupted or something
@ -68,10 +73,17 @@ void handleWebSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
quits OBS, we will get disconnected.
*/
is_currently_connected = false;
// reconnect-try every 2s, restart every 24h
if (disconnect_restart_counter > 43200) {
ESP.restart();
}
break;
case WStype_CONNECTED:
Serial.printf("[WS] connected to %s\n", payload);
disconnect_restart_counter = 0;
// Find out if we need authentication
webSocket.sendTXT("{\"request-type\":\"GetAuthRequired\",\"message-id\":\"1\"}");
break;
@ -238,18 +250,11 @@ void setup() {
FastLED.setBrightness(LED_BRIGHTNESS);
#ifdef STATUS_LED
digitalWrite(STATUS_LED, LOW);
pinMode(STATUS_LED, OUTPUT);
#endif
set_error();
delay(100);
set_idle();
delay(100);
set_error();
delay(100);
set_idle();
delay(100);
#else
set_error();
#endif
Serial.printf("[Tally] connecting to wifi ssid: %s\n", WIFI_SSID);
@ -257,7 +262,18 @@ void setup() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
while(WiFi.status() != WL_CONNECTED) {
delay(250);
delay(200);
#ifdef STATUS_LED
digitalWrite(STATUS_LED, HIGH);
#else
set_idle();
#endif
delay(50);
#ifdef STATUS_LED
digitalWrite(STATUS_LED, LOW);
#else
set_error();
#endif
Serial.print(".");
}
Serial.println();
@ -266,9 +282,13 @@ void setup() {
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
#ifdef STATUS_LED
digitalWrite(STATUS_LED, HIGH);
#else
set_idle();
#endif
Serial.println("[Tally] connecting to OBS");
Serial.println("[Tally] initializing OBS websocket connection");
webSocket.begin(OBS_HOST, OBS_PORT, "/");
webSocket.onEvent(handleWebSocketEvent);
webSocket.setReconnectInterval(2000);

Loading…
Cancel
Save