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:)

Sunday 23 October 2016

Trying C# and Visual Studio

I have never really worked with Microsofts development tools so I wanted to try. Visual Studio has a free community version that suits this purpose.

After a bit of thinking I thought I could make a RPN calculator, a simple program but I can still get some complexity there for fun.

Some points I want to try:

  • Operators as classes, just some basic oo-programming
  • Basic operators built-in, some operators loaded dynamically from dll
  • Unit testing
  • Refactoring
Dynamic loading of classes seems pretty simple, you just get the reference to Assembly (e.g. exe or dll) and then just look filter those classes that you are interested. getOperatorsFromAssembly function in Program.cs does the heavy lifting and is called from initOperators. Currently the only dll name is hardcoded, and a better solution would be to put all dll's in some directory and just load all of them, but I most likely won't do that.

Unit testing, Visual Studio can create test class stubs for a specific class, which is nice. Running tests is simple.

Basic refactoring, renaming and moving blocks of code to functions is straightforward.

Results can be found in github https://github.com/mika-koivusaari/rpn_calculator.