ESP8266: Good enough for a battery powered sensor?

ESP8266_power_measurements_ battery_lifeDuring the last 2 weeks I’ve collected a lot of ESP-8266 power usage charts as shown here. Enough to conclude that a ESP8266 can be used to build battery powered sensors (e.g. a temperature sensor) but how happy you’ll be after some time highly depends on the report interval. A report interval of 4 samples per hour will lead to a battery life of roughly 1 year. Increasing the report interval to 60 times an hour (once per minute) will lead to a much shorter battery life: less than 3 weeks.

The biggest problem lies in Wifi. It takes my ESP-12 about 1 … 3,5 seconds to get a Wifi connection after waking up from deep sleep. And this is by far the largest contributor to the total time needed to get a sensor value out the door. I tried several things to see if I could shorten the time needed to get a Wifi connection but had no luck; a dedicated AP for a single ESP-12, moving the AP within 2 m. range of the ESP, changing channels – nothing. Maybe turning off (WPA) security may help, but I don’t consider that as a real option. Maybe a static IP address may help (instead of using DHCP) but that’s not a real option either. Increasing the report interval of the sensor is the only thing I can think of right now to get a good battery life (of 1 year or more).

So that’s it, ESP8266 is a no-go? No way! The ESP8266 can still be very helpful in a lot of ways, I’ve had a lot of fun while exploring the ESP8266 and will definitely use them whenever I need always-on Wifi for so,me future project. And I made a nice ‘poor mans digital scope’ during all the fun as well. But it won’t work well when you want to use a relatively short report interval

Is there an alternative for the ESP8266? Lots of ’em, mine is called Zigbee. I’ve been using Zigbee modules since 2010 or so and built my own Zigbee sensors based on a JeeNode + XBee series 2 modules. For motion, temperature, barometric pressure, light intensity (lux). Batteries last about a year, that’s OK. If I would let a ESP8266 do exactly the same thing (with the same reporting interval, sensor type) as a combination of JeeNode & XBee, the batteries would probably last about 8 weeks. Wooaahhh…

I have some unused 3.3V Arduino Pro Mini’s and some XBee’s and will make some similar power usage charts of those; an XBee is ready to send in 35 milliseconds after power-up, so here the numbers will be quite different …

For those interested I’ve put together a page with some of the ESP power usage charts here, so you can see for yourself.

And now it’s time to move on … cause a new Odroid-C1 arrived on which I’m gonna play with OpenHAB, CometVisu, PencilBlue and some more interesting stuff :-)

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.

ESP8266 in deep sleep

With the ESP-12 modules on a breadboard adapter I was finally ready for some tinkering. The plan for today was very simple: flash NodeMcu firmware, start programming the ESP8266 in Lua and try deep sleep mode.

I put an ESP-12 on a breadboard, used a FTDI-ish thing to connect the ESP-12 to my Windows PC and used a 3.3/5V breadboard power supply (set to 3.3V) to power the ESP-12.

ESP-12 in deep sleep

Flash!

Time to flash the thing! I knew that sometime in January MQTT was added to the NodeMCU firmware so I searched for a recent firmware version that contained the MQTT code. I read some rumors that MQTT seemed to be broken in the latest NodeMcu firmware releases – on the ESP8266 forum I read that v20150127 was the latest release where MQTT still worked; yesterday I read that it was due to the addition of MQTT v3.1.1 support.

Tools and other things I downloaded to get started with the ESP-12 were:
NodeMcu firmware
NodeMcu flasher
LuaUploader
LuaLoader

The latter 2 have some overlap in functionality – it looks like LuaLoader will be my favorite. OK; now that I have a flash tool and the v20150127 firmware – what’s next? After some trial and error I found out that I had to change something on the ‘Config’ page of the flash tool:

NodeMCU Flasher

I unchecked items 2, 3 and 4 and let the first item point to the right firmware image I wanted to use. Figuring this out took me longer than soldering the breadboard adapter … A wire from GPIOØ to GND followed by a cold boot set the ESP-12 in firmware upload mode, clicking “Flash” on the Operation tab was enough to flash the firmware.

After some playing around with “Hello World”- and “Blink”-like Lua scripts it was time to do something that would be a bit more exciting – things like interrupts, deep sleep and some MQTT of course.

First I wanted to know everything about deep sleep; I found this forum post and read about another mode the ESP8266 could be in – zombie mode. I wanted to avoid that mode of course so I took the suggested zombie counter measures which is pulling up GPIOØ & GPIO2 to VCC with ~5kΩ. And for using the deep sleep mode RST & GPIO16 have to be connected to each other and also pulled up to VCC; and of course CH_PD as usual.

Boot loop protection!

