Rui Santos
Published © GPL3+

ESP8266 Web Server (Without Arduino)

How to build a web server with a $4 WiFi Module called ESP8266 without using an Arduino.

BeginnerShowcase (no instructions)24,481
ESP8266 Web Server (Without Arduino)

Things used in this project

Hardware components

FTDI Programmer
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1

Story

Read more

Schematics

Web Server Schematics

Follow the schematics below to complete your web server project

Code

Upload the Lua code to your ESP8266

Lua
Copy the code below and save it as "init.lua". Then upload that code to your ESP8266
wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR_NETWORK_NAME","YOUR_NETWORK_PASSWORD")
print(wifi.sta.getip())
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

Credits

Rui Santos

Rui Santos

1 project • 35 followers
Author, Blogger, Electronics Enthusiast and Entrepreneur. For Electronics Projects, Tutorials and Reviews visit my blog: randomnerdtutorials.com

Comments