|
| 1 | +/**************************************************************************************************************************** |
| 2 | + FullyFeatured_QNEthernet.ino |
| 3 | + |
| 4 | + AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, Teensy41_QNEthernet_PubF7, etc. with current AsyncTCP support |
| 5 | + |
| 6 | + Based on and modified from : |
| 7 | + |
| 8 | + 1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client) |
| 9 | + |
| 10 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +#include "defines.h" |
| 14 | + |
| 15 | +// Check connection every 1s |
| 16 | +#define MQTT_CHECK_INTERVAL_MS 1000 |
| 17 | + |
| 18 | +#include <AsyncMqtt_Generic.h> |
| 19 | + |
| 20 | +#include <Ticker.h> // https://github.com/sstaub/Ticker |
| 21 | + |
| 22 | +//#define MQTT_HOST IPAddress(192, 168, 2, 110) |
| 23 | +#define MQTT_HOST "broker.emqx.io" // Broker address |
| 24 | +#define MQTT_PORT 1883 |
| 25 | + |
| 26 | +const char *PubTopic = "async-mqtt/Teensy41_QNEthernet_Pub"; // Topic to publish |
| 27 | + |
| 28 | +AsyncMqttClient mqttClient; |
| 29 | + |
| 30 | +void connectToMqtt(); |
| 31 | +void connectToMqttCheck(); |
| 32 | + |
| 33 | +// Repeat forever, millis() resolution |
| 34 | +Ticker connectToMqttTicker(connectToMqttCheck, MQTT_CHECK_INTERVAL_MS, 0, MILLIS); |
| 35 | + |
| 36 | +bool connectedEthernet = false; |
| 37 | +bool connectedMQTT = false; |
| 38 | + |
| 39 | +void connectToMqttCheck() |
| 40 | +{ |
| 41 | + if (Ethernet.linkStatus() == LinkON) |
| 42 | + { |
| 43 | + //Ethernet.maintain(); |
| 44 | + |
| 45 | + if (!connectedMQTT) |
| 46 | + { |
| 47 | + mqttClient.connect(); |
| 48 | + } |
| 49 | + |
| 50 | + if (!connectedEthernet) |
| 51 | + { |
| 52 | + Serial.println("Ethernet reconnected."); |
| 53 | + connectedEthernet = true; |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + if (connectedEthernet) |
| 59 | + { |
| 60 | + Serial.println("Ethernet disconnected"); |
| 61 | + connectedEthernet = false; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void connectToMqtt() |
| 67 | +{ |
| 68 | + Serial.println("Connecting to MQTT..."); |
| 69 | + mqttClient.connect(); |
| 70 | +} |
| 71 | + |
| 72 | +void printSeparationLine() |
| 73 | +{ |
| 74 | + Serial.println("************************************************"); |
| 75 | +} |
| 76 | + |
| 77 | +void onMqttConnect(bool sessionPresent) |
| 78 | +{ |
| 79 | + Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST); |
| 80 | + Serial.print(", port: "); Serial.println(MQTT_PORT); |
| 81 | + Serial.print("PubTopic: "); Serial.println(PubTopic); |
| 82 | + |
| 83 | + connectedMQTT = true; |
| 84 | + |
| 85 | + printSeparationLine(); |
| 86 | + Serial.print("Session present: "); Serial.println(sessionPresent); |
| 87 | + |
| 88 | + uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2); |
| 89 | + Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub); |
| 90 | + |
| 91 | + mqttClient.publish(PubTopic, 0, true, "Teensy41_QNEthernet_Pub Test1"); |
| 92 | + Serial.println("Publishing at QoS 0"); |
| 93 | + |
| 94 | + uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "Teensy41_QNEthernet_Pub Test2"); |
| 95 | + Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1); |
| 96 | + |
| 97 | + uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "Teensy41_QNEthernet_Pub Test3"); |
| 98 | + Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2); |
| 99 | + |
| 100 | + printSeparationLine(); |
| 101 | +} |
| 102 | + |
| 103 | +void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) |
| 104 | +{ |
| 105 | + (void) reason; |
| 106 | + |
| 107 | + connectedMQTT = false; |
| 108 | + |
| 109 | + Serial.println("Disconnected from MQTT."); |
| 110 | +} |
| 111 | + |
| 112 | +void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos) |
| 113 | +{ |
| 114 | + Serial.println("Subscribe acknowledged."); |
| 115 | + Serial.print(" packetId: "); Serial.println(packetId); |
| 116 | + Serial.print(" qos: "); Serial.println(qos); |
| 117 | +} |
| 118 | + |
| 119 | +void onMqttUnsubscribe(const uint16_t& packetId) |
| 120 | +{ |
| 121 | + Serial.println("Unsubscribe acknowledged."); |
| 122 | + Serial.print(" packetId: "); Serial.println(packetId); |
| 123 | +} |
| 124 | + |
| 125 | +void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties, |
| 126 | + const size_t& len, const size_t& index, const size_t& total) |
| 127 | +{ |
| 128 | + char message[len + 1]; |
| 129 | + |
| 130 | + memcpy(message, payload, len); |
| 131 | + message[len] = 0; |
| 132 | + |
| 133 | + Serial.println("Publish received."); |
| 134 | + Serial.print(" topic: "); Serial.println(topic); |
| 135 | + Serial.print(" message: "); Serial.println(message); |
| 136 | + Serial.print(" qos: "); Serial.println(properties.qos); |
| 137 | + Serial.print(" dup: "); Serial.println(properties.dup); |
| 138 | + Serial.print(" retain: "); Serial.println(properties.retain); |
| 139 | + Serial.print(" len: "); Serial.println(len); |
| 140 | + Serial.print(" index: "); Serial.println(index); |
| 141 | + Serial.print(" total: "); Serial.println(total); |
| 142 | +} |
| 143 | + |
| 144 | +void onMqttPublish(const uint16_t& packetId) |
| 145 | +{ |
| 146 | + Serial.println("Publish acknowledged."); |
| 147 | + Serial.print(" packetId: "); Serial.println(packetId); |
| 148 | +} |
| 149 | + |
| 150 | +void setup() |
| 151 | +{ |
| 152 | + Serial.begin(115200); |
| 153 | + while (!Serial); |
| 154 | + |
| 155 | + delay(500); |
| 156 | + |
| 157 | + Serial.print("\nStarting FullyFeatured_QNEthernet on "); Serial.println(BOARD_NAME); |
| 158 | + Serial.println(ASYNC_MQTT_GENERIC_VERSION); |
| 159 | + |
| 160 | +#if USING_DHCP |
| 161 | + // Start the Ethernet connection, using DHCP |
| 162 | + Serial.print("Initialize Ethernet using DHCP => "); |
| 163 | + Ethernet.begin(); |
| 164 | +#else |
| 165 | + // Start the Ethernet connection, using static IP |
| 166 | + Serial.print("Initialize Ethernet using static IP => "); |
| 167 | + Ethernet.begin(myIP, myNetmask, myGW); |
| 168 | + Ethernet.setDNSServerIP(mydnsServer); |
| 169 | +#endif |
| 170 | + |
| 171 | + if (!Ethernet.waitForLocalIP(5000)) |
| 172 | + { |
| 173 | + Serial.println(F("Failed to configure Ethernet")); |
| 174 | + |
| 175 | + if (!Ethernet.linkStatus()) |
| 176 | + { |
| 177 | + Serial.println(F("Ethernet cable is not connected.")); |
| 178 | + } |
| 179 | + |
| 180 | + // Stay here forever |
| 181 | + while (true) |
| 182 | + { |
| 183 | + delay(1); |
| 184 | + } |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + Serial.print(F("Connected! IP address:")); Serial.println(Ethernet.localIP()); |
| 189 | + } |
| 190 | + |
| 191 | +#if USING_DHCP |
| 192 | + delay(1000); |
| 193 | +#else |
| 194 | + delay(2000); |
| 195 | +#endif |
| 196 | + |
| 197 | + connectedEthernet = true; |
| 198 | + |
| 199 | + mqttClient.onConnect(onMqttConnect); |
| 200 | + mqttClient.onDisconnect(onMqttDisconnect); |
| 201 | + mqttClient.onSubscribe(onMqttSubscribe); |
| 202 | + mqttClient.onUnsubscribe(onMqttUnsubscribe); |
| 203 | + mqttClient.onMessage(onMqttMessage); |
| 204 | + mqttClient.onPublish(onMqttPublish); |
| 205 | + |
| 206 | + mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
| 207 | + |
| 208 | + connectToMqttTicker.start(); //start the ticker. |
| 209 | + |
| 210 | + connectToMqtt(); |
| 211 | +} |
| 212 | + |
| 213 | +void loop() |
| 214 | +{ |
| 215 | + connectToMqttTicker.update(); //update the ticker. |
| 216 | +} |
0 commit comments