2021-12-12 13:22:30 +00:00
|
|
|
#include <config.h>
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
static_assert(sizeof(WIFI_SSID) > 1, "WIFI_SSID is empty");
|
|
|
|
static_assert(sizeof(OBS_HOST) > 1, "OBS_HOST is empty");
|
|
|
|
static_assert(sizeof(OBS_SOURCE) > 1, "OBS_SOURCE is empty");
|
2021-12-05 14:14:11 +00:00
|
|
|
|
|
|
|
#define FASTLED_ESP8266_RAW_PIN_ORDER
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 11:16:06 +00:00
|
|
|
#include <Arduino.h>
|
2021-12-09 10:30:42 +00:00
|
|
|
#include <Hash.h>
|
2021-12-05 14:14:11 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WebSocketsClient.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <Base64.h>
|
|
|
|
#include <FastLED.h>
|
|
|
|
|
2021-12-05 07:46:41 +00:00
|
|
|
CRGB leds[LED_COUNT];
|
|
|
|
WebSocketsClient webSocket;
|
2021-12-05 13:39:59 +00:00
|
|
|
|
|
|
|
#ifdef OBS_PASS
|
2021-12-12 13:22:30 +00:00
|
|
|
static_assert(sizeof(OBS_PASS) > 1, "OBS_PASS must be non-empty if defined");
|
|
|
|
|
2021-12-05 13:39:59 +00:00
|
|
|
#include <SHA256.h>
|
|
|
|
#define HASH_SIZE 32
|
|
|
|
|
2021-12-05 07:46:41 +00:00
|
|
|
SHA256 sha256;
|
2021-12-05 13:39:59 +00:00
|
|
|
#endif
|
2021-12-05 07:46:41 +00:00
|
|
|
|
|
|
|
bool is_currently_live = false;
|
|
|
|
bool is_currently_preview = false;
|
2021-12-12 13:22:30 +00:00
|
|
|
bool is_currently_connected = false;
|
2021-12-05 07:46:41 +00:00
|
|
|
|
|
|
|
void set_program() {
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.println("[Tally] PROGRAM");
|
2021-12-05 07:46:41 +00:00
|
|
|
fill_solid(leds, LED_COUNT, CRGB::Red);
|
|
|
|
FastLED.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_preview() {
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.println("[Tally] PREVIEW");
|
2021-12-05 07:46:41 +00:00
|
|
|
fill_solid(leds, LED_COUNT, CRGB::Green);
|
|
|
|
FastLED.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_idle() {
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.println("[Tally] IDLE");
|
2021-12-05 07:46:41 +00:00
|
|
|
fill_solid(leds, LED_COUNT, CRGB::Black);
|
|
|
|
FastLED.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_error() {
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.println("[Tally] ERROR");
|
2021-12-05 07:46:41 +00:00
|
|
|
fill_solid(leds, LED_COUNT, CRGB::Purple);
|
|
|
|
FastLED.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleWebSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
|
|
|
switch(type) {
|
|
|
|
case WStype_DISCONNECTED:
|
|
|
|
Serial.println("[WS] disconnected");
|
|
|
|
/*
|
|
|
|
We do NOT set the LEDs to off in here on purpose. Maybe
|
|
|
|
just the wifi connection was interrupted or something
|
|
|
|
like that. It is important we don't disable the LEDs, in
|
|
|
|
case the OBS instance is still running.
|
|
|
|
|
2021-12-05 15:41:50 +00:00
|
|
|
We can't set the LEDs to "error", because if someone
|
|
|
|
quits OBS, we will get disconnected.
|
2021-12-05 07:46:41 +00:00
|
|
|
*/
|
2021-12-12 13:22:30 +00:00
|
|
|
is_currently_connected = false;
|
2021-12-05 07:46:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WStype_CONNECTED:
|
|
|
|
Serial.printf("[WS] connected to %s\n", payload);
|
2021-12-05 15:41:50 +00:00
|
|
|
// Find out if we need authentication
|
2021-12-05 07:46:41 +00:00
|
|
|
webSocket.sendTXT("{\"request-type\":\"GetAuthRequired\",\"message-id\":\"1\"}");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WStype_TEXT: {
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.printf("[WS] %s\n", payload);
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
StaticJsonDocument<10000> doc;
|
2021-12-05 07:46:41 +00:00
|
|
|
DeserializationError error = deserializeJson(doc, payload);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
Serial.print("deserializeJson() failed: ");
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.containsKey("authRequired")) {
|
2021-12-12 13:22:30 +00:00
|
|
|
if (doc["authRequired"]) {
|
|
|
|
#ifdef OBS_PASS
|
|
|
|
Serial.println("[OBS] auth requested");
|
|
|
|
|
|
|
|
sha256.reset();
|
|
|
|
sha256.update(OBS_PASS, strlen(OBS_PASS));
|
|
|
|
const char* salt = doc["salt"];
|
|
|
|
sha256.update(salt, strlen(salt));
|
|
|
|
char value[HASH_SIZE];
|
|
|
|
sha256.finalize(value, HASH_SIZE);
|
|
|
|
|
|
|
|
Serial.print("[OBS] sha256 authentication hash is: ");
|
|
|
|
for (size_t i = 0; i < HASH_SIZE; i++) {
|
|
|
|
Serial.print(static_cast<unsigned int>(value[i]), HEX);
|
|
|
|
}
|
|
|
|
Serial.println();
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
int encodedLength = Base64.encodedLength(HASH_SIZE);
|
|
|
|
char encodedPassSaltHash[encodedLength];
|
|
|
|
Base64.encode(encodedPassSaltHash, value, HASH_SIZE);
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
const char* challenge = doc["challenge"];
|
|
|
|
sha256.reset();
|
|
|
|
sha256.update(encodedPassSaltHash, encodedLength);
|
|
|
|
sha256.update(challenge, strlen(challenge));
|
|
|
|
sha256.finalize(value, HASH_SIZE);
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
Serial.print("[OBS] sha256 challenge hash is: ");
|
|
|
|
for (size_t i = 0; i < HASH_SIZE; i++) {
|
|
|
|
Serial.print(static_cast<unsigned int>(value[i]), HEX);
|
|
|
|
}
|
|
|
|
Serial.println();
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
char encodedAuthString[encodedLength];
|
|
|
|
Base64.encode(encodedAuthString, value, HASH_SIZE);
|
2021-12-05 07:46:41 +00:00
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
String authRequest = String("{\"request-type\":\"Authenticate\",\"message-id\":\"2\",\"auth\":\"") + encodedAuthString + "\"}";
|
|
|
|
webSocket.sendTXT(authRequest);
|
|
|
|
#else
|
|
|
|
Serial.println("[OBS] auth requested, but not configured!");
|
|
|
|
set_error();
|
|
|
|
delay(10000);
|
|
|
|
ESP.restart();
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#ifdef OBS_PASS
|
|
|
|
Serial.println("[OBS] auth configured, but not needed");
|
|
|
|
#endif
|
|
|
|
webSocket.sendTXT("{\"request-type\":\"GetCurrentScene\",\"message-id\":\"3\"}");
|
|
|
|
webSocket.sendTXT("{\"request-type\":\"GetPreviewScene\",\"message-id\":\"4\"}");
|
|
|
|
is_currently_connected = true;
|
|
|
|
}
|
2021-12-05 07:46:41 +00:00
|
|
|
break;
|
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
#ifdef OBS_PASS
|
2021-12-05 14:23:39 +00:00
|
|
|
} else if (doc.containsKey("message-id") && doc["message-id"] == "2") {
|
2021-12-05 07:46:41 +00:00
|
|
|
if (strcmp(doc["status"], "ok") == 0) {
|
|
|
|
Serial.println("[OBS] authentication successful");
|
2021-12-05 15:37:39 +00:00
|
|
|
webSocket.sendTXT("{\"request-type\":\"GetCurrentScene\",\"message-id\":\"3\"}");
|
|
|
|
webSocket.sendTXT("{\"request-type\":\"GetPreviewScene\",\"message-id\":\"4\"}");
|
2021-12-12 13:22:30 +00:00
|
|
|
is_currently_connected = true;
|
2021-12-05 07:46:41 +00:00
|
|
|
} else {
|
2021-12-05 14:23:39 +00:00
|
|
|
Serial.println("[OBS] authentication FAILED");
|
2021-12-05 07:46:41 +00:00
|
|
|
set_error();
|
2021-12-12 13:22:30 +00:00
|
|
|
delay(10000);
|
|
|
|
ESP.restart();
|
2021-12-05 07:46:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-12-05 14:23:39 +00:00
|
|
|
#endif
|
2021-12-12 13:22:30 +00:00
|
|
|
} else if (doc.containsKey("sources")) {
|
2021-12-05 07:46:41 +00:00
|
|
|
bool my_source_in_current_event = false;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < doc["sources"].size(); i++) {
|
|
|
|
if (strcmp(doc["sources"][i]["name"], OBS_SOURCE) == 0) {
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.println("[OBS] Source found in current event");
|
2021-12-05 07:46:41 +00:00
|
|
|
my_source_in_current_event = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 15:37:39 +00:00
|
|
|
if ((doc.containsKey("update-type") &&strcmp(doc["update-type"], "SwitchScenes") == 0) || (doc.containsKey("message-id") && strcmp(doc["message-id"], "3") == 0)) {
|
|
|
|
Serial.println("[OBS] program event");
|
2021-12-05 07:46:41 +00:00
|
|
|
if (my_source_in_current_event) {
|
|
|
|
is_currently_live = true;
|
|
|
|
} else {
|
|
|
|
is_currently_live = false;
|
|
|
|
}
|
2021-12-05 15:37:39 +00:00
|
|
|
} else if ((doc.containsKey("update-type") &&strcmp(doc["update-type"], "PreviewSceneChanged") == 0) || (doc.containsKey("message-id") && strcmp(doc["message-id"], "4") == 0)) {
|
|
|
|
Serial.println("[OBS] preview event");
|
2021-12-05 07:46:41 +00:00
|
|
|
if (my_source_in_current_event) {
|
|
|
|
is_currently_preview = true;
|
|
|
|
} else {
|
|
|
|
is_currently_preview = false;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 15:46:34 +00:00
|
|
|
} else if (doc.containsKey("update-type")) {
|
|
|
|
if (strcmp(doc["update-type"], "Exiting") == 0) {
|
|
|
|
Serial.println("[OBS] quit");
|
|
|
|
is_currently_preview = false;
|
|
|
|
is_currently_live = false;
|
2021-12-12 13:22:30 +00:00
|
|
|
is_currently_connected = false;
|
2021-12-05 15:46:34 +00:00
|
|
|
} else if (strcmp(doc["update-type"], "StudioModeSwitched") == 0 && !doc["new-state"]) {
|
|
|
|
Serial.println("[OBS] studio mode disabled");
|
|
|
|
is_currently_preview = false;
|
|
|
|
}
|
2021-12-05 07:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_currently_live) {
|
|
|
|
set_program();
|
|
|
|
} else if (is_currently_preview) {
|
|
|
|
set_preview();
|
|
|
|
} else {
|
|
|
|
set_idle();
|
|
|
|
}
|
2021-12-09 11:34:42 +00:00
|
|
|
|
|
|
|
break;
|
2021-12-05 07:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case WStype_PING:
|
|
|
|
case WStype_PONG:
|
2021-12-05 14:23:39 +00:00
|
|
|
Serial.println("[WS] ping/pong");
|
2021-12-05 07:46:41 +00:00
|
|
|
break;
|
2021-12-05 15:37:39 +00:00
|
|
|
|
|
|
|
case WStype_BIN:
|
|
|
|
case WStype_ERROR:
|
|
|
|
case WStype_FRAGMENT_TEXT_START:
|
|
|
|
case WStype_FRAGMENT_BIN_START:
|
|
|
|
case WStype_FRAGMENT:
|
|
|
|
case WStype_FRAGMENT_FIN:
|
|
|
|
break;
|
2021-12-05 07:46:41 +00:00
|
|
|
}
|
2021-12-12 13:22:30 +00:00
|
|
|
|
|
|
|
#ifdef STATUS_LED
|
|
|
|
if (is_currently_connected) {
|
|
|
|
digitalWrite(STATUS_LED, LOW);
|
|
|
|
} else {
|
|
|
|
digitalWrite(STATUS_LED, HIGH);
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-05 07:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
Serial.println("[Tally] starting up");
|
|
|
|
|
|
|
|
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_COUNT);
|
|
|
|
FastLED.setBrightness(LED_BRIGHTNESS);
|
|
|
|
|
2021-12-12 13:22:30 +00:00
|
|
|
#ifdef STATUS_LED
|
|
|
|
pinMode(STATUS_LED, OUTPUT);
|
|
|
|
#endif
|
|
|
|
|
2021-12-05 07:46:41 +00:00
|
|
|
set_error();
|
2021-12-05 15:37:39 +00:00
|
|
|
delay(100);
|
2021-12-05 07:46:41 +00:00
|
|
|
set_idle();
|
2021-12-05 15:37:39 +00:00
|
|
|
delay(100);
|
2021-12-05 07:46:41 +00:00
|
|
|
set_error();
|
2021-12-05 15:37:39 +00:00
|
|
|
delay(100);
|
2021-12-05 07:46:41 +00:00
|
|
|
set_idle();
|
2021-12-05 15:37:39 +00:00
|
|
|
delay(100);
|
|
|
|
set_error();
|
2021-12-05 07:46:41 +00:00
|
|
|
|
|
|
|
Serial.printf("[Tally] connecting to wifi ssid: %s\n", WIFI_SSID);
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
|
|
|
|
|
|
|
while(WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(250);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println();
|
2021-12-05 14:14:11 +00:00
|
|
|
Serial.print("[Tally] connected to wifi, ip address ");
|
2021-12-05 07:46:41 +00:00
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
2021-12-05 15:37:39 +00:00
|
|
|
set_idle();
|
|
|
|
|
2021-12-05 07:46:41 +00:00
|
|
|
Serial.println("[Tally] connecting to OBS");
|
|
|
|
webSocket.begin(OBS_HOST, OBS_PORT, "/");
|
|
|
|
webSocket.onEvent(handleWebSocketEvent);
|
|
|
|
webSocket.setReconnectInterval(2000);
|
|
|
|
|
|
|
|
Serial.println("[Tally] startup complete");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
webSocket.loop();
|
|
|
|
}
|