0% found this document useful (0 votes)
6K views

ESP8266 Basic On-Off

This document configures an ESP8266 WiFi module to operate as a WiFi station and connect to a specified router and password. It then creates a TCP network server listening on port 80. It defines two GPIO pins as outputs for controlling LEDs. When a request is received, it parses the request and checks for GET parameters to control writing high or low values to the GPIO pins, toggling the LEDs. A response is sent back with an HTML page allowing control of the LEDs.

Uploaded by

lvolders4833
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views

ESP8266 Basic On-Off

This document configures an ESP8266 WiFi module to operate as a WiFi station and connect to a specified router and password. It then creates a TCP network server listening on port 80. It defines two GPIO pins as outputs for controlling LEDs. When a request is received, it parses the request and checks for GET parameters to control writing high or low values to the GPIO pins, toggling the LEDs. A response is sent back with an HTML page allowing control of the LEDs.

Uploaded by

lvolders4833
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

wifi.setmode(wifi.

STATION)
wifi.sta.config("ROUTERNAME","PASSWORD")
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
led1 = 3
led2 = 4
gpio.mode(3, gpio.OUTPUT)
gpio.mode(4, gpio.OUTPUT)
gpio.write(3, gpio.LOW);
gpio.write(4, gpio.LOW);
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.."<h1> Copyright Lucje 2015</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
--buf = buf.."<option".._on..">ON</opton><option".._off..">OFF</option><
/select></form>";
client:send(buf);
client:close();
collectgarbage();
end)
end)

You might also like