Introduction: ESP8266 Based Web Configurable Wifi General Purpose Control (Part II)

About: Schlumberger Open Software Technology, a software framework for developing geo-science applications. 5 patents Visit http://t.co/QW8tQhMNgW

NOTE: A ready to go board can be purchased here

On Part II I will go over 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.

You can get more details on my projects at Horacio Bouzas Web site

The control firmware

The firmware needs to be able to allow the user to connect to the module and set the SSID and password of the network the module will be connected to. The steps to design the firmware are as follows:

1. set the module to server (AP) mode

2. set a name and password

3. create a server

4. listen on port 80

5. module address is 192.168.4.1

6. present a web page asking for SSID and password

7. switch to Station mode and set SSID and password

8. connect to network

9. start listening for events coming on the ip address assigned by the network and port 9999

Here is the code to do it:

file.open("init.lua","w")

file.writeline([[print("WIFI control")]])

-- put module in AP mode

file.writeline([[wifi.setmode(wifi.SOFTAP)]])

file.writeline([[print("ESP8266 mode is: " .. wifi.getmode())]])

file.writeline([[cfg={}]])

-- Set the SSID of the module in AP mode and access password

file.writeline([[cfg.ssid="ESP_STATION"]])

file.writeline([[cfg.pwd="the_ESP8266_WIFI_password"]])

file.writeline([[if ssid and password then]])

file.writeline([[print("ESP8266 SSID is: " .. cfg.ssid .. " and PASSWORD is: " .. cfg.password)]])

file.writeline([[end]])

-- Now you should see an SSID wireless router named ESP_STATION when you scan for available WIFI networks

-- Lets connect to the module from a computer of mobile device. So, find the SSID and connect using the password selected

file.writeline([[wifi.ap.config(cfg)]])

file.writeline([[ap_mac = wifi.ap.getmac()]])

-- create a server on port 80 and wait for a connection, when a connection is coming in function c will be executed

file.writeline([[sv=net.createServer(net.TCP,30)]])

