Wednesday 30 November 2016

Magic ice wand

Santa Claus delegated manufacturing of one magic ice wand for me. After looking through my junk pile I found:

  • Clear plastic sheet
  • Blue led + resistor
  • One 18650 cell
  • Metal pipe
  • Push button
In addition I had to order one cell 18650 charger module with micro usb input.

Metal pipe functions as the handle, and plastic sheet as the ice part. I had to glue three sheets together with super glue to get the right thickness. Then I just soldered all together, tested it and added some hot glue and tape. Plastic was formed to icicle with a belt sander.


Metal pipe was painted pink and then I put everything together with hot glue.




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.

Monday 19 September 2016

Measuring battery voltage with ESP8266

If you run IoT devices on batteries then it would be nice to know how much capacity is left on the battery, so you can change it before the device dies. Easiest way to do this is measuring battery voltage. Esp8266 has adc, but it's only from zero to one volt. The correct way would probably be to use Non-Inverting Op-Amp Level Shifter, but that would require many components and negative voltage. Easier approach is to just use voltage divider and just get the maximum voltage to acceptable range.

Using a calculator we get resistor values of 120kOhm and 39kOhm for getting 4.1 volts to 1 volt. For a low voltage of 3 volts these resistors would give 0.736 volts. So we are using 0.264 of esp8266 adc range of 0 - 1 volts. Adc is 10 bits so we have 0.264*1024=270 different values, even with some noise that should be enough for our purpose.



I made a small lua script that reads adc every minute and then posts the result with mqtt. Then used mosquitto_sub from Mosquitto to subscribe to that topic and log the messages.
mosquitto_sub -h roope.local -t /esp/battery | while read line; do echo -n $(date -Ins); echo -e "\t$line"; done | tee esp_battery.log Where roope.local is the mqtt broker machine.

Measurements and calculations can be found here https://docs.google.com/spreadsheets/d/1t18V1jrTRbj8nI5lrpctb8Fs9KzmpQ9O05dR5AeF37k/edit?usp=sharing. Esp_battery tab has raw data and formula has cleaned up data and formula.

I used google sheets linest function to calculate a function to get voltage from adc values, voltage=0.003539154715*adc+0.3756228338.

adcvoltage
10003.914777549
9003.560862077
8003.206946606
7002.853031134
6002.499115663

One problem for voltage divider is that it uses some power all the time. I should probably check how much that is, and if it considerably affects how long the battery will last. But for my next battery powered project, it will most likely report battery status also.


Thursday 8 September 2016

You have mail 2

After having done the hardware for checking mail I had to make some kind of user interface to show that data. I had previously used an old IPhone to display temperature and power usage, so it was an easy choice to continue using it.

Previously I had just used a simple php script to fetch and display data, now I wanted to try python and Flask. I used a tutorial found here to get started.

Code is pretty simple, temperature and electricity usage is fetched every time from database, but this page is usually only open on the extra phone that displays this data so there shouldn't be any reason to cache that data. That data is also updated to database every minute so it should be refreshed every time. Mail status is received over mqtt and it's sent only when it changes so it has to be cached.


I also made a couple of changes to the hardware/firmware section, for the firmware I set the retain flag in the mqtt message so that when clients connect they get the last know value of mail. For the hardware I added a bigger antenna because I have some trouble with the wifi reception. Reception is still not as good as I would like, I have to think about it. Maybe second wifi ap, I'm also thinking if I should build a separate network for IoT-devices, currently there is only one wifi device, but that could easily change. I also changed the psu to one that outputs more current and has a lower quiescent current as I thought that the wifi problem might be because the ESP8266 is not getting enough current to transmit, but that didn't solve the problem. Before I had a cheap chinese buck/boost converter, and now I have https://www.pololu.com/product/2830 which can ouput more current, but is only a buck converter and needs at least 4 volts to output 3.3 volts. So I had to replace the three aaa batteries with four aa batteries to get enough voltage.

Also there is a problem with the mailbox itself:) It let's sunlight through somewhere and that's enough so we can't see if the led is on or not. I'll have to think how to put some seals there.

Future improvements

Hardware
  • Monitor battery state.
  • Don't turn radio on on wake up, only when we need to send. NodeMCU bug might prevent this currently.
