diff --git a/ModuleMath/InputSystem.h b/ModuleMath/InputSystem.h index 5bb1636..88c2f5f 100644 --- a/ModuleMath/InputSystem.h +++ b/ModuleMath/InputSystem.h @@ -1,4 +1,4 @@ -#include +#include #define buttonPin 25 #define inputPauseRegisterMills 1500 diff --git a/ModuleMath/MathSystem.h b/ModuleMath/MathSystem.h index 554b80b..05bc246 100644 --- a/ModuleMath/MathSystem.h +++ b/ModuleMath/MathSystem.h @@ -1,4 +1,4 @@ -#include +#include // -1 if error int a = 0; diff --git a/ModuleMath/ModuleMath.ino b/ModuleMath/ModuleMath.ino index b2936a2..c01d317 100644 --- a/ModuleMath/ModuleMath.ino +++ b/ModuleMath/ModuleMath.ino @@ -1,21 +1,73 @@ +#define UseMQTT +#ifdef UseMQTT +#include +#include // include before MQTTPubSubClient.h +#include + +const char *ssid = "AIRPORT-FREE-WIFI-NEW"; +const char *pass = "alexalex"; + +WebSocketsClient client; +MQTTPubSubClient mqtt; +#endif + #include #include "InputSystem.h" #include "MathSystem.h" #define extenderPin 26 -#define buttonPin 25 +#define buttonPin 37 #define UIRefreshDelay 250 String mathExpression = ""; bool hasWon = false; +void connect() +{ +#ifdef UseMQTT +connect_to_wifi: + Serial.print("connecting to wifi..."); + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(1000); + } + Serial.println(" connected!"); + +connect_to_host: + Serial.println("connecting to host..."); + client.disconnect(); + client.begin("gitea.deprived.dev", 6667, "/", "mqtt"); // "mqtt" is required + client.setReconnectInterval(2000); + + Serial.print("connecting to mqtt broker..."); + while (!mqtt.connect("Module-Math", "alex", "1111")) + { + Serial.print("."); + delay(1000); + if (WiFi.status() != WL_CONNECTED) + { + Serial.println("WiFi disconnected"); + goto connect_to_wifi; + } + if (!client.isConnected()) + { + Serial.println("WebSocketsClient disconnected"); + goto connect_to_host; + } + } + Serial.println(" connected!"); +#endif +} + void onInputDone(int inputNum){ Serial.println("Reveiced: " + String(inputNum)); Serial.println("Target: " + String(targetResult)); if (inputNum == targetResult){ hasWon = true; + mqtt.publish("/DDUStatusPing", "winMath"); } if (!hasWon){ @@ -25,6 +77,16 @@ void onInputDone(int inputNum){ } void setup() { + #ifdef UseMQTT + WiFi.begin(ssid, pass); + + // initialize mqtt client + mqtt.begin(client); + + // connect to wifi, host and mqtt broker + connect(); + #endif + M5.begin(); M5.Lcd.setRotation(1); @@ -83,6 +145,9 @@ void drawUI(){ void loop() { updateInput(); +#ifdef UseMQTT + mqtt.update(); +#endif if (isInputMode || millis() % UIRefreshDelay < 100) drawUI(); diff --git a/ModuleTimer/ModuleTimer.ino b/ModuleTimer/ModuleTimer.ino index ca6c1ec..b4d6378 100644 --- a/ModuleTimer/ModuleTimer.ino +++ b/ModuleTimer/ModuleTimer.ino @@ -20,6 +20,8 @@ unsigned long startTime; const unsigned long countdownDuration = 5 * 60 * 1000; // 5 minutes in milliseconds unsigned long penalties = 0; +bool stopTimer = false; + void connect() { #ifdef UseMQTT @@ -60,10 +62,28 @@ connect_to_host: void reset() { + stopTimer = false; startTime = millis(); penalties = 0; } +void penalize(){ + penalties += penaltyAmount; + M5.Lcd.fillScreen(RED); +} + +void handleDDUStatusPing(const String &payload, const size_t size){ + int index = payload.indexOf("wrong"); + if (index != -1) + { + // Extract t ext following "wrong" + String textBehindWrong = payload.substring(index + 5); // Assuming "wrong" is 5 characters long + Serial.println("Module wrong: " + textBehindWrong); + + penalize(); + } +} + void setup() { WiFi.begin(ssid, pass); @@ -83,12 +103,11 @@ void setup() M5.Lcd.println("Loading..."); Serial.begin(115200); - startTime = millis(); // Record the start time - + startTime = millis(); + // subscribe topic and callback which is called when /hello has come - mqtt.subscribe("/StatusPing", [](const String &payload, const size_t size) - { - Serial.println(payload); }); + mqtt.subscribe("/DDUStatusPing", [](const String &payload, const size_t size) + { handleDDUStatusPing(payload, size); }); } bool pressed = false; @@ -102,8 +121,9 @@ void loop() unsigned long remainingTime = countdownDuration - elapsedTime; // Check if the time has run out - if (remainingTime <= 0) + if (stopTimer || remainingTime <= 0) { + stopTimer = true; displayTime(0, 0); // Display 00:00 turnScreenRed(); return; @@ -122,8 +142,8 @@ void loop() if (!pressed && M5.Touch.ispressed()) { - Serial.println("Pressed"); - penalties += penaltyAmount; + //Serial.println("Pressed"); + penalize(); pressed = true; } else if (pressed && !M5.Touch.ispressed()) @@ -134,7 +154,7 @@ void loop() void displayTime(unsigned long minutes, unsigned long seconds) { - M5.Lcd.setCursor(75, 50); + M5.Lcd.setCursor(60, 50); M5.Lcd.setTextColor(TFT_WHITE); M5.Lcd.setTextSize(100); M5.Lcd.printf("%02d:%02d", minutes, seconds);