Feature Request: get current timestamp over cloud

I understand now. :clock230:

I can give you the logic to do that, I’ve even got most of it written! I’ll post it this afternoon. :slight_smile:

3 Likes

Hey everyone,

Thanks for the input. Just want you all to know that at least some eyes here at Spark saw this request and know how helpful it would be to streamline this for everyone. I’ve added it to my community feedback notes and will make sure it gets brought up during our next Sprint Planning meeting.

3 Likes

Until the feature is ready, i use timeapi.org

here is my example code: https://github.com/synox/spark-core-examples/blob/master/http_timeapi.cpp

1 Like

@Coffee Hey there man, does that code actually works for you? I’ve tried flashing it (copy/paste) no modifications at all and it doesn’t flash it, although it says it does… The spark core just doesn’t receives it, doesn’t restart or anything at all, if I however change line #27 (“long timestamp = currentTimeMilis();”) to “long timestamp = 0;” it does get flashed. Weird.

Seems like it’s a silent failure with the compiler!

I traced it down to commenting out line 88:

setenv("TZ", "UTC", 1);

But I have no idea why it’s failing to compile.

Lol line 90:

// sy/11.Jan.2014: Not sure if setenv works at all...

EDIT: I also had to change line 115 to:
String response = "";//http_get("www.timeapi.org", "/utc/now");
To even get it to the parseDate() function before having other issues with setenv();

So I’m not sure what’s going on in there either…

Popping in to say I added this to the compiler backlog, thanks!
Edit: by that I mean figuring out why “setenv” caused the code to not compile :smile:

1 Like

Nice work @Coffee! I was using TimeAPI.org in my netTime Library as well, but I just haven’t had time to work on it. I started working on it, then decided I wanted to add some LCD support which had me debugging I2C issues, which in turn got me working on JTAG stuff.

@Dave Is anyone on the team working on adding support for the RTC? I’ve been adding some notes about it as I go through the datasheets.

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:

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