147 lines
3.3 KiB
C++
147 lines
3.3 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;
|
|
|
|
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()
|
|
{
|
|
startTime = millis();
|
|
penalties = 0;
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
WiFi.begin(ssid, pass);
|
|
|
|
// initialize mqtt client
|
|
mqtt.begin(client);
|
|
|
|
// connect to wifi, host and mqtt broker
|
|
connect();
|
|
|
|
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);
|
|
|
|
startTime = millis(); // Record the start time
|
|
|
|
// subscribe topic and callback which is called when /hello has come
|
|
mqtt.subscribe("/StatusPing", [](const String &payload, const size_t size)
|
|
{
|
|
Serial.println(payload); });
|
|
}
|
|
|
|
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 (remainingTime <= 0)
|
|
{
|
|
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");
|
|
penalties += penaltyAmount;
|
|
pressed = true;
|
|
}
|
|
else if (pressed && !M5.Touch.ispressed())
|
|
{
|
|
pressed = false;
|
|
}
|
|
}
|
|
|
|
void displayTime(unsigned long minutes, unsigned long seconds)
|
|
{
|
|
M5.Lcd.setCursor(75, 50);
|
|
M5.Lcd.setTextColor(TFT_WHITE);
|
|
M5.Lcd.setTextSize(100);
|
|
M5.Lcd.printf("%02d:%02d", minutes, seconds);
|
|
}
|
|
|
|
void turnScreenRed()
|
|
{
|
|
M5.Lcd.fillScreen(TFT_RED);
|
|
}
|