Skip to content

Commit b596015

Browse files
committed
Add Example14_AssistNowOffline
1 parent 1b08488 commit b596015

File tree

6 files changed

+748
-5
lines changed

6 files changed

+748
-5
lines changed

examples/SARA-R5_Example12_AssistNowOnline/SARA-R5_Example12_AssistNowOnline.ino

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
3636

37-
#include <IPAddress.h>
38-
3937
#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
4038

4139
// Uncomment the next line to connect to the SARA-R5 using hardware Serial1
@@ -57,6 +55,12 @@ SARA_R5 mySARA;
5755
// Change the pin number if required.
5856
//SARA_R5 mySARA(34);
5957

58+
// Create a SARA_R5 object to use throughout the sketch
59+
// If you are using the LTE GNSS Breakout, and have access to the SARA's RESET_N pin, you can pass that to the library too
60+
// allowing it to do an emergency shutdown if required.
61+
// Change the pin numbers if required.
62+
//SARA_R5 mySARA(34, 35); // PWR_ON, RESET_N
63+
6064
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6165

6266
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS
@@ -72,7 +76,7 @@ void setup()
7276

7377
// Wait for user to press key to begin
7478
Serial.println(F("SARA-R5 Example"));
75-
Serial.println(F("Press any key to begin"));
79+
Serial.println(F("Wait for the SARA NI LED to light up - then press any key to begin"));
7680

7781
while (!Serial.available()) // Wait for the user to press a key (send any serial character)
7882
;

examples/SARA-R5_Example13_SetClockWithNTP/SARA-R5_NTP.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
// Many thanks to: Michael Margolis, Tom Igoe, Arturo Guadalupi, et al
88

99
// NTP Server
10+
const char* ntpServer = "pool.ntp.org"; // The Network Time Protocol Server
1011
//const char* ntpServer = "africa.pool.ntp.org"; // The Network Time Protocol Server
1112
//const char* ntpServer = "asia.pool.ntp.org"; // The Network Time Protocol Server
1213
//const char* ntpServer = "europe.pool.ntp.org"; // The Network Time Protocol Server
13-
const char* ntpServer = "north-america.pool.ntp.org"; // The Network Time Protocol Server
14+
//const char* ntpServer = "north-america.pool.ntp.org"; // The Network Time Protocol Server
1415
//const char* ntpServer = "oceania.pool.ntp.org"; // The Network Time Protocol Server
1516
//const char* ntpServer = "south-america.pool.ntp.org"; // The Network Time Protocol Server
1617

