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

This commit is contained in:
Franzi 2021-12-30 13:58:26 +01:00
parent 1d21401bb7
commit f92662b34d
Signed by: kunsi
GPG Key ID: 12E3D2136B818350
1 changed files with 33 additions and 13 deletions

View File

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