https://github.com/jollen/simplest-tutorial-led-control-browser-esp8266
https://github.com/jollen/simplest-tutorial-led-control-browser-esp8266
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jollen/simplest-tutorial-led-control-browser-esp8266
- Owner: jollen
- Created: 2016-03-12T14:41:40.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-12T14:43:17.000Z (about 10 years ago)
- Last Synced: 2025-10-18T05:58:18.459Z (8 months ago)
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# simplest-tutorial-turn-led-on-browser-esp8266
## Step 1:
Run the code on NodeMCU (ESP8266)
```
-- Select IO - GPIO0
outpin=3
gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW)
function power(stat)
if stat=="ON" then gpio.write(outpin,gpio.HIGH)
return end
if stat=="OFF" then gpio.write(outpin,gpio.LOW)
return end
end
-- Print IP address
ip = wifi.sta.getip()
print(ip)
-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)
wifi.sta.config("jollenchen", "qqqqqqqq")
wifi.sta.autoconnect(1)
-- Create a server
-- and set 30s time out for a inactive client
sv = net.createServer(net.TCP, 30)
-- Server listen on 80
-- Print HTTP headers to console
sv:listen(80,function(c)
c:on("receive", function(conn, payload)
print(payload)
if (string.find(payload, "POST /power/on") ~= nil) then
power("ON")
elseif (string.find(payload, "POST /power/off") ~= nil) then
power("OFF")
end
conn:send("HTTP/1.1 200 OK\n\n")
conn:close()
end)
end)
```
## Step 2
Call ```/power/on``` and ```/power/off``` APIs by curl.
```
$ curl -X POST http://192.168.1.1/power/on
```
You need to replace the IP address with yours.