Software
  • Move temperature and electricity to mqtt also.
  • Add today's forecast.

Wednesday 31 August 2016

Fixing the apple press

After almost two falls our apple press broke, threads on it wheren't that good to begin with. The rod was ok but the nut had too weak threads. First I tried to find a suitable replacement nut but the threads where some weird size, probably imperial, so I had to change the rod too.



I used a 20 mm trapezoidal thread.


After fixing it works again.

Wednesday 24 August 2016

Making apple juice

Fall, apples are getting ripe. Time make some juice.

We have several apple trees, and we know that if we just freeze slices we probably wont use them. But we do drink juice. We have tried juicers, but they tend to be meant for smaller scale, few liters maybe. Then I came across Matthias Wandel's homepage and his apple cider making page https://woodgears.ca/cider/. I copied the design somewhat and came up with this.


It's current in prototype stage, I want to make a bigger place to put the apples, preferably so large that I can just pour a whole moving box of apples there and let it crunch away. Also a better stand would be nice. The vfd in the back is just to test different speeds, I intend to replace that motor with something a bit smaller and one phase. Video of the crusher can be found here https://youtu.be/YjaJNmqus-Y

I haven't had time to build a press so I'm forced to use this store bought. It does it's job but could be better.

Today i harvested two trees, about four moving boxes full of apples, 30 liters of juice.

update: Second time, about 75 kg of apples gives about 25 litres of juice.

Tuesday 9 August 2016

You have mail, power usage 2

Still no luck, batteries died over the weekend.

One of my thoughts was that if something goes wrong, for example we don't get wifi connection, then the device just sits there sucking power. So i added a timer just after we wake up that waits for 10 seconds and then goes to sleep, 10 seconds should be plenty time to get a connection and send data.

Also added was error handling to wifi and mqtt connections, if we don't get a connection we go to sleep and try again.

I also added a feature that we only send messages when state changes, and we only wake up the modem when we need to send something. For this I needed a new firmware that has rtcmem module. But the master had changed SDK version and I couldn't get the new firmware to work. I tried erasing (http://nodemcu.readthedocs.io/en/latest/en/flash/#upgrading-firmware) flash but still couldn't get it to work. I had to get the excellent Nodemcu-build docker image and use it to build a previous version. Which worked fine. I tried using the lowest power option in deep sleep, but there seems to be some problem with it, and I couldn't get it to work https://github.com/nodemcu/nodemcu-firmware/issues/1225.

On thing that also was on my mind was the really small antenna that came with the Esp-201 modules. Most likely a better antenna would give less trouble. I had some old adsl-modems laying around that had built in wifi with external antennas. I salvaged an antenna from one of them, and now I get better reception.


Thursday 4 August 2016

You have mail, power usage.

After a less than 24 hours my mail sensors died. Batteries where around 1 volt, so it had drained them pretty quickly, something had to be done.

First I started to measure how many mA my circuit uses. But I was faced with a continually resetting Esp8266. At first I thought it had somehow messed the program in it's flash because of low voltage situation, but after a while of debugging I got the idea to measure the voltage, and it turns out that when measuring mA my multimeter drops the voltage from 3.3 to 3.1, and that makes the Esp8266 go crazy. Then I moved amperage measuring before the dc-dc converter. After that the circuit worked fine. There is a nice article about this issue here http://www.eevblog.com/files/uCurrentArticle.pdf

The following amperage readings are before the dc to dc converter when I powered the circuit from usb at 5 volts. When Esp8266 was active the circuit drawed around 40 - 100 mA of current. In deep sleep it took about 8 mA, which was quite high. After a bit of googling I found that the power led was using significant amount of power, so I took it out. Power went down to 3.4 mA, which is still quite high. After some thinking I started looking at how much TSL257 draws current, and it was typical 1.9 mA and max 3.5 mA according to datasheet. After moving TSL257 power to gpio, so it's only on during mail check, deep sleep current is 0.5mA, which is pretty much all taken by the dc-dc converter. Which I can't do anything about, without buying a new converter, so I'll settle for this now. If aaa batteries are about 1000 mAh then they should last about 1000/0.5/24=83 days, so over two months. Which at least gives me some time to think what I will do.

