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!

Tagged , . Bookmark the permalink.

2 Responses to ESP8266 in deep sleep

  1. Pingback: Building a battery powered WiFi IoT Sensor with ESP8266, MS-5611 (GY-63), nodemcu and MQTT | ago control - open source home automation system

  2. binaryam says:

    I read your boot loop protection, and was none the wiser and linked thru to the nodemcu api doc, read it, then spat my tea out as realisation of what you did dawned on me.

    That’s really funny, and the kind of daft thing I would likely have done as well.

    Thanks for the laughs and the great tip, that will be my lua init from now on as well..

Leave a Reply

Your email address will not be published. Required fields are marked *