Samstag, 30. Juli 2016

ESP8266 NodeMCU / LUA - Reprogram with init.lua and deepsleep

Adding a snippet helps re-programming a ESP with
init.lua calling deep sleep without flashing the whole
firmware again..
If you name your main program init.lua on a ESP8266 with NodeMCU firmware and it uses deep sleep, you need to flash the whole firmware again to upload a new program version. But there is a nice workaround. When using Esplorer, you can put small scripts on the buttons called "Snippets". When connecting the USB-to-serial adapter, hitting "Open", you have a short time for pressing this snippet button and the commands will be executed.

This way it's not not necessary to re-flash the whole firmware, but just upload your new version and test on.

Click on the "Snippets" tab, choose the snippet you want to add on the left side, and add these two lines:
file.remove('init.old')
file.rename('init.lua','init.old')

You can also rename the button with a text that suits the task.

Sonntag, 17. Juli 2016

Home logging: Power save with ESP based sensors

Until now, the ESP based sensors were running as a tiny web server. This means they're at full power all the time, 80mA average according to the data sheet. With soldering a tiny wire to a pin on the ESP-01-modules it is possible to let the ESP sleep for some time - at less than 100µA usually. This greatly reduces power consumption and even makes battery based wifi sensors possible. The ESP is awake for collecting and sending data for a few seconds and then sleeps a long time.

For using deepsleep, ESP-01 needs a wire from Pin 8 (GPIO16)
to RESET. Magnifier glasses help with this modification.

I had to rewrite my data fetching logic for that. Instead of calling the web servers on the ESP modules, the modules now collect the data, send it to the Raspberry Pi and go to sleep for five minutes - more or less, the timer is quite inaccurate. 300 seconds tend to result in 280s, plus some seconds for connecting to Wifi, reading the sensor and connecting and sending the data to the server. Thus the rrd databases must accept the data more frequently, I had to rebuild them:


 rrdtool create /var/www/rrd/bedroom.rrd -s 280 DS:temp:GAUGE:600:-40:100 DS:hum:GAUGE:600:0:120 RRA:AVERAGE:0.5:1:576 RRA:AVERAGE:0.5:6:672 RRA:AVERAGE:0.5:24:732 RRA:AVERAGE:0.5:144:1460


To receive the data on the RasPi, a python CGI scipt helps:
http://pastebin.com/jXQRPKbt

The LUA script has several optimizations already which help it running faster (creating the send string with a table and table.concat for example) and let the ESP sleep earlier again. One enhancement is to compile the needed modules (Si7021.lua to Si7021.lc, delete the .lua afterwards). The main program can be compiled as well and renamed to init.lua for faster startup. A minor glitch in that program keeps the ESP awake unneccessary long - moving the deepsleep call from the on:disconnect handler to the on:receive one helps getting the wake-time down by several seconds.
For mobile ESPs until now the MCP1700 LDO behind a LiIon battery gives the best performance. Without the power LED, but a Si7021 connected the quiescent current during deep sleep is down to ~45µA. A DHT22 worked well over weeks, but the quiescent current is way higher and it is really imprecise and slow.