|
| 1 | +/**************************************************************************************************************************** |
| 2 | + MQTTClient_SSL.ino - Dead simple SSL MQTT Client for Ethernet shields |
| 3 | +
|
| 4 | + For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720) |
| 5 | +
|
| 6 | + WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer |
| 7 | +
|
| 8 | + Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases |
| 9 | + Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01 |
| 10 | + Licensed under MIT license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +/* |
| 14 | + Basic MQTT example (with SSL!) |
| 15 | + This sketch demonstrates the basic capabilities of the library. |
| 16 | + It connects to an MQTT server then: |
| 17 | + - publishes {Hello from MQTTClient_SSL on SAM DUE, millis = xxxxx} to the topic [MQTT_Pub] |
| 18 | + - subscribes to the topic [MQTT_Sub], printing out any messages |
| 19 | + it receives. NB - it assumes the received payloads are strings not binary |
| 20 | + It will reconnect to the server if the connection is lost using a blocking |
| 21 | + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to |
| 22 | + achieve the same result without blocking the main loop. |
| 23 | +*/ |
| 24 | + |
| 25 | +#define DEBUG_ETHERNET_WEBSERVER_PORT Serial |
| 26 | + |
| 27 | +// Debug Level from 0 to 4 |
| 28 | +#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3 |
| 29 | + |
| 30 | +#include <WebServer_WT32_ETH01.h> |
| 31 | + |
| 32 | +#include <WiFiClientSecure.h> |
| 33 | + |
| 34 | +// Select the IP address according to your local network |
| 35 | +IPAddress myIP(192, 168, 2, 232); |
| 36 | +IPAddress myGW(192, 168, 2, 1); |
| 37 | +IPAddress mySN(255, 255, 255, 0); |
| 38 | + |
| 39 | +// Google DNS Server IP |
| 40 | +IPAddress myDNS(8, 8, 8, 8); |
| 41 | + |
| 42 | +bool eth_connected = false; |
| 43 | + |
| 44 | +#include "certificates.h" |
| 45 | +#include <PubSubClient.h> |
| 46 | + |
| 47 | +const char* mqttServer = "broker.emqx.io"; // Broker address |
| 48 | + |
| 49 | +const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique |
| 50 | +const char *TOPIC = "MQTT_Pub"; // Topic to subcribe to |
| 51 | +const char *subTopic = "MQTT_Sub"; // Topic to subcribe to |
| 52 | + |
| 53 | +unsigned long lastMsg = 0; |
| 54 | + |
| 55 | +// Initialize the SSL client library |
| 56 | +// Arguments: EthernetClient, our trust anchors |
| 57 | + |
| 58 | +void callback(char* topic, byte* payload, unsigned int length) |
| 59 | +{ |
| 60 | + Serial.print("Message arrived ["); |
| 61 | + Serial.print(topic); |
| 62 | + Serial.print("] "); |
| 63 | + |
| 64 | + for (unsigned int i = 0; i < length; i++) |
| 65 | + { |
| 66 | + Serial.print((char)payload[i]); |
| 67 | + } |
| 68 | + |
| 69 | + Serial.println(); |
| 70 | +} |
| 71 | + |
| 72 | +#define USE_PTR true |
| 73 | + |
| 74 | +#if USE_PTR |
| 75 | + |
| 76 | + WiFiClientSecure* ethClientSSL; |
| 77 | + |
| 78 | + PubSubClient* client; |
| 79 | + |
| 80 | +#else |
| 81 | + |
| 82 | + WiFiClientSecure ethClientSSL; |
| 83 | + |
| 84 | + PubSubClient client(mqttServer, 8883, callback, ethClientSSL); |
| 85 | + |
| 86 | +#endif |
| 87 | + |
| 88 | +#if USE_PTR |
| 89 | +void reconnect() |
| 90 | +{ |
| 91 | + // Loop until we're reconnected |
| 92 | + while (!client->connected()) |
| 93 | + { |
| 94 | + Serial.print("Attempting MQTTS connection to "); |
| 95 | + Serial.print(mqttServer); |
| 96 | + |
| 97 | + // Attempt to connect |
| 98 | + if (client->connect(ID)) |
| 99 | + { |
| 100 | + Serial.println("...connected"); |
| 101 | + |
| 102 | + // Once connected, publish an announcement... |
| 103 | + String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME); |
| 104 | + |
| 105 | + client->publish(TOPIC, data.c_str()); |
| 106 | + |
| 107 | + //Serial.println("Published connection message successfully!"); |
| 108 | + //Serial.print("Subcribed to: "); |
| 109 | + //Serial.println(subTopic); |
| 110 | + |
| 111 | + // ... and resubscribe |
| 112 | + client->subscribe(subTopic); |
| 113 | + // for loopback testing |
| 114 | + client->subscribe(TOPIC); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + Serial.print("failed, rc="); |
| 119 | + Serial.print(client->state()); |
| 120 | + Serial.println(" try again in 5 seconds"); |
| 121 | + |
| 122 | + // Wait 5 seconds before retrying |
| 123 | + delay(5000); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#else |
| 129 | + |
| 130 | +void reconnect() |
| 131 | +{ |
| 132 | + // Loop until we're reconnected |
| 133 | + while (!client.connected()) |
| 134 | + { |
| 135 | + Serial.print("Attempting MQTT connection to "); |
| 136 | + Serial.print(mqttServer); |
| 137 | + |
| 138 | + // Attempt to connect |
| 139 | + if (client.connect(ID)) |
| 140 | + { |
| 141 | + Serial.println("...connected"); |
| 142 | + |
| 143 | + // Once connected, publish an announcement... |
| 144 | + String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME); |
| 145 | + |
| 146 | + client.publish(TOPIC, data.c_str()); |
| 147 | + |
| 148 | + //Serial.println("Published connection message successfully!"); |
| 149 | + //Serial.print("Subcribed to: "); |
| 150 | + //Serial.println(subTopic); |
| 151 | + |
| 152 | + // ... and resubscribe |
| 153 | + client.subscribe(subTopic); |
| 154 | + // for loopback testing |
| 155 | + client.subscribe(TOPIC); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + Serial.print("failed, rc="); |
| 160 | + Serial.print(client.state()); |
| 161 | + Serial.println(" try again in 5 seconds"); |
| 162 | + |
| 163 | + // Wait 5 seconds before retrying |
| 164 | + delay(5000); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | +#endif |
| 169 | + |
| 170 | +// Not sure if WiFiClientSecure checks the validity date of the certificate. |
| 171 | +// Setting clock just to be sure... |
| 172 | +void setClock() |
| 173 | +{ |
| 174 | + configTime(0, 0, "pool.ntp.org"); |
| 175 | + |
| 176 | + Serial.print(F("Waiting for NTP time sync: ")); |
| 177 | + time_t nowSecs = time(nullptr); |
| 178 | + |
| 179 | + while (nowSecs < 8 * 3600 * 2) |
| 180 | + { |
| 181 | + delay(500); |
| 182 | + Serial.print(F(".")); |
| 183 | + yield(); |
| 184 | + nowSecs = time(nullptr); |
| 185 | + } |
| 186 | + |
| 187 | + Serial.println(); |
| 188 | + struct tm timeinfo; |
| 189 | + gmtime_r(&nowSecs, &timeinfo); |
| 190 | + Serial.print(F("Current time: ")); |
| 191 | + Serial.print(asctime(&timeinfo)); |
| 192 | +} |
| 193 | + |
| 194 | +void WiFiEvent(WiFiEvent_t event) |
| 195 | +{ |
| 196 | + switch (event) |
| 197 | + { |
| 198 | + case SYSTEM_EVENT_ETH_START: |
| 199 | + Serial.println("\nETH Started"); |
| 200 | + //set eth hostname here |
| 201 | + ETH.setHostname("WT32-ETH01"); |
| 202 | + break; |
| 203 | + case SYSTEM_EVENT_ETH_CONNECTED: |
| 204 | + Serial.println("ETH Connected"); |
| 205 | + break; |
| 206 | + |
| 207 | + case SYSTEM_EVENT_ETH_GOT_IP: |
| 208 | + if (!eth_connected) |
| 209 | + { |
| 210 | + Serial.print("ETH MAC: "); |
| 211 | + Serial.print(ETH.macAddress()); |
| 212 | + Serial.print(", IPv4: "); |
| 213 | + Serial.print(ETH.localIP()); |
| 214 | + |
| 215 | + if (ETH.fullDuplex()) |
| 216 | + { |
| 217 | + Serial.print(", FULL_DUPLEX"); |
| 218 | + } |
| 219 | + |
| 220 | + Serial.print(", "); |
| 221 | + Serial.print(ETH.linkSpeed()); |
| 222 | + Serial.println("Mbps"); |
| 223 | + eth_connected = true; |
| 224 | + } |
| 225 | + |
| 226 | + break; |
| 227 | + |
| 228 | + case SYSTEM_EVENT_ETH_DISCONNECTED: |
| 229 | + Serial.println("ETH Disconnected"); |
| 230 | + eth_connected = false; |
| 231 | + break; |
| 232 | + |
| 233 | + case SYSTEM_EVENT_ETH_STOP: |
| 234 | + Serial.println("\nETH Stopped"); |
| 235 | + eth_connected = false; |
| 236 | + break; |
| 237 | + |
| 238 | + default: |
| 239 | + break; |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +void setup() |
| 244 | +{ |
| 245 | + // Open serial communications and wait for port to open: |
| 246 | + Serial.begin(115200); |
| 247 | + while (!Serial); |
| 248 | + |
| 249 | + // Using this if Serial debugging is not necessary or not using Serial port |
| 250 | + //while (!Serial && (millis() < 3000)); |
| 251 | + |
| 252 | + Serial.print("\nStarting MQTTClient_SSL on " + String(ARDUINO_BOARD)); |
| 253 | + Serial.println(" with " + String(SHIELD_TYPE)); |
| 254 | + Serial.println(WEBSERVER_WT32_ETH01_VERSION); |
| 255 | + |
| 256 | + //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, |
| 257 | + // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); |
| 258 | + //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); |
| 259 | + ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); |
| 260 | + |
| 261 | + // Static IP, leave without this line to get IP via DHCP |
| 262 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 263 | + ETH.config(myIP, myGW, mySN, myDNS); |
| 264 | + |
| 265 | + WiFi.onEvent(WiFiEvent); |
| 266 | + |
| 267 | + while (!eth_connected) |
| 268 | + delay(100); |
| 269 | + |
| 270 | + setClock(); |
| 271 | + |
| 272 | +#if USE_PTR |
| 273 | + |
| 274 | + ethClientSSL = new WiFiClientSecure; |
| 275 | + |
| 276 | + if (ethClientSSL) |
| 277 | + { |
| 278 | + ethClientSSL->setCACert(rootCACertificate); |
| 279 | + } |
| 280 | + |
| 281 | + client = new PubSubClient(mqttServer, 8883, callback, *ethClientSSL); |
| 282 | + |
| 283 | + // Note - the default maximum packet size is 256 bytes. If the |
| 284 | + // combined length of clientId, username and password exceed this use the |
| 285 | + // following to increase the buffer size: |
| 286 | + client->setBufferSize(2048); |
| 287 | + |
| 288 | +#else |
| 289 | + |
| 290 | + ethClientSSL.setCACert(rootCACertificate); |
| 291 | + |
| 292 | + // Note - the default maximum packet size is 256 bytes. If the |
| 293 | + // combined length of clientId, username and password exceed this use the |
| 294 | + // following to increase the buffer size: |
| 295 | + client.setBufferSize(2048); |
| 296 | + |
| 297 | +#endif |
| 298 | + |
| 299 | +} |
| 300 | + |
| 301 | + |
| 302 | +#define MQTT_PUBLISH_INTERVAL_MS 10000L |
| 303 | + |
| 304 | +String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME); |
| 305 | +const char *pubData = data.c_str(); |
| 306 | + |
| 307 | +#if USE_PTR |
| 308 | + |
| 309 | +void loop() |
| 310 | +{ |
| 311 | + static unsigned long now; |
| 312 | + |
| 313 | + if (!client->connected()) |
| 314 | + { |
| 315 | + reconnect(); |
| 316 | + } |
| 317 | + |
| 318 | + // Sending Data |
| 319 | + now = millis(); |
| 320 | + |
| 321 | + if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS) |
| 322 | + { |
| 323 | + lastMsg = now; |
| 324 | + |
| 325 | + if (!client->publish(TOPIC, pubData)) |
| 326 | + { |
| 327 | + Serial.println("Message failed to send."); |
| 328 | + } |
| 329 | + |
| 330 | + Serial.print("Message Send : " + String(TOPIC) + " => "); |
| 331 | + Serial.println(data); |
| 332 | + } |
| 333 | + |
| 334 | + client->loop(); |
| 335 | +} |
| 336 | + |
| 337 | +#else |
| 338 | + |
| 339 | +void loop() |
| 340 | +{ |
| 341 | + static unsigned long now; |
| 342 | + |
| 343 | + if (!client.connected()) |
| 344 | + { |
| 345 | + reconnect(); |
| 346 | + } |
| 347 | + |
| 348 | + // Sending Data |
| 349 | + now = millis(); |
| 350 | + |
| 351 | + if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS) |
| 352 | + { |
| 353 | + lastMsg = now; |
| 354 | + |
| 355 | + if (!client.publish(TOPIC, pubData)) |
| 356 | + { |
| 357 | + Serial.println("Message failed to send."); |
| 358 | + } |
| 359 | + |
| 360 | + Serial.print("Message Send : " + String(TOPIC) + " => "); |
| 361 | + Serial.println(data); |
| 362 | + } |
| 363 | + |
| 364 | + client.loop(); |
| 365 | +} |
| 366 | + |
| 367 | +#endif |
0 commit comments