initial commit
initial commit

file:b/config.lua (new)
--- /dev/null
+++ b/config.lua
@@ -1,1 +1,31 @@
+-- config.lua --
 
+lcdScreen = false
+
+serialDebug = true
+
+-- network
+wifiEnabled = true
+network = {}
+--network.ip = "192.168.0.51"
+--network.netmask = "255.255.255.0"
+--network.gateway = "192.168.0.1"
+
+-- MQTT
+prefix = "/iotcc"
+device = "/" .. wifi.sta.getmac() -- set the device as the wifi card mac address. Is unique, also easy to trace
+
+mqttConfig = {}
+mqttConfig.enabled = true
+mqttConfig.host = ''
+mqttConfig.port = ''
+mqttConfig.user = ''
+mqttConfig.pass = ''
+mqttConfig.secure = 0
+mqttConfig.connected = false
+
+-- other
+deviceTitle = "Relay bord"
+ntpserver = "194.177.34.116"
+timezoneHours = 2
+

file:b/func.lua (new)
--- /dev/null
+++ b/func.lua
@@ -1,1 +1,86 @@
+-- func.lua --
 
+-- setup SPI and connect display
+function init_spi_display()
+   -- Hardware SPI CLK  = GPIO14
+   -- Hardware SPI MOSI = GPIO13
+   -- Hardware SPI MISO = GPIO12 (not used)
+   -- Hardware SPI /CS  = GPIO15 (not used)
+   -- CS, D/C, and RES can be assigned freely to available GPIOs
+   local cs  = 8 -- GPIO15, pull-down 10k to GND
+   local dc  = 4 -- GPIO2
+   local res = 0 -- GPIO16
+
+   spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
+   -- we won't be using the HSPI /CS line, so disable it again
+   gpio.mode(8, gpio.INPUT, gpio.PULLUP)
+
+   -- initialize the matching driver for your display
+   disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
+end
+
+function getDateTime ()
+    tm = rtctime.epoch2cal(rtctime.get())
+    return string.format("%02d/%02d/%04d %02d:%02d",  tm["day"], tm["mon"], tm["year"], tm["hour"], tm["min"])
+end
+
+function lcdPrint(txt)
+    if serialDebug then print(txt) end
+    if lcdScreen then
+        disp:setColor(255, 0, 0);
+        disp:drawBox(0, 210, 320, 20);
+        disp:setPrintPos(2, 225)
+        disp:setColor(255, 255, 255);
+        disp:setFont(ucg.font_7x13B_tr)
+        disp:print(txt)
+
+        tmr.stop(2)
+        tmr.alarm(2, 10000, 1, function()
+            disp:setColor(0, 0, 0);
+            disp:drawBox(0, 201, 320,30);
+            tmr.stop(2)
+        end)
+    end
+end
+
+function printText(text, font, x, y, r, g, b)
+    if lcdScreen then
+        if font == 18 then
+            disp:setFont(ucg.font_helvB18_hr)
+        else
+            disp:setFont(ucg.font_helvB10_hr)
+        end
+        disp:setFontPosBottom()
+        local w = disp:getStrWidth(text)
+        local h = disp:getFontAscent() - disp:getFontDescent()
+        if x == nil then
+            x = ((disp:getWidth() - disp:getStrWidth(text)) / 2)
+        end
+        disp:setColor(0, 0, 0)
+        disp:drawBox(x, y-h, w, h)
+        disp:setColor(255, 255, 255)
+        disp:setPrintPos(x, y)
+        disp:print(text)
+    end
+end
+
+function update()
+    datetime = getDateTime()
+    lcdPrint("Update run at " .. datetime)
+
+    printText(datetime, 18, nil, 60)
+
+    sData = {}
+    if mqttConfig.connected == true then
+        mqtt:publish(sData.topic .. "/config", sjson.encode(sData), 1, 0, function(conn)
+        end)
+    end
+end
+
+-- configure wifi reset button
+function pin3cb()
+    lcdPrint("Resetting wireless configuration and restarting")
+    node.restore()
+    node.restart()
+end
+

file:b/init.lua (new)
--- /dev/null
+++ b/init.lua
@@ -1,1 +1,97 @@
+-- init.lua --
 
