Skip to content

One thing leads to another: Time for the pi, GPS, ntp, ESP8266, microPython, Lua

The pi boots without any calendar time reference. If it comes up on the I’net, it uses ntp to set the clock but offline it has no reference. Things work pretty well by just ignoring the clock and I could just set it manually if need be. The other options would be to use a clock chip connected via I2C or, for just a bit more in cost, a GPS dongle. The GPS would need a view of the sky, though. Still. GPS is more accurate and provides other possibilities. And then there’s the GPS modules intended for drone use that have pressure sensors to measure altitude, temperature sensors to calibrate the pressure sensor, accelerometers, magnetometers, and gyroscopes for measuring attitude and motion. That gets even more delicious on options and possibilities.

There’s lot of stuff out there about using a GPS on a pi for a stratum 1 time server but the idea of getting a better sky view and using the sensors for environmental (e.g. weather station basis) sensing brings up the idea of a solar powered external doohicky on a wifi network to provide time and weather data using standard network protocols. That leads to the ESP8266 and its successor the ESP3212. Even though that wifi radio microcontroller has been out only a couple of years, it has a vigorous community, a lot of support, and even ntp, GPS, and weather station examples to serve as a starting point.

Since the successor isn’t readily available yet, initial development would be based on the ESP8266.  There are inexpensive boards for that with power regulators, USB access to the serial port, and other development conveniences. NodeMCU (documentation) seems to have provided a reference design that many others use. A version of Lua is provided with those boards to provide interactive script development needing only a terminal. Another option is to use the Arduino IDE with the ESP8266 add-ons and then tap into that massive ecosystem. Adafruit has a good rundown on how to do this with their HUZZAH version of the NodeMCU board. The Arduino process is, basically, compiled C. Another option is microPython which is a rather recent effort that provides a modern interpreted environment.

One factor that pops up in looking at these things is memory. The ESP8266 has about 50k for user data out of 96kB on chip data RAM and 64 kB instruction RAM (wikipedia). In comparison, the TRS80 Model 1 in 1980 (wikipedia) used up to 16k of instruction ROM and 48k for data RAM. The TRS80 used tape or, later, floppy for 100k of external storage. The ESP8266 usually has a 4 MB flash memory chip accessed by SPI and its clock speed is about 40 times that of the early personal computer but costs more than 100 times less. 

Bill Gates got a usable interpreter for those early PC’s into 4 KB and BASIC is still an option on the modern microcontrollers as Geoff’s Mighty Mite project illustrates. That modern firmware for a chip has a lot more memory to work with at much faster speeds so it’s more like a 1990’s BASIC than the 1980 TRS80 version. The Picaxe BASIC fits in here, too, as it is designed to run on older low end microcontrollers not that far from the TRS80 capabilities.

I’ll probably experiment with Lua some as it comes preloaded on the NodeMCU board I ordered. It appears to have some good features but the memory restrictions seem to cause some problems, it uses floating point numbers for everything unless you do a custom compile, and doesn’t have quite the breadth of community for support and portability that other options have. microPython also has some memory issues but it has the community, support, and portability benefits. The Arduino IDE with its C basis and compiled code takes care of the memory issues and also provides the support and community. So it looks like I’ll focus on microPython to see what the chip can do and how it works and the Arduino IDE for building final code. Both should have good transfer facility to the newer ESP3212.

Start with something simple – set the clock on a Raspberry Pi – and look where it leads! How does GPS work and how can it be used as a time standard? What is the ntp protocol and how is it implemented and what sorts of issues does it address? What about time resolution, calendars, epochs, accuracy, latency, and other such issues? What are the issues in using the pi for GPS based time service versus using a dedicated microcontroller with a standard wifi link using established protocols? What is the best development environment and firmware protocol for the microcontroller? — it’s not so simple anymore.