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!


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.

I added the example for parsing a date here:

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(" HTTP/1.0\n");
             client.print("HOST: ");
             //        client.print("Connection: close\n\n");
     } else {

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

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

    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


int currenttime;
long timestamp;
void setup() {
// Serial.begin(9600);

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

    timestamp = currentUnixTimestamp();

// Serial.print("time: ");
// Serial.println(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:


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


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.


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:

This post was flagged by the community and is temporarily hidden.