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