forked from lionello/nodemcu-httpd
-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.lua
134 lines (111 loc) · 3.75 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
-- Restore RGB
if file.open("rgb", "r") then
R = tonumber(file.readline())
G = tonumber(file.readline())
B = tonumber(file.readline())
file.close()
-- Do not go "back to black"; go to white instead
if R == 1023 and G == 1023 and B == 1023 then R=0 G=0 B=0 end
pwm.setup(5, 191, R)
pwm.start(5)
pwm.setup(6, 193, G)
pwm.start(6)
pwm.setup(7, 197, B)
pwm.start(7)
end
function saveColor()
local r = pwm.getduty(5)
local g = pwm.getduty(6)
local b = pwm.getduty(7)
if R ~= r or G ~= g or B ~= b then
if file.open("rgb", "w") then
file.writeline(r) R=r
file.writeline(g) G=g
file.writeline(b) B=b
file.close()
end
end
end
-- -- Begin WiFi configuration
local wifiConfig = {}
-- wifi.STATION -- station: join a WiFi network
-- wifi.SOFTAP -- access point: create a WiFi network
-- wifi.wifi.STATIONAP -- both station and access point
wifiConfig.mode = wifi.STATION -- both station and access point
wifiConfig.accessPointConfig = {}
wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create
wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters
wifiConfig.accessPointIpConfig = {}
wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
wifiConfig.accessPointIpConfig.gateway = "192.168.111.1"
wifiConfig.stationPointConfig = {}
wifiConfig.stationPointConfig.ssid = "dsl" -- Name of the WiFi network you want to join
wifiConfig.stationPointConfig.pwd = "0xdeadbeef" -- Password for the WiFi network
-- Tell the chip to connect to the access point
wifi.setmode(wifiConfig.mode)
print('set (mode='..wifi.getmode()..')')
if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
print('AP MAC: ',wifi.ap.getmac())
wifi.ap.config(wifiConfig.accessPointConfig)
wifi.ap.setip(wifiConfig.accessPointIpConfig)
end
if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
print('Client MAC: ',wifi.sta.getmac())
wifi.sta.config(wifiConfig.stationPointConfig)
end
print('chip: ',node.chipid())
print('heap: ',node.heap())
wifiConfig = nil
collectgarbage()
-- End WiFi configuration
-- Compile server code and remove original .lua files.
-- This only happens the first time afer the .lua files are uploaded.
local compileAndRemoveIfNeeded = function(f)
if file.open(f) then
file.close()
print('Compiling:', f)
node.compile(f)
file.remove(f)
collectgarbage()
end
end
local serverFiles = {'httpserver.lua'}
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil
serverFiles = nil
collectgarbage()
-- Connect to the WiFi access point.
-- Once the device is connected, you may start the HTTP server.
if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
local joinCounter = 0
local joinMaxAttempts = 5
local t = tmr.create()
t:alarm(3000, tmr.ALARM_AUTO, function()
local ip = wifi.sta.getip()
if ip == nil and joinCounter < joinMaxAttempts then
print('Connecting to WiFi Access Point ...')
joinCounter = joinCounter +1
else
if joinCounter == joinMaxAttempts then
print('Failed to connect to WiFi Access Point.')
else
print('IP: ',ip)
dofile("httpserver.lc")(80)
end
t:unregister()
joinCounter = nil
joinMaxAttempts = nil
collectgarbage()
t:alarm(5000, tmr.ALARM_AUTO, saveColor)
end
end
)
end
-- Uncomment to automatically start the server in port 80
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
--dofile("httpserver.lc")(80)
end
function ls()
for k,v in pairs(file.list()) do print(k,v) end
end