+-- 10 seconds on start for code errors
+print("10 seconds startup delay. use tmr.stop(0) to stop booting")
+tmr.alarm(0, 10000, 1, function()
+    dofile("config.lua")
+    dofile("func.lua")
+    init()
+    tmr.stop(0)
+end)
+
+function init()
+    -- configure wifi reset button
+    gpio.trig(3, "down", pin3cb)
+
+    -- initialize display
+    if lcdScreen == true
+        init_spi_display()
+
+        disp:begin(ucg.FONT_MODE_TRANSPARENT)
+        disp:clearScreen()
+        disp:setRotate90()
+
+        disp:setFont(ucg.font_helvB18_hr)
+        disp:setColor(255, 255, 255);
+        disp:setPrintPos((disp:getWidth() - disp:getStrWidth(deviceTitle)) / 2, 25)
+        disp:print(deviceTitle)
+    end
+
+    if wifiEnabled then
+        local def_sta_config=wifi.sta.getdefaultconfig(true)
+        if def_sta_config.ssid ~= "" then
+            lcdPrint(string.format("Found wifi config data in flash\n\tssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
+            wifi.sta.config(def_sta_config)
+            if network.ip ~= "" then
+                --wifi.sta.setip(network)
+            end
+        else
+            lcdPrint("No wifi config found on flash. Turning on IoT Setup")
+        end
+
+        -- configure wifi via enduser setup
+        --wifi.setmode(wifi.STATIONAP)
+        --wifi.ap.config({ssid="IoTSetup_" .. wifi.sta.getmac(), auth=wifi.OPEN})
+        --enduser_setup.manual(true)
+        enduser_setup.start(
+            function()
+                do
+                    if network.ip ~= "" then
+                        --wifi.sta.setip(network)
+                    end
+                    tmr.stop(0)
+                    tmr.alarm(1, 3000, 1, function()
+                        if wifi.sta.getip()==nil then
+                            lcdPrint("\tConnected to access point, obtaining IP address ...")
+                        else
+                            def_sta_config=wifi.sta.getdefaultconfig(true)
+                            lcdPrint(string.format("\tConnected to access point ssid:\"%s\"\tpassword:\"%s\"%s", def_sta_config.ssid, def_sta_config.pwd, (type(def_sta_config.bssid)=="string" and "\tbssid:\""..def_sta_config.bssid.."\"" or "")))
+                            lcdPrint('\tip: ', wifi.sta.getip())
+                            --enduser_setup.stop()
+                            -- run the mqtt file
+                            if file.exists("mqtt.lc") then
+                                dofile("mqtt.lc")
+                            else
+                                dofile("mqtt.lua")
+                            end
+                            tmr.stop(1)
+                        end
+                    end)
+                end
+            end,
+            function(err, str)
+                lcdPrint("enduser_setup: Err #" .. err .. ": " .. str)
+            end
+        );
+
+        tmr.alarm(0, 3000, 1, function()
+            if wifi.sta.getip()==nil then
+                def_sta_config=wifi.sta.getdefaultconfig(true)
+                lcdPrint("Connecting to access point " .. def_sta_config.ssid)
+            else
+                tmr.stop(0)
+            end
+        end)
+    end
+
+    cron.reset()
+    cron.schedule("* * * * *", function(e)
+        --print("----------------Every minute----------------")
+    end)
+
+    -- restart every hour
+    cron.schedule("1 * * * *", function(e)
+        --node.restart()
+    end)
+end
+

file:b/mqtt.lua (new)
--- /dev/null
+++ b/mqtt.lua
@@ -1,1 +1,57 @@
+-- mqtt.lua --
 
+sntp.sync(ntpserver,
+    function(sec, usec, server, info)
+        rtctime.set(sec + timezoneHours * 60 * 60, 0)
+        lcdPrint('NTP sync success! ' .. getDateTime())
+        update()
+    end,
+    function()
+        lcdPrint('NTP sync failed!')
+    end,
+    1
+)
+
+-- initiate the mqtt client and set keepalive timer to 120sec
+if mqttConfig.enabled == true then
+    mqtt = mqtt.Client(device, 120, mqttConfig.user, mqttConfig.pass, 1)
+
+    mqtt:on("connect", function(con)
+        lcdPrint("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
+    end)
+    mqtt:on("offline", function(con)
+        lcdPrint("\tDisconected from " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker, reconnecting\n")
+        mqttConfig.connected = false
+    end)
+
+    -- on receive message
+    mqtt:on("message", function(conn, topic, message)
+        lcdPrint("\tReceived topic : " .. topic .. " / message : " .. message)
+
+        -- catch json errors
+        local ok, json = pcall(sjson.decode, message)
+        if ok then
+        else
+            lcdPrint("\tError parsing JSON : " .. message)
+            return
+        end
+
+        if topic == prefix .. "/device" then
+            lcdPrint('\tNew request from IoT Control Center: clientId="' .. json.clientId .. '"');
+            if json.time then
+                rtctime.set(json.time + timezoneHours * 60 * 60, 0)
+            end
+            mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"pageId" : 60, "pageName" : "Relay board", "icon": "fas fa-plug"}]}', 1, 0, function(conn)
+            end)
+        end
+    end)
+
+    mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, function(conn)
+        lcdPrint("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
+        mqttConfig.connected = true
+        -- subscribe topic with qos = 1
+        mqtt:subscribe({[prefix .. "/+/+/data"]=1, [prefix .. "/device"]=1}, function(conn)
+        end)
+    end)
+end
+

file:b/relay.lua (new)
--- /dev/null
+++ b/relay.lua
@@ -1,1 +1,27 @@
+-- init.lua --
 
+local in1 = 1
+gpio.mode(in1, gpio.OUTPUT)
+gpio.write(in1, gpio.LOW)
+status1 = false
+
+tmr.alarm(0, 3000, 1, function()
+    if status1 then
+        status1 = false
+        gpio.write(in1, gpio.LOW)
+    else
+        status1 = true
+        gpio.write(in1, gpio.HIGH) 
+    end
+    print (status1)
+end)
+
+-- configure wifi reset button
+pin = 3
+function pin3cb()
+    print("Resetting wireless configuration and restarting")
+    node.restore()
+    node.restart()
+end
+gpio.trig(pin, "down", pin3cb)
+

comments