В данном коде находится вся программа, с которой идет устройство.
local cjson = require("cjson")
local base = require("base")
local pro2timer = require("pro2timer")
local logger = require("logger")
local utils = require("utils")
local atoi = tonumber
local settings = Settings.load()
local log = logger(nil, nil, nil, nil, atoi(settings.sys_ntp_off))
Temp = base.Temp({base.IO1pin, base.IO2pin, base.IO3pin})
Temp1, Temp2, Temp3 = -9997.0, -9997.0, -9997.0
-- Поток опроса температурных датчиков
thread.start(function()
while true do
local t = Temp:readAll()
Temp1, Temp2, Temp3 = t[1], t[2], t[3]
thread.sleep(30)
end
end, nil, 21, nil, "Temp")
-- Callback для обработки http-запросов
function Http_get(params)
if params.out1 then
base.out1.set(params.out1 == "1")
end
if params.out2 then
base.out2.set(params.out2 == "1")
end
if params.out3 then
base.out3.set(params.out3 == "1")
end
return cjson.encode({ t1 = Temp1, t2 = Temp2, t3 = Temp3, uptime=os.uptime(2),
out1 = base.out1.get(), out2 = base.out2.get(), out3 = base.out3.get(),
in1 = base.in1.get(), in2 = base.in2.get(), in3 = base.in3.get() })
end
if settings.sys_sound then
base.buzz.beep()
end
-- Алгоритм Watchdog
if settings.wdt_tim == "on" then
local pusher = base.Pusher(base.out1, base.out2, settings.wdt_ch1, settings.wdt_ch2, atoi(settings.wdt_t2), atoi(settings.wdt_t3), atoi(settings.wdt_t4), atoi(settings.wdt_t5)) or nil
local usb = settings.wdt_usb == "on"
local url = #settings.wdt_url>0 and settings.wdt_url or false
local btn = settings.wdt_in == "on" and base.wdt_in or false
local usb_counter, usb_checker = 0, 0
local timer
timer = pro2timer.new(tmr.TMR1, atoi(settings.wdt_t1), base.rled, function()
pusher:push()
timer:reload()
if settings.wdt_log then
log("Trying to reload")
end
if settings.sys_sound then
base.buzz.beep(100)
end
end)
if usb then
thread.start(function()
while true do
local cmd = io.read("*line")
if cmd == "~U" then
usb_counter = (usb_counter+1) & 0xffff
elseif cmd == "~I" then
local _, version, _, _ = os.version()
print("~I"..cpu.board().." "..version)
end
thread.sleepms(100)
end
end)
end
local function check(usb, url, btn)
local success, res = false, true
if usb then
res = (usb_checker ~= usb_counter)
usb_checker = usb_counter
end
if url and res then
success, tim = pcall(net.mping, url)
if success then
res = tim > 0
else
res = false
end
end
if btn and res then
res = btn.pressed()
end
return res
end
local tempRoutine = utils.routine(180, function ()
if settings.wdt_log then
log(string.format("Temp1: %.1f, Temp2: %.1f, Temp3: %.1f", Temp1, Temp2, Temp3))
end
end)
base.gled.on()
while true do
if check(usb, url, btn) then
pusher:clear()
timer:reload()
base.gled.off()
thread.sleep(1)
end
tempRoutine()
base.gled.on()
thread.sleep(2)
end
end