ESP8266 based web configurable wifi general purpose control (Part I)

NOTE: Ready to go boards can be purchased here

From AT commands to firmware controlled

The kind of new ESP8266 wifi module is not only cheap, about 3-4 dollars, its also a pretty versatile module that enables tons of remote control applications (yeah, internet of things). The first project that came to mind is to move my antenna switch control from being run by a RaspberryPi board and an 8 relay board, which makes the project pass the 100 dollar mark, to being controlled itself by a wifi board that can be programmed. Well, the ESP8266 was the solution for me, and cheap! I will describe the project, and although it is not finished yet, I will get as far as I can and then continue updating as I progress.

 Getting started

There is plenty of material out there to get anyone going. The key things to know is that you will need a USB to serial module to initially talk to the ESP8266. Any FTDI232 based module will pretty much work. Then you need to chose a serial terminal to send commands to the serial module that will send commands to the ESP8266. Something like CoolTerm or SSCOM32 would do the job. I use SSCOM on Windows and CoolTerm on the Mac, simply because CoolTerm for Windows is not working for me (crashes). Also, when you start copying LUA code into the module, CoolTerm does a great job.

My setup

ESP8266 of course, pic below.

ESP8266_V091

Any USB to serial module would do it. Here is one:

FTDIModule

 

Now connect them as below. Note that GPIO0 should be left floating except for firmware upgrade.

esp8266-reflash-firmware copy

Getting ready to load new firmware

Connect your ESP8266 module as follows:

  • Vcc = 3.3V (needs around 300-400mA peak)
  • Gnd = -ve ground
  • CH_PD = Chip enable so always +ve
  • RST = Leave floating or ground to reset
  • GPIO0 = Normally floating but this needs to be grounded when you start the update.
  • UTXD = Tx data connect to Txd on FTDI/Serial interface
  • URXD = Rx data connect to Rx of FTDI/Serial interface

You also need to download the new firmware and update tool from ElectroDragon: http://goo.gl/uHM6aO The firmware tool is called XTCOM UTIL.zip

Before you start the update, make sure your ESP8266 is working normally as follows:

  1. Connect-up as above but leave GPIO0 floating.
  2. Access the serial interface with a terminal program such as PuTTY or Termite.
  3. Set the terminal s/w baud rate to either 57600 or 115200 (depends on whether you have an early or later model). The CoolComponents models are 115200.
  4. Power-up the ESP8266 and you should see some readable text indicating that it’s ready. If you get nonsense try changing the baud rate.
  5. If all is well, try some AT commands.
  6. Try AT+RST which should re-start the module.
  7. Try AT+GMR which should give the firmware version.
  8. NB: When entering AT commands there are no spaces and you cannot use backspace to correct errors as each character is sent as it is typed!

If all is well, you can move on to the firmware update.

Loading the LUA interpreter firmware

This project requires firmware programming in LUA, so the next thing we will do is load the firmware. Here you can find more detailed instructions on how to load firmware and a number of tools to do it. Lets download the LUA firmware from here. And the flash tool from here. Flash the module making sure you pull down GPIO0, and turn the module off and on; flash start memory is 0x000000.
Disconnect GPIO0, turn the module off and on, If everything went smooth, now you can type node.restart() on the serial terminal (I’m using CoolTerm on Mac for this example) and you should get a response like in the picture below.

LUA_reboot

Simple LUA example

Lets look at very simple example of programming the module. We will use the startup functionality built in the LUA firmware as follows:

  1. create a file named init.lua
  2. write LUA commands into this file
  3. close the file

This file will get executed every time the module boots. This gives you a lot of possibilities if you want to use the module as a server or a device responding to events. A complete reference to the ESPressif SDK can be found here. Lets write some simple code.

— Open the file for writing
file.open(“init.lua”,”w”)
— Write a simple text message that will be echoed to the terminal
file.writeline([[print(“Some simple message”)]])
— Set the mode to SOFTAP
file.writeline([[wifi.setmode(wifi.SOFTAP)]])
— Get the new mode and print it
file.writeline([[print(“ESP8266 mode is: ” .. wifi.getmode())]])
— Close the file
file.close()

Put these code in a file (textfile) and we will send the text file to the ESP8266 using CoolTerm. Make sure that the Connection->Options>transmit settings allow a delay so the lines can be transmitted without error, as follows:

CooltermSettings

Select Connection->Options->Send textfile and send the above file. You should get the output below:

Exmaple_output

Yeah! All working! Now lets get a bit more complex.

A more elaborated LUA example: GPIO0 blinker with reset signal on GPIO2

We will write some code that sets up GPIO0 and GPIO2, creates a TCP server and turns GPIO0 on and off ‘n’ number of times according to the value sent through TCP, upon completion of the sequence it turns GPIO2 off and on so it can be use as a reset signal. Here it goes:

— Open the file for writing
file.open(“init.lua”,”w”)
— Open the file for writing
file.writeline([[print(“GPIO0 blinker and GPIO2 reset”)]])
— Open GPIO0 and 2 for output
file.writeline([[gpio.mode(8,gpio.OUTPUT)]])
file.writeline([[gpio.mode(9,gpio.OUTPUT)]])
— Wait a bit to ensure completion
file.writeline([[tmr.delay(10) ]])
— Cycle GPIO2 so it can be use as reset signal
file.writeline([[gpio.write(8,gpio.HIGH)]])
file.writeline([[tmr.delay(10)]])
file.writeline([[gpio.write(8,gpio.LOW)]])
— Create TCP server
file.writeline([[sv=net.createServer(net.TCP, 30) ]])
— Listen on port 9999, and callback function ‘c’
file.writeline([[sv:listen(9999,function(c)]])
— Wait until payload ‘pl ‘ is received
file.writeline([[c:on(“receive”, function(c, pl)]])
— Check for valid number received (should be between 1 and 16 for this example)
file.writeline([[if tonumber(pl) ~= nil then]])
file.writeline([[if tonumber(pl) >= 1 and tonumber(pl) <= 16 then]])
file.writeline([[print(tonumber(pl))]])
file.writeline([[tmr.delay(10)]])
— Send another reset event on GPIO2
file.writeline([[gpio.write(8,gpio.HIGH)]])
file.writeline([[gpio.write(8,gpio.LOW)]])
— Loop according to the number received
file.writeline([[for count =1,tonumber(pl) do]])
file.writeline([[ print(count)]])
file.writeline([[tmr.delay(10) ]])
— Blink GPIO0
file.writeline([[ gpio.write(9,gpio.LOW)]])
file.writeline([[tmr.delay(10)]])
file.writeline([[gpio.write(9,gpio.HIGH)]])
file.writeline([[c:send(“Sequence finished”) ]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[c:send(“Action completed”) ]])
file.writeline([[end)]])
file.writeline([[end)]])
file.close()

Lets send the file to the module. You should see the output below:

Blinker

Lets do a node.restart(), you should see the first welcome message and the program will go into wait for an event to come from port 9999.

blinkerRestart

Now you need to connect the module to the network, so lets issue the following commands. You will need your wifi router SSID and password :

wifi.setmode(wifi.STATION)
wifi.sta.config(“your_SSID”,”your_password”)
print(wifi.sta.getip())

SettingIP1

To test the events receiving part, you can use any old TCP terminal emulator, I use ‘TCP Test Tool” for iPhone, and it allows me to connect and send data to the module. Here is a screen picture of the iPhone app, connected to 192.168.1.49 port 9999 and sending the number 8, receiving back the messages.

TCP_Test_Tool

Now lets see what we received on the serial terminal:

ReceivingTest1

And there you go, we received the number eight and cycled through the blinker 8 times, that means GPIO0 cycled 8 times while GPIO2 cycled once to indicate a reset at the end of the sequence.

Conclusion

The ESP8266 is a cheap but powerful self contained wifi computer. It can be controlled through firmware upload and if you chose to do it with the LUA firmware, things get quite simple and results are achieved. The possibilities are limitless. On the next posting I will explain how to program the ESP8266 so a USB to serial module and a terminal is not necessary: it will all be done via web. Also, I will explain how to drive a binary counter and control 4 relays (multi on/multi off) using GPIO0 as control and GPIO2 as reset. This way the ESP8266 really turns into a powerful device that can be added to any automation project and its easy to program on site.

References

I tried to list all the helpful resources I found, I’ll update as more show up.

ESP8266 specs, descriptions, etc, by Nurdsape
Firmware that lets you easily control the GPIO0 and GPIO2 pins, by Electrodragon
ESP8266 community GitHub space. Web server example, Arduino example, bcc compiler and code examples
Firmware upgrade detailed instructions by UKHASnet
More documentation, SDK, firmware, flash tool, XTCOM tool, etc
More tools, documents, toolchain for Linux and Windows
LUA based interactive firmware, examples and tools by NODEMCU
SSCOM32E.exe serial terminal
ESP8266 and LUA examples  by Scargill
Problems with FTDI drivers? Check this out
ESP8266 specs, descriptions, tools from Electrodragon
ESP8266 firmware and SDK by ESPressif
Find most of the available firmware versions on this link

Advertisements

About hbouzas

Born in Buenos Aires, Argentina. Studied Physics at the University of Buenos Aires. Joined Schlumberger in February 1985 in Houston, Texas, and worked in several technical and managerial positions until 2000. From 2000 until 2008 held several management positions in Abingdon, UK; Calgary, Alberta and London, UK. Worked in the areas of Geophysical Exploration, Geological Modeling, Structural Modeling, Reservoir Modeling and Petroleum Economics and holds several patents. He is currently the Norway Technology Center Manager for Schlumberger Information Solutions and is based in Oslo and Stavanger. Main interest are software, technology, innovation, 3D visualization, design, human computer interaction, energy, environment.
This entry was posted in Uncategorized. Bookmark the permalink.

9 Responses to ESP8266 based web configurable wifi general purpose control (Part I)

  1. uriziel01pl says:

    Great article indeed.
    When we can expect next one from this series? I think it’s essential to newcommers to read such detailed worklogs so they see some results fast enough they wont get bored and leave 😉
    Good job, keep it up!

  2. William says:

    Hey Horacio, great article! Found it from the youtube page. BTW, there’s a typo on the link there.

  3. Uzair Arshad says:

    Hey there You are a life saver man !! well is it ok if i perform this and can i go back from where i started .. i dont want to loose the previous firmware ..please update thanks

  4. Uzair Arshad says:

    LUA firmware link is missing

  5. CJ says:

    Thank you for your efforts, Horacio. Very informative, but I still need a little help because a portion of this code isn’t working for me.

    Where can I find more information on this code snippet below? I can’t determine what programming language this is (I can’t find info in the Lua documentation)? Thank you.

    — Create TCP server
    file.writeline([[sv=net.createServer(net.TCP, 30) ]])
    — Listen on port 9999, and callback function ‘c’
    file.writeline([[sv:listen(9999,function(c)]])
    — Wait until payload ‘pl ‘ is received
    file.writeline([[c:on(“receive”, function(c, pl)]])

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s