Models https://docs.djangoproject.com/en/3.0/topics/db/models/ fields and behavior of
For my birthday I was gifted an Arduino starter set. This present called for a maker project. For this I have created a kitchen clock with a build in e-ink display that shows the departure times of the next tram station. This allowed me to reduce the waiting times at the tram stop in the morning. This project, which included the following technologies
- Microcontroller boards Wemos Lolin D32 and Adafruit Feather nRF52
- Bluetooth Low Energy
- Waveshare e-Paper display
I want to showcase in this post.
Update July 2019: I switched to a Raspberry Pi Zero for the Central device. This is described in this post: ble-central-device-with-node-js/
The basic idea is to recreate the digital sign at the tram stops inside a regular kitchen clock.
I started out with a Wemos Lolin D32 development board that had wifi and bluetooth onboard. My first task was to use the board to get the departure times from the local transport company (MVG) and extract the relevant information.
After a suggestion from a colleague I setup the Lolin32 board to broadcast the departure information via Bluetooth low energy (BLE) to a second micro controller board. This is the Adafruit Feather nRF52 with BLE onboard. The Lolin32 pushes the departure information over BLE as soon as the information changes. This is important for the Feather, since the Feather as well as the e-Paper should consume as little energy as possible. With this push strategy it is assured that the Feather only wakes if the display needs updating. Feather and e-Paper are running on battery power.
Next I set up the e-Paper to display these information. The e-Paper is hooked up to the Feather. Setting up the e-Paper included some noteworthy tweaks. Namely I tuned the display to a fast refresh mode, which resolved the annoying flickering of the e-Paper default page refresh. And secondly I had to created a custom font to display the letters on the display in an appropriate size.
I included the e-Paper display into a round kitchen clock. This was maybe the easiest part, since the back of the clock is made from plastic.
Lolin32 and Public Transport
The first task was to connect the Lolin32 board to my local wifi network. As a programming environment I have used the offline ArduinoIDE. Connecting the Lolin32 to the network was quite easy using the samples provided with the ArduinoIDE, e.g. "WiFiClientSecure".
Next I used the API development tool Postman to play with the API of the local public transport company MVG. Unfortunatly I could not find an official documentation of the transport schedule API of the MVG, but I found the Github project mvg-api that was using it. From this project I could understand how to put together a http request for fetching the departure times I was interested in. Using the Postman tool to build the http request is a lot quicker compared to testing it on the Lolin32. After I constructed the query that returned the right data, i.e. departure information for the station that I am interested in, I inserted the request into the Arduino code. The http request looked something like this:
Converting from Unix Time to Date
The Lolin32 does not have a build in clock. And since I want to extract the duration in minutes for the departures I had to get the server time from the server response header and compare it to the Unix Time of the departures. For this I found a bit of code on the web that does the conversion from the date string in the server response to a DateTime type:
Bluetooth Low Energy Setup
The configuration of Bluetooth low energy (BLE) in this setup is as follows. The Lolin32 will be plugged into a power suply and call the MVG service on regular time intervals. The logic on the Lolin32 will determine if the minutes till a departure have changed and only then push the updated data to the Feather. In this setup the Lolin32 serves as a server and the Feather serves as a client.
The Lolin32 is acting as a BLE central device. This means that all connections are established by it. The Feather will act as a peripheral device and request a connection to the central. An entry point for more details on the BLE architecture can be found e.g. at the Nordic Forum.
Fast Refresh Mod for e-Paper
For showing the data on the clock I wanted a low energy display that could run for several months only on battery power. For this reason I decided on the e-Paper display from Waveshare.
The default refresh mode of the e-Paper, i.e. when a new frame is to be shown on the display, is turning all pixels white, then black, then white again. Only after this cycle the actual frame is displayed on the device. After digging around on the web I found that the hacker Ben Krasnow dug around the acual workings of the device and presents a driver mod on his blog. What a joy to have access to such niche information – hurray the internet!
After playing around with the information from Ben Krasnow I actually got the device to fast refresh.
Creating Custom Font for e-Paper
The e-Paper display comes with a limited set of font sizes. Unfortunatly I needed a bigger font to make the departure times clearly visible from across the room. This was a challenge, that again was solved by the infinite diversity of the www.
I found a tool with which it is possible to create custom fonts in the format needed by the e-Paper driver, i.e. a cpp header file. This tool is called lcd-image-converter. The program runs on Windows or can be compiled from source.
Data Send from Lolin32 to Feather
The data is pushed from the central Lolin32 to the Feather. The data is transported in a so called BLE characteristic. Unfortunatly the BLE spectification only allows 20 Bytes for a characteristic. I seems that in principle this could be extended to up to 512 Bytes, but I did not get it to work with more that 20 Bytes for the combination of Lolin32 and Feather. So I encoded the information I want to send into 20 Bytes like this:
Of course this needs to be decoded on the Feather:
Modifying the Wall Clock
I bought a minimalistic used wall clock off ebay to modify with the e-Paper. It was a pretty easy task since the back of the clock was made from plasic. It just took a demel rotary tool and some patience:
I connected the e-Paper display and a lipo battery to the Feather. After fully charging the battery and connecting the BLE link of Feather and Lolin32 everything was running smoothly. Unfortunatly the battery only lasted for a few days. So I dug a bit into optimizing the power consumption of the Feather. To check the improvements of any tuning I measured the current draw of the Feather with a multimeter.
It turned out that there is a simple way of drastically reducing the power consumption. I read a bunch of web posts which I do not recall, but it boils down to registering a callback function on the characteristic update event. The callback function takes the updated characteristic, decodes it and writes the departure times to the display. This event triggered operation ensures that the device only wakes if data needs to be processed and sleeps otherwise. This reduced the power consumption drastically and the battery should hold for a few months.
I have pushed the source code the my Github repo. Check it out here.
I know that this post is far from a complete or perfect, thus feel free to contact me about any questions you have – happy hacking!