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.
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.
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.