Skip to content

Commit d35bce3

Browse files
authored
Merge pull request #36 from sparkfun/release_candidate
Version 1.1.9
2 parents 22e738f + 8815d38 commit d35bce3

File tree

5 files changed

+275
-2
lines changed

5 files changed

+275
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
/*
2+
3+
SARA-R5 Example
4+
===============
5+
6+
ThingSpeak (MQTT)
7+
8+
Written by: Paul Clark
9+
Date: November 14th 2023
10+
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/
15+
16+
And: https://uk.mathworks.com/help/thingspeak/mqtt-basics.html#responsive_offcanvas
17+
18+
You will need to:
19+
Create a ThingSpeak User Account – https://thingspeak.com/login
20+
Create a new Channel by selecting Channels, My Channels, and then New Channel
21+
Note the Channel ID and copy&paste it into myChannelID below
22+
Click on the Devices drop-down at the top of the screen and select MQTT
23+
Create a new MQTT Device using "Add a new device". Give it a name
24+
Authorize the New Channel you created above, then Add Channel, then Add Device
25+
Copy the Username, Client ID and password into myUsername, myClientID and myPassword below
26+
The random temperature reading will be added to the channel as "Field 1"
27+
28+
Feel like supporting open source hardware?
29+
Buy a board from SparkFun!
30+
31+
Licence: MIT
32+
Please see LICENSE.md for full details
33+
34+
*/
35+
36+
#include <IPAddress.h>
37+
38+
// ThingSpeak via MQTT Publish
39+
40+
String brokerName = "mqtt3.thingspeak.com"; // MQTT Broker
41+
42+
const int brokerPort = 1883; // MQTT port (TCP, no encryption)
43+
44+
String myUsername = "OAAxOjYHIwooJykfCiYoEx0";
45+
46+
String myClientID = "OAAxOjYHIwooJykfCiYoEx0";
47+
48+
String myPassword = "RqY/6L246tULLVWUzCqJBX/V";
49+
50+
String myChannelID = "1225363"; // Public View: https://thingspeak.com/channels/1225363
51+
52+
// SARA-R5
53+
54+
#include <SparkFun_u-blox_SARA-R5_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_SARA-R5_Arduino_Library
55+
56+
// Uncomment the next line to connect to the SARA-R5 using hardware Serial1
57+
#define saraSerial Serial1
58+
59+
// Uncomment the next line to create a SoftwareSerial object to pass to the SARA-R5 library instead
60+
//SoftwareSerial saraSerial(8, 9);
61+
62+
// Create a SARA_R5 object to use throughout the sketch
63+
// Usually we would tell the library which GPIO pin to use to control the SARA power (see below),
64+
// but we can start the SARA without a power pin. It just means we need to manually
65+
// turn the power on if required! ;-D
66+
SARA_R5 mySARA;
67+
68+
// Create a SARA_R5 object to use throughout the sketch
69+
// We need to tell the library what GPIO pin is connected to the SARA power pin.
70+
// If you're using the MicroMod Asset Tracker and the MicroMod Artemis Processor Board,
71+
// the pin name is G2 which is connected to pin AD34.
72+
// Change the pin number if required.
73+
//SARA_R5 mySARA(34);
74+
75+
// processMQTTcommandResult is provided to the SARA-R5 library via a
76+
// callback setter -- setMQTTCommandCallback. (See the end of setup())
77+
void processMQTTcommandResult(int command, int result)
78+
{
79+
Serial.println();
80+
Serial.print(F("MQTT Command Result: command: "));
81+
Serial.print(command);
82+
Serial.print(F(" result: "));
83+
Serial.print(result);
84+
if (result == 0)
85+
Serial.print(F(" (fail)"));
86+
if (result == 1)
87+
Serial.print(F(" (success)"));
88+
Serial.println();
89+
90+
// Get and print the most recent MQTT protocol error
91+
int error_class;
92+
int error_code;
93+
mySARA.getMQTTprotocolError(&error_class, &error_code);
94+
Serial.print(F("Most recent MQTT protocol error: class: "));
95+
Serial.print(error_class);
96+
Serial.print(F(" code: "));
97+
Serial.print(error_code);
98+
if (error_code == 0)
99+
Serial.print(F(" (no error)"));
100+
Serial.println();
101+
}
102+
103+
void setup()
104+
{
105+
delay(1000);
106+
107+
String currentOperator = "";
108+
109+
Serial.begin(115200); // Start the serial console
110+
111+
// Wait for user to press key to begin
112+
Serial.println(F("SARA-R5 Example"));
113+
Serial.println(F("Wait for the SARA NI LED to light up - then press any key to begin"));
114+
115+
while (!Serial.available()) // Wait for the user to press a key (send any serial character)
116+
;
117+
while (Serial.available()) // Empty the serial RX buffer
118+
Serial.read();
119+
120+
//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
121+
122+
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
123+
// Comment the next line if required
124+
mySARA.invertPowerPin(true);
125+
126+
// Initialize the SARA
127+
if (mySARA.begin(saraSerial, 115200) )
128+
{
129+
Serial.println(F("SARA-R5 connected!"));
130+
}
131+
else
132+
{
133+
Serial.println(F("Unable to communicate with the SARA."));
134+
Serial.println(F("Manually power-on (hold the SARA On button for 3 seconds) on and try again."));
135+
while (1) ; // Loop forever on fail
136+
}
137+
Serial.println();
138+
139+
// First check to see if we're connected to an operator:
140+
if (mySARA.getOperator(&currentOperator) == SARA_R5_SUCCESS)
141+
{
142+
Serial.print(F("Connected to: "));
143+
Serial.println(currentOperator);
144+
}
145+
else
146+
{
147+
Serial.print(F("The SARA is not yet connected to an operator. Please use the previous examples to connect. Or wait and retry. Freezing..."));
148+
while (1)
149+
; // Do nothing more
150+
}
151+
152+
// Deactivate the PSD profile - in case one is already active
153+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_DEACTIVATE) != SARA_R5_SUCCESS)
154+
{
155+
Serial.println(F("Warning: performPDPaction (deactivate profile) failed. Probably because no profile was active."));
156+
}
157+
158+
// Load the PSD profile from NVM - these were saved by a previous example
159+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_LOAD) != SARA_R5_SUCCESS)
160+
{
161+
Serial.println(F("performPDPaction (load from NVM) failed! Freezing..."));
162+
while (1)
163+
; // Do nothing more
164+
}
165+
166+
// Activate the PSD profile
167+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_ACTIVATE) != SARA_R5_SUCCESS)
168+
{
169+
Serial.println(F("performPDPaction (activate profile) failed! Freezing..."));
170+
while (1)
171+
; // Do nothing more
172+
}
173+
174+
// Set up the MQTT command callback
175+
mySARA.setMQTTCommandCallback(processMQTTcommandResult);
176+
177+
// Disable the security profile
178+
mySARA.setMQTTsecure(false);
179+
180+
// Set Client ID
181+
mySARA.setMQTTclientId(myClientID);
182+
183+
// Set the broker name and port
184+
mySARA.setMQTTserver(brokerName, brokerPort);
185+
186+
// Set the user name and password
187+
mySARA.setMQTTcredentials(myUsername, myPassword);
188+
189+
// Connect
190+
if (mySARA.connectMQTT() == SARA_R5_SUCCESS)
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"));
209+
else
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+
}
218+
}
219+
220+
void loop()
221+
{
222+
float temperature = ((float)random(2000,3000)) / 100.0; // Create a random temperature between 20 and 30
223+
224+
// Send data using MQTT Publish
225+
String Topic = "channels/" + myChannelID + "/publish";
226+
String DataField = "field1=" + String(temperature) + "&status=MQTTPUBLISH";
227+
228+
Serial.println();
229+
Serial.print(F("Publishing a temperature of "));
230+
Serial.print(String(temperature));
231+
Serial.println(F(" to ThingSpeak"));
232+
233+
// Publish the text message
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+
}
261+
262+
// Wait for ~10 seconds
263+
for (int i = 0; i < 1000; i++)
264+
{
265+
mySARA.bufferedPoll(); // Keep processing data from the SARA
266+
delay(10);
267+
}
268+
}

