-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
47 lines (38 loc) · 1.1 KB
/
init.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
station_cfg={}
station_cfg.ssid="smart-home"
station_cfg.pwd="smart-home"
wifi.setmode(wifi.STATION)
wifi.sta.config(station_cfg)
wifi.sta.connect()
print(wifi.sta.getip())
switch=0
singal=4
gpio.mode(switch,gpio.OUTPUT)
gpio.mode(singal, gpio.OUTPUT)
gpio.write(singal,gpio.HIGH)
gpio.write(switch,gpio.HIGH)
tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
if wifi.sta.getip() == nil then
print('Waiting for IP ...')
gpio.write(singal,gpio.LOW)
gpio.write(singal,gpio.HIGH)
else
print('IP is ' .. wifi.sta.getip())
gpio.write(singal,gpio.LOW)
tmr.stop(1)
end
end)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
local _, _, method, vars = string.find(payload, "([A-Z]+) /(.+) HTTP")
if(vars == "off")then
gpio.write(switch, gpio.HIGH)
conn:send("<h1> Light OFF.</h1>")
elseif(vars == "on")then
gpio.write(switch, gpio.LOW)
conn:send("<h1> Light ON.</h1>")
end
end)
end)