|
| 1 | +/**************************************************************************************************************************** |
| 2 | + FullyFeatureSSL_ESP32_ENC.ino |
| 3 | +
|
| 4 | + AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, 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 | +extern "C" |
| 16 | +{ |
| 17 | +#include "freertos/FreeRTOS.h" |
| 18 | +#include "freertos/timers.h" |
| 19 | +} |
| 20 | + |
| 21 | +#define ASYNC_TCP_SSL_ENABLED true |
| 22 | +//#define ASYNC_TCP_SSL_ENABLED false |
| 23 | + |
| 24 | +#include <AsyncMqtt_Generic.h> |
| 25 | + |
| 26 | +//#define MQTT_HOST IPAddress(192, 168, 2, 110) |
| 27 | +#define MQTT_HOST "broker.emqx.io" // Broker address |
| 28 | + |
| 29 | +#if ASYNC_TCP_SSL_ENABLED |
| 30 | + |
| 31 | +#define MQTT_SECURE true |
| 32 | + |
| 33 | +const uint8_t MQTT_SERVER_FINGERPRINT[] = {0x7e, 0x36, 0x22, 0x01, 0xf9, 0x7e, 0x99, 0x2f, 0xc5, 0xdb, 0x3d, 0xbe, 0xac, 0x48, 0x67, 0x5b, 0x5d, 0x47, 0x94, 0xd2}; |
| 34 | +const char *PubTopic = "async-mqtt/ESP32_ENC_SSL_Pub"; // Topic to publish |
| 35 | + |
| 36 | +#define MQTT_PORT 8883 |
| 37 | + |
| 38 | +#else |
| 39 | + |
| 40 | +const char *PubTopic = "async-mqtt/ESP32_ENC_Pub"; // Topic to publish |
| 41 | + |
| 42 | +#define MQTT_PORT 1883 |
| 43 | + |
| 44 | +#endif |
| 45 | + |
| 46 | +AsyncMqttClient mqttClient; |
| 47 | +TimerHandle_t mqttReconnectTimer; |
| 48 | + |
| 49 | +void connectToMqtt() |
| 50 | +{ |
| 51 | + Serial.println("Connecting to MQTT..."); |
| 52 | + mqttClient.connect(); |
| 53 | +} |
| 54 | + |
| 55 | +void ETH_event(WiFiEvent_t event) |
| 56 | +{ |
| 57 | + switch (event) |
| 58 | + { |
| 59 | +#if USING_CORE_ESP32_CORE_V200_PLUS |
| 60 | + |
| 61 | + case ARDUINO_EVENT_ETH_START: |
| 62 | + Serial.println("ETH starting"); |
| 63 | + break; |
| 64 | + |
| 65 | + case ARDUINO_EVENT_ETH_CONNECTED: |
| 66 | + Serial.println("ETH connected"); |
| 67 | + break; |
| 68 | + |
| 69 | + case ARDUINO_EVENT_ETH_GOT_IP: |
| 70 | + Serial.println("ETH got IP"); |
| 71 | + Serial.print("IP address: "); |
| 72 | + Serial.println(ETH.localIP()); |
| 73 | + connectToMqtt(); |
| 74 | + break; |
| 75 | + |
| 76 | + case ARDUINO_EVENT_ETH_DISCONNECTED: |
| 77 | + Serial.println("ETH lost connection"); |
| 78 | + |
| 79 | + // ensure we don't reconnect to MQTT when no ETH |
| 80 | + xTimerStop(mqttReconnectTimer, 0); |
| 81 | + |
| 82 | + break; |
| 83 | + |
| 84 | + case ARDUINO_EVENT_ETH_STOP: |
| 85 | + Serial.println("ETH stops"); |
| 86 | + |
| 87 | + // ensure we don't reconnect to MQTT when no ETH |
| 88 | + xTimerStop(mqttReconnectTimer, 0); |
| 89 | + |
| 90 | + break; |
| 91 | +#else |
| 92 | + |
| 93 | + case SYSTEM_EVENT_ETH_CONNECTED: |
| 94 | + erial.println(F("ETH Connected")); |
| 95 | + break; |
| 96 | + |
| 97 | + case SYSTEM_EVENT_ETH_GOT_IP: |
| 98 | + Serial.println("ETH connected"); |
| 99 | + Serial.println("IP address: "); |
| 100 | + Serial.println(ETH.localIP()); |
| 101 | + connectToMqtt(); |
| 102 | + break; |
| 103 | + |
| 104 | + case SYSTEM_EVENT_ETH_DISCONNECTED: |
| 105 | + case SYSTEM_EVENT_ETH_STOP: |
| 106 | + Serial.println("ETH lost connection"); |
| 107 | + |
| 108 | + // ensure we don't reconnect to MQTT when no ETH |
| 109 | + xTimerStop(mqttReconnectTimer, 0); |
| 110 | + |
| 111 | + break; |
| 112 | +#endif |
| 113 | + |
| 114 | + default: |
| 115 | + break; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void printSeparationLine() |
| 120 | +{ |
| 121 | + Serial.println("************************************************"); |
| 122 | +} |
| 123 | + |
| 124 | +void onMqttConnect(bool sessionPresent) |
| 125 | +{ |
| 126 | + Serial.print("Connected to MQTT broker: "); |
| 127 | + Serial.print(MQTT_HOST); |
| 128 | + Serial.print(", port: "); |
| 129 | + Serial.println(MQTT_PORT); |
| 130 | + Serial.print("PubTopic: "); |
| 131 | + Serial.println(PubTopic); |
| 132 | + |
| 133 | + printSeparationLine(); |
| 134 | + Serial.print("Session present: "); |
| 135 | + Serial.println(sessionPresent); |
| 136 | + |
| 137 | + uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2); |
| 138 | + Serial.print("Subscribing at QoS 2, packetId: "); |
| 139 | + Serial.println(packetIdSub); |
| 140 | + |
| 141 | + mqttClient.publish(PubTopic, 0, true, "ESP32_ENC Test"); |
| 142 | + Serial.println("Publishing at QoS 0"); |
| 143 | + |
| 144 | + uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2"); |
| 145 | + Serial.print("Publishing at QoS 1, packetId: "); |
| 146 | + Serial.println(packetIdPub1); |
| 147 | + |
| 148 | + uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3"); |
| 149 | + Serial.print("Publishing at QoS 2, packetId: "); |
| 150 | + Serial.println(packetIdPub2); |
| 151 | + |
| 152 | + printSeparationLine(); |
| 153 | +} |
| 154 | + |
| 155 | +void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) |
| 156 | +{ |
| 157 | + (void) reason; |
| 158 | + |
| 159 | + Serial.println("Disconnected from MQTT."); |
| 160 | + |
| 161 | + if (ESP32_ENC_isConnected()) |
| 162 | + { |
| 163 | + xTimerStart(mqttReconnectTimer, 0); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos) |
| 168 | +{ |
| 169 | + Serial.println("Subscribe acknowledged."); |
| 170 | + Serial.print(" packetId: "); |
| 171 | + Serial.println(packetId); |
| 172 | + Serial.print(" qos: "); |
| 173 | + Serial.println(qos); |
| 174 | +} |
| 175 | + |
| 176 | +void onMqttUnsubscribe(const uint16_t& packetId) |
| 177 | +{ |
| 178 | + Serial.println("Unsubscribe acknowledged."); |
| 179 | + Serial.print(" packetId: "); |
| 180 | + Serial.println(packetId); |
| 181 | +} |
| 182 | + |
| 183 | +void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties, |
| 184 | + const size_t& len, const size_t& index, const size_t& total) |
| 185 | +{ |
| 186 | + (void) payload; |
| 187 | + |
| 188 | + Serial.println("Publish received."); |
| 189 | + Serial.print(" topic: "); |
| 190 | + Serial.println(topic); |
| 191 | + Serial.print(" qos: "); |
| 192 | + Serial.println(properties.qos); |
| 193 | + Serial.print(" dup: "); |
| 194 | + Serial.println(properties.dup); |
| 195 | + Serial.print(" retain: "); |
| 196 | + Serial.println(properties.retain); |
| 197 | + Serial.print(" len: "); |
| 198 | + Serial.println(len); |
| 199 | + Serial.print(" index: "); |
| 200 | + Serial.println(index); |
| 201 | + Serial.print(" total: "); |
| 202 | + Serial.println(total); |
| 203 | +} |
| 204 | + |
| 205 | +void onMqttPublish(const uint16_t& packetId) |
| 206 | +{ |
| 207 | + Serial.println("Publish acknowledged"); |
| 208 | + Serial.print(" packetId: "); |
| 209 | + Serial.println(packetId); |
| 210 | +} |
| 211 | + |
| 212 | +void setup() |
| 213 | +{ |
| 214 | + Serial.begin(115200); |
| 215 | + |
| 216 | + while (!Serial && millis() < 5000); |
| 217 | + |
| 218 | + Serial.print("\nStarting FullyFeatureSSL_ESP32_ENC on "); |
| 219 | + Serial.print(BOARD_NAME); |
| 220 | + Serial.println(" with " + String(SHIELD_TYPE)); |
| 221 | + Serial.println(WEBSERVER_ESP32_ENC_VERSION); |
| 222 | + Serial.println(ASYNC_MQTT_GENERIC_VERSION); |
| 223 | + |
| 224 | + AMQTT_LOGWARN(F("Default SPI pinout:")); |
| 225 | + AMQTT_LOGWARN1(F("MOSI:"), MOSI_GPIO); |
| 226 | + AMQTT_LOGWARN1(F("MISO:"), MISO_GPIO); |
| 227 | + AMQTT_LOGWARN1(F("SCK:"), SCK_GPIO); |
| 228 | + AMQTT_LOGWARN1(F("CS:"), CS_GPIO); |
| 229 | + AMQTT_LOGWARN1(F("INT:"), INT_GPIO); |
| 230 | + AMQTT_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ); |
| 231 | + AMQTT_LOGWARN(F("=========================")); |
| 232 | + |
| 233 | + mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, |
| 234 | + reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt)); |
| 235 | + |
| 236 | + mqttClient.onConnect(onMqttConnect); |
| 237 | + mqttClient.onDisconnect(onMqttDisconnect); |
| 238 | + mqttClient.onSubscribe(onMqttSubscribe); |
| 239 | + mqttClient.onUnsubscribe(onMqttUnsubscribe); |
| 240 | + mqttClient.onMessage(onMqttMessage); |
| 241 | + mqttClient.onPublish(onMqttPublish); |
| 242 | + |
| 243 | + mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
| 244 | + |
| 245 | +#if ASYNC_TCP_SSL_ENABLED |
| 246 | + mqttClient.setSecure(MQTT_SECURE); |
| 247 | + |
| 248 | + if (MQTT_SECURE) |
| 249 | + { |
| 250 | + //mqttClient.addServerFingerprint((const uint8_t[])MQTT_SERVER_FINGERPRINT); |
| 251 | + mqttClient.addServerFingerprint((const uint8_t *)MQTT_SERVER_FINGERPRINT); |
| 252 | + } |
| 253 | + |
| 254 | +#endif |
| 255 | + |
| 256 | + /////////////////////////////////// |
| 257 | + |
| 258 | + // To be called before ETH.begin() |
| 259 | + WiFi.onEvent(ETH_event); |
| 260 | + |
| 261 | + // start the ethernet connection and the server: |
| 262 | + // Use DHCP dynamic IP and random mac |
| 263 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 264 | + |
| 265 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, |
| 266 | + // int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac); |
| 267 | + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST ); |
| 268 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST, mac[index] ); |
| 269 | + |
| 270 | + // Static IP, leave without this line to get IP via DHCP |
| 271 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 272 | + //ETH.config(myIP, myGW, mySN, myDNS); |
| 273 | + |
| 274 | + ESP32_ENC_waitForConnect(); |
| 275 | + |
| 276 | + /////////////////////////////////// |
| 277 | +} |
| 278 | + |
| 279 | +void loop() |
| 280 | +{ |
| 281 | +} |
0 commit comments