@@ -40,7 +41,7 @@ bool getNTPTime(uint8_t *y, uint8_t *mo, uint8_t *d, uint8_t *h, uint8_t *min, u
4041

4142
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4243

43-
//Open a socket to the NTP server
44+
//Allocate a UDP socket to talk to the NTP server
4445

4546
int socketNum = mySARA.socketOpen(SARA_R5_UDP);
4647
if (socketNum == -1)
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
2+
#include "secrets.h" // Update secrets.h with your AssistNow token string
3+
4+
// u-blox AssistNow https servers
5+
const char assistNowOfflineServer[] = "offline-live1.services.u-blox.com";
6+
//const char assistNowOfflineServer[] = "offline-live2.services.u-blox.com"; // Alternate server
7+
8+
const char getQuery[] = "GetOfflineData.ashx?";
9+
const char tokenPrefix[] = "token=";
10+
const char tokenSuffix[] = ";";
11+
const char getGNSS[] = "gnss=gps,glo;"; // GNSS can be: gps,qzss,glo,bds,gal
12+
const char getFormat[] = "format=mga;"; // Data format. Leave set to mga for M8 onwards. Can be aid.
13+
const char getPeriod[] = "period=1;"; // Optional. The number of weeks into the future that the data will be valid. Can be 1-5. Default = 4.
14+
const char getMgaResolution[] = "resolution=1;"; // Optional. Data resolution: 1 = every day; 2 = every other day; 3 = every 3rd day.
15+
//Note: always use resolution=1. findMGAANOForDate does not yet support finding the 'closest' date. It needs an exact match.
16+
17+
volatile bool httpResultSeen = false; // Flag to indicate that the HTTP URC was received
18+
19+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20+
21+
// processHTTPcommandResult is provided to the SARA-R5 library via a
22+
// callback setter -- setHTTPCommandCallback. (See the end of setup())
23+
void processHTTPcommandResult(int profile, int command, int result)
24+
{
25+
Serial.println();
26+
Serial.print(F("HTTP Command Result: profile: "));
27+
Serial.print(profile);
28+
Serial.print(F(" command: "));
29+
Serial.print(command);
30+
Serial.print(F(" result: "));
31+
Serial.print(result);
32+
if (result == 0)
33+
Serial.print(F(" (fail)"));
34+
if (result == 1)
35+
Serial.print(F(" (success)"));
36+
Serial.println();
37+
38+
// Get and print the most recent HTTP protocol error
39+
int error_class;
40+
int error_code;
41+
mySARA.getHTTPprotocolError(0, &error_class, &error_code);
42+
Serial.print(F("Most recent HTTP protocol error: class: "));
43+
Serial.print(error_class);
44+
Serial.print(F(" code: "));
45+
Serial.print(error_code);
46+
if (error_code == 0)
47+
Serial.print(F(" (no error)"));
48+
Serial.println();
49+
50+
httpResultSeen = true; // Set the flag
51+
}
52+
53+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
54+
55+
bool getAssistNowOfflineData(String theFilename)
56+
{
57+
// Use HTTP GET to receive the AssistNow_Offline data. Store it in the SARA-R5's internal file system.
58+
59+
String theServer = assistNowOfflineServer; // Convert the AssistNow server to String
60+
61+
const int REQUEST_BUFFER_SIZE = 256;
62+
char theRequest[REQUEST_BUFFER_SIZE];
63+
64+
// Assemble the request
65+
// Note the slash at the beginning
66+
snprintf(theRequest, REQUEST_BUFFER_SIZE, "/%s%s%s%s%s%s%s%s",
67+
getQuery,
68+
tokenPrefix,
69+
myAssistNowToken,
70+
tokenSuffix,
71+
getGNSS,
72+
getFormat,
73+
getPeriod,
74+
getMgaResolution
75+
);
76+
77+
78+
String theRequestStr = theRequest; // Convert to String
79+
80+
Serial.print(F("getAssistNowOfflineData: HTTP GET is https://"));
81+
Serial.print(theServer);
82+
Serial.println(theRequestStr);
83+
84+
Serial.print(F("getAssistNowOfflineData: the AssistNow data will be stored in: "));
85+
Serial.println(theFilename);
86+
87+
// Reset HTTP profile 0
88+
mySARA.resetHTTPprofile(0);
89+
90+
// Set the server name
91+
mySARA.setHTTPserverName(0, theServer);
92+
93+
// Use HTTPS
94+
mySARA.setHTTPsecure(0, false); // Setting this to true causes the GET to fail. Maybe due to the default CMNG profile?
95+
96+
// Set a callback to process the HTTP command result
97+
mySARA.setHTTPCommandCallback(&processHTTPcommandResult);
98+
99+
httpResultSeen = false; // Clear the flag
100+
101+
// HTTP GET
102+
mySARA.sendHTTPGET(0, theRequestStr, theFilename);
103+
104+
// Wait for 20 seconds while calling mySARA.bufferedPoll() to see the HTTP result.
105+
Serial.print(F("getAssistNowOfflineData: Waiting up to 20 seconds for the HTTP Result"));
106+
int i = 0;
107+
while ((i < 20000) && (httpResultSeen == false))
108+
{
109+
mySARA.bufferedPoll(); // Keep processing data from the SARA so we can catch the HTTP command result
110+
i++;
111+
delay(1);
112+
if (i % 1000 == 0)
113+
Serial.print(F("."));
114+
}
115+
Serial.println();
116+
117+
if (httpResultSeen == false)
118+
{
119+
Serial.print(F("getAssistNowOfflineData: HTTP GET failed!"));
120+
return false;
121+
}
122+
123+
int fileSize;
124+
if (mySARA.getFileSize(theFilename, &fileSize) != SARA_R5_SUCCESS)
125+
{
126+
Serial.print(F("getAssistNowOfflineData: No file written?!"));
127+
return false;
128+
}
129+
130+
Serial.print(F("File size is: "));
131+
Serial.println(fileSize);
132+
133+
return true;
134+
}
135+
136+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
137+
138+
void prettyPrintString(String theString) // Pretty-print a String in HEX and ASCII format
139+
{
140+
int theLength = theString.length();
141+
142+
Serial.println();
143+
Serial.print(F("String length is "));
144+
Serial.print(theLength);
145+
Serial.print(F(" (0x"));
146+
Serial.print(theLength, HEX);
147+
Serial.println(F(")"));
148+
Serial.println();
149+
150+
for (int i = 0; i < theLength; i += 16)
151+
{
152+
if (i < 10000) Serial.print(F("0"));
153+
if (i < 1000) Serial.print(F("0"));
154+
if (i < 100) Serial.print(F("0"));
155+
if (i < 10) Serial.print(F("0"));
156+
Serial.print(i);
157+
158+
Serial.print(F(" 0x"));
159+
160+
if (i < 0x1000) Serial.print(F("0"));
161+
if (i < 0x100) Serial.print(F("0"));
162+
if (i < 0x10) Serial.print(F("0"));
163+
Serial.print(i, HEX);
164+
165+
Serial.print(F(" "));
166+
167+
int j;
168+
for (j = 0; ((i + j) < theLength) && (j < 16); j++)
169+
{
170+
if (theString[i + j] < 0x10) Serial.print(F("0"));
171+
Serial.print(theString[i + j], HEX);
172+
Serial.print(F(" "));
173+
}
174+
175+
if (((i + j) == theLength) && (j < 16))
176+
{
177+
for (int k = 0; k < (16 - (theLength % 16)); k++)
178+
{
179+
Serial.print(F(" "));
180+
}
181+
}
182+
183+
for (j = 0; ((i + j) < theLength) && (j < 16); j++)
184+
{
185+
if ((theString[i + j] >= 0x20) && (theString[i + j] <= 0x7E))
186+
Serial.write(theString[i + j]);
187+
else
188+
Serial.print(F("."));
189+
}
190+
191+
Serial.println();
192+
}
193+
194+
Serial.println();
195+
}

0 commit comments

Comments
 (0)