|
| 1 | +/* |
| 2 | + STM32F746G-DISCOVERY MQTT example to publish on https://io.adafruit.com broker |
| 3 | +
|
| 4 | + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview |
| 5 | +
|
| 6 | + This sketch demonstrates the capabilities of the pubsub library in combination |
| 7 | + with the STM32F746G-DISCOVERY board. |
| 8 | +
|
| 9 | + It will use several components of the board and you need to install corresponding libraries : |
| 10 | + - Ethernet (STM32Ethernet) : https://github.com/stm32duino/STM32Ethernet |
| 11 | + - Temperature and humidity sensor (HTS221) : https://github.com/stm32duino/HTS221 |
| 12 | + - Arduino Client for MQTT: https://github.com/knolleary/pubsubclient |
| 13 | +
|
| 14 | + To get temperature and humidity you will need the X-NUCLEO-IKS01A1, a motion MEMS and |
| 15 | + environmental sensor expansion board for the STM32 board. |
| 16 | +
|
| 17 | + You can find more information on the board and exapansion board here : |
| 18 | + http://www.st.com/en/evaluation-tools/32f746gdiscovery.html |
| 19 | + http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-sense-hw/x-nucleo-iks01a1.html) |
| 20 | +
|
| 21 | + You will need to create some feeds in your Adafruit IO Dashboard. |
| 22 | + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io |
| 23 | + to know how register and create your Adafruit IO dashboard and feeds: |
| 24 | + - hello: Text block |
| 25 | + - onoff: Toggle block |
| 26 | + - temp: Stream block |
| 27 | + - hum: Gauge block |
| 28 | + A screenshot of the dashboard is available in the img/ directory: dashboard_adafruit.png |
| 29 | +
|
| 30 | + It connects to the Adafruit IO's MQTT server (a.k.a broker) server then: |
| 31 | + - publishes announcement "Hi, I'm STM32 user!" to the topic 'AIO_USERNAME"/feeds/hello"' |
| 32 | + - subscribes to the topic AIO_USERNAME"/feeds/onoff"', switching LED_BUILTIN state |
| 33 | + - publishes temperature and humidity from the HTS221 sensor to the topic |
| 34 | + 'AIO_USERNAME"/feeds/temp"' and 'AIO_USERNAME"/feeds/hum"' every 10 seconds |
| 35 | +
|
| 36 | + It will reconnect to the server if the connection is lost using a blocking |
| 37 | + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to |
| 38 | + achieve the same result without blocking the main loop. |
| 39 | +
|
| 40 | +*/ |
| 41 | +#include <LwIP.h> |
| 42 | +#include <STM32Ethernet.h> |
| 43 | +#include <PubSubClient.h> |
| 44 | +#include <HTS221Sensor.h> |
| 45 | + |
| 46 | +// Adafruit.io Setup |
| 47 | +#define AIO_SERVER "io.adafruit.com" |
| 48 | +#define AIO_SERVERPORT 1883 |
| 49 | +#define AIO_USERNAME "......" |
| 50 | +#define AIO_KEY "........" |
| 51 | + |
| 52 | +// Etherrnet setup |
| 53 | +// Update these with values suitable for your network. |
| 54 | +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; |
| 55 | +// Set the static IP address to use if the DHCP fails to assign |
| 56 | +IPAddress ip(192, 168, 0, 177); |
| 57 | +EthernetClient STClient; |
| 58 | + |
| 59 | +// i2c sensors |
| 60 | +HTS221Sensor *HumTemp; |
| 61 | + |
| 62 | +PubSubClient client(STClient); |
| 63 | +long lastMsg = 0; |
| 64 | +char msg[8]; |
| 65 | + |
| 66 | +void setup() { |
| 67 | + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output |
| 68 | + Serial.begin(115200); |
| 69 | + |
| 70 | + client.setServer(AIO_SERVER, AIO_SERVERPORT); |
| 71 | + client.setCallback(callback); |
| 72 | + |
| 73 | + // Start the Ethernet connection: |
| 74 | + if (Ethernet.begin(mac) == 0) { |
| 75 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 76 | + // Try to congifure using IP address instead of DHCP: |
| 77 | + Ethernet.begin(mac, ip); |
| 78 | + } |
| 79 | + // Allow the hardware to sort itself out |
| 80 | + delay(1500); |
| 81 | + |
| 82 | + // Initialize I2C bus. |
| 83 | + Wire.begin(); |
| 84 | + |
| 85 | + // Initlialize components. |
| 86 | + HumTemp = new HTS221Sensor (&Wire); |
| 87 | + HumTemp->Enable(); |
| 88 | +} |
| 89 | + |
| 90 | +void callback(char* topic, byte* payload, unsigned int length) { |
| 91 | + |
| 92 | +#if 0 |
| 93 | + Serial.print("Message arrived ["); |
| 94 | + Serial.print(topic); |
| 95 | + Serial.print("] "); |
| 96 | + for (unsigned int i = 0; i < length; i++) { |
| 97 | + Serial.print((char)payload[i]); |
| 98 | + } |
| 99 | + Serial.println(); |
| 100 | +#else |
| 101 | + UNUSED(topic); |
| 102 | +#endif |
| 103 | + if (length > 1) { |
| 104 | + // Switch on/off the LED (payload messages can be 'ON' or 'OFF') |
| 105 | + if ((char)payload[1] == 'N') { |
| 106 | + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on |
| 107 | + } else { |
| 108 | + digitalWrite(LED_BUILTIN, LOW); // Turn the LED off |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void reconnect() { |
| 114 | + // Loop until we're reconnected |
| 115 | + while (!client.connected()) { |
| 116 | + Serial.print("Attempting MQTT connection..."); |
| 117 | + // Attempt to connect |
| 118 | + // Note - the default maximum packet size is 128 bytes. If the |
| 119 | + // combined length of clientId, username and password exceed this, |
| 120 | + // you will need to increase the value of MQTT_MAX_PACKET_SIZE in |
| 121 | + // PubSubClient.h |
| 122 | + if (client.connect("STM32Client", AIO_USERNAME, AIO_KEY)) { |
| 123 | + Serial.println("connected"); |
| 124 | + // Once connected, publish an announcement... |
| 125 | + client.publish(AIO_USERNAME"/feeds/hello", "Hi, I'm STM32 user!"); |
| 126 | + // ... and resubscribe |
| 127 | + client.subscribe(AIO_USERNAME"/feeds/onoff"); |
| 128 | + } else { |
| 129 | + Serial.print("failed, rc="); |
| 130 | + Serial.print(client.state()); |
| 131 | + Serial.println(" try again in 5 seconds"); |
| 132 | + // Wait 5 seconds before retrying |
| 133 | + delay(5000); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void loop() { |
| 139 | + float temperature, humidity; |
| 140 | + if (!client.connected()) { |
| 141 | + reconnect(); |
| 142 | + } |
| 143 | + client.loop(); |
| 144 | + |
| 145 | + long now = millis(); |
| 146 | + if (now - lastMsg > 10000) { |
| 147 | + lastMsg = now; |
| 148 | + HumTemp->GetTemperature(&temperature); |
| 149 | + dtostrf(temperature, 2, 2, msg); |
| 150 | + Serial.print("Publish temperature "); |
| 151 | + Serial.println(msg); |
| 152 | + client.publish(AIO_USERNAME"/feeds/temp", msg); |
| 153 | + |
| 154 | + HumTemp->GetHumidity(&humidity); |
| 155 | + snprintf (msg, 8, "%u", (unsigned int)humidity); |
| 156 | + Serial.print("Publish humidity "); |
| 157 | + Serial.println(msg); |
| 158 | + client.publish(AIO_USERNAME"/feeds/hum", msg); |
| 159 | + } |
| 160 | +} |
0 commit comments