Skip to content

Commit 3455093

Browse files
committed
Add combined ThingSpeak MQTT Publish and Subscribe example
1 parent 4423251 commit 3455093

File tree

1 file changed

+65
-12
lines changed

1 file changed

+65
-12
lines changed
+65-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
Written by: Paul Clark
99
Date: November 14th 2023
1010
11-
This example uses the SARA's mobile data connection to publish random temperatures on ThingSpeak using MQTT.
12-
https://thingspeak.com/
11+
This example uses the SARA's mobile data connection and MQTT to publish random temperatures on ThingSpeak.
12+
It also subscribes to the same topic (channel) so you can read the data back again!
13+
14+
See: https://thingspeak.com/
1315
14-
See: https://uk.mathworks.com/help/thingspeak/mqtt-basics.html#responsive_offcanvas
16+
And: https://uk.mathworks.com/help/thingspeak/mqtt-basics.html#responsive_offcanvas
1517
1618
You will need to:
1719
Create a ThingSpeak User Account – https://thingspeak.com/login
@@ -45,7 +47,7 @@ String myClientID = "OAAxOjYHIwooJykfCiYoEx0";
4547

4648
String myPassword = "RqY/6L246tULLVWUzCqJBX/V";
4749

48-
String myChannelID = "1225363";
50+
String myChannelID = "1225363"; // Public View: https://thingspeak.com/channels/1225363
4951

5052
// SARA-R5
5153

@@ -115,7 +117,7 @@ void setup()
115117
while (Serial.available()) // Empty the serial RX buffer
116118
Serial.read();
117119

118-
mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
120+
//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
119121

120122
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
121123
// Comment the next line if required
@@ -186,9 +188,33 @@ void setup()
186188

187189
// Connect
188190
if (mySARA.connectMQTT() == SARA_R5_SUCCESS)
189-
Serial.println(F("MQTT connected"));
191+
Serial.println(F("MQTT connect: success"));
192+
else
193+
Serial.println(F("MQTT connect: failed!"));
194+
195+
// The LTE modem has difficulties subscribing/unsubscribing more than one topic at the same time.
196+
// We can only start one operation at a time wait for the URC and add a extra delay before we can
197+
// do the next operation.
198+
// Wait for ~2 seconds
199+
for (int i = 0; i < 200; i++)
200+
{
201+
mySARA.bufferedPoll(); // Keep processing data from the SARA
202+
delay(10);
203+
}
204+
205+
// Subscribe to the channel topic, so we can read the data back again
206+
String subscribeTopic = "channels/" + myChannelID + "/subscribe/fields/field1";
207+
if (mySARA.subscribeMQTTtopic(0, subscribeTopic) == SARA_R5_SUCCESS) // QoS = 0
208+
Serial.println(F("MQTT subscribe: success"));
190209
else
191-
Serial.println(F("MQTT failed to connect!"));
210+
Serial.println(F("MQTT subscribe: failed!"));
211+
212+
// Wait for ~2 seconds
213+
for (int i = 0; i < 200; i++)
214+
{
215+
mySARA.bufferedPoll(); // Keep processing data from the SARA
216+
delay(10);
217+
}
192218
}
193219

194220
void loop()
@@ -199,17 +225,44 @@ void loop()
199225
String Topic = "channels/" + myChannelID + "/publish";
200226
String DataField = "field1=" + String(temperature) + "&status=MQTTPUBLISH";
201227

228+
Serial.println();
202229
Serial.print(F("Publishing a temperature of "));
203230
Serial.print(String(temperature));
204231
Serial.println(F(" to ThingSpeak"));
205232

206233
// Publish the text message
207-
mySARA.mqttPublishTextMsg(Topic, DataField.c_str(), 0, true); // This defaults to QoS = 0, and retain = false
234+
mySARA.mqttPublishTextMsg(Topic, DataField.c_str(), 0, true); // QoS = 0, retain = true
235+
236+
// Wait for ~10 seconds
237+
for (int i = 0; i < 1000; i++)
238+
{
239+
mySARA.bufferedPoll(); // Keep processing data from the SARA
240+
delay(10);
241+
}
242+
243+
// Check for any received data
244+
// The MQTT API does not allow getting the size before actually reading the data.
245+
// So we have to allocate a big enough buffer.
246+
const int MQTT_MAX_MSG_SIZE = 1024;
247+
static uint8_t buf[MQTT_MAX_MSG_SIZE];
248+
String topic;
249+
int len = -1;
250+
int qos = -1;
251+
if (mySARA.readMQTT(&qos, &topic, buf, MQTT_MAX_MSG_SIZE, &len) == SARA_R5_SUCCESS)
252+
{
253+
if (len > 0)
254+
{
255+
Serial.println();
256+
Serial.print(F("Subscribed MQTT data: "));
257+
Serial.write((const char *)buf, len);
258+
Serial.println();
259+
}
260+
}
208261

209-
// Wait for 20 seconds
210-
for (int i = 0; i < 20000; i++)
262+
// Wait for ~10 seconds
263+
for (int i = 0; i < 1000; i++)
211264
{
212-
mySARA.poll(); // Keep processing data from the SARA
213-
delay(1);
265+
mySARA.bufferedPoll(); // Keep processing data from the SARA
266+
delay(10);
214267
}
215268
}

0 commit comments

Comments
 (0)