Tuesday 2 August 2016

You have mail

Have you ever wondered has the mail arrived yet? I got my ESP8266 some while ago and was wondering what to do with it. After a while I thought it would be perfect for checking if mail had arrived, and I mean the traditional physical kind with envelopes.

Hardware

I figured some kind of light gate would be good for detecting if there is something in the mailbox. At first I tried with ldr and some led's, but couldn't get it to work. Either it didn't notice the led or ambient light was enough. Probably if I had a different type of ldr with different resistance it could have worked, but I didn't. So I used a TSL257 that I had lying aroung, and it seems to do the trick.

On the hardware side everything is simple, TSL257 connected to input pin, and a led with a transistor connected on a output pin. Because Esp8266 can only output a maximum of 12mA on it's pins I put a transistor to drive the led, so I could drive it at a higher amperage. Fritzing ESP-201 module from https://github.com/ydonnelly/ESP8266_fritzing.



Since I use deep sleep between checks XPD has to be connected to reset pin(black wire). Resistors on top of the Esp-201 and the yellow wire are for normal boot mode, as the module doesn't have these ready.

If pin2 (gpio4) is pulled low then the normal check and sleep is not done, this is so you can update lua scripts without reflashing the whole thing.

For a power supply I use three aaa batteries in series and a step down converted to get the 3.3 volts that is needed.

Since the circuit was so tiny I didn't use a pcb but just soldered the components together. Wires going to top right are for led and wires going bottom left are for TSL257.



I used a ESP-201 module, because it has an external antenna, and it's small and cheap. Because it doesn't have a usb connector it also has slightly smaller power usage, but I have to use a separate usb to serial module for programming. At first I used cheap clone ordered from aliexpress, but kept getting errors when flashing. So I bought a different module from a local shop and it worker like a charm. Others have also had trouble with cheap usb to serial modules when flashing ESP8266 modules.

Here is the device installed. Installation is not permanent, so I can better debug or change something if need be. Also we will probably get a new mailbox soon. Which would be good because our current mailbox is made from plastic and it lets light in just enough that when sun is shining it interferes. I had to tape the inside to block sun light.



And here is the inside.


Software

For programming I wanted to use NodeMCU and lua. I wanted to see if a interpreted language would be good enough, and it also has mqtt support.

Script itself is pretty simple:

  1. wakeup
  2. check if we see light, if so then there is too much light -> error
  3. turn on led
  4. check if we see light, light -> no mail, no light -> mail
  5. send message
  6. sleep
Programming with NodeMCU is pretty straightforward. But because it's event driven there is a bit of extra work with callbacks for this kind of batch job.

Code can be found here https://github.com/mika-koivusaari/you_have_mail.

Future improvements

After deep sleep check mail status first, and only if it has changed start modem and send message. Not sure how much this would improve battery life, but if it seems too short then I'll try.

Report battery status. Esp8266 only has adc range from 0 to 1 volts, so I would need to scale the voltage from three aaa batteries to that. Need to look into that sometime, it would probably be useful for other projects too.

Look for mqtt broker with mDNS, NodeMCU doesn't current have a mDNS client, so this will have to wait for that.

Next I'll have to make some kind user interface so I can actually see when the mail arrives.

Wednesday 6 July 2016

Comparing solar radiation shields

I have three temperature sensors with calibration data that I want to test with different solar radiation shields.


From the left, normal shield made from plastic plates, tube made from twinwall sheet used in advertisement plates, same kind of tube but with fan.

Sensors (DS18B20) are read with OWFS and a small python script that starts the conversion for all sensors, reads them and then sends them to a Google Sheet using gspread. For some reason simultaneous conversion doesn't work for me, I get cached values. So I changed the script to just read sensors one after the other. Reading is done every minute. Also writing to Google Sheets is really slow, especially when done cell by cell. So I moved writing to Sheets to a different script that is run once a day.

Currently data is flowing in nicely, but I forgot to check which sensor is in which shield, so I have to check that the next time I travel to where those sensors are.

Writing to Google sheets is really easy with gspread, I can definitely recommend it if you need to work with Google sheets.

Raw data can be found in this Google sheet.

Python script in https://github.com/mika-koivusaari/read_ds18b20_gspread

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.