And of course, the first script I made with a node.dsleep() in it didn’t work .. well, it did what it was supposed to do, but not what I meant it to do! Some error in the code caused a reboot within a few seconds and there was no way I could stop this boot loop; nothing helped. Only after re-flashing the firmware I regained control over my ESP-12… So the first thing I did was searching for a workaround/solution for this and found one here, so now my init.lua (the NodeMcu autoexec.bat 😉 looks like this:

FileToExecute="printtext.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

Yeah I know,  this adds an extra delay of 5 seconds after a restart, but this is much, much better than the need to re-flash each time you make a mistake – and since my experience with Lua is like 1~2 hours, I think that this will be my init.lua for a loong time.

Results for this evening: Deep sleep seems to be working… onwards!

New ESP8266 (ESP-12) modules ready

Today, while my son and I were visiting the NMM, the breadboard adapters for my new ESP8266 type ESP-12 modules arrived. Finally …

ESP12_on_breadboardI was a bit surprised by their size though; the adapter covered the whole middle area of the breadboard, leaving no room to plug in wires as you can see. So instead of using the headers that came with the adapter, I used headers with a length of 18 mm. This way the adapter board is still held at its place on the breadboard and I can use female wires to connect to the ESP-12.

Now let’s see what has happened in the ESP8266 scene during the time I was away 😉

WordPress on Banana Pi?

WordPress is one of those applications of which I was not sure whether a small couple-of-watts computer like a Raspberry Pi, Banana Pi or Odroid could handle it. WordPress always felt a bit sluggish… Well, there’s only one way to find out, right? Just do it 😉BananaPi

So this afternoon I moved my WordPress site from a Hyper-V Fedora Linux VM to a Banana Pi that didn’t have that much to do yet – and right now, you’re looking at it! (WordPress on a Banana PI, that is)

Voice control revisited: the Web Speech API

After exploring some things last April it became quiet regarding Voice control. I also played with the Web Speech API for some time but never finished it. But last weekend (while still waiting for my ESP-8266’s (ESP-12) to arrive) I decided to give it another try, even though I backed the Homey project on Kickstarter so I probably won’t even need to spend time on this – this is just for fun.

Home screenVoice control page

The Web Speech API documentation is not that hard to understand and there are dozens of good examples to be found – just search for “Web Speech API demo” or something similar and you’ll find plenty of good examples.

I had already made a small ‘voice’ button on the Home page of our Web-app so all I had to was finish the page behind that button. A large start/stop button to control the voice recognition and an area in which the results of the speech recognition could be displayed. Speech recognition works very good, impressive stuff!

The code is very short and simple actually:

<div data-role="page" id="pgstt" data-theme="a" data-content-theme="a">
  <div data-role="header"><h2>Spraak commando</h2></div><!-- /header -->
  <div data-role="header"><h2 id="sttstatus"></h2></div><!-- /header -->
  <div data-role="content">
  <button id="sttbutton" onclick="toggleStartStop()"><img id="sttbuttonimg" src="icons/micbut.png" /></button>
  <div style="border:dotted;padding:10px">
    <span id="interim_span" style="color:grey"></span>
  </div>

  <script type="text/javascript">
    var recognizing;
    var recognition = new webkitSpeechRecognition();
    recognition.lang = "nl-NL";
    recognition.continuous = true;
    recognition.interim = true;
    reset();
    recognition.onend = reset;

    recognition.onresult = function (event) {
      var interim = "";
      var last = event.results[event.results.length-1][0].transcript;
      interim_span.innerHTML += last;
      cmdPublish('speech', last);
    }

    function setLS(t) {
      sttstatus.innerHTML = t;
    }

    function reset() {
      console.log('Stopped');
      recognizing = false;
      $("#sttbuttonimg").attr("src","icons/micbut.png");
      setLS("Gestopt");
    }

    function toggleStartStop() {
      if (recognizing) {
        $("#sttbuttonimg").attr("src","icons/micbut.png");
        setLS("Gestopt");
        recognition.abort();
        reset();
      } else {
        recognition.start();
        recognizing = true;
        $("#sttbuttonimg").attr("src","icons/micbutl.png");
        setLS("Luisteren ...");
        interim_span.innerHTML = "";
      }
    }
  </script>
  </div><!-- /content -->
  <div data-role="footer" data-position="fixed">
  </div><!-- /footer -->
</div><!-- /page -->

That’s it. Primus takes care of delivering the text to the server-side NodeJS script which passes it on to the Nools rules engine which I use to automate things. I can now makes rules like this:

//---------------------------------------------------------
rule hobbytestopen {
    when {
      or(
        m1: Message m1.t == 'sensor/value' && m1.changedTo('open'),
        m1: Message m1.t == 'speech' && m1.contains('test licht aan')
        );
    }
    then {
        unchange(m1);
        log('Execute rule Office test open');
        publish('command/plcbus','{"address":"B02", "command":"ON"}');
    }
}

Now this rule can be triggered by either a sensor or a speech command which contains the words ‘test’ licht’ and ‘aan’ (for the non-Dutch: “test light on”). The only restriction yet is that those words need to be in the order as specified in the condition.

That’s not good enough of course, cause not only saying “test licht aan” would trigger this rule but saying “blaastest wijst uit dat ik lichtelijk ben aangeschoten” would also … not really intelligent 😉 But those are just small issues that can easily be handled.