Last active
November 28, 2021 14:29
-
-
Save prohazko2/9b76399bf63ad9f1d40b2f891074a6a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* from: https://github.com/marvinroger/async-mqtt-client/blob/v0.9.0/examples/FullyFeatured-ESP8266/FullyFeatured-ESP8266.ino | |
*/ | |
/* | |
platform = espressif8266 | |
lib_deps = marvinroger/AsyncMqttClient@^0.9.0 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <Ticker.h> | |
#include <AsyncMqttClient.h> | |
#define WIFI_SSID "<wifi-ssid>" | |
#define WIFI_PASSWORD "<wifi-password>" | |
#define MQTT_HOST "dev.rightech.io" | |
#define MQTT_PORT 1883 | |
#define MQTT_CLIENT_ID "<client-id>" | |
#define PUB_DELAY (5 * 1000) | |
AsyncMqttClient mqttClient; | |
Ticker mqttReconnectTimer; | |
WiFiEventHandler wifiConnectHandler; | |
WiFiEventHandler wifiDisconnectHandler; | |
Ticker wifiReconnectTimer; | |
void connectToWifi() | |
{ | |
Serial.println("Connecting to Wi-Fi..."); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
} | |
void connectToMqtt() | |
{ | |
Serial.println("Connecting to MQTT..."); | |
mqttClient.connect(); | |
} | |
void onWifiConnect(const WiFiEventStationModeGotIP &event) | |
{ | |
Serial.println("Connected to Wi-Fi."); | |
connectToMqtt(); | |
} | |
void onWifiDisconnect(const WiFiEventStationModeDisconnected &event) | |
{ | |
Serial.println("Disconnected from Wi-Fi."); | |
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi | |
wifiReconnectTimer.once(2, connectToWifi); | |
} | |
void onMqttConnect(bool sessionPresent) | |
{ | |
Serial.println("Connected to MQTT."); | |
Serial.print("Session present: "); | |
Serial.println(sessionPresent); | |
uint16_t packetIdSub = mqttClient.subscribe("test/lol", 2); | |
Serial.print("Subscribing at QoS 2, packetId: "); | |
Serial.println(packetIdSub); | |
} | |
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) | |
{ | |
int r = static_cast<uint8_t>(reason); | |
Serial.println("Disconnected from MQTT."); | |
Serial.print(" reason: "); | |
Serial.println(r); | |
if (WiFi.isConnected()) | |
{ | |
mqttReconnectTimer.once(2, connectToMqtt); | |
} | |
} | |
void onMqttSubscribe(uint16_t packetId, uint8_t qos) | |
{ | |
Serial.println("Subscribe acknowledged."); | |
Serial.print(" packetId: "); | |
Serial.println(packetId); | |
Serial.print(" qos: "); | |
Serial.println(qos); | |
} | |
void onMqttUnsubscribe(uint16_t packetId) | |
{ | |
Serial.println("Unsubscribe acknowledged."); | |
Serial.print(" packetId: "); | |
Serial.println(packetId); | |
} | |
void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) | |
{ | |
Serial.println("Publish received."); | |
Serial.print(" topic: "); | |
Serial.println(topic); | |
} | |
void onMqttPublish(uint16_t packetId) | |
{ | |
Serial.println("Publish acknowledged."); | |
Serial.print(" packetId: "); | |
Serial.println(packetId); | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
Serial.println(); | |
Serial.println(); | |
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); | |
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); | |
mqttClient.onConnect(onMqttConnect); | |
mqttClient.onDisconnect(onMqttDisconnect); | |
mqttClient.onSubscribe(onMqttSubscribe); | |
mqttClient.onUnsubscribe(onMqttUnsubscribe); | |
mqttClient.onMessage(onMqttMessage); | |
mqttClient.onPublish(onMqttPublish); | |
mqttClient.setServer(MQTT_HOST, MQTT_PORT); | |
mqttClient.setClientId(MQTT_CLIENT_ID); | |
connectToWifi(); | |
} | |
int n = 1; | |
long last = 0; | |
void loop() | |
{ | |
long now = millis(); | |
if (mqttClient.connected() && (now - last > PUB_DELAY)) | |
{ | |
uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, String(n++).c_str()); | |
Serial.print("Publishing at QoS 1, packetId: "); | |
Serial.println(packetIdPub1); | |
last = now; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment