Feature Request: get current timestamp over cloud

Hi @timb,

We have a task to add a time library, which I think would either grab the time from the server, or easily wrap NTP requests to a user-specified server. I don’t think we’ve had a chance to work on it yet, so it’s still just in the backlog. I think having some code to make syncing the clock easier would be awesome.

@Coffee Thanks for sharing this! @BDub, I actually had to comment line number #89!

tzset();

Thanks for the help guys!

Okay, cool. I’ll play around with it a bit once I get these I2C changes done. (Which, by the way, is going great! I’m looking into using DMA for I2C now, to increase the stability.)

1 Like

Wow you guys are fast with testing. :blush: I had the solution ready, but forget to push it last night :stuck_out_tongue:

The Problem was, that tzset() used 60KB of memory. So the total of 110k was too much for the core. It was a dirty notworking hack anyway.

The new version gets the unixTimestamp from the server, instead of parsing it offline.
https://github.com/synox/spark-core-examples/blob/master/http_timeapi.cpp

I added the example for parsing a date here:
https://github.com/synox/spark-core-examples/blob/master/parse_datestring.cpp?000

1 Like

Hi, did you have any success using DMA yet? I’d like to use DMA (and an IRQ routine to handle the Transfer Complete Interrupt) for SPI. So if you already got something working for I2C, that would be a great starting point.

@luz Yeah, there should be some specific registers for DMA SPI transfer you need to use. Let me go through my notes and pull out the relevant info for you. I’ll also share my I2C code so you can get an idea of the steps you’ll need to take. :smile:

Thanks! The reference manual for the STM32F10xxx is pretty clear about the channel setup procedure, however the things I don’t know is how to actually code that in the spark environment. I have no idea what headers the web IDE includes, and which symbols I could access to get to those registers. Same for the TCI IRQ setup - I have no pointers yet about how to install it from the web IDE C environment.

Of course, seeing a piece of code setting up a DMA channel (any channel, I can figure out SPI details then) would be awesome!

BTW: do you know which DMA channels are in use by the spark core firmware itself?

Hi @Coffee were you able to run the code? I try to push it over but for me is not working. I created a Spark variable in order to read it with a Get request but I’m getting 0.

#include "application.h"

// ------------- HTTP functions --------------

/**

  • make http request and return body
    /
    TCPClient client;
    char buffer[512];
    String http_get(char const
    hostname, String path) {

     if (client.connect(hostname, 80)) {
             client.print("GET ");
             client.print(path);
             client.print(" HTTP/1.0\n");
             client.print("HOST: ");
             client.println(hostname);
             client.print("\n");
             //        client.print("Connection: close\n\n");
             client.flush();
     } else {
    

// Serial.println(“connection failed”);
client.stop();
return NULL;
}

    // Block until first byte is read.
    client.read();
    for (unsigned int i = 0; i < sizeof(buffer) && client.available(); i++) {
            char c = client.read();
            if (c == -1) {
                    break;
            }
            buffer[i] = c;
    }
    client.stop();

    String response(buffer);
    int bodyPos = response.indexOf("\r\n\r\n");
    if (bodyPos == -1) {

// Serial.println(“can not find http reponse body”);
return NULL;
}
return response.substring(bodyPos + 4);
}

// ------------- DATE / TIME functions --------------

/**

  • returns current time since epoche, from a http server.
    */
    long currentUnixTimestamp() {
    // Serial.println(“getting current time”);

     String response = http_get("www.timeapi.org", "/utc/now?\\s");
     if (response != NULL) {
    

// Serial.print(“timeapi time=”);
// Serial.println(response);
return atoi(response.c_str());
} else {
return 0;
}
}

/*timeapi example :
*
GET /utc/now?\s HTTP/1.0
Host: www.timeapi.org
Connection: close

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 11 Jan 2014 11:59:37 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 25
Connection: keep-alive
X-Frame-Options: sameorigin
X-Xss-Protection: 1; mode=block

1389485095
*/

int currenttime;
long timestamp;
void setup() {
// Serial.begin(9600);
Spark.variable(“ctime”,&timestamp,INT);
}

unsigned int nextTime = 0; // next time to contact the server
void loop() {
if (nextTime > millis()) {
return;
}

    timestamp = currentUnixTimestamp();

// Serial.print("time: ");
// Serial.println(timestamp);
currenttime=timestamp;

    nextTime = millis() + 10000;

}

Also, did you compile the time.h library into the core?

Thanks man!

there is an issue with http, see here: https://community.spark.io/t/tcpclient-available-does-not-return-length/1169/8

there is an example where i use time.h

You are amazing @Coffee , great work on it! I will try this ASAP :slight_smile:

1 Like

I have posted a new time library for :spark: over here:

https://community.spark.io/t/real-time-clock-library-for-spark/2925

You can get a UTC (Zulu) time and date stamp in ISO format as an Arduino String like this:

“2014-02-17T17:14:09Z”

You can get an ISO format time and date stamp with timezone and daylight savings too, but the UTC version is likely what you really want.

3 Likes

This is awesome @bko. I was getting current time over GET from my server using Node.js…this might be a little more accurate :slight_smile: