-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
58 lines (50 loc) · 1.56 KB
/
Copy pathinit.lua
File metadata and controls
58 lines (50 loc) · 1.56 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
-- init.lua
-- may need to set the following variables in-file (if short on memory)
ssid = ""
pass = ""
id = "ESP8266" --client id for mqtt
broker = "192.168.0.56" --url/ip of broker
buser = "esp" --broker username
bpass = "heating" --broker password
lwt = "lwt/door" --last will and testament topic
top = "door/com" --com topic
sta = "door/state" --state topic
--sensor = 4 -- GPIO2
--setup sensor pin
gpio.mode(4, gpio.INPUT, gpio.PULLUP)
-- set counter variables
local a = 0 -- Counter of trys to connect to wifi
local b = 200 -- Maximum number of WIFI Testings while waiting for connection
function checkWIFI()
if ( a > b ) then
print("Sorry. Not able to connect")
else
if ( ( wifi.sta.getip() ~= nil ) and ( wifi.sta.getip() ~= "0.0.0.0" ) )then
tmr.alarm( 1 , 2000 , 0 , function()
--launch file
dofile("poll.lua")
end )
else
-- Reset alarm again
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
print("Checking WIFI..." .. a)
a = a + 1
end
end
end
print("-- Starting up! ")
-- Lets see if we are already connected by getting the IP
if ( ( wifi.sta.getip() == nil ) or ( wifi.sta.getip() == "0.0.0.0" ) ) then
-- We aren't connected, so let's connect
print("Configuring WIFI....")
wifi.setmode( wifi.STATION )
wifi.sta.config(ssid, pass)
print("Waiting for connection")
tmr.alarm( 0 , 2500 , 0 , checkWIFI ) -- Call checkWIFI 2.5S in the future.
else
-- We are connected, so just run the launch code.
print("IP Address: " .. wifi.sta.getip())
a = nil
b = nil
dofile("poll.lua")
end