The ROS Garden - Part 2

The eyes and ears

At the core of any control system is the state vector that contains all the variables of the observed system. The system observations (the "observed" state) are typically done using a variety of sensors, and then filtered to approximate the "true" state of the system. The main variables in our simple system will be temperature and humidity, two variables for which an enormous variety of sensors exist on the market.

At the low end of the spectrum, the DHT11 and DHT12 sensors are an interesting pair, but don't come in a very interesting package, requiring a certain level of mechanical integration to be properly mounted.

Make a casing, some sort of mount, add a wire, and this'll do for about 1$


After a bit of research, I've settled for the AM2315 package, which while a bit more pricey, comes in a much more suitable package to monitor a closed space, and as a bonus, comes with two mounting brackets that really struck my inner lazy spirit.
Or for 30$ you can get this more accurate baby with zero messing around.

Writing the core driver

The AM2315 is a classic chinese component: the datasheet is in broken english, and filled with errors that will leave the integrator pulling his hair before descending into a dark spiral of despair.

First of all, figuring out the wiring was ... interesting ... at best:

Well, yes. Of course. 
Hopefully, the problem of becoming seemingly proficient in something without putting in actual work has been long solved by Google, in this case the Google Lens application being able to translate in real time:

Ni hao, motherf*cker.


Second, when I say the datasheet is wrong, I do mean it in a very specific tone: the datasheet is WRONG. More specifically, the 0xB8 I2C address is WRONG. A simple scan of the I2C bus with i2c-tools will yield an address of ... nothing.  Before you scurry away to double check the wiring and start wondering if Google misled you all those years, reading the datasheet will provide a crucial clue: the device uses a sleep mode in order to save power and avoid being heated up by stand-by current. Doing two quick scans in succession will ensure that the second scan hits the device while woke, and yield an real address of 0x5C. Waiting a second more and doing a third one will confirm that it's back to sleep. Well, thank you for that.

It's not dead, it's pinin' for the fjords...at a different I2C address.

Now that we have the device's true address, and that we know that it needs a swift kick in the ass before actually answering, the rest of the code is relatively easy. Thanks to the Linux kernel's I2C support, if the driver was unable to send a byte to a device because it was asleep, the write() system call will return 0 bytes written. This can be used in a repeated loop to know when the device is awake and ready to receive our real command to read the temperature and humidity. From this point on, the datasheet is fairly reliable. You can see the whole code for the AM2315 module on GitHub .

ROS-ing it up

ROS operates on the principle of nodes and topics. The data model is of a publisher-subscriber mechanism, which means that nodes can publish data to a topic, which makes it available to all the other nodes which subscribed to it. Here, we have created a simple topic called "Environment" which receives custom messages containing timestamped temperature and humidity information from out sensor node, along with a zone identifier to identify which monitoring zone the data corresponds to. At the other end of the data stream, a simple text logger takes the data and writes it to a text file called log.txt.


Houston, we have a data stream


Leaving it to run a few days, we can appreciate the stability and ease of use of a Raspberry Pi platform. Note that the file logging paradigm is not appropriate for long term use, since it will wear down the Pi's SD card over time. It's fine to test things in the short term though. Plotting our log in matlab, we have temperature and humidity over the course of a few days:

The greenhouse effect, in numbers

Interestingly (or not), we can readily see the greenhouse's effect on ambient temperature and humidity, accumulating heat from the sunlight during the day while driving down humidity.





Comments

Popular Posts