From 32605f504a16fbf7368e1c08f2af2f0cbc5d2178 Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Wed, 15 May 2024 17:08:20 +0200 Subject: [PATCH] correcting timer mqtt --- ModuleTimer/ModuleTimer.ino | 7 +--- mqttExample/mqttExample.ino | 77 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/ModuleTimer/ModuleTimer.ino b/ModuleTimer/ModuleTimer.ino index cef003b..ca6c1ec 100644 --- a/ModuleTimer/ModuleTimer.ino +++ b/ModuleTimer/ModuleTimer.ino @@ -85,14 +85,9 @@ void setup() 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) + mqtt.subscribe("/StatusPing", [](const String &payload, const size_t size) { - Serial.print("/hello "); Serial.println(payload); }); } diff --git a/mqttExample/mqttExample.ino b/mqttExample/mqttExample.ino index e69de29..8e93777 100644 --- a/mqttExample/mqttExample.ino +++ b/mqttExample/mqttExample.ino @@ -0,0 +1,77 @@ +#include +#include // include before MQTTPubSubClient.h +#include + +const char* ssid = "AIRPORT-FREE-WIFI-NEW"; +const char* pass = "alexalex"; + +WebSocketsClient client; +MQTTPubSubClient mqtt; + +void connect() { +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!"); +} + +void setup() { + Serial.begin(115200); + WiFi.begin(ssid, pass); + + // initialize mqtt client + mqtt.begin(client); + + // connect to wifi, host and mqtt broker + connect(); + + // 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); + }); +} + +void loop() { + mqtt.update(); // should be called + + if (!mqtt.isConnected()) { + connect(); + } + + // // publish message + // static uint32_t prev_ms = millis(); + // if (millis() > prev_ms + 1000) { + // prev_ms = millis(); + // mqtt.publish("/hello", "world"); + // } +}