Started mqtt
This commit is contained in:
parent
a5c1f4b734
commit
da088d5549
|
@ -1,3 +1,16 @@
|
||||||
|
#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>
|
#include <M5Core2.h>
|
||||||
|
|
||||||
#define penaltyAmount 30 * 1000
|
#define penaltyAmount 30 * 1000
|
||||||
|
@ -7,6 +20,44 @@ unsigned long startTime;
|
||||||
const unsigned long countdownDuration = 5 * 60 * 1000; // 5 minutes in milliseconds
|
const unsigned long countdownDuration = 5 * 60 * 1000; // 5 minutes in milliseconds
|
||||||
unsigned long penalties = 0;
|
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()
|
void reset()
|
||||||
{
|
{
|
||||||
startTime = millis();
|
startTime = millis();
|
||||||
|
@ -15,17 +66,43 @@ void reset()
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
WiFi.begin(ssid, pass);
|
||||||
|
|
||||||
|
// initialize mqtt client
|
||||||
|
mqtt.begin(client);
|
||||||
|
|
||||||
|
// connect to wifi, host and mqtt broker
|
||||||
|
connect();
|
||||||
|
|
||||||
M5.begin();
|
M5.begin();
|
||||||
M5.Lcd.begin();
|
M5.Lcd.begin();
|
||||||
M5.Lcd.fillScreen(TFT_BLACK); // Set initial background color
|
M5.Lcd.fillScreen(TFT_BLACK); // Set initial background color
|
||||||
startTime = millis(); // Record the start time
|
M5.Lcd.setCursor(0, 0);
|
||||||
|
M5.Lcd.setTextColor(WHITE);
|
||||||
|
M5.Lcd.setTextSize(7);
|
||||||
|
M5.Lcd.println("Loading...");
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
Serial.begin(9600);
|
startTime = millis(); // Record the start time
|
||||||
|
|
||||||
|
// subscribe callback which is called when every packet has come
|
||||||
|
mqtt.subscribe([](const String &topic, const String &payload, const size_t size)
|
||||||
|
{ Serial.println("mqtt received: " + topic + " - " + payload); });
|
||||||
|
|
||||||
|
// subscribe topic and callback which is called when /hello has come
|
||||||
|
mqtt.subscribe("/hello", [](const String &payload, const size_t size)
|
||||||
|
{
|
||||||
|
Serial.print("/hello ");
|
||||||
|
Serial.println(payload); });
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pressed = false;
|
bool pressed = false;
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
#ifdef UseMQTT
|
||||||
|
mqtt.update();
|
||||||
|
#endif
|
||||||
|
|
||||||
unsigned long elapsedTime = millis() - startTime + penalties;
|
unsigned long elapsedTime = millis() - startTime + penalties;
|
||||||
unsigned long remainingTime = countdownDuration - elapsedTime;
|
unsigned long remainingTime = countdownDuration - elapsedTime;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue