Friday, 28 October 2016

Wifi temperature sensor

I have need for some temperature sensors where I'm too lazy install cables or the need is only for a short while. I also wanted to try micropython on ESP8266, so why not combine the two.

Installing micropython on ESP8266 is pretty simple, but somewhat time consuming the first time. Micropythons ESP8266 has good build instructions. Same goes goes for the needed esp open sdk. Building the esp-open-sdk takes pretty long time, but you don't have to update it that often.

What I want to do is have the eps8266 measure temperatures once a minute and then send the values with MQTT. Because I'm using DS18b20 sensors and they have a unique address I can just add that to the MQTT topic, something like /house/temperatures/1wire/285cb4ce0100009a and then have time and value in the message.

Micropython has a separate library repository that has MQTT library, but trying to just import that on ESP8266 gives an error and it seems that error is because there is not enough ram to compile to source, so the only way to use it is to precompile it by copying it to modules directory before building the firmware. That way it gets precompiled and you can use just like any other python module.

I'm thinking of sending the time in the message along with the value, for that I need to keep somewhat accurate time on the ESP8266.  I wrote a little script to see how accurate the rtc was, and it drifted about ten minutes in five hours, so I definitely should check the time every time I wake up from deep sleep. Script can be found here https://gist.github.com/mika-koivusaari/90febd8a101412bf0e5c71d9c736e855

Fritzing drawing of the connections. There isn't much stuff so I just soldered it all on the board. Sensors, power and battery monitor leads are of course with connectors.  I actually used a 18650 cell but didn't have a Fritzing part for that. I also used a dc - dc converter instead of a linear regulator.


Finished product, held together with heat shrink tube and hot glue:)




Code is pretty simple.

  • Wake up
  • Check if stop pin is down
  • Make measuments (only temperature and battery voltage at this time)
  • Wait for wifi connection
  • Connect to MQTT broker
  • Send values
  • Go to deep sleep

Code and Fritzing files can be found in https://github.com/mika-koivusaari/remote_sensor.

There's probably some bugs still that I have to sort out. But for future I might add some different sensors, for example humidity. Only send measured data every x minutes and store it between sends, to conserve battery. Maybe think some more about MQTT topics so they would be logical and extendable. Some more configuration options, and a local ntp server.

Next I should probably code a MySql - MQTT gateway so I can store those readings:)

No comments:

Post a Comment