Thursday, 16 June 2016

Calibrating DS18B20 sensors to show the same

I have a weather station in our summer house. It currently has a radiation shield made from plastic plates for the thermometer. It's "common knowledge" that if you have more air flow through you sensors it reflects more accurately ambient temperature. So forced airflow with a fan would more likely be better.

I want to test how much better. For this I need thermometers that show exactly the same value in the same temperature. So I'm going to put three sensors in salt water that's heated to about 40 degrees Celsius and then let it cool down to about -20°. Reading sensor values every minute should give a reasonable calibration table.

Motor in the top is for circulating water so I get more accurate results. It's a bit ugly setup but I only plan on using this for one time so I didn't bother with anything fancy.



Or at least that was the plan. 40° -> 20° went well but after I put the whole thing in the freezer all the DS18B20 sensors stopped working. I'm guessing some moisture related problem. I took the setup out of the freezer and let it heat from about -3° that it had cooled to room temperature. I'm probably not going to leave the test running for the winter so that range should be enough.

I used screen to read the values from serial "screen -L /dev/ttyUSB1". As cat seems to stop when arduino takes it's time to boot.

Data is in a Google spreadsheet DS18B20 calibration data.

There are three sensors A, B and C. Here are their maximum differences, and average differences.
A-B A-C B-C
Max difference 0.4375 0.3125 0.2500
Avg difference 0.0537 0.0353 0.0433

I'm thinking that the differences are not that big and I shouldn't worry about them. And some of it is most likely because I put a different amount of hotglue to the sensors to shield them.

Code used can be found in github https://github.com/mika-koivusaari/read_all_ds18b20


Thursday, 9 June 2016

Arduino counter

I needed a way to read pulses from our house and geothermal heat pumps electricity meters. Of course there are many pulse counters all ready, but I had make my own:) Just a simple sketch that counts pulses from pins 2 and 3 reports them on the usb serial when asked.


Main meter is read optically with a TSL257 and the geothermal pump meter is directly connect to the meter.

Code in github https://github.com/mika-koivusaari/read_kwh_pulse

I use a python daemon to relay the data to MySql, code again in github https://github.com/mika-koivusaari/read_pulse_mysql

Monday, 30 May 2016

Bathroom lighting

We are building a upstairs bathroom. In downstairs we already have the light controlled by a PIR sensors. Which works great, except at night. When you have to go to do your business half sleep and suddenly the lights go at full blast, good luck at getting to sleep after that. I need to have a way to make the lights only light very dim at night.

So basically the functional specification is as follows:
1. Lights on when someone enters
2. If it's night lights are dim.
3. If it's day lights are fully on.
4. User can manually force full lights.

Probably the most difficult was figuring out when it's night. A simple clock would only give moderate results because we don't usually go to sleep at a fixed time. I decided to solve the problem with a light sensor in the lobby where you enter the bathroom. If it's day then sun is shining in the lobby, if it's evening and dark outside but you have the lobby lights on then you are not sleeping and want full lights.

There are three led strips for lighting, and most likely one of them is behind the mirror, they should all be separately controllable to give optimum lighting.

Pir sensor is just a standard mains voltage pir sensor that switches on the power source that powers the led strips and arduino.

Full light switch is also a normal mains switch that switch on power source source and gives a signal to arduino that full light is requested.

For the light sensor I chose TSL257, because I had one lying around and it has linear light to voltage ratio. Power source is just some generic power source from aliexpress.  Because the led strips are 24 volts I had to get that down to acceptable level for arduino, for that I used a generic stepdown converter, again from aliexpress. If I used 12 volt led strips I could just use that straight to power arduino. Full light switch gives mains voltage so that needs to transformed to a suitable voltage for arduino to read, an old cellphone charger is fine, just to make sure it gives a voltage below 5V I will put a zener diode in there, and also a bleeder resistor so it doesn't stay on for too long after the power has been cut off.

For some reason I have problem with the TSL257, when it's on a long lead it doesn't give correct results for some reason. Which is odd because I have one looking at pulses from our electricity meter and it works just fine with a long wire.

I also put a possibility to set the full brightness, which was nice because two led strips about 2,7 meters long where way too bright.

I have made the controller to veroboard and it seems to function, it hasn't been installed permanently yet because the mains wiring isn't done yet. I'm not an electronics engineer so this might look somewhat ugly:) In the few days it has been in use I already found a bug, I didn't put any hysteresis in the dark/light sensing part of the code, so in the correct lighting it flickers somewhat.


Current code https://github.com/mika-koivusaari/bathroom_light_controller

I'm in the process of making a schema with Fritzing but I have to first find or make TSL257 for Fritzing.

Friday, 27 May 2016

Making a python daemon

I have arduino (https://github.com/mika-koivusaari/read_kwh_pulse) that counts pulses, and you can then query those counts through usb serial. Currently it measures electricity consumption of our house and geothermal heat pump. I needed a way to store those values in a database.

I quickly cobbled a script that just read values every minute and inserted them to MySql. It worked pretty fine, except you had to start it manually if something went wrong and when the server was rebooted. So I started to rewrite it to a proper unix daemon, and it's hard:)

For the serial part I used pyserial. http://www.varesano.net/blog/fabio/serial%20rs232%20connections%20python has a good example.

Python has a couple of implementations for daemonizing a process, first I tried with https://pypi.python.org/pypi/python-daemon/ but it didn't seem to have good documentation, so I switched to http://pep3143daemon.readthedocs.io/en/latest/.

Problems I encountered.

Security, when you start you can have root privileges, but when you are running you really shouldn't be root. So when you reload your configuration when the user ask and try to open files it's very well possible you can't.

Signals, you should close all your resources nicely. So you should catch signals like SIGTERM. But I just couldn't get that to work. But it turns out all I had to do was read the documentation. https://docs.python.org/2/library/signal.html Signal handlers need to take two parameters, signal number and current stack frame.

Debugging, still haven't figured this one out. Daemon processes need to close all filehandles before becoming a daemon, and I'm guessing that (atleast PyCharm) opens some socket to the process it's debugging and when the daemonize process closes that you can't debug anymore. And it's really hard to find errors when you also don't have stdout or stderr and you haven't yet opened your log file, or there is something wrong with your logging.

Current code for my daemon is in github https://github.com/mika-koivusaari/read_pulse_mysql

Thursday, 26 May 2016

Outside temperature and electricity usage

Our living room/kitchen is on the south side of our house, wireless outside thermometers can't reach it from the north side. But I have a server recording outside temperatures, and maybe a whole weather station in the future. So I only had to display the temperature I already had. We also had an old IPhone and a radio with IPhone dock. So I only had to write a small script that displays the current temperature in a web page and automatically updates. Then use the IPhone to display the page.


It also displays the electricity consumption in the last minute.

Maybe in the future I'll build a better app with more information, todays forecast or something. Data could be sent with mqtt maybe.