3Gbee time sync

I’m trying to convert the old Mbili code for syncing the RTC with the time server. So far I have changed
bool retval = gprsbee.doHTTPGET(APN, APN_USERNAME, APN_PASSWORD, TIME_URL, buffer, sizeof(buffer));
bool retval = sodaq_3gbee.httpRequest(TIME_URL, 80, “/”, GET, buffer, sizeof(buffer));

and it is connecting and retrieving the time signal. But the buffer now looks like this and I’m not sure of the best way to strip out the time code (or if I just have the httpget command slightly wrong)…

HTTP/1.1 200 OK
Date: Wed, 27 Jul 2016 00:50:18 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 11
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain


Any suggestions much appreciated.

The HTTP protocol requires that the HEADER and the BODY be separated by a blank line.

The BODY is any content after the first blank line.

I should also add that according to the specification of HTTP the line endings are defined as CRLF ("\r\n"). So the BODY starts after the first instance of “\r\n\r\n”.

thanks Gabriel, I can isolate the time as follows:

char *ptr = buffer;      
ptr = strstr(buffer, "\r\n\r\n");
ptr += 4;
SerialUSB.print("ptr: ");

but I’m not sure how to convert this as we did before

uint32_t newTs = strtoul(buffer, &ptr, 0);

Sorry my command of data types is decidedly shaky!

The strstr() method will return a NULL pointer if the substring was not found. For safety reasons you should check for that.

The first parameter of strtoul() takes the pointer to the char* buffer from where to start the conversion, I believe in ignores any leading whitespace. It is easier to start at ptr as you would have to check if ptr+4 is valid in order to use that location. The second parameter is the pointer to the end of the conversion point, here we can pass NULL. The final parameter is the number base, 0 is effectively auto.

char *ptr = strstr(buffer, "\r\n\r\n");
uint32_t newTs = 0;
if (ptr != NULL) {
    newTs = strtoul(ptr, NULL, 0);

fantastic, all working now - you make it look easy!