123 lines
3.4 KiB
Arduino
123 lines
3.4 KiB
Arduino
|
#include <ESP8266WiFi.h>
|
||
|
#include <Ticker.h>
|
||
|
#include <AsyncMqttClient.h>
|
||
|
|
||
|
#define WIFI_SSID "My_Wi-Fi"
|
||
|
#define WIFI_PASSWORD "my-awesome-password"
|
||
|
|
||
|
#define MQTT_HOST IPAddress(192, 168, 1, 10)
|
||
|
#define MQTT_PORT 1883
|
||
|
|
||
|
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 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 connectToMqtt() {
|
||
|
Serial.println("Connecting to MQTT...");
|
||
|
mqttClient.connect();
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
mqttClient.publish("test/lol", 0, true, "test 1");
|
||
|
Serial.println("Publishing at QoS 0");
|
||
|
uint16_t packetIdPub1 = mqttClient.publish("test/lol", 1, true, "test 2");
|
||
|
Serial.print("Publishing at QoS 1, packetId: ");
|
||
|
Serial.println(packetIdPub1);
|
||
|
uint16_t packetIdPub2 = mqttClient.publish("test/lol", 2, true, "test 3");
|
||
|
Serial.print("Publishing at QoS 2, packetId: ");
|
||
|
Serial.println(packetIdPub2);
|
||
|
}
|
||
|
|
||
|
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||
|
Serial.println("Disconnected from MQTT.");
|
||
|
|
||
|
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);
|
||
|
Serial.print(" qos: ");
|
||
|
Serial.println(properties.qos);
|
||
|
Serial.print(" dup: ");
|
||
|
Serial.println(properties.dup);
|
||
|
Serial.print(" retain: ");
|
||
|
Serial.println(properties.retain);
|
||
|
Serial.print(" len: ");
|
||
|
Serial.println(len);
|
||
|
Serial.print(" index: ");
|
||
|
Serial.println(index);
|
||
|
Serial.print(" total: ");
|
||
|
Serial.println(total);
|
||
|
}
|
||
|
|
||
|
void onMqttPublish(uint16_t packetId) {
|
||
|
Serial.println("Publish acknowledged.");
|
||
|
Serial.print(" packetId: ");
|
||
|
Serial.println(packetId);
|
||
|
}
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(115200);
|
||
|
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);
|
||
|
|
||
|
connectToWifi();
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
}
|