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:
- 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:
- Connect-up as above but leave GPIO0 floating.
- Access the serial interface with a terminal program such as PuTTY or Termite.
- 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.
- 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. If all is well, try some AT commands.
- Try AT+RST which should re-start the module.
- Try AT+GMR which should give the firmware version.
- 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
— Write a simple text message that will be echoed to the terminal
file.writeline([[print(“Some simple message”)]])
— Set the mode to SOFTAP
— Get the new mode and print it
file.writeline([[print(“ESP8266 mode is: ” .. wifi.getmode())]])
— Close the file
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
-- Open the file for writing
file.writeline([[print("GPIO0 blinker and GPIO2 reset")]])
-- Open GPIO0 and 2 for output
-- Wait a bit to ensure completion
-- Cycle GPIO2 so it can be use as reset signal
-- Create TCP server
file.writeline([[sv=net.createServer(net.TCP, 30) ]])
-- Listen on port 9999, and callback 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]])
-- Send another reset event on GPIO2
-- Loop according to the number received
file.writeline([[for count =1,tonumber(pl) do]])
-- Blink GPIO0 file.writeline([[ gpio.write(9,gpio.LOW)]])
file.writeline([[c:send("Sequence finished") ]])
file.writeline([[c:send("Action completed") ]])
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 :
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.