local display_names = { valve_1 = "Irrigation Sector 1", valve_2 = "Irrigation Sector 2", valve_3 = "Irrigation Sector 3 or USB load output", load = "Load power output", irrigation_timer = "Irrigation timer", web = "Web server", telnet = "Telnet server", mpptracker ="Solar Maximum Power Point Charger", ftp = "FTP server" } function load(cmd) if (cmd == 'turn_OFF') then gpio.wakeup(14, gpio.INTR_LOW) gpio.write(14, 0) load_disabled=true end if (cmd == 'turn_ON' and low_voltage_disconnect_state == 1) then gpio.wakeup(14, gpio.INTR_HIGH) gpio.write(14, 1) load_disabled=false end return not (load_disabled or low_voltage_disconnect_state ~= 1) end local function gpio_device(pin, state_var) return function(cmd) if cmd == 'turn_OFF' then gpio.wakeup(pin, gpio.INTR_LOW) gpio.write(pin, 0) _G[state_var] = false end if cmd == 'turn_ON' and low_voltage_disconnect_state == 1 then gpio.wakeup(pin, gpio.INTR_HIGH) gpio.write(pin, 1) _G[state_var] = true end if cmd == 'status' then return _G[state_var] and low_voltage_disconnect_state == 1 end end end local valve_1 = gpio_device(26, "valve_1_state") local valve_2 = gpio_device(27, "valve_2_state") local valve_3 = gpio_device(12, "valve_3_state") -- Items that represent irrigation valves in the web interface local valve_items = { valve_1 = true, valve_2 = true, valve_3 = true } local function any_valve_is_on() return valve_1_state == true or valve_2_state == true or valve_3_state == true end local function stop_irrigation_watchdog() local running = irr_watchdog:state() if running == true then irr_watchdog:stop() end end local function start_irrigation_watchdog() local running = irr_watchdog:state() -- nil means that the timer has not been registered. if running == nil then print("Cannot start irr_watchdog: timer is not registered") return false end if running == false then return irr_watchdog:start() end return true end local items = { ftp = 'TCP_21_ftp.lua', telnet = 'TCP_23_telnet.lua', web = 'TCP_80_web.lua', mpptracker = mppttimer, irrigation_timer = irr_watchdog, load = load, valve_1 = valve_1, valve_2 = valve_2, valve_3 = valve_3 } local item_order = { 'valve_1', 'valve_2', 'valve_3', 'load', 'mpptracker', 'irrigation_timer', 'web', 'telnet', 'ftp' } local function startstopstatus(cmd, what) local item = items[what] if item == nil then return false end local item_type = type(item) ---------------------------------------------------------------- -- GPIO devices: valves and load output ---------------------------------------------------------------- if item_type == "function" then -- A valve is being switched manually through the web page. if valve_items[what] then if cmd == "turn_ON" then -- Stop the watchdog before opening the valve so that -- it cannot terminate this manual irrigation. stop_irrigation_watchdog() item(cmd) -- If opening failed, for example because of the -- low-voltage disconnect, restore the watchdog. if not item("status") and not any_valve_is_on() then start_irrigation_watchdog() end return item("status") end if cmd == "turn_OFF" then item(cmd) -- Resume automatic irrigation only after all manually -- controlled valves have been closed. if not any_valve_is_on() then start_irrigation_watchdog() end return item("status") end end -- Non-valve function, currently the load output. return item(cmd) end ---------------------------------------------------------------- -- TCP servers ---------------------------------------------------------------- if item_type == "string" then if cmd == "status" then return tcp_servers[item] == true end if cmd == "turn_OFF" then server_deactivate(item) return false end if cmd == "turn_ON" then server_activate(item) return true end return false end ---------------------------------------------------------------- -- NodeMCU timer objects ---------------------------------------------------------------- if item_type == "userdata" then if cmd == "status" then local running = item:state() return running == true end if cmd == "turn_OFF" then item:stop() return false end if cmd == "turn_ON" then -- The irrigation watchdog must not be started while -- manual irrigation is active. if what == "irrigation_timer" and any_valve_is_on() then print( "Cannot start irrigation timer: " .. "one or more valves are still open" ) return false end return item:start() == true end end return false end -- MAIN REQUEST HANDLER return function(info) if not authenticated() then return end local p = info.headers.path -- Routing logic if p:match('^/control/turn_ON/') then startstopstatus('turn_ON', p:sub(18)) elseif p:match('^/control/turn_OFF/') then startstopstatus('turn_OFF', p:sub(19)) end -- Send preamble, navigation and styles send_buffered(info.http_preamble) send_buffered( "" .. "

" .. "Status page | " .. "System Config | " .. "Time | " .. "Manual | " .. "Reboot" .. "

Control Panel

" ) for _, k in ipairs(item_order) do local status = 'OFF' local command = 'turn_ON' local status_class = 'status-OFF' if startstopstatus('status', k) then status = 'ON' command = 'turn_OFF' status_class = 'status-ON' end local label = display_names[k] or k send_buffered([[
]] .. label .. [[: ]] .. status .. [[
]]) end send_buffered("") end