removed SPI LCD for now, topic is /iotcc/[macaddress]/relay master
removed SPI LCD for now, topic is /iotcc/[macaddress]/relay

file:a/config.lua -> file:b/config.lua
--- a/config.lua
+++ b/config.lua
@@ -1,6 +1,4 @@
 -- config.lua --
-
-lcdScreen = false
 
 serialDebug = true
 

file:a/func.lua -> file:b/func.lua
--- a/func.lua
+++ b/func.lua
@@ -1,74 +1,17 @@
 -- 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)
+function _print(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)
+    _print("Update run at " .. datetime)
 
     sData = {}
     if mqttConfig.connected == true then
@@ -79,7 +22,7 @@
 
 -- configure wifi reset button
 function pin3cb()
-    lcdPrint("Resetting wireless configuration and restarting")
+    _print("Resetting wireless configuration and restarting")
     node.restore()
     node.restart()
 end

file:a/init.lua -> file:b/init.lua
--- a/init.lua
+++ b/init.lua
@@ -13,30 +13,16 @@
     -- 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 "")))
+            _print(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")
+            _print("No wifi config found on flash. Turning on IoT Setup")
         end
 
         -- configure wifi via enduser setup
@@ -52,11 +38,11 @@
                     tmr.stop(0)
                     tmr.alarm(1, 3000, 1, function()
                         if wifi.sta.getip()==nil then
-                            lcdPrint("\tConnected to access point, obtaining IP address ...")
+                            _print("\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())
+                            _print(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 "")))
+                            _print('\tip: ', wifi.sta.getip())
                             --enduser_setup.stop()
                             -- run the mqtt file
                             if file.exists("mqtt.lc") then
@@ -70,14 +56,14 @@
                 end
             end,
             function(err, str)
-                lcdPrint("enduser_setup: Err #" .. err .. ": " .. str)
+                _print("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)
+                _print("Connecting to access point " .. def_sta_config.ssid)
             else
                 tmr.stop(0)
             end

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -3,11 +3,11 @@
 sntp.sync(ntpserver,
     function(sec, usec, server, info)
         rtctime.set(sec + timezoneHours * 60 * 60, 0)
-        lcdPrint('NTP sync success! ' .. getDateTime())
+        _print('NTP sync success! ' .. getDateTime())
         update()
     end,
     function()
-        lcdPrint('NTP sync failed!')
+        _print('NTP sync failed!')
     end,
     1
 )
@@ -17,37 +17,46 @@
     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")
+        _print("\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")
+        _print("\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)
+        _print("\tReceived topic : " .. topic .. " / message : " .. message)
 
         -- catch json errors
         local ok, json = pcall(sjson.decode, message)
         if ok then
         else
-            lcdPrint("\tError parsing JSON : " .. message)
+            _print("\tError parsing JSON : " .. message)
             return
         end
 
         if topic == prefix .. "/device" then
-            lcdPrint('\tNew request from IoT Control Center: clientId="' .. json.clientId .. '"');
+            _print('\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)
+        else if topic == prefix .. device .. "/relay/data" then
+            gpio.mode(json.pin, gpio.OUTPUT)
+            if json.data == 0 then
+                gpio.write(json.pin, gpio.LOW)
+            else
+                gpio.write(json.pin, gpio.HIGH)
+            end
+            mqtt:publish(prefix .. device .. "/relay/confirm", '{"topic": "' .. topic .. '", "data": "' .. json.data .. '"}', 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")
+        _print("\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)

file:a/relay.lua -> file:b/relay.lua
--- a/relay.lua
+++ b/relay.lua
@@ -11,7 +11,7 @@
         gpio.write(in1, gpio.LOW)
     else
         status1 = true
-        gpio.write(in1, gpio.HIGH) 
+        gpio.write(in1, gpio.HIGH)
     end
     print (status1)
 end)

comments