-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
138 lines (122 loc) · 3.67 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
timer_started = nil
blink_level = gpio.LOW
LED = nil
leds = nil
collectgarbage()
print("Heap:(bytes)", node.heap())
function blinky()
if blink_level == gpio.LOW then
blink_level = gpio.HIGH
else
blink_level = gpio.LOW
end
for _, led in pairs(leds) do
led.value.set(led.id)
end
collectgarbage()
end
function startup()
print("in startup")
local wifiname = "wifi.lua"
local wifiapmode = false
if file.list()[wifiname] then
if not pcall(dofile, wifiname) then
print("could not dofile " ..wifiname)
wifiapmode = true
end
else
print("could not find " ..wifiname)
wifiapmode = true
end
if wifiapmode then
local wificonfig = { ssid="NodeMCU", pwd="Passwort" }
local netconfig = { ip="192.168.1.1", netmask="255.255.255.0", gateway="192.168.1.1" }
print("Open Wifi access point " ..wificonfig.ssid.. " / " ..wificonfig.pwd)
wifi.setmode(wifi.SOFTAP)
wifi.ap.config(wificonfig)
wifi.ap.setip(netconfig)
print("Network IP " ..netconfig.ip.. " / " ..netconfig.netmask)
end
timer_started=false
LED = {
ON = { key="ON", set=function(id)
if pcall(gpio.write, id, gpio.LOW) then
return true
else
print("ERROR: on call gpio.write(" .. id .. ", " .. gpio.LOW .. ")")
return false
end
end
},
OFF = { key="OFF", set=function(id)
if pcall(gpio.write, id, gpio.HIGH) then
return true
else
print("ERROR: on call gpio.write(" .. id .. ", " .. gpio.HIGH .. ")")
return false
end
end
},
BLINK= { key="BLINK", set=function(id)
if pcall(gpio.write, id, blink_level) then
return true
else
print("ERROR: on call gpio.write(" .. id .. ", " .. blink_level .. ")")
return false
end
end
}
}
leds = {
GPIO0 = { id=0, value=LED.OFF },
GPIO2 = { id=4, value=LED.OFF },
RED = { id=1, value=LED.OFF },
YELLOW = { id=2, value=LED.OFF },
GREEN = { id=3, value=LED.OFF }
}
for _, led in pairs(leds) do
if not pcall(gpio.mode, led.id, gpio.OUTPUT)then
print("ERROR: on call gpio.mode(" .. led.id .. ", " .. gpio.OUTPUT .. ")")
end
led.value.set(led.id)
end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
print("in receive")
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
local buf = "<h1> ESP8266 Web Server</h1>"
for k, _ in pairs(leds) do
buf = buf .. "<p>" .. k .. " "
buf = buf .. "<a href=\"?led=" .. k .. "&state=" .. LED.ON.key .. "\"><button>ON</button></a> "
buf = buf .. "<a href=\"?led=" .. k .. "&state=" .. LED.OFF.key .. "\"><button>OFF</button></a> "
buf = buf .. "<a href=\"?led=" .. k .. "&state=" .. LED.BLINK.key .. "\"><button>BLINK</button></a></p>"
end
local pin = _GET.led and leds[_GET.led]
local state = _GET.state and LED[_GET.state]
if(pin and state)then
print("pin:", pin.id)
print("state:", state.key)
pin.value = state
pin.value.set(pin.id)
timer_started = timer_started or tmr.alarm(1, 500, tmr.ALARM_AUTO, blinky)
else
print("invalid pin:", pin)
print("invalid state:", state)
end
client:send(buf)
client:close()
collectgarbage()
end)
end)
end
tmr.alarm(0, 5000, tmr.ALARM_SINGLE, startup)