forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_init_.lua
More file actions
135 lines (110 loc) · 4.12 KB
/
Copy path_init_.lua
File metadata and controls
135 lines (110 loc) · 4.12 KB
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
-- Note that NodeMCU index number does not equal GPIO pin number.
-- see... http://nodemcu.readthedocs.io/en/dev/en/modules/gpio/
print('\nInitalizing GPIO')
a = {3,4} --add your pins here (*BUG* adding too many hangs system)
for i=1, table.getn(a), 1 do
print('clearing GPIO', a[i])
gpio.write(i, gpio.HIGH)
gpio.mode(i, gpio.OUTPUT, gpio.FLOAT)
tmr.delay(1000) -- in microseconds
gpio.mode(i, gpio.INPUT, gpio.FLOAT)
gpio.write(i, gpio.LOW)
end
node.setcpufreq(node.CPU160MHZ)
local wifiConfig = {}
wifiConfig.mode = wifi.SOFTAP
if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
wifiConfig.accessPointConfig = {}
wifiConfig.accessPointConfig.ssid = "ESP-Node1"
wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid()
wifiConfig.accessPointIpConfig = {}
wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
wifiConfig.accessPointIpConfig.gateway = "192.168.111.1"
end
if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
wifiConfig.stationConfig = {}
wifiConfig.stationConfig.ssid = "Internet"
wifiConfig.stationConfig.pwd = ""
end
wifi.setmode(wifiConfig.mode)
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.stationConfig.ssid, wifiConfig.stationConfig.pwd, 1)
end
print('chip: ',node.chipid())
print('heap: ',node.heap())
wifiConfig = nil
collectgarbage()
-- Compile server code and remove original .lua files.
-- This only happens the first time after server .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',
'httpserver-b64decode.lua',
'httpserver-basicauth.lua',
'httpserver-conf.lua',
'httpserver-connection.lua',
'httpserver-error.lua',
'httpserver-header.lua',
'httpserver-request.lua',
'httpserver-static.lua',
}
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil
serverFiles = nil
collectgarbage()
-- Function for starting the server.
-- If you compiled the mdns module, then it will register the server with that name.
local startServer = function(ip, hostname)
local serverPort = 80
if (dofile("httpserver.lc")(serverPort)) then
print("nodemcu-httpserver running at:")
print(" http://" .. ip .. ":" .. serverPort)
if (mdns) then
mdns.register(hostname, { description="A tiny server", service="http", port=serverPort, location='Earth' })
print (' http://' .. hostname .. '.local.:' .. serverPort)
end
end
end
if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
-- Connect to the WiFi access point and start server once connected.
-- If the server loses connectivity, server will restart.
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(args)
print("Connected to WiFi Access Point. Got IP: " .. args["IP"])
startServer(args["IP"], "nodemcu")
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(args)
print("Lost connectivity! Restarting...")
node.restart()
end)
end)
-- What if after a while (30 seconds) we didn't connect? Restart and keep trying.
local watchdogTimer = tmr.create()
watchdogTimer:register(30000, tmr.ALARM_SINGLE, function (watchdogTimer)
local ip = wifi.sta.getip()
if (not ip) then ip = wifi.ap.getip() end
if ip == nil then
print("No IP after a while. Restarting...")
node.restart()
else
--print("Successfully got IP. Good, no need to restart.")
watchdogTimer:unregister()
end
end)
watchdogTimer:start()
else
startServer(wifi.ap.getip(), "nodemcu")
end