Connecting a Smart Streetlamp to ThingSpeak with macchina.io

At the place where our office is located there’s also a “smart city” research project going on, including a cool autonomous shuttle and a couple of “smart” street lamps. These lamps contain environmental sensors, as well as other gadgets like a display and cameras. The idea was to get the sensor data – temperature, humidity, pressure, wind speed, particulate matter (pm2.5) and noise level – out of the lamp and to a cloud service for further processing. Should be quite easy to solve with macchina.io and a Raspberry Pi 2 Model B I had lying around.

Smart Streetlamp

The main issue was figuring out the communication protocol to obtain the sensor data. After receiving a protocol description (in Chinese) from the manufacturer of the streetlamp, and thanks to Google Translate, work could commence. The lamp uses a rather low-level non-standard communication protocol from the dark ages of industrial communication, delimiting frames with STX (0x02) and ETX (0x03) characters, and escaping special characters in between. There’s also a small header (message type and address, one byte each) and a checksum at the end, but no overall packet length. Now, no one would use a checksum in a TCP-based protocol nowadays. But the observation that there were occasional checksum errors made me think that there’s probably a serial (RS-485?) connection to a PLC or something similar somewhere along the way in the lamp and we are talking to a serial device server. Anyway, it took a good half an hour or so to figure out the protocol and receive the sensor data with a small Poco-based C++ program. Poco::MemoryInputStream and Poco::BinaryReader were very helpful.

Next step was bringing the sensor data to the macchina.io sensor and device services API. No big deal either, and after a few hours the protocol was properly implemented, and the data available via sensors services in macchina.io.

Smart Streetlamp sensor data in macchina.io SensorLog

Last step was to decide what to do with the sensor data. Of course the sensor data can be easily logged and visualized with the SensorLog sample application that comes with macchina.io. However, I also wanted to get the data off the Raspberry Pi and to a cloud service for further storage and processing. For that purpose, I chose ThingSpeak™, which is free (with some restrictions) and has an easy-to-use HTTP API. A few lines of JavaScript later, the data was available in a ThingSpeak channel, with some nice gauges and graphs for visualization. Data from the sensors is read every five seconds, and every minute the last 60 seconds’ maximum value for each sensor is transmitted to ThingSpeak. The whole project took about a day and made some people here quite happy, as they can finally do something interesting with the data from the lamp’s sensors.

Smart Streetlamp sensor data in ThingSpeak

There are already some ideas for further work. One interesting project would be counting the cars passing by processing images from the camera with OpenCV – some more serious edge computing than just aggregating sensor data. Also, the environmental sensor data contains occasional outliers (possibly from faulty sensor readings), which could be filtered on the Raspberry Pi before sending them to the cloud.

Update 2019-03-08: Filtering of out-of-range values is now done before computing the 60-second maximum.

Tagged , , , ,