Showing posts with label nodemcu. Show all posts
Showing posts with label nodemcu. Show all posts

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.


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.