ESP8266 current / power usage charts

After a first test with a 4.7 kΩ resistor it was time to connect the INA219 to the ESP8266 + DS18B20 setup I wrote about some time ago. But first I extended the VB.Net app so that I could do the following with it:

  • Set a trigger value so that the app will start collecting the samples after the measured current rises above that trigger value;
  • Set the INA219 Calibration mode from the app, not hard-coded in the Python script running on the RPi;
  • Tooltips for the data points on the chart;
  • Leave a comment in a textbox and a ‘screenshot’ button to save the whole form as .jpg file;
  • Automatic Y-Axis scaling, automatic re-sampling, automatic screenshots (yeah, I love automation…)

I finished most items last Tuesday so now I’m ready to start charting the ESP8266 power usage 🙂

The setup is the same as in the post I mentioned above and I changed the code so that it would mimic Harald Kleins code as much as possible (DHT22 became DS18B20 and MQTT was added) so that we could compare our measurements: Haralds measurements were done with a digital scope + uCurrent Gold and mine with the INA219 (duh). Here’s my current chart (click the image for a larger version with a bigger chart area)  and Haralds version:

INA219 sample of ESP-8266 + DS18B20

What do we see here? A detailed analysis of what’s going on inside the ESP8266 in terms of current draw. The chart is in fact a detailed ‘power map’ cause by calculating the area (mA * milliseconds) between the blue line and the X-Axis we know the power usage for a single sample. With the sample interval, deep sleep current and the power the battery can deliver it should be possible to make a good estimate of what can be expected in terms of battery life. To be continued…

A simple current sensor visualization app

INA219_RPi_VBNet

So what do you do when you want to buy a uCurrent Gold but it’s not in stock? Search for a (temporary) alternative. So that’s what I did and the screenshot above shows what this alternative produces 🙂

An Adafruit INA219 Breakout Board, a Raspberry Pi, a Python script for doing the i2c to the INA219 and MQTT on the RPi, an MQTT broker and a VB.Net app on my Windows machine to collect the data and visualize it with a Chart control. (BTW, the uCurrent Gold has been ordered just a few hours ago – it’s in stock again!)

Most of the time was lost on trying to get the INA219 to work with a Devantech USB-ISS but I didn’t manage to get it working for some strange reason. I even used a BugLogic to analyze the i2c traffic – all I saw were NAK‘s and the USB-ISS I2C_TEST command always returned 0 too; weird.

After I decided to drop the USB-ISS for this and moved to a Raspberry Pi everything went much better. Enabling i2c on the RPi, Python (pip), installing mosquitto, INA219 Python library and I was almost ready – but still not knowing whether this would actually produce something useful…?

The example Python script only needed to be changed a bit:

#!/usr/bin/python

from Subfact_ina219 import INA219
import time
import mosquitto

print 'Starting ...'

def on_connect(self, rc, res):
  print 'Connected'

#On recipt of a message create a pynotification and show it
def on_message(self, data, msg):
  print("%s, %s" % (msg.topic, msg.payload))

client = mosquitto.Mosquitto("ina219", clean_session=True)
#define the callbacks
client.on_message = on_message
client.on_connect = on_connect

client.connect("192.168.10.179", 1883)

ina = INA219(0x41)
client.subscribe("inacommand", 2)

while True:
  try:
    value = ina.getCurrent_mA()
    client.publish("inadata", "%.5f" % value  , 0)

  except KeyboardInterrupt:
    print ' Exiting...Keyboard interrupt'
    break
    
  except:
    print 'unexpected error'

And I added a function to set the INA219 to max 16V and 400 mA and made that the default.

One item left: a tool to collect and visualize the measurements done by the INA219. Whenever I need a quick & dirty app with a form and some buttons I prefer VB.Net – it’s the easiest & quickest way to do small tasks like this. 130 lines of VB code is all it takes… I can expand this app with a lot of ‘extras’  like triggers, setting the INA219 to one of its 3 calibration modes, whether it should publish shunt-, bus- or current measurements and so on. Communicating with the INA219 connected to the RPi is simple, cause it’s done with MQTT 😉 But for now I think this app will suffice.

2-3 hours later I had the app finished for some first results as can be seen in the screenshot @ the top of this post. Apparently the VB.Net receives almost 2800 samples in a period of 5 seconds; that’s about 560 samples/second. Not bad… the screenshot is taken while the INA219 was measuring the current flowing through a 4.7kΩ resistor @ 5V.

Now it’s time to hook up a ESP8266 to the INA219 and start sampling! Don’t know when that will be though …

 

