correcting timer mqtt
This commit is contained in:
parent
da088d5549
commit
32605f504a
|
@ -85,14 +85,9 @@ void setup()
|
||||||
|
|
||||||
startTime = millis(); // Record the start time
|
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
|
// 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); });
|
Serial.println(payload); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
#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;
|
||||||
|
|
||||||
|
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");
|
||||||
|
// }
|
||||||
|
}
|
Loading…
Reference in New Issue