file.writeline([[sv:listen(80,function(c)]])

file.writeline([[c:on("receive", function(c, pl)]])

-- print the payload pl received from the connection

file.writeline([[print(pl)]])

file.writeline([[print(string.len(pl))]])

-- wait until SSID comes back and parse the SSID and the password

file.writeline([[print(string.match(pl,"GET"))]])

file.writeline([[ssid_start,ssid_end=string.find(pl,"SSID=")]])

file.writeline([[if ssid_start and ssid_end then]])

file.writeline([[amper1_start, amper1_end =string.find(pl,"&", ssid_end+1)]])

file.writeline([[if amper1_start and amper1_end then]])

file.writeline([[http_start, http_end =string.find(pl,"HTTP/1.1", ssid_end+1)]])

file.writeline([[if http_start and http_end then]])

file.writeline([[ssid=string.sub(pl,ssid_end+1, amper1_start-1)]])

file.writeline([[password=string.sub(pl,amper1_end+10, http_start-2)]])

file.writeline([[print("ESP8266 connecting to SSID: " .. ssid .. " with PASSWORD: " .. password)]])

file.writeline([[if ssid and password then]])

file.writeline([[sv:close()]])

-- close the server and set the module to STATION mode

file.writeline([[wifi.setmode(wifi.STATIONAP)]])

file.writeline([[print("ESP8266 mode now is: " .. wifi.getmode())]])

-- configure the module wso it can connect to the network using the received SSID and password

file.writeline([[wifi.sta.config(ssid,password)]])

file.writeline([[print("Setting up ESP8266 for station mode…Please wait.")]])

file.writeline([[tmr.delay(10000000)]])

file.writeline([[print("ESP8266 STATION IP now is: " .. wifi.sta.getip())]])

file.writeline([[print("ESP8266 AP IP now is: " .. wifi.ap.getip())]])

-- now the module is configured and connected to the network so lets start setting things up for the control logic

file.writeline([[gpio.mode(8,gpio.OUTPUT)]])

file.writeline([[gpio.mode(9,gpio.OUTPUT)]])

file.writeline([[tmr.delay(10) ]])

file.writeline([[gpio.write(8,gpio.HIGH)]])

file.writeline([[tmr.delay(10)]])

file.writeline([[gpio.write(8,gpio.LOW)]])

file.writeline([[sv=net.createServer(net.TCP, 30) ]])

file.writeline([[sv:listen(9999,function(c)]])

file.writeline([[c:on("receive", function(c, pl)]])

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

file.writeline([[gpio.write(8,gpio.HIGH)]])

file.writeline([[tmr.delay(10)]])

file.writeline([[gpio.write(8,gpio.LOW)]])

file.writeline([[for count =1,tonumber(pl) do]])

file.writeline([[ print(count)]])

file.writeline([[tmr.delay(10) ]])

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([[print("ESP8266 STATION IP now is: " .. new_ip)]])

file.writeline([[c:send("ESP8266 STATION IP now is: " .. new_ip) ]])

file.writeline([[c:send("Action completed") ]])

file.writeline([[end)]])

file.writeline([[end)]])

file.writeline([[end]])

file.writeline([[end]])

file.writeline([[end]])

file.writeline([[end]])

-- this is the web page that requests the SSID and password from the user

file.writeline([[c:send(" ")]])

file.writeline([[c:send(" ")]])

file.writeline([[c:send(" ")]])

file.writeline([[c:send("ESP8266 Wireless control setup")]])

file.writeline([[mac_mess1 = "The module MAC address is: " .. ap_mac]])

file.writeline([[mac_mess2 = "You will need this MAC address to find the IP address of the module, please take note of it."]])

file.writeline([[c:send("" .. mac_mess1 .. "")]])

file.writeline([[c:send("" .. mac_mess2 .. "")]])

file.writeline([[c:send("Enter SSID and Password for your WIFI router")]])

file.writeline([[c:send("

")]])

file.writeline([[c:send("

")]])

file.writeline([[c:send("SSID:")]])

file.writeline([[c:send("")]])

file.writeline([[c:send("
")]])

file.writeline([[c:send("Password:")]])

file.writeline([[c:send("")]])

file.writeline([[c:send("")]])

file.writeline([[end)]])

file.writeline([[end)]])

file.close()

Step 1: Loading the Firmware and Testing

Loading the firmware and testing

Load the file with the code written above as explained in Part I. Once the file is sent to the module you should do a node.restart() and will see that the ESP8266 is now in mode 2 or AP mode, server.

OK. The module is now in AP mode so it is like a WIFI router and you should see ESP_STATION on your list of WIFI networks.

Now connect to ESP_STATION using the password that you have programmed into the firmware, "the_ESP8266_WIFI_password" above. Now connect to 192.168.4.1 from a browser. You should see the webpage below. Enter SSID and password of your WIFI network so the ESP8266 can connect to it.

After a few seconds of waiting you should receive a message on the web browser with the IP address assigned to the module by your router; if you get a 0.0.0.0, just refresh the browser pointing to 192.168.4.1 and you should get the new IP address. Since for this test we are monitoring the ESP8266 through a serial terminal, lets check what the output was:

On your browser you should see a message showing the module IP address after connecting to your WIFI network

Now the ESP8266 is connected to our WIFI network and has IP address 192.168.1.49. However, if you don;t get the browser to show the IP address, you can always refer to the MAC address of the ESP8266 shown on the SSID/Password entry page and the user will have to find the IP address by going into the router setup, depending on what kind of router that is. I will in the future figure out how to ensure that IP address is always posted back to the browser.

Summary

On Part II we saw how to configure the ESP8266 directly from a web browser and bypassing the USB to serial. Now we are getting closer to something that can be commercialized. On Part III (yes, sorry, I chopped it into 3 parts) I will show how to connect the GPIO0 and GPIO2 signals to some binary counters and driver to drive relays or any other device.