-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
49 lines (42 loc) · 1.44 KB
/
init.lua
File metadata and controls
49 lines (42 loc) · 1.44 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
function fif(cond, if_true, if_false)
if cond then return if_true else return if_false end
end
-- this should set the globals wifi_ssid, wifi_password, thingspeak_api_key, thingspeak_channel_id
require('credentials')
require('thermometer')
require('relay')
require('storage')
require('httpserv')
require('thingspeak')
current_temp = nil
target_temp = read_target_temp()
-- temp check, start/stop cooling and send result to thingspeak
function work()
get_current_temp(function (read_temp)
current_temp = read_temp
print('current_temp:' .. tostring(current_temp) .. ' target_temp:' .. tostring(target_temp))
if not current_temp or not target_temp then
set_cooling(false)
elseif current_temp < target_temp - 0.25 then
set_cooling(false)
elseif current_temp > target_temp + 0.25 then
set_cooling(true)
end
thingspeak_write(thingspeak_api_key, target_temp, current_temp, get_cooling())
end)
end
function main()
print('main started')
print('target_temp:' .. tostring(target_temp))
work()
tmr.alarm(0, 30000, tmr.ALARM_AUTO, work)
-- (re)start http server when we get an ip
wifi.sta.eventMonReg(wifi.STA_GOTIP, restart_http_server)
wifi.sta.eventMonStart(1000)
-- init wifi
wifi.sta.sethostname('ferm-chamber')
wifi.setmode(wifi.STATION)
local autoconnect = 1
wifi.sta.config(wifi_ssid, wifi_password, autoconnect)
end
main()