Skip to content

Commit 0e8ee6d

Browse files
authored
Merge pull request #2 from fpistm/MQTTExamples
Mqtt examples
2 parents c9e39c7 + d6fa824 commit 0e8ee6d

File tree

12 files changed

+878
-2
lines changed

12 files changed

+878
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Ethernet_MQTT_Adafruit.io
2+
3+
This example demonstrates the capabilities of the [PubSubClient](https://github.com/knolleary/pubsubclient) library in combination
4+
with the [STM32F746G-DISCOVERY](http://www.st.com/en/evaluation-tools/32f746gdiscovery.html) board to publish on https://io.adafruit.com broker
5+
6+
See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview
7+
8+
# Dependencies
9+
10+
Install the following libraries using the Arduino IDE: **_Sketch -> Include Library -> Manage Libraries_** and search:
11+
12+
* [PubSubClient](https://github.com/knolleary/pubsubclient): Arduino Client for MQTT.
13+
* [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet): Ethernet library for STM32 based board with on-board Ethernet connector.
14+
* [STM32duino HTS221](https://github.com/stm32duino/HTS221): Ethernet library for STM32 based board with on-board Ethernet connector.
15+
16+
# Hardware
17+
18+
To get temperature and humidity you will need the [X-NUCLEO-IKS01A1](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), a motion MEMS and environmental sensor expansion board for the STM32 board.
19+
20+
# Adafruit IO Dashboard.
21+
22+
You will need to create some feeds in your Adafruit IO Dashboard.
23+
24+
See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io
25+
26+
to know how register and create your Adafruit IO dashboard and feeds:
27+
* hello: Text block
28+
* onoff: Toggle block
29+
* temp: Stream block
30+
* hum: Gauge block
31+
32+
A screenshot of the dashboard:
33+
[[/img/dashboard_adafruit.png|alt="Dashboard Adafruit"]]
34+
35+
It connects to the Adafruit IO's MQTT server (a.k.a broker) server then:
36+
* publishes announcement "_Hi, I'm STM32 user!_" to the topic `AIO_USERNAME"/feeds/hello"`
37+
* subscribes to the topic `AIO_USERNAME"/feeds/onoff"`, switching `LED_BUILTIN` state
38+
* publishes temperature and humidity from the HTS221 sensor to the topic
39+
`AIO_USERNAME"/feeds/temp"` and `AIO_USERNAME"/feeds/hum"` every 10 seconds

examples/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino renamed to examples/Boards/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ boolean swipe_detected() {
122122
int gesture_code;
123123
int status;
124124
boolean ret = false;
125-
125+
126126
sensor_vl53l0x->StartMeasurement();
127127

128128
int top_done = 0;
@@ -339,7 +339,7 @@ void setup() {
339339

340340
/* Configure the User Button in GPIO Mode */
341341
pinMode(USER_BTN, INPUT);
342-
342+
343343
// Initialize I2C bus.
344344
dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL);
345345
dev_i2c->begin();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# WiFi_MQTT_Adafruit.io
2+
3+
This example demonstrates the capabilities of the [PubSubClient](https://github.com/knolleary/pubsubclient) library in combination
4+
with the [B-L475E-IOT01A](http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html) board to publish on https://io.adafruit.com broker
5+
6+
See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview
7+
8+
# Dependencies
9+
10+
Install the following libraries using the Arduino IDE: **_Sketch -> Include Library -> Manage Libraries_** and search:
11+
12+
* [PubSubClient](https://github.com/knolleary/pubsubclient): Arduino Client for MQTT.
13+
* [STM32duino ISM43362-M3G-L44](https://github.com/stm32duino/WiFi-ISM43362-M3G-L44): WiFi library for the [B-L475E-IOT01A](http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html) board.
14+
* [STM32duino HTS221](https://github.com/stm32duino/HTS221): Ethernet library for STM32 based board with on-board Ethernet connector.
15+
16+
# Adafruit IO Dashboard.
17+
18+
You will need to create some feeds in your Adafruit IO Dashboard.
19+
20+
See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io
21+
22+
to know how register and create your Adafruit IO dashboard and feeds:
23+
* hello: Text block
24+
* onoff: Toggle block
25+
* temp: Stream block
26+
* hum: Gauge block
27+
28+
A screenshot of the dashboard:
29+
[[/img/dashboard_adafruit.png|alt="Dashboard Adafruit"]]
30+
31+
It connects to the Adafruit IO's MQTT server (a.k.a broker) server then:
32+
* publishes announcement "_Hi, I'm STM32 user!_" to the topic `AIO_USERNAME"/feeds/hello"`
33+
* subscribes to the topic `AIO_USERNAME"/feeds/onoff"`, switching `LED_BUILTIN` state
34+
* publishes temperature and humidity from the HTS221 sensor to the topic
35+
`AIO_USERNAME"/feeds/temp"` and `AIO_USERNAME"/feeds/hum"` every 10 seconds

0 commit comments

Comments
 (0)