diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/include/config_example.h b/include/config_example.h new file mode 100644 index 0000000..d27865f --- /dev/null +++ b/include/config_example.h @@ -0,0 +1,30 @@ +// Type of RGB LED, adjusts the brightness calculations. +#define LED_TYPE 0 // 0 = Anode, 1 = Cathode + +// GPIO pins for each colour +#define RED_LED_PIN 4 +#define GREEN_LED_PIN 0 +#define BLUE_LED_PIN 2 + +// Defines how many gradients a PWM pin has on this device, for ESP devices +// its 1024, on other systems its 255. Adjust as needed if you don't get +// accurate colour representation. +#define PWM_MAX_VALUE 1024 + +// WIFI connection details +#define WIFI_SSID "ssid" +#define WIFI_PASS "s3cur3" + +// Enables the webserver +#define WEBSERVER + +// Enable MQTT connection (not complete) +//#define ENABLE_MQTT +#define MQTT_SERVER "mqtt.local" +#define MQTT_PORT 1883 + +// Enable Telegram connection +//#define ENABLE_TELEGRAM +#define TELEGRAM_BOT_TOKEN "xxx:xxx" + + diff --git a/include/statuses.h b/include/statuses.h new file mode 100644 index 0000000..42b0642 --- /dev/null +++ b/include/statuses.h @@ -0,0 +1,18 @@ +struct status +{ + char name[20]; + int r; + int g; + int b; + int blink; +}; + +struct status statuses[] = { + {"busy", 255, 0, 0, 0}, + {"available", 0, 255, 0, 0}, + {"away", 150, 255, 0, 0}, + {"ooo", 255, 0, 255, 0}, + {"offline", 0, 0, 0, 0}, + {"blue", 0, 0, 255, 10} +}; +#define STATUSES_COUNT 6 \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 63d19db..166f854 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,4 +11,8 @@ [env:wemos-d1] platform = espressif8266 board = d1 +upload_speed = 460800 framework = arduino +lib_deps = + witnessmenow/UniversalTelegramBot@^1.3.0 + knolleary/PubSubClient@^2.8 diff --git a/src/busylight.cpp b/src/busylight.cpp index 3e4ec64..d5a3f5a 100644 --- a/src/busylight.cpp +++ b/src/busylight.cpp @@ -1,49 +1,36 @@ // Busylight - A web accessible busylight // (c) 2021 Andrew Williams -// This code is licensed under MIT license (see LICENSE.txt for details) +// This code is licensed under MIT license (see LICENSE for details) #include #include #include -#ifdef WEBSERVER -#include -#endif -#ifdef MQTT -#include -#endif - -#define PWM_MAX_VALUE 1024 - -struct status -{ - char name[20]; - int r; - int g; - int b; - int blink; -}; - -struct status statuses[] = { - {"busy", 255, 0, 0, 0}, - {"available", 0, 255, 0, 0}, - {"away", 150, 255, 0, 0}, - {"ooo", 255, 0, 255, 0}, - {"offline", 0, 0, 0, 0}, - {"blue", 0, 0, 255, 10} -}; -#define STATUSES_COUNT 6 - -//##################################### +#include WiFiClient wifi; -#ifdef WEBSERVER -ESP8266WebServer server; -#endif -#ifdef MQTT -PubSubClient pubsub(wifi); -#endif +WiFiClientSecure wifi_secure; char current_state[20]; +#ifdef ENABLE_WEBSERVER +#include +ESP8266WebServer server; +#endif + +#ifdef ENABLE_MQTT +#include +PubSubClient pubsub(wifi); +#endif + +#ifdef ENABLE_TELEGRAM +#include + +X509List cert(TELEGRAM_CERTIFICATE_ROOT); +UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, wifi_secure); +const unsigned long BOT_MTBS = 1000; +unsigned long telegram_bot_last_time; +#endif + +// Calculates the PWM value for the RGB LED, taking into account Anode/Cathode int RGB_calc(int value) { #if LED_TYPE == 0 return PWM_MAX_VALUE - ((value / 255.f) * PWM_MAX_VALUE); @@ -52,6 +39,7 @@ int RGB_calc(int value) { #endif } +// Writes out the RBG colours to the pins void RGB_color(int r, int g, int b) { analogWrite(RED_LED_PIN, RGB_calc(r)); @@ -59,6 +47,7 @@ void RGB_color(int r, int g, int b) analogWrite(BLUE_LED_PIN, RGB_calc(b)); } +// Switches status based on a status struct provided void Switch_Status(struct status status) { if (status.blink > 0) { bool bstate = false; @@ -77,6 +66,7 @@ void Switch_Status(struct status status) { strcpy(current_state, status.name); } +// Switches status based on a name of a status struct bool Switch_Status(String name) { Serial.println("Switching to " + name); for(int j=0; j BOT_MTBS) { + int msg_count = bot.getUpdates(bot.last_message_received + 1); + while (msg_count) { + Serial.println("Recevied Telegram Message"); + Telegram_Message(msg_count); + msg_count = bot.getUpdates(bot.last_message_received + 1); + } + telegram_bot_last_time = millis(); + } +#endif + +#ifdef ENABLE_WEBSERVER server.handleClient(); #endif }