-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
60 lines (46 loc) · 1.27 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
local icon = require "icon"
local settings = require "settings"
local weather = require "weather"
local function config_dns()
net.dns.setdnsserver(settings.dns.primary, 0)
net.dns.setdnsserver(settings.dns.secondary, 1)
end
local function config_broadcast(ssid_name)
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid = ssid_name})
print("Broadcasting SSID: " .. ssid_name)
end
local function on_weather_data(data)
if data ~= nil then
code = data.weather[1].icon
emoji = icon.by_code[code]
name = data.name
if #name > 20 then name = name.sub(0, 20) end
temp = data.main.temp
ssid = string.format("%s %d° %s", name, temp, emoji)
config_broadcast(ssid)
end
end
local function on_got_ip(ip)
print("Connected as: " .. ip)
config_dns()
weather.fetch(on_weather_data)
end
local function init_wifi()
wifi.setmode(wifi.STATION)
wifi.eventmon.register(
wifi.eventmon.STA_GOT_IP,
function (event) on_got_ip(event.IP) end
)
end
local function connect_station()
wifi.sta.config({
ssid = settings.station.ssid,
pwd = settings.station.password,
save = false
})
end
print("Starting...")
init_wifi()
connect_station()
tmr.softwd(settings.watch_dog)