keywords.txt

+4
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,16 @@ sendHTTPPOSTfile KEYWORD2
164164
nvMQTT KEYWORD2
165165
setMQTTclientId KEYWORD2
166166
setMQTTserver KEYWORD2
167+
setMQTTcredentials KEYWORD2
167168
setMQTTsecure KEYWORD2
168169
connectMQTT KEYWORD2
169170
disconnectMQTT KEYWORD2
170171
subscribeMQTTtopic KEYWORD2
171172
unsubscribeMQTTtopic KEYWORD2
172173
readMQTT KEYWORD2
174+
mqttPublishTextMsg KEYWORD2
175+
mqttPublishBinaryMsg KEYWORD2
176+
mqttPublishFromFile KEYWORD2
173177
getMQTTprotocolError KEYWORD2
174178
resetSecurityProfile KEYWORD2
175179
configSecurityProfileString KEYWORD2

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun u-blox SARA-R5 Arduino Library
2-
version=1.1.8
2+
version=1.1.9
33
author=SparkFun Electronics <[email protected]>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure cloud<br/><br/>

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4814,7 +4814,7 @@ SARA_R5_error_t SARA_R5::setSecurityManager(SARA_R5_sec_manager_opcode_t opcode,
48144814
_debugPort->println(F(" bytes"));
48154815
}
48164816
hwWriteData(data.c_str(), dataLen);
4817-
err = waitForResponse(SARA_R5_RESPONSE_OK, SARA_R5_RESPONSE_ERROR, SARA_R5_STANDARD_RESPONSE_TIMEOUT*3);
4817+
err = waitForResponse(SARA_R5_RESPONSE_OK, SARA_R5_RESPONSE_ERROR, SARA_R5_SECURITY_RESPONSE_TIMEOUT);
48184818
}
48194819

48204820

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#define SARA_R5_IP_CONNECT_TIMEOUT 130000
9090
#define SARA_R5_POLL_DELAY 1
9191
#define SARA_R5_SOCKET_WRITE_TIMEOUT 10000
92+
#define SARA_R5_SECURITY_RESPONSE_TIMEOUT 10000
9293

9394
// ## Suported AT Commands
9495
// ### General

0 commit comments

Comments
 (0)