8
8
Written by: Paul Clark
9
9
Date: November 14th 2023
10
10
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/
13
15
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
15
17
16
18
You will need to:
17
19
Create a ThingSpeak User Account – https://thingspeak.com/login
@@ -45,7 +47,7 @@ String myClientID = "OAAxOjYHIwooJykfCiYoEx0";
45
47
46
48
String myPassword = " RqY/6L246tULLVWUzCqJBX/V" ;
47
49
48
- String myChannelID = " 1225363" ;
50
+ String myChannelID = " 1225363" ; // Public View: https://thingspeak.com/channels/1225363
49
51
50
52
// SARA-R5
51
53
@@ -115,7 +117,7 @@ void setup()
115
117
while (Serial.available ()) // Empty the serial RX buffer
116
118
Serial.read ();
117
119
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
119
121
120
122
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
121
123
// Comment the next line if required
@@ -186,9 +188,33 @@ void setup()
186
188
187
189
// Connect
188
190
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" ));
190
209
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
+ }
192
218
}
193
219
194
220
void loop ()
@@ -199,17 +225,44 @@ void loop()
199
225
String Topic = " channels/" + myChannelID + " /publish" ;
200
226
String DataField = " field1=" + String (temperature) + " &status=MQTTPUBLISH" ;
201
227
228
+ Serial.println ();
202
229
Serial.print (F (" Publishing a temperature of " ));
203
230
Serial.print (String (temperature));
204
231
Serial.println (F (" to ThingSpeak" ));
205
232
206
233
// 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
+ }
208
261
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++)
211
264
{
212
- mySARA.poll (); // Keep processing data from the SARA
213
- delay (1 );
265
+ mySARA.bufferedPoll (); // Keep processing data from the SARA
266
+ delay (10 );
214
267
}
215
268
}
0 commit comments