#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 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){ M5.Lcd.fillScreen(RED); delay(1000); } } 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); initRandomSeed(); digitalWrite(extenderPin, HIGH); // Push out pinMode(extenderPin, OUTPUT_OPEN_DRAIN); pinMode(buttonPin, INPUT_PULLUP); Serial.begin(115200); mathExpression = generateQuestion(); initInputSystem(*onInputDone); } void drawUI(){ if (!hasWon) { M5.Lcd.fillScreen(BLACK); if (!isInputMode) { // Draw expression M5.Lcd.setCursor(10, 50); M5.Lcd.setTextSize(4); M5.Lcd.print(mathExpression); } else { // Draw pressed times M5.Lcd.setCursor(80, 40); M5.Lcd.setTextSize(7); M5.Lcd.print(pressedTimes); } } else { M5.Lcd.fillScreen(GREEN); digitalWrite(extenderPin, LOW); // Wait for reset click while (digitalRead(buttonPin) == HIGH){ delay(10); } while (digitalRead(buttonPin) == LOW) { delay(10); } // Reset digitalWrite(extenderPin, HIGH); mathExpression = generateQuestion(); hasWon = false; } } void loop() { updateInput(); #ifdef UseMQTT mqtt.update(); #endif if (isInputMode || millis() % UIRefreshDelay < 100) drawUI(); delay(25); }