How I Put a Temperature Sensor on the NDN Testbed
The frontpage of my recently renovated website shows a room temperature reading of my apartment. This is not your average IoT project, but a showcase of how an IoT device can communicate directly over Named Data Networking (NDN). This article explains where does this temperature reading come from, how is it turned into NDN Data packets, how the sensor appears on the NDN testbed, and how the temperature reading shows up on the website.
If you have been reading my posts, you may have guessed that the temperature reading comes from an ESP8266. Of course! It is actually my very first ESP8266, which I received from Losant IoT Inc. It comes with a TMP36 analog temperature sensor, and has been reporting my room temperature to Losant platform since May 2016.
Soon after I setup the sensor, I had a new idea: I should make the sensor dual-stack, reporting temperature to both Losant and NDN! To make this happen, the ESP8266 must speak NDN protocol. There isn't an NDN library for the ESP8266, but ndn-cpp-lite, a lightweight NDN client library offered by UCLA REMAP, supports the Arduino Yun. While their Arduino Yun example is not pretty because it is not a proper Arduino library but requires on a non-standard compilation procedure, it illustrates the key points of porting ndn-cpp-lite to Arduino IDE: keep the C and C++ files you need, and delete the others. Following the example, I packaged ndn-cpp-lite into esp8266ndn, an Arduino library for the ESP8266 microcontroller to speak NDN protocol. I also added a pair of ndnping client and server into the esp8266ndn library, which I once wore as a jewelry.
Having the esp8266ndn library, the ESP8266 can then create NDN Data packets with temperature readings. To minimize programming, I decide to integrate temperature readings into the ndnping server: Data produced by ndnping server carries a temperature reading and some other information in the payload. Links to this and other source code are at the end of this article.
The producer code can respond to ndnping normally:
At the same time, the Data payload includes temperature, WiFi SSID, local IP address, and device uptime:
Since the sensor is dual-stack, it was also necessary to change the Losant side to use an asynchronous MQTT library, so that the sensor can simultaneously send updates to Losant's MQTT broker and respond to NDN Interests.
I implemented a "face" in the esp8266ndn library that connects the NDN testbed via UDP tunnel. This is the easy part.
The hard part is how to register a prefix on the NDN testbed. The standard method of obtaining a "back route" (a route from the testbed to an end host) is through NFD's auto prefix propagation, but this isn't feasible for the ESP8266 because ESP8266 doesn't have the horsepower to run NFD. But I have a trick: sign prefix registration commands on a server, download a signed command to the ESP8266, and send it to the testbed router. With this trick, the ESP8266 shows up on the NDN testbed:
The final step is to retrieve temperature readings from the NDN testbed and display it on the website. Using the NDN-JS library, the script sends an Interest to retrieve a Data freshly produced by the sensor, extracts the temperature field, and displays it on the frontpage. Links to this and other source code are at the end of this article.
This article describes the inner workings of the temperature reading feature on yoursunny.com frontpage. An ESP8266 in my apartment reports the temperature to Losant platform and at the same time registers itself as a producer on the NDN testbed. The website uses NDN-JS to retrieve the temperature reading directly from the ESP8266 sensor device, which doubles as a ndnping server and includes the temperature reading the payload of NDN Data packets.