Skip to content

Commit 183bd54

Browse files
committed
Added examples using PubSubClient (Arduino Client for MQTT)
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 2a1dcb5 commit 183bd54

File tree

8 files changed

+692
-0
lines changed

8 files changed

+692
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# PubSubClient
2+
3+
This library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT.
4+
5+
https://github.com/knolleary/pubsubclient
6+
7+
# Dependencies
8+
9+
Install the following libraries using the Arduino IDE: **_Sketch -> Include Library -> Manage Libraries_** and search:
10+
11+
* [PubSubClient](https://github.com/knolleary/pubsubclient): Arduino Client for MQTT.
12+
* [STM32duino ISM43362-M3G-L44](https://github.com/stm32duino/WiFi-ISM43362-M3G-L44): WiFi library for the B-L475E-IOT01A board.
13+
* [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet): Ethernet library for STM32 based board with on-board Ethernet connector.
14+
15+
# Examples
16+
17+
## mqtt_B-L475E-IOT01A
18+
This example is based on the mqtt_esp8266 provided with PubSubClient library.
19+
It has been modified to use the [STM32duino ISM43362-M3G-L44](https://github.com/stm32duino/WiFi-ISM43362-M3G-L44) WiFi library with [B-L475E-IOT01A](http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html) board
20+
21+
## mqtt_STM32Ethernet
22+
This example is based on the mqtt_basic provided with PubSubClient library.
23+
It has been modified to use the [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet) Ethernet library with a STM32 based board
24+
with on-board Ethernet connector.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Basic STM32 Ethernet MQTT example
3+
4+
This sketch demonstrates the basic capabilities of the library.
5+
It connects to an MQTT server then:
6+
- publishes "hello world" to the topic "outTopic" every two seconds
7+
- subscribes to the topic "inTopic", printing out any messages
8+
it receives. NB - it assumes the received payloads are strings not binary
9+
10+
It will reconnect to the server if the connection is lost using a blocking
11+
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
12+
achieve the same result without blocking the main loop.
13+
14+
*/
15+
16+
#include <LwIP.h>
17+
#include <STM32Ethernet.h>
18+
#include <PubSubClient.h>
19+
20+
// Update these with values suitable for your network.
21+
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
22+
// Set the static IP address to use if the DHCP fails to assign
23+
IPAddress ip(192, 168, 0, 177);
24+
// if you don't want to use DNS (and reduce your sketch size)
25+
// use the numeric IP instead of the name for the server:
26+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
27+
char server[] = "broker.mqtt-dashboard.com"; // name address for mqtt broker (using DNS)
28+
long lastMsg = 0;
29+
char msg[50];
30+
long value = 0;
31+
32+
void callback(char* topic, byte* payload, unsigned int length) {
33+
Serial.print("Message arrived [");
34+
Serial.print(topic);
35+
Serial.print("] ");
36+
for (unsigned int i=0;i<length;i++) {
37+
Serial.print((char)payload[i]);
38+
}
39+
Serial.println();
40+
}
41+
42+
EthernetClient ethClient;
43+
PubSubClient client(ethClient);
44+
45+
void reconnect() {
46+
47+
// Loop until we're reconnected
48+
while (!client.connected()) {
49+
Serial.print("Attempting MQTT connection...");
50+
// Attempt to connect
51+
if (client.connect("arduinoClient72")) {
52+
Serial.println("connected");
53+
// Once connected, publish an announcement...
54+
client.publish("outTopic","hello world");
55+
// ... and resubscribe
56+
client.subscribe("inTopic");
57+
} else {
58+
Serial.print("failed, rc=");
59+
Serial.print(client.state());
60+
Serial.println(" try again in 5 seconds");
61+
// Wait 5 seconds before retrying
62+
delay(5000);
63+
}
64+
}
65+
}
66+
67+
void setup()
68+
{
69+
Serial.begin(115200);
70+
71+
client.setServer(server, 1883);
72+
client.setCallback(callback);
73+
74+
// Start the Ethernet connection:
75+
if (Ethernet.begin(mac) == 0) {
76+
Serial.println("Failed to configure Ethernet using DHCP");
77+
// Try to congifure using IP address instead of DHCP:
78+
Ethernet.begin(mac, ip);
79+
}
80+
// Allow the hardware to sort itself out
81+
delay(1500);
82+
}
83+
84+
void loop()
85+
{
86+
if (!client.connected()) {
87+
reconnect();
88+
}
89+
client.loop();
90+
91+
long now = millis();
92+
if (now - lastMsg > 2000) {
93+
lastMsg = now;
94+
++value;
95+
snprintf (msg, 50, "hello world #%ld", value);
96+
Serial.print("Publish message: ");
97+
Serial.println(msg);
98+
client.publish("outTopic", msg);
99+
}
100+
}

0 commit comments

Comments
 (0)