A Teensy 3.1, an Xbee, and NTP to sync the RTC

Status
Not open for further replies.

xzeion

Member
Could there have been any more acronyms in the title? probably not.

On to the issue at hand. I would like to sync the teensy Real Time Clock with a Time Server. I am currently using an XBee S6B WiFi that is connected to my local network. (and thus the internet)

Another important detail is I am using the xBee over its serial interface, not its spi interface so that changes allot of the code. I am having trouble with finding a place to start with all of this. I think if I can retrofit the NTP example for the teensy to work with the xBee instead of an Ethernet adapter I would be in a good place but I am feeling a bit lost at the moment.

I have the teensy and xbee details to worry about and the network packet and NTP protocols to decode. Tis all a bit overwhelming for getting started. Can anyone help me sift through all of this?

The end goal is to sync the teensy 3.1 RTC to either network time or internet time over WiFi.

Thanks Guys!

PS: I will keep this post up to date with any progress i have made and when I come to a solution I will also post that here
 
I am thinking a place to start might be figuring out how to get a time stamp over the xbee. Once I can figure out how to get one in I can then apply it to the RTC
 
The mating xbee would have to connect by serial usb to a computer with internet access in order to use NTP
 
I already have an xBee talking directly to a TCP socket (probably should have mentioned that) and I can send and receive from that. Is there a way to get the time stamp that way from a Linux server? Keep in mind these xBees are the Wifi versions.
 
A quick review

Teensy ==> xBee WIFI ==> TCP port ==> (request time) ==> through xBee WIFI using serial port ==>teensy time set function


How do i do the time request bit ? Is there a better way to get the time on the teensy?
 
If your teensy can receive gps signals, get a gps card and hook it up (for example, this is the cheapest buy-it-now gps that I saw on ebay looking for arduino + gps: http://www.ebay.com/itm/1PC-Ublox-N...539?pt=LH_DefaultDomain_0&hash=item2a3ea27c8b). The gps card I mention looks like it wants a serial connection (i.e. it has RX and TX). You would use one of the 3 Teensy serial ports to hook it up, and read from it. Part of the GPS spec is accurate time information.

I would think you would want to solder in a 32.768kHz crystal so that the Teensy's real time clock will generally keep the time, and hook up a coin battery to keep the RTC powered when the Teensy 3.x is turned off. Then you would only need to sync every so often.

If you need accurate time and you aren't always in wi-fi or gps radio range, you might want to consider getting a Chronodot i2c real time clock (https://www.adafruit.com/products/255), that keeps more accurate time that most of the real time clocks, due to the fact it has a temperature sensor to calibrate the time keeping.

You also might want to browse this library to see how set the RTC if you haven't already seen it: https://www.pjrc.com/teensy/td_libs_Time.html
 
Is there a better way to get the time on the teensy?

May I suggest a GPS receiver? Adafruit, Sparkfun, etc. have them for around $30 and you get everything you need, down to the atomic clock. The only downside is reception, which works in most wooden buildings (worked in mine down 2 floors and a roof).

I'd also consider a wifi or Ethernet module (see wiznet unit sold on this site) and have the teensy talk directly to the NTP server.

Your current approach seems a bit complicated if keeping accurate time is the object...

Last but not least, build a good, temperature-compensated RTC and lose so little time (2ppm or less with a DS3231, for example) that staying connected is irrelevant.
 
Michael, you're amazing. We have the same thoughts and are typing them at the same time. I agree that the RTC on the teensy is another great option (just like the Chronodot, which is based on the DS3231), especially if you compensate for temperature and have characterized the PPM clock error as a function of temperature.
 
Thank you all for your suggestions but I am using the xBee WIFI to talk over a wifi network. Consider it a wireless N adapter for the teensy that uses a serial port. I just need to figure out how to make a TCP packet that can request the time and return it to the requesting client.

So in short. How do I ask my computer for the time via a TCP packet?

I already have a crystal and battery setup with my teensy
 
Is there not a NTP example in the library already. It might take some changing around. There is also the processing sketch that sends the computers time to the teensy via serial. So maybe you can change that around to work with NTP. I assume you want the teensy to some how ask for the time from linux and do this via wifi. I am not sure how you would do that with linx, that is way beyond me.
 
The time library I mentioned (https://www.pjrc.com/teensy/td_libs_Time.html#ntp) claims to have an example that works with ethernet. Wifi is just another networking standard, and if you have a full wifi connection, you should be able to use the same code, perhaps with a few tweaks on opening the connection.

If those are still going over your head, you are going to have to knuckle down and learn several complex tasks all at the same time or perhaps use an alternate approach like using gps.

I would think the big problem is just learning how to do network programming. It may make sense to write a program that does what ntpdate does on Linux, and when you understand how to open up a socket, look up hostnames via DNS, format UDP packets, talk to a time server, etc. Do this in Linux or Windows, where you don't have to worry about all of the limitations imposed by using a Teensy. Don't try to set the time, just print the time. I would suggest NOT just blindly copying the ntpdate code, since the whole point is that you learn the basics of doing a network request.

Then once you've connected to a time server, and can print it out, do the same thing on the Teensy using your setup. At first don't worry about setting the RTC, just print the time on the USB monitor or a 16x2/seven segment display.

After you've made the network connection, then you need to do the final step of updating your clock to use the ntpdate time, and code in delays so that every so often, you ask for a time. It is considered bad manners to poll time servers too frequently, so you want to set the time, and then every so often (hours or minutes) ask for it again to adjust your clock. It is also bad manners to use a fixed IP address for the time server(s), you should query DNS for the host name to allow hosts to start and stop being official time servers.
 
Last edited:
you said
Teensy ==> xBee WIFI

that suggests that you have a Digi XBee WiFi. I misspoke above thinking it was an 802.15.4 XBee.
The simplest way to get time from the Internet is not NTP - that protocol is rather complex. But there are lots of C code examples you can use if the XBee WiFi has no built-in time client.

SIMPLER

A simpler alternative I've used is the daytime protocol (a NIST standard).
On this web page, scroll down to the DayTime section
http://www.nist.gov/pml/div688/grp40/its.cfm

You just connect to one of their servers and the defined port number and you get back ASCII text easy to parse. That's all.
Note that if during testing, you connect too often, that server will ignore you for a while. So I've used a list of servers and a random choice.
 
Thank you all for some excellent guidance. I will give some of this a go in python first and see if I can get things rolling. I may have to rewire my xBee WiFi to use SPI mode (which is much more complicated) instead of connecting via serial. Reason being I am unsure if it uses proper packets in serial mode. So I have several complex interfaces to work through. Lets go eat an elephant. One "byte" at a time...
 
I use this RN-XV WiFly module with an XBee footprint:

https://www.sparkfun.com/products/10822

When in command mode, setting the time via a UDP time server request is as simple as:

Code:
time<return>

and getting the time is just:

Code:
show t<return>

This can be done either by serial interface or telnet.

Here's a snapshot from the user's guide:

time.png
 
Status
Not open for further replies.
Back
Top