Showing posts with label esp8266. Show all posts
Showing posts with label esp8266. Show all posts

Saturday, 10 March 2018

Solar heat, update.

Some points I noticed while my solar heat system was installed last spring.

The glass cracked in couple of days, most likely the window frame was too tight. It was probably loose enough for normal use but in my use it heated to over 50 degrees and I'm guessing that was too much. Next time I'll make a new frame and make sure the glass is really loose in there.

I'm measuring the small solar panel wrong, I should be measuring amps and not voltage. Needs to be fixed sometime in the future.

ESP8266 real time clock is very inaccurate. I fixed this by ignoring the time sent and using the receiving servers time.

There's not enough error handling in my code and sometimes it crashes, most likely it's in the MQTT or NTP part, network trouble probably.

Using just convection didn't give that much heat so I added a fan. Currently it starts the fan when heated temperature is 20 degrees above inside temperature, when heated temperature is less than 10 degrees above room temperature then fan speed is decreased.

I used a standard 120 mm computer case fan I had lying around. "Case" for the fan was fabricobbeled from plastic cellular board I had salvaged from trash.


Here's an example of a sunny day, blue is outside temp (north side of the house), red is bottom of the collector, yellow is heated air and green is inside of our garage. Magenta is the "speed" of the fan, it's actually 10 bit pwm value of the ESP8266, as the fan doesn't have a pin for measuring speed, but it gives some idea on how fast the fan is running.

Updated source and Fritzing scheman can be found on github. https://github.com/mika-koivusaari/solar_air_heat_controller

Friday, 17 March 2017

Solar heat

Our garage currently has no heating, sometimes when it's cold for a long time I put a electric heater there to make sure it doesn't go below freezing. For some time now I have thought about making a solar heater there and now I had some time to do it. One of the outside walls faces south and there is already an air intake, so that's a good starting point. I decided to only heat the incoming air and not make a full blown solar heat system with water circulation, it's cheaper and also I like to start small.

Collector

A house near us had it's windows replaced so I got one of the old ones for free. It was about the right size which reduced the work needed. Basically it's just a wooden box with polyurethane insulation I found in a dumpster, with the window put in front. Just behind the window is a tin foil paper with the paper side painted black and facing the window and sun. That way space where air heats and comes inside is covered with aluminium and there is no paint that might give out some fumes when heated. I ran out of paint so the collector isn't painted as black as it should, I'll probably paint it better in the summer as I have to take it out anyway so our garage wont heat too much. But I thought it would be good to get it installed and making heat, I can make it better later when I have some more data on how it works.

Controller

The controller needs foremost to control the flap on the air intake, because there are usually no people in the garage there is no need to get fresh air all the time, so the flap can be closed when there is no heat coming. Generally when air is hotter inside the collector we open the flap more, and when it's colder we close the flap some more. For operation there's only need for two temperature sensors, heated air and inside air, but I also want to monitor cold air coming to the collector so I have three sensors. That way I can monitor how much air heats and whats the temperature difference between north and south sides of our house. There is no fan, air is only moved by convection.

I also had a small solar cell from a Ikea garden lamp, which I added to get some reference to compare to when I make changes to the collector or controller. I put some resistors and diodes as a load, and scaled the voltage to 0 - 1 volts with a voltage divider.

All the values, temperatures, solar cell voltage and servo position are sent out with MQTT and then logged to a database so I can monitor how it works. And when I make changes I can see if it was a good or bad change.

On the left the protoboard version, and on the right the final veroboard version. Power supply is just some generic psu I had lying around as I use two dc-dc converters to make the needed 5 (LCD and servo) and 3.3 (ESP8266 and DS18B20 sensors) volts.


Controller in it's "final" installation. After one day I lowered the position of the servo some. It only opened the flap a few centimeters, now it opens some more.



Source and fritzing files can be found in github https://github.com/mika-koivusaari/solar_air_heat_controller

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

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.


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.