-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpd.lua
55 lines (44 loc) · 1.48 KB
/
httpd.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- Simple NodeMCU web server (done is a not so nodeie fashion :-)
--
-- Written by Scott Beasley 2015
-- Open and free to change and use. Enjoy.
--
-- Your Wifi connection data
local SSID = "HUAWEI-E5172-5273"
local SSID_PASSWORD = "RTEE2351E09"
s:listen(80,function(c)
c:on("receive",function(c,pl)
for v,i in pairs{2,8,9} do
gpio.mode(i,gpio.OUTPUT)
c:send("\ngpio("..i.."):"..gpio.read(i))
if string.find(pl,"gpio"..i.."=0") then gpio.write(i,0) end
if string.find(pl,"gpio"..i.."=1") then gpio.write(i,1) end
c:send("\nnew_gpio("..i.."):"..gpio.read(i))
print(i)
end
c:send("\nTMR:"..tmr.now().." MEM:"..node.heap())
c:on("sent",function(c) c:close() end)
end)
end)
function wait_for_wifi_conn ( )
tmr.alarm (1, 1000, 1, function ( )
if wifi.sta.getip ( ) == nil then
print ("Waiting for Wifi connection")
else
tmr.stop (1)
print ("ESP8266 mode is: " .. wifi.getmode ( ))
print ("The module MAC address is: " .. wifi.ap.getmac ( ))
print ("Config done, IP is " .. wifi.sta.getip ( ))
end
end)
end
-- Configure the ESP as a station (client)
wifi.setmode (wifi.STATION)
wifi.sta.config (SSID, SSID_PASSWORD)
wifi.sta.autoconnect (1)
-- Hang out until we get a wifi connection before the httpd server is started.
wait_for_wifi_conn ( )
-- Create the httpd server
--svr = net.createServer (net.TCP, 30)
-- Server listening on port 80, call connect function if a request is received
--svr:listen (80, connect)