DDU_Exammen/ModuleTimer/ModuleTimer.ino

167 lines
3.8 KiB
C++

#define UseMQTT
#ifdef UseMQTT
#include <WiFi.h>
#include <WebSocketsClient.h> // include before MQTTPubSubClient.h
#include <MQTTPubSubClient.h>
const char *ssid = "AIRPORT-FREE-WIFI-NEW";
const char *pass = "alexalex";
WebSocketsClient client;
MQTTPubSubClient mqtt;
#endif
#include <M5Core2.h>
#define penaltyAmount 30 * 1000
#define drawInterval 1000
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
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(String(millis()), "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 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()
{
M5.begin();
M5.Lcd.begin();
M5.Lcd.fillScreen(TFT_BLACK); // Set initial background color
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(7);
M5.Lcd.println("Loading...");
Serial.begin(115200);
WiFi.begin(ssid, pass);
// initialize mqtt client
mqtt.begin(client);
// connect to wifi, host and mqtt broker
connect();
startTime = millis();
// subscribe topic and callback which is called when /hello has come
mqtt.subscribe("/DDUStatusPing", [](const String &payload, const size_t size)
{ handleDDUStatusPing(payload, size); });
}
bool pressed = false;
void loop()
{
#ifdef UseMQTT
mqtt.update();
#endif
unsigned long elapsedTime = millis() - startTime + penalties;
unsigned long remainingTime = countdownDuration - elapsedTime;
// Check if the time has run out
if (stopTimer || remainingTime <= 0)
{
stopTimer = true;
displayTime(0, 0); // Display 00:00
turnScreenRed();
return;
}
// Calculate remaining minutes and seconds
unsigned long minutes = remainingTime / (60 * 1000);
unsigned long seconds = (remainingTime / 1000) % 60;
// Display remaining time
if (millis() % drawInterval < 10)
{
M5.Lcd.fillScreen(BLACK);
displayTime(minutes, seconds);
}
if (!pressed && M5.Touch.ispressed())
{
//Serial.println("Pressed");
penalize();
pressed = true;
}
else if (pressed && !M5.Touch.ispressed())
{
pressed = false;
}
}
void displayTime(unsigned long minutes, unsigned long seconds)
{
M5.Lcd.setCursor(60, 50);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.setTextSize(100);
M5.Lcd.printf("%02d:%02d", minutes, seconds);
}
void turnScreenRed()
{
M5.Lcd.fillScreen(TFT_RED);
}