Move snprintf to work with any architecture.

This commit is contained in:
Nathan Seidle
2021-12-12 21:46:36 -07:00
parent 1b3f033792
commit 356a43ded6
@@ -76,7 +76,7 @@ void setup()
Wire.begin(); //Start I2C Wire.begin(); //Start I2C
while(myGNSS.begin() == false) //Connect to the Ublox module using Wire port while (myGNSS.begin() == false) //Connect to the Ublox module using Wire port
{ {
Serial.println(F("u-blox GPS not detected at default I2C address. Please check wiring. Freezing.")); Serial.println(F("u-blox GPS not detected at default I2C address. Please check wiring. Freezing."));
delay(2000); delay(2000);
@@ -93,7 +93,8 @@ void setup()
Serial.print(F("Connecting to local WiFi")); Serial.print(F("Connecting to local WiFi"));
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED)
{
delay(500); delay(500);
Serial.print(F(".")); Serial.print(F("."));
} }
@@ -102,7 +103,8 @@ void setup()
Serial.print(F("WiFi connected with IP: ")); Serial.print(F("WiFi connected with IP: "));
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
while (Serial.available()) Serial.read(); while (Serial.available())
Serial.read();
} }
void loop() void loop()
@@ -110,7 +112,8 @@ void loop()
if (Serial.available()) if (Serial.available())
{ {
beginClient(); beginClient();
while (Serial.available()) Serial.read(); //Empty buffer of any newline chars while (Serial.available())
Serial.read(); //Empty buffer of any newline chars
} }
Serial.println(F("Press any key to start NTRIP Client.")); Serial.println(F("Press any key to start NTRIP Client."));
@@ -126,7 +129,8 @@ void beginClient()
Serial.println(F("Subscribing to Caster. Press key to stop")); Serial.println(F("Subscribing to Caster. Press key to stop"));
delay(10); //Wait for any serial to arrive delay(10); //Wait for any serial to arrive
while (Serial.available()) Serial.read(); //Flush while (Serial.available())
Serial.read(); //Flush
while (Serial.available() == 0) while (Serial.available() == 0)
{ {
@@ -181,13 +185,14 @@ void beginClient()
String strEncodedCredentials = b.encode(userCredentials); String strEncodedCredentials = b.encode(userCredentials);
char encodedCredentials[strEncodedCredentials.length() + 1]; char encodedCredentials[strEncodedCredentials.length() + 1];
strEncodedCredentials.toCharArray(encodedCredentials, sizeof(encodedCredentials)); //Convert String to char array strEncodedCredentials.toCharArray(encodedCredentials, sizeof(encodedCredentials)); //Convert String to char array
snprintf(credentials, sizeof(credentials), "Authorization: Basic %s\r\n", encodedCredentials);
#else #else
//Encode with nfriendly library //Encode with nfriendly library
int encodedLen = base64_enc_len(strlen(userCredentials)); int encodedLen = base64_enc_len(strlen(userCredentials));
char encodedCredentials[encodedLen]; //Create array large enough to house encoded data char encodedCredentials[encodedLen]; //Create array large enough to house encoded data
base64_encode(encodedCredentials, userCredentials, strlen(userCredentials)); //Note: Input array is consumed base64_encode(encodedCredentials, userCredentials, strlen(userCredentials)); //Note: Input array is consumed
#endif #endif
snprintf(credentials, sizeof(credentials), "Authorization: Basic %s\r\n", encodedCredentials);
} }
strncat(serverRequest, credentials, SERVER_BUFFER_SIZE); strncat(serverRequest, credentials, SERVER_BUFFER_SIZE);
strncat(serverRequest, "\r\n", SERVER_BUFFER_SIZE); strncat(serverRequest, "\r\n", SERVER_BUFFER_SIZE);
@@ -221,7 +226,8 @@ void beginClient()
int responseSpot = 0; int responseSpot = 0;
while (ntripClient.available()) while (ntripClient.available())
{ {
if (responseSpot == sizeof(response) - 1) break; if (responseSpot == sizeof(response) - 1)
break;
response[responseSpot++] = ntripClient.read(); response[responseSpot++] = ntripClient.read();
if (strstr(response, "200") > 0) //Look for '200 OK' if (strstr(response, "200") > 0) //Look for '200 OK'
@@ -263,7 +269,8 @@ void beginClient()
{ {
//Serial.write(ntripClient.read()); //Pipe to serial port is fine but beware, it's a lot of binary data //Serial.write(ntripClient.read()); //Pipe to serial port is fine but beware, it's a lot of binary data
rtcmData[rtcmCount++] = ntripClient.read(); rtcmData[rtcmCount++] = ntripClient.read();
if (rtcmCount == sizeof(rtcmData)) break; if (rtcmCount == sizeof(rtcmData))
break;
} }
if (rtcmCount > 0) if (rtcmCount > 0)
@@ -278,12 +285,7 @@ void beginClient()
} }
//Provide the caster with our current position as needed //Provide the caster with our current position as needed
if (ntripClient.connected() == true if (ntripClient.connected() == true && transmitLocation == true && (millis() - lastTransmittedGGA_ms) > timeBetweenGGAUpdate_ms && ggaSentenceComplete == true && ggaTransmitComplete == false)
&& transmitLocation == true
&& (millis() - lastTransmittedGGA_ms) > timeBetweenGGAUpdate_ms
&& ggaSentenceComplete == true
&& ggaTransmitComplete == false
)
{ {
Serial.print(F("Pushing GGA to server: ")); Serial.print(F("Pushing GGA to server: "));
Serial.println(ggaSentence); Serial.println(ggaSentence);
@@ -315,7 +317,8 @@ void beginClient()
int responseSpot = 0; int responseSpot = 0;
while (ntripClient.available()) while (ntripClient.available())
{ {
if (responseSpot == sizeof(response) - 1) break; if (responseSpot == sizeof(response) - 1)
break;
response[responseSpot++] = ntripClient.read(); response[responseSpot++] = ntripClient.read();
if (strstr(response, "200") > 0) //Look for '200 OK' if (strstr(response, "200") > 0) //Look for '200 OK'