Introduction: 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.

You can see more of my projects on www.horaciobouzas.com

Step 1: My Setup

NOTE: Ready to go boards can be purchased here

ESP8266 of course. Any USB to serial module would do it. See picture Now connect them as below. Note that GPIO0 should be left floating except for firmware upgrade.

Step 2: ​Getting Ready to Load New Firmware

Connect your ESP8266 module as follows:

  1. Vcc = 3.3V (needs around 300-400mA peak)
  2. Gnd = -ve ground
  3. CH_PD = Chip enable so always +ve
  4. RST = Leave floating or ground to reset
  5. GPIO0 = Normally floating but this needs to be grounded when you start the update.
  6. UTXD = Tx data connect to Txd on FTDI/Serial interface
  7. 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.
  5. If you get nonsense try changing the baud rate. 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.

Step 3: 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.

Step 4: ​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:

create a file named init.lua

write LUA commands into this file

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, see first image.

Select Connection->Options->Send textfile and send the above file. You should get the output shown in the second image.

Step 5: ​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

ile.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]])

ile.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 shown in the image.

Step 6: Putting It All Together

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.

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())

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. See picture of the iPhone app, connected to 192.168.1.49 port 9999 and sending the number 8, receiving back the messages.

Now lets see what we received on the serial terminal.

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.

Step 7: 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 Instructable 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.

Comments

author
chrisjx (author)2015-04-21

"ElectroDragon: http://goo.gl/uHM6aO The firmware tool is called XTCOM UTIL.zip"

Does not tell me where to find XTCOM UTL..zip

Can you please include the actual link.

author
Gelfling6 (author)chrisjx2017-07-15

Likewise, just checked this 2 years after you posted it... the utility is no-longer available at the link...

author
Gelfling6 (author)Gelfling62017-07-15

Took some googling, but.... Found it!

New link:

https://developer.mbed.org/users/sschocke/code/WiF...

You need to scroll down, and see a file-link called [esp-flasher.zip], which will take you to a copy, which is inside the .zip file.. DOS-Only.. (and they wonder why i've gone to LUA/NodeMCU & ESPlorer on Linux? Nothing personal.) But xtcomm is supposed to be used for ONLY the AT/Espressif firmware.

author
roliop (author)2016-04-28

Interesting.

Incidentally, it's not 'LUA' but 'Lua', the Portuguese for 'moon', being an improvement on the previous 'Sol' language.

rp.

author
roliop (author)2016-04-28

Interesting.

Incidentally, it's not 'LUA' but 'Lua', the Portuguese for 'moon', being an improvement on the previous 'Sol' language.

rp.

author
MaryamM2 (author)2015-07-13

author
dunk8888young (author)2015-04-23

Hi i dont understand,i cant get mine working,there are so many bin firmware,there is at firmware and node mcu single bin firmware,ime trying to get a ssd1306 oled to work on it,i cant even get the examples working,i just dont understand esplora eather,there is send upload copy esp8266 then run compile,what the hell,way to much stuff and no clear noob instructions on what all the stuff does,what are the bins for why are there so many and why do they go wherte they go,ive a nodemcu dev board v0.9 please point me in right direction,also you say put text in text file and upload via cool term. do i save as init.lua or init.lua.txt what file format is it,as you dont say,cheers

author
--boss-- (author)dunk8888young2015-05-07

Hi i would suggest that you get a tool called ESPplorer, init.lua is the code that runs when you turn on the device, you can also click the format button to get rid of any random files(should get rid of dofile(")). Also if you have a esp8266 esp 7 or esp 12, gpio 15 has to be connected to ground( dont worry about this if you chip only has 6 pins). Learn lua coding laguage, it will help.

author
ashwini3112 (author)2015-04-28

to dunk

hello,

save your file as init.lua

first of all download lua1.0.2.2. from node mcu

author
dunk8888young (author)2015-04-23

Do i send lua code to the esp8266 or save to it,or compile save then send ive no idea,i cant find any clear instructions on what to do and what it all means ime so streased ive been trying every day for 3 weeks now,ive two of these so cant be faulty

author
dunk8888young (author)2015-04-23

I can flash firmware and thats it,i cant seem to be able to upload lua code and make it run,

> dofile('')
cannot open

and what does this mean bellow as i get it all the time

stdin:1:

author
dunk8888young (author)2015-04-23

Also what does init.lua mean does it mean initiate the file or what,chers

author
james.heires made it! (author)2015-04-19

Horacio,

Thanks for the tutorial. I built this example, and it works until I power cycle the 8266. As such, I have a few questions:

1. Where is init.lua stored in your example? I assume it's stored on the 8266 module, because I cannot find on my PC's file system.

2. My 8266 loses the program upon power cycle. How can I write the program into non-volatile memory, so that it automagically restarts if power is lost?

File Apr 19, 12 27 45 PM.jpeg
author
vleis.walker (author)2015-02-24

Great stuff, well done. Thank you for the detailed tutorial!

author
seamster (author)2014-12-19

Thanks for this tutorial!

About This Instructable

69,036views

82favorites

License:

Bio: Schlumberger Open Software Technology, a software framework for developing geo-science applications. 5 patents Visit http://t.co/QW8tQhMNgW
More by HoracioBouzas:ESP8266 based web configurable wifi general purpose control (Part II)ESP8266 based web configurable wifi general purpose control (Part III)ESP8266 based web configurable wifi general purpose control (Part I)
Add instructable to: