Skip to content

08 Project 1: Hello World! Blink LED Through Webserver

Edwin Wong edited this page May 19, 2015 · 1 revision

Controlling the LED through a simple web server

###Technologies, Tools, and Resources used:

  • An ESP-01 or the ESP8266-12 Full evaluation board, flashed with NodeMCU
  • A PC or laptop to program the device with
  • A 3.3V capable Serial-USB adapter
  • Jumper cables
  • A breadboard
  • An LED
  • A wireless network to connect to

###The objective:

  • Connect the ESP8266 to an access point
  • Set up the ESP8266 to become a wireless access point
  • Create and host a web server on the ESP8266, that allow device to connect to it through TCP/IP
  • Control the GPIO pin on ESP8266 from the web server
  • Learn how to make a Lua script run automatically on booting the ESP8266

It's time to get connected.

1.Connect to an access point, use the following code to achieve this:
Note: Replace the ‘SSID’ and ‘password’ with the correct name and password for your own wireless network connection.

wifi.setmode(wifi.STATION) -- connect as a wireless client
wifi.sta.config("SSID", "password") -- set credentials to connect to the existing network

Information: There are 3 Wi-Fi mode that can be set on the ESP8266.

  • wifi.STATION acts as a wireless client and is used to connect to a wireless Access Point (AP).
  • wifi.SOFTAP acts as an Access Point - the ESP8266 will broadcast an SSID, and other wireless devices will be able to connect to it.
  • wifi.STATIONAP is when the ESP8266 functions in both of the previous modes at once, acting as both a wireless access point and as a client on an existing network.

The following are the examples how to set the mode for the ESP8266:

Set STATION mode:

wifi.setmode(wifi.STATION) -- connect as a wireless client
wifi.sta.config("SSID","password") -- set credentials to connect to the existing network

Set SOFTAP mode:

wifi.setmode(wifi.SOFTAP) -- set mode as access point
wifi.ap.config({ssid= "ESP_TEST", pwd="123456789"}) -- configure own SSID as ‘ESP_TEST’ and password as ‘123456789’

Set STATIONAP mode:

wifi.setmode(wifi. STATIONAP) -- set mode to station access point
wifi.sta.config("SSID","password") -- configure network credential to connect to access point network
wifi.ap.config({ssid= "ESP_TEST", pwd="123456789"}) -- configure own SSID as ‘ESP_TEST’ and password of ‘123456789’

To verify what mode you are in (mode 1 - STATION, mode 2 - SOFTAP, mode 3 - STATIONAP),
use this command:

print(wifi.getmode())

2.To check if the ESP8266 is connected to the access point. The IP address, subnet mask, and gateway IP address will be shown using the following command. Some useful commands can also be found at the ESPlorer right panel at the bottom.

If in STATION mode:

print(wifi.sta.getip()) -- show the IP address that has been assigned to the device

If in SOFTAP mode:

print(wifi.ap.getip()) -- show the IP address that has been assigned to the device

3.Once verified that the IP address is assigned to the ESP8266. A simple HTTP web server can be created on the ESP8266.

Try out the following example code from the NodeMCU website (http://nodemcu.com/index_en.html), by copying it to ESPlorer:

-- Simple web server for the ESP8266
-- Author: NodeMCU.com

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
        print(payload)
       	conn:send("<h1> Hello, NodeMcu.</h1>")
    end)
	conn:on("sent",function(conn) conn:close() end)
end)

4.To test if the web server is working, open a web browser and type the IP address of the ESP8266 assigned to it on the URL. Go to step 2 to verify the IP address. If using the SOFTAP mode, make sure the device you want to check the web server is connected to the ESP8266 broadcast SSID.

5.From the ESPlorer IDE, you can see that the device is accessing the web server that has been created on the ESP8266.

6.Now let's add some code that will control the LED attached to the GPIO pin through the web server on the ESP8266. The option list is added to the web server, allowing the option to set the GPIO pin to high or low.
Try this code out and remember to replace the ‘SSID’ and ‘password’ to your own access point network connection.

-- Simple web server for the ESP8266, for turning an LED on and off
-- Author: Edward Francis Gilbert  Date: Sunday 10 May 2015

function checkWiFiStatus()
  local s = wifi.sta.status()
  print("WiFi Status: " .. s) 
  if s == 5 then
    tmr.stop(0)
    print("Connected on " .. wifi.sta.getip())
  end
end

wifi.setmode(wifi.STATION)
wifi.sta.config("ssid", "password")
print('Attempting connection')
wifi.sta.connect()
tmr.alarm(0, 1000, 1, checkWifiStatus)

gpio.mode(3, gpio.OUTPUT)
-- Let's start off with the light off
gpio.write(3, gpio.LOW)

srv=net.createServer(net.TCP, 3)
    print("Server created on " .. wifi.sta.getip())
srv:listen(80,function(conn)
    conn:on("receive",function(conn,request)
    print(request)
    _, j = string.find(request, 'led_light_switch=')
    if j ~= nil then
        command = string.sub(request, j + 1)
            if command == 'on' then
                gpio.write(3, gpio.HIGH)
            else
                gpio.write(3, gpio.LOW)
            end
    end
    conn:send('<!DOCTYPE html>')
    conn:send('<html lang="en">')
    conn:send('<head><meta charset="utf-8" />')
    conn:send('<title>Hello, World!</title></head>')
    conn:send('<body><h1>Hello, World!</h1>')
    -- Send the current status of the LED
    if gpio.read(3) == gpio.HIGH then
        led = "ON"
    else
        led = "OFF"
    end
    conn:send('<p>The light is ' .. led .. '</p>')
    conn:send('<form method="post">')
    conn:send('<input type="radio" name="led_light_switch" value="on">ON</input><br />')
    conn:send('<input type="radio" name="led_light_switch" value="off">OFF</input><br />')
    conn:send('<input type="submit" value="Light Switch" />')
    conn:send('</form></body></html>')
    end)
end)

7.Test it in the same way at step 4. Open the web browser and use the ESP8266 IP address at the URL to access the web server. This time the option of setting the GPIO pin on/off can be seen. web toggle

8.The program code for the ESP8266 on ESPlorer IDE can be saved to an init.lua file. When the ESP8266 is booted up, the Lua interpreter searches for this file, and will run the program automatically. save to init

9.You can verify if the init.lua file is saved to the ESP8266 by clicking on the reload button in the ESPlorer IDE. reload