|
| 1 | +/* |
| 2 | + Basic B-L475E-IOT01A MQTT example |
| 3 | +
|
| 4 | + This example is based on the mqtt_esp8266 provided with PubSubClient library. |
| 5 | +
|
| 6 | + This sketch demonstrates the capabilities of the pubsub library in combination |
| 7 | + with the B-L475E-IOT01A board. |
| 8 | +
|
| 9 | + It connects to an MQTT server then: |
| 10 | + - publishes "hello world" to the topic "outTopic" every two seconds |
| 11 | + - subscribes to the topic "inTopic", printing out any messages |
| 12 | + it receives. NB - it assumes the received payloads are strings not binary |
| 13 | + - If the first character of the topic "inTopic" is an 1, switch ON the LED_BUILTIN Led, |
| 14 | + else switch it off |
| 15 | +
|
| 16 | + It will reconnect to the server if the connection is lost using a blocking |
| 17 | + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to |
| 18 | + achieve the same result without blocking the main loop. |
| 19 | +*/ |
| 20 | +#include <SPI.h> |
| 21 | +#include <WiFiST.h> |
| 22 | +#include <PubSubClient.h> |
| 23 | + |
| 24 | +// Update these with values suitable for your network. |
| 25 | +char ssid[] = "......."; |
| 26 | +const char* password = "......."; |
| 27 | +const char* mqtt_server = "broker.mqtt-dashboard.com"; |
| 28 | + |
| 29 | +SPIClass SPI_3(PC12, PC11, PC10); |
| 30 | +WiFiClass WiFi(&SPI_3, PE0, PE1, PE8, PB13); |
| 31 | +WiFiClient STClient; |
| 32 | +int status = WL_IDLE_STATUS; // the Wifi radio's status |
| 33 | + |
| 34 | +PubSubClient client(STClient); |
| 35 | +long lastMsg = 0; |
| 36 | +char msg[50]; |
| 37 | +long value = 0; |
| 38 | + |
| 39 | +void setup() { |
| 40 | + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output |
| 41 | + Serial.begin(115200); |
| 42 | + setup_wifi(); |
| 43 | + client.setServer(mqtt_server, 1883); |
| 44 | + client.setCallback(callback); |
| 45 | +} |
| 46 | + |
| 47 | +void setup_wifi() { |
| 48 | + |
| 49 | + delay(10); |
| 50 | + |
| 51 | + // initialize the WiFi module: |
| 52 | + if (WiFi.status() == WL_NO_SHIELD) { |
| 53 | + Serial.println("WiFi module not detected"); |
| 54 | + // don't continue: |
| 55 | + while (true); |
| 56 | + } |
| 57 | + |
| 58 | + // print firmware version: |
| 59 | + String fv = WiFi.firmwareVersion(); |
| 60 | + Serial.print("Firmware version: "); |
| 61 | + Serial.println(fv); |
| 62 | + |
| 63 | + if (fv != "C3.5.2.3.BETA9") { |
| 64 | + Serial.println("Please upgrade the firmware"); |
| 65 | + } |
| 66 | + |
| 67 | + // attempt to connect to Wifi network: |
| 68 | + Serial.print("Attempting to connect to network: "); |
| 69 | + Serial.println(ssid); |
| 70 | + while (status != WL_CONNECTED) { |
| 71 | + Serial.print("."); |
| 72 | + // Connect to WPA2 network: |
| 73 | + status = WiFi.begin(ssid, password); |
| 74 | + if (status != WL_CONNECTED) { |
| 75 | + // Connect to WPA (TKIP) network: |
| 76 | + status = WiFi.begin(ssid, password, ES_WIFI_SEC_WPA); |
| 77 | + } |
| 78 | + // wait 10 seconds for connection: |
| 79 | + delay(10000); |
| 80 | + } |
| 81 | + |
| 82 | + Serial.println(); |
| 83 | + Serial.println("WiFi connected"); |
| 84 | + Serial.println("IP address: "); |
| 85 | + Serial.println(WiFi.localIP()); |
| 86 | +} |
| 87 | + |
| 88 | +void callback(char* topic, byte* payload, unsigned int length) { |
| 89 | + Serial.print("Message arrived ["); |
| 90 | + Serial.print(topic); |
| 91 | + Serial.print("] "); |
| 92 | + for (unsigned int i = 0; i < length; i++) { |
| 93 | + Serial.print((char)payload[i]); |
| 94 | + } |
| 95 | + Serial.println(); |
| 96 | + |
| 97 | + // Switch on the LED if an 1 was received as first character |
| 98 | + if ((char)payload[0] == '1') { |
| 99 | + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on |
| 100 | + } else { |
| 101 | + digitalWrite(LED_BUILTIN, LOW); // Turn the LED off |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void reconnect() { |
| 106 | + // Loop until we're reconnected |
| 107 | + while (!client.connected()) { |
| 108 | + Serial.print("Attempting MQTT connection..."); |
| 109 | + // Attempt to connect |
| 110 | + if (client.connect("B-L475E-IOT01AClient")) { |
| 111 | + Serial.println("connected"); |
| 112 | + // Once connected, publish an announcement... |
| 113 | + client.publish("outTopic", "hello world"); |
| 114 | + // ... and resubscribe |
| 115 | + client.subscribe("inTopic"); |
| 116 | + } else { |
| 117 | + Serial.print("failed, rc="); |
| 118 | + Serial.print(client.state()); |
| 119 | + Serial.println(" try again in 5 seconds"); |
| 120 | + // Wait 5 seconds before retrying |
| 121 | + delay(5000); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +void loop() { |
| 126 | + |
| 127 | + if (!client.connected()) { |
| 128 | + reconnect(); |
| 129 | + } |
| 130 | + client.loop(); |
| 131 | + |
| 132 | + long now = millis(); |
| 133 | + if (now - lastMsg > 2000) { |
| 134 | + lastMsg = now; |
| 135 | + ++value; |
| 136 | + snprintf (msg, 50, "hello world #%ld", value); |
| 137 | + Serial.print("Publish message: "); |
| 138 | + Serial.println(msg); |
| 139 | + client.publish("outTopic", msg); |
| 140 | + } |
| 141 | +} |
0 commit comments