## Saturday, January 16, 2016

### How Hot Is The Lounge?

“When you cannot express it in numbers, your knowledge is of a meager and unsatisfactory kind”
― Lord Kelvin

Approaching Christmas it was hot, enough to keep checking the lounge room temperature obsessively. To keep the house cool without air conditioning the windows are opened at night then closed during the heat. Recently it was an extreme 37° outside while inside was a more livable 27°.
 These digital thermometers are great but they only tell you the temperature right now.
A couple of digital thermometers at less than A$2 per piece are used to know when it's hotter out than in and then I have to to keep observing so as to close or open the windows appropriately. Obsession though, requires better than spot observations. How cool does it stay inside compared to out, how often is it uncomfortably hot, how cool does it need to be at night to cool the house sufficiently and so on? The house has a lot of thermal mass so how much less effective is this technique when there is successive hot days? Answering these questions requires more data. No problem, I'll get a sensor that records observations and to make analysis easy it should publish the data online.  This sensor from Wireless Loggers looks good and runs on a coin battery but is a hefty A$ 77 per unit.
 These sensors also need a wireless ethernet base station which adds complexity and another A$164 in expense. The closest consumer product I could find was from Wireless Loggers . This was a more expensive solution than I'd anticipated and the data was online but only accessible through a phone app. Sometimes things don't advance as fast as you'd expect. I was putting sensors inside cow's stomachs back in 2004 and watching temperatures remotely as they walked around eating and drinking using similar bespoke technology. Back then we were expecting this would be point and click easy and cost only a few dollars once sensor networks were commercialised, yet 12 years later it still looks difficult. A pencil and paper was looking attractive but I'd also bought a convenient to use NodeMCU version of the esp8266 12-E microcontroller with integrated WiFi as I was reading about them everywhere as being functional and cheap, this one from Ali Express at just US$4.15 delivered.
 The esp8266 12-E modules are tiny, cheap, lots of I/O and reasonably powerful.
 Development is a lot more convenient when the esp8266 12-E is deployed in this NodeMCU format. It can be programmed directly from a USB port on a PC, powered from a USB charger during use and provides easy access to all I/O pins when plugged into a prototyping breadboard.
Could I use this newfangled esp8266 to satisfy my curiosity?

After Christmas I got to spend a few days with Chris, a smart guy I've known since high school. He is living the dream, with the Hawkesbury River 3 metres from the door. It would be hard to get bored with that, but just in case he has a spare house at Chittaway Point, again with a wharf in the back yard. Between the crabbing, bush walking and watching boats pass from the balcony we had a go at learning how to use the esp8266.

The esp8266 first became available as an AT command driven serial modem but now several alternative development environments are available of which  LUA seems the most popular. We opted for Arduino which needs to be configured to program the esp8266 and the easiest way to get started is to follow the "Installing with Boards Manager" instructions. With Arduino, the standard method of testing if everything is set up correctly  is to compile and upload the code to blink a led on the target device. We found an esp8266 version of blink and in a few minutes were watching a blinking led. Trivial perhaps, but satisfying. A lot later there was simulated data periodically being uploaded to an online service.

We'd made more progress than I expected and I hadn't sourced a temperature sensor, so that had to wait until I returned home. A DS18B20 temperature sensor looks ideal at 5 units for A$3.88 and they are also available with a metre long waterproof cable for A$2.22. The DS18B20 reads temperature resolved to 9 bits by default and is configurable to 12 bits with an absolute accuracy of +/- 0.5 degrees. It needs only power, earth and a connection to a single GPIO pin. Multiple sensors can also be connected to the same pin. The DS18B20 documentation says a 4.7K ohm pull up resistor is required between the power and data pins and other implementations specify this also but the internal pull up in the ESP8266 12-E which is reported to range between 30K - 100K ohms has proved sufficient for me with two sensors connected physically close to the esp8266 12-E module. Later I came across a pedant aiming to Save The Earth One Resistor At A Time who analyses why a resistor is rarely required.
 Connecting the DS18B20 temperature is this simple. Most documentation says a 4.7K pull up resistor is required between the data and power lines but the internal pull up resistor is good enough.
Using the sensor requires the Arduino temperature control library, which in turn requires the One Wire library for communication. It is possible to have installed a One Wire library previously so there might be two. If so the version that was installed with the other esp8266 libraries must be used.

We want to capture rapid temperature changes but minimise data streamed, so the algorithm I settled on is:-

• read the temperature every 5 seconds,
• if it hasn't changed much go back to sleep,
• otherwise log measured temperature to a web service.
If no data has been logged for a while usually the temperature hasn't changed but if I don't see new data I worry the logger has failed, so to provide reassurance, log at least every10 minutes.

After implementing this, when I have the sensor running, we can see live data:

or we can view it in a dashboard. Feel free to try this yourself, using our code. Before compiling, config-sample.h needs to be renamed to config.h and all the "TODO" fields replaced with appropriate values. For example, you must enter your WiFi access point name and password.

The problem was solved for only a few dollars if you put zero value on your time but this was as much a learning experience as a solution and I've seen enough for it to be already apparent why the esp8266 has generated so much interest. A couple of bucks for a tiny integrated microcontroller, with lots of I/O and inbuilt communications is what is required for the oft spoken of sensor networks to emerge on a scale proponents have envisaged. CSIRO spent many millions trying to develop something similar, marketed as a Fleck and now relegated to the sensor networks museum.  The esp8266 from Espressif Systems (Shanghai) is an order of magnitude more capable. That capability has attracted a buzz, will surely attract competitors and Espressif are close to releasing an even more capable esp32 that can additionally communicate over Bluetooth Low Energy.

I now know how hot the lounge has been though this is not yet a quality solution and there are others including a Lua version by Eyal Lebedinsky from our local hacker space. Ideally you want a device to arrive in the mail, fit a battery, look at a web browser and see the temperature. Getting a device that works out of the box and also comes with enough information to make tinkering easy would be nice. If time has value, the Wireless Loggers would have been much cheaper this time around but properly developed, the esp8266 could be at the core of a far superior solution and surely someone will do a commercial esp8266 based temperature logger soon.