ESP8266 power analysis

mA_measurements

After a lot of hours I think I’ve finally created a way to see what’s going on in terms of power usage while the ESP-8266 is not in deep sleep. Cause that’s just as important as its deep sleep usage. Triggering data collection, getting the collected data to my PC for further processing and charting is what I’ll start working on tomorrow – what will I see once it’s working, that’s the big question right now 😉

ESP-8266: questions, questions

Things are going well with the ESP8266. After almost 72 hours I think that the conclusion may be that the test setup on the breadboard is working reliable; the ESP-12 has managed to come out of deep sleep and read a DS18B20 1-Wire sensor more than 12000 times without any glitch. That’s OK. I also found a way to interrupt the deep sleep it’s in by grounding RST; as far as I know this is the only way to accomplish this. It’s a somewhat strange interrupt of course, cause the ESP is not doing anything while it’s being interrupted … but hey, as long as it does what I need, I don’t care. Maybe wake-up call better describes what it actually is.

ESP-8266 ESP-12

To proceed further and find out whether the ESP-12 can be battery powered (with a minimal battery lifetime of 1 year) it’s time to do some measurements. The power it consumes seems to be documented, but I want to see it myself. Another interesting thing to know is: how long does it actually take the ESP start, connect to Wifi, initialize the GPIO, read the DS18B20, set up a TCP connection, send some data and go back to sleep. And what can be done to keep this period of high energy level as short as possible (or better: as energy-efficient as possible)? Compile the lua code with node.compile() perhaps? I’ll need a more recent firmware for that. And what’s the time from the wake-up call till the TCP server gets the data, short enough to use the ESP8266 for stuff like motion and such? How do I pull the pin to GND with a high signal from a PIR and how do I prevent re-triggering for some time? Questions …

A nice side project I’ll focus on first and for which I ordered some components today, is trying to get some more insight in the power consumption of the ESP-8266 to see if there’s any optimization possible there, without having to buy an expensive scope…

ESP8266: testing deep sleep & interrupts !?


This weekend I’ve been testing one of my ESP-12 module for more than just a few minutes; more like a few hours. Just to see how ‘robust’ the ESP8266 actually is. The story about ‘zombie mode‘, where the module didn’t restart after the deep sleep period had ended, made me curious whether the ESP8266 was stable enough (to use at all).

So I created these 2 scripts to make it do something useful for a change:

init.lua:

FileToExecute="gettemp.lua"
l = file.list();
for k,v in pairs(l) do
  if k == FileToExecute then
    print("*** You've got 5 sec to stop timer 0 ***")
    tmr.alarm(0, 5000, 0, function()
      print("Executing ".. FileToExecute)
      dofile(FileToExecute)
    end)
  end
end

gettemp.lua:

require('ds18b20')

serv_ip = '192.168.10.168'
serv_port = 8001
gpio4 = 2
interval = 20000000
ds18b20.setup(gpio4)

function getTemp()
  temp = ds18b20.read()
  conn = net.createConnection(net.TCP, 0)
  conn:on("connection", function(socket)
    socket:send(""..node.chipid().." "..tries.." "..temp.."\r")
  end)
  conn:on("sent",function(conn)
    conn:close()
  end)
  conn:on("disconnection", function(conn)
    node.dsleep(interval-tmr.now(), 0)
  end)

  conn:connect(serv_port, serv_ip)

end                   

function connect()
  tries = tries + 1
  wifi_ip = wifi.sta.getip()
  if wifi_ip == nil then
    if tries < 5 then
      tmr.alarm(0, 1000, 0, connect)
    else
      node.dsleep(interval-tmr.now())
    end
  else
    getTemp()
  end
end

tries = 0
connect()

Nothing spectacular – it reads a DS18B20 sensor, sends the temperature and some other data to a NodeJS TCP server, goes to deep sleep and wakes up 20 seconds after the previous wake-up, so that the module does this 180 times per hour. It has been running since Friday night.

ESP8266 measuring DS18B20 and going to deep sleep

But then I found another interesting post about deep sleep and interrupts. And if my interpretation is right, this could be what I’ve been looking for – I just had to try it out:

You can easily esp8266 wake from a deep sleep. Just bring on to the RST short negative pulse. I use it.

 

Well…it works! (on a ESP-12 that is, haven’t tested any other model).

ESP8266 wake-up wire

 

Whenever the black wire at the bottom of picture makes contact with the RST pin and effectively pulling it to GND, the ESP8266 wakes up – this needs further research! 🙂

For now, I resumed the reliability test for another night.