|
| 1 | +/**************************************************************************************************************************** |
| 2 | + MQTT_ThingStream.ino - Dead simple MQTT Client for Ethernet shields |
| 3 | +
|
| 4 | + For STM32F/L/H/G/WB/MP1 with built-in Ethernet LAN8742A (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 shield/module |
| 5 | + |
| 6 | + EthernetWebServer_SSL_STM32 is a library for STM32 using the Ethernet shields to run WebServer and Client with/without SSL |
| 7 | +
|
| 8 | + Use SSLClient Library code from https://github.com/OPEnSLab-OSU/SSLClient |
| 9 | + |
| 10 | + Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer_SSL_STM32 |
| 11 | + Licensed under MIT license |
| 12 | + *****************************************************************************************************************************/ |
| 13 | +/* |
| 14 | + Basic MQTT example (without 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 NUCLEO_F767ZI} to the topic [STM32_Pub] |
| 18 | + - subscribes to the topic [STM32_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 | + You will need to populate "certificates.h" with your trust anchors |
| 25 | + (see https://github.com/OPEnSLab-OSU/SSLClient/blob/master/TrustAnchors.md) |
| 26 | + and my_cert/my_key with your certificate/private key pair |
| 27 | + (see https://github.com/OPEnSLab-OSU/SSLClient#mtls). |
| 28 | +*/ |
| 29 | + |
| 30 | +#include "defines.h" |
| 31 | + |
| 32 | +#include <PubSubClient.h> |
| 33 | + |
| 34 | +const char my_cert[] = "FIXME"; |
| 35 | +const char my_key[] = "FIXME"; |
| 36 | + |
| 37 | +#define USING_THINGSTREAM_IO true |
| 38 | + |
| 39 | +#if USING_THINGSTREAM_IO |
| 40 | + |
| 41 | +const char *MQTT_PREFIX_TOPIC = "esp32-sniffer/"; |
| 42 | +const char *MQTT_ANNOUNCE_TOPIC = "/status"; |
| 43 | +const char *MQTT_CONTROL_TOPIC = "/control"; |
| 44 | +const char *MQTT_BLE_TOPIC = "/ble"; |
| 45 | + |
| 46 | + |
| 47 | +// GOT FROM ThingsStream! |
| 48 | +const char *MQTT_SERVER = "mqtt.thingstream.io"; |
| 49 | +const char *MQTT_USER = "MQTT_USER"; |
| 50 | +const char *MQTT_PASS = "MQTT_PASS"; |
| 51 | +const char *MQTT_CLIENT_ID = "MQTT_CLIENT_ID"; |
| 52 | + |
| 53 | +String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC; |
| 54 | +String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC; |
| 55 | + |
| 56 | +#else |
| 57 | + |
| 58 | +const char* MQTT_SERVER = "broker.emqx.io"; // Broker address |
| 59 | + |
| 60 | +const char* ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique |
| 61 | +String topic = "STM32_Pub"; // Topic to subcribe to |
| 62 | +String subTopic = "STM32_Sub"; // Topic to subcribe to |
| 63 | + |
| 64 | +#endif |
| 65 | + |
| 66 | +void mqtt_receive_callback(char* topic, byte* payload, unsigned int length); |
| 67 | + |
| 68 | +const int MQTT_PORT = 1883; //if you use SSL //1883 no SSL |
| 69 | + |
| 70 | +String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE); |
| 71 | +const char *pubData = data.c_str(); |
| 72 | + |
| 73 | +unsigned long lastMsg = 0; |
| 74 | + |
| 75 | +// Initialize the SSL client library |
| 76 | +// Arguments: EthernetClient, our trust anchors |
| 77 | + |
| 78 | + |
| 79 | +EthernetClient ethClient; |
| 80 | + |
| 81 | +PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClient); |
| 82 | + |
| 83 | +/* |
| 84 | + Called whenever a payload is received from a subscribed MQTT topic |
| 85 | +*/ |
| 86 | +void mqtt_receive_callback(char* topic, byte* payload, unsigned int length) |
| 87 | +{ |
| 88 | + Serial.print("MQTT Message receive ["); |
| 89 | + Serial.print(topic); |
| 90 | + Serial.print("] "); |
| 91 | + |
| 92 | + for (int i = 0; i < length; i++) |
| 93 | + { |
| 94 | + Serial.print((char)payload[i]); |
| 95 | + } |
| 96 | + |
| 97 | + Serial.println(); |
| 98 | +} |
| 99 | + |
| 100 | +void reconnect() |
| 101 | +{ |
| 102 | + // Loop until we're reconnected |
| 103 | + while (!client.connected()) |
| 104 | + { |
| 105 | + Serial.print("Attempting MQTT connection to "); |
| 106 | + Serial.println(MQTT_SERVER); |
| 107 | + |
| 108 | + // Attempt to connect |
| 109 | + |
| 110 | +#if USING_THINGSTREAM_IO |
| 111 | + int connect_status = client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, topic.c_str(), 2, false, ""); |
| 112 | +#else |
| 113 | + int connect_status = client.connect(ID); |
| 114 | +#endif |
| 115 | + |
| 116 | + if (connect_status) |
| 117 | + { |
| 118 | + Serial.println("...connected"); |
| 119 | + |
| 120 | + // Once connected, publish an announcement... |
| 121 | + client.publish(topic.c_str(), data.c_str()); |
| 122 | + |
| 123 | + Serial.println("Published connection message successfully!"); |
| 124 | + |
| 125 | + Serial.print("Subcribed to: "); |
| 126 | + Serial.println(subTopic); |
| 127 | + |
| 128 | + // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9 |
| 129 | + //ethClientSSL.flush(); |
| 130 | + // ... and resubscribe |
| 131 | + client.subscribe(subTopic.c_str()); |
| 132 | + // for loopback testing |
| 133 | + client.subscribe(topic.c_str()); |
| 134 | + // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9 |
| 135 | + //ethClientSSL.flush(); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + Serial.print("failed, rc="); |
| 140 | + Serial.print(client.state()); |
| 141 | + Serial.println(" try again in 5 seconds"); |
| 142 | + |
| 143 | + // Wait 5 seconds before retrying |
| 144 | + delay(5000); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +void setup() |
| 150 | +{ |
| 151 | + // Open serial communications and wait for port to open: |
| 152 | + Serial.begin(115200); |
| 153 | + while (!Serial); |
| 154 | + |
| 155 | + Serial.print("\nStart MQTT_ThingStream on " + String(BOARD_NAME)); |
| 156 | + Serial.println(" with " + String(SHIELD_TYPE)); |
| 157 | + |
| 158 | + ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN); |
| 159 | + |
| 160 | + ET_LOGWARN(F("Default SPI pinout:")); |
| 161 | + ET_LOGWARN1(F("MOSI:"), MOSI); |
| 162 | + ET_LOGWARN1(F("MISO:"), MISO); |
| 163 | + ET_LOGWARN1(F("SCK:"), SCK); |
| 164 | + ET_LOGWARN1(F("SS:"), SS); |
| 165 | + ET_LOGWARN(F("=========================")); |
| 166 | + |
| 167 | + #if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET) |
| 168 | + // For other boards, to change if necessary |
| 169 | + #if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC ) |
| 170 | + // Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries |
| 171 | + Ethernet.init (USE_THIS_SS_PIN); |
| 172 | + |
| 173 | + #elif USE_ETHERNET3 |
| 174 | + // Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer |
| 175 | + #ifndef ETHERNET3_MAX_SOCK_NUM |
| 176 | + #define ETHERNET3_MAX_SOCK_NUM 4 |
| 177 | + #endif |
| 178 | + |
| 179 | + Ethernet.setCsPin (USE_THIS_SS_PIN); |
| 180 | + Ethernet.init (ETHERNET3_MAX_SOCK_NUM); |
| 181 | + |
| 182 | + #elif USE_CUSTOM_ETHERNET |
| 183 | + // You have to add initialization for your Custom Ethernet here |
| 184 | + // This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough |
| 185 | + //Ethernet.init(USE_THIS_SS_PIN); |
| 186 | + |
| 187 | + #endif //( ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC ) |
| 188 | + #endif |
| 189 | + |
| 190 | + // start the ethernet connection and the server: |
| 191 | + // Use DHCP dynamic IP and random mac |
| 192 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 193 | + // Use Static IP |
| 194 | + //Ethernet.begin(mac[index], ip); |
| 195 | + Ethernet.begin(mac[index]); |
| 196 | + |
| 197 | + // you're connected now, so print out the data |
| 198 | + Serial.print(F("You're connected to the network, IP = ")); |
| 199 | + Serial.println(Ethernet.localIP()); |
| 200 | + |
| 201 | + // Note - the default maximum packet size is 256 bytes. If the |
| 202 | + // combined length of clientId, username and password exceed this use the |
| 203 | + // following to increase the buffer size: |
| 204 | + //client.setBufferSize(256); |
| 205 | + |
| 206 | + Serial.println("***************************************"); |
| 207 | + Serial.println(topic); |
| 208 | + Serial.println("***************************************"); |
| 209 | +} |
| 210 | + |
| 211 | +#define MQTT_PUBLISH_INTERVAL_MS 5000L |
| 212 | + |
| 213 | +void loop() |
| 214 | +{ |
| 215 | + static unsigned long now; |
| 216 | + |
| 217 | + if (!client.connected()) |
| 218 | + { |
| 219 | + reconnect(); |
| 220 | + } |
| 221 | + |
| 222 | + // Sending Data |
| 223 | + now = millis(); |
| 224 | + |
| 225 | + if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS) |
| 226 | + { |
| 227 | + lastMsg = now; |
| 228 | + |
| 229 | + if (!client.publish(topic.c_str(), pubData)) |
| 230 | + { |
| 231 | + Serial.println("Message failed to send."); |
| 232 | + } |
| 233 | + |
| 234 | + Serial.print("MQTT Message Send : " + topic + " => "); |
| 235 | + Serial.println(data); |
| 236 | + } |
| 237 | + |
| 238 | + client.loop(); |
| 239 | +} |
0 commit comments