|
| 1 | +/**************************************************************************************************************************** |
| 2 | + FullyFeatured_RP2040W.ino |
| 3 | + |
| 4 | + AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, Teensy41_QNEthernet, STM32F7, etc. |
| 5 | + with current AsyncTCP support |
| 6 | + |
| 7 | + Based on and modified from : |
| 8 | + |
| 9 | + 1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client) |
| 10 | + |
| 11 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic |
| 12 | + *****************************************************************************************************************************/ |
| 13 | + |
| 14 | +#include "defines.h" |
| 15 | + |
| 16 | +#include <WiFi.h> |
| 17 | +#include <Ticker.h> |
| 18 | + |
| 19 | +// Check connection every 1s |
| 20 | +#define MQTT_CHECK_INTERVAL_MS 1000 |
| 21 | + |
| 22 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
| 23 | +#include <AsyncMqtt_Generic.h> |
| 24 | + |
| 25 | +#include <Ticker.h> // https://github.com/sstaub/Ticker |
| 26 | + |
| 27 | +//#define MQTT_HOST IPAddress(192, 168, 2, 110) |
| 28 | +#define MQTT_HOST "broker.emqx.io" // Broker address |
| 29 | +#define MQTT_PORT 1883 |
| 30 | + |
| 31 | +const char *PubTopic = "async-mqtt/RP2040W_Pub"; // Topic to publish |
| 32 | + |
| 33 | +AsyncMqttClient mqttClient; |
| 34 | + |
| 35 | +int status = WL_IDLE_STATUS; |
| 36 | + |
| 37 | +void connectToMqtt(); |
| 38 | +void connectToMqttLoop(); |
| 39 | + |
| 40 | +// Repeat forever, millis() resolution |
| 41 | +Ticker connectToMqttTicker(connectToMqttLoop, MQTT_CHECK_INTERVAL_MS, 0, MILLIS); |
| 42 | + |
| 43 | +bool connectedWiFi = false; |
| 44 | +bool connectedMQTT = false; |
| 45 | + |
| 46 | +void printWifiStatus() |
| 47 | +{ |
| 48 | + // print the SSID of the network you're attached to: |
| 49 | + Serial.print("Connected to SSID: "); |
| 50 | + Serial.println(WiFi.SSID()); |
| 51 | + |
| 52 | + // print your board's IP address: |
| 53 | + IPAddress ip = WiFi.localIP(); |
| 54 | + Serial.print("Local IP Address: "); |
| 55 | + Serial.println(ip); |
| 56 | + |
| 57 | + // print the received signal strength: |
| 58 | + long rssi = WiFi.RSSI(); |
| 59 | + Serial.print("Signal strength (RSSI):"); |
| 60 | + Serial.print(rssi); |
| 61 | + Serial.println(" dBm"); |
| 62 | +} |
| 63 | + |
| 64 | +bool connectToWifi() |
| 65 | +{ |
| 66 | + // check for the WiFi module: |
| 67 | + if (WiFi.status() == WL_NO_MODULE) |
| 68 | + { |
| 69 | + Serial.println("Communication with WiFi module failed!"); |
| 70 | + // don't continue |
| 71 | + while (true); |
| 72 | + } |
| 73 | + |
| 74 | + Serial.print(F("Connecting to SSID: ")); Serial.println(WIFI_SSID); |
| 75 | + |
| 76 | +#define MAX_NUM_WIFI_CONNECT_TRIES_PER_LOOP 20 |
| 77 | + |
| 78 | + uint8_t numWiFiConnectTries = 0; |
| 79 | + |
| 80 | + // attempt to connect to WiFi network |
| 81 | + while ( (status != WL_CONNECTED) && (numWiFiConnectTries++ < MAX_NUM_WIFI_CONNECT_TRIES_PER_LOOP) ) |
| 82 | + { |
| 83 | + status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 84 | + |
| 85 | + delay(500); |
| 86 | + } |
| 87 | + |
| 88 | + if (status != WL_CONNECTED) |
| 89 | + { |
| 90 | + // Restart for Portenta as something is very wrong |
| 91 | + Serial.println("Resetting. Can't connect to any WiFi"); |
| 92 | + |
| 93 | + NVIC_SystemReset(); |
| 94 | + } |
| 95 | + |
| 96 | + printWifiStatus(); |
| 97 | + |
| 98 | + connectedWiFi = (status == WL_CONNECTED); |
| 99 | + |
| 100 | + return (status == WL_CONNECTED); |
| 101 | +} |
| 102 | + |
| 103 | +void connectToMqttLoop() |
| 104 | +{ |
| 105 | + if ( (WiFi.status() == WL_CONNECTED) && (WiFi.RSSI() != 0) ) // temporary workaround |
| 106 | + { |
| 107 | + if (!connectedMQTT) |
| 108 | + { |
| 109 | + mqttClient.connect(); |
| 110 | + } |
| 111 | + |
| 112 | + if (!connectedWiFi) |
| 113 | + { |
| 114 | + Serial.println("WiFi reconnected"); |
| 115 | + connectedWiFi = true; |
| 116 | + } |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + if (connectedWiFi) |
| 121 | + { |
| 122 | + Serial.println("WiFi disconnected. Reconnecting"); |
| 123 | + connectedWiFi = false; |
| 124 | + |
| 125 | + connectToWifi(); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +void connectToMqtt() |
| 131 | +{ |
| 132 | + Serial.println("Connecting to MQTT..."); |
| 133 | + mqttClient.connect(); |
| 134 | +} |
| 135 | + |
| 136 | +void printSeparationLine() |
| 137 | +{ |
| 138 | + Serial.println("************************************************"); |
| 139 | +} |
| 140 | + |
| 141 | +void onMqttConnect(bool sessionPresent) |
| 142 | +{ |
| 143 | + Serial.print("Connected to MQTT broker: "); Serial.print(MQTT_HOST); |
| 144 | + Serial.print(", port: "); Serial.println(MQTT_PORT); |
| 145 | + Serial.print("PubTopic: "); Serial.println(PubTopic); |
| 146 | + |
| 147 | + connectedMQTT = true; |
| 148 | + |
| 149 | + printSeparationLine(); |
| 150 | + Serial.print("Session present: "); Serial.println(sessionPresent); |
| 151 | + |
| 152 | + uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2); |
| 153 | + Serial.print("Subscribing at QoS 2, packetId: "); Serial.println(packetIdSub); |
| 154 | + |
| 155 | + mqttClient.publish(PubTopic, 0, true, "STM32 Test1"); |
| 156 | + Serial.println("Publishing at QoS 0"); |
| 157 | + |
| 158 | + uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "STM32 Test2"); |
| 159 | + Serial.print("Publishing at QoS 1, packetId: "); Serial.println(packetIdPub1); |
| 160 | + |
| 161 | + uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "STM32 Test3"); |
| 162 | + Serial.print("Publishing at QoS 2, packetId: "); Serial.println(packetIdPub2); |
| 163 | + |
| 164 | + printSeparationLine(); |
| 165 | +} |
| 166 | + |
| 167 | +void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) |
| 168 | +{ |
| 169 | + (void) reason; |
| 170 | + |
| 171 | + connectedMQTT = false; |
| 172 | + |
| 173 | + Serial.println("Disconnected from MQTT."); |
| 174 | +} |
| 175 | + |
| 176 | +void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos) |
| 177 | +{ |
| 178 | + Serial.println("Subscribe acknowledged."); |
| 179 | + Serial.print(" packetId: "); Serial.println(packetId); |
| 180 | + Serial.print(" qos: "); Serial.println(qos); |
| 181 | +} |
| 182 | + |
| 183 | +void onMqttUnsubscribe(const uint16_t& packetId) |
| 184 | +{ |
| 185 | + Serial.println("Unsubscribe acknowledged."); |
| 186 | + Serial.print(" packetId: "); Serial.println(packetId); |
| 187 | +} |
| 188 | + |
| 189 | +void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties, |
| 190 | + const size_t& len, const size_t& index, const size_t& total) |
| 191 | +{ |
| 192 | + char message[len + 1]; |
| 193 | + |
| 194 | + memcpy(message, payload, len); |
| 195 | + message[len] = 0; |
| 196 | + |
| 197 | + Serial.println("Publish received."); |
| 198 | + Serial.print(" topic: "); Serial.println(topic); |
| 199 | + Serial.print(" message: "); Serial.println(message); |
| 200 | + Serial.print(" qos: "); Serial.println(properties.qos); |
| 201 | + Serial.print(" dup: "); Serial.println(properties.dup); |
| 202 | + Serial.print(" retain: "); Serial.println(properties.retain); |
| 203 | + Serial.print(" len: "); Serial.println(len); |
| 204 | + Serial.print(" index: "); Serial.println(index); |
| 205 | + Serial.print(" total: "); Serial.println(total); |
| 206 | +} |
| 207 | + |
| 208 | +void onMqttPublish(const uint16_t& packetId) |
| 209 | +{ |
| 210 | + Serial.println("Publish acknowledged."); |
| 211 | + Serial.print(" packetId: "); Serial.println(packetId); |
| 212 | +} |
| 213 | + |
| 214 | +void setup() |
| 215 | +{ |
| 216 | + Serial.begin(115200); |
| 217 | + while (!Serial && millis() < 5000); |
| 218 | + |
| 219 | + Serial.print("\nStarting FullyFeature_RP2040W on "); Serial.println(BOARD_NAME); |
| 220 | + Serial.println(ASYNC_MQTT_GENERIC_VERSION); |
| 221 | + |
| 222 | + /////////////////////////////////// |
| 223 | + |
| 224 | + connectToWifi(); |
| 225 | + |
| 226 | + /////////////////////////////////// |
| 227 | + |
| 228 | + mqttClient.onConnect(onMqttConnect); |
| 229 | + mqttClient.onDisconnect(onMqttDisconnect); |
| 230 | + mqttClient.onSubscribe(onMqttSubscribe); |
| 231 | + mqttClient.onUnsubscribe(onMqttUnsubscribe); |
| 232 | + mqttClient.onMessage(onMqttMessage); |
| 233 | + mqttClient.onPublish(onMqttPublish); |
| 234 | + |
| 235 | + mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
| 236 | + |
| 237 | + connectToMqttTicker.start(); //start the ticker. |
| 238 | + |
| 239 | + connectToMqtt(); |
| 240 | +} |
| 241 | + |
| 242 | +void loop() |
| 243 | +{ |
| 244 | + connectToMqttTicker.update(); //update the ticker. |
| 245 | +} |
0 commit comments