diff --git a/main.lua b/main.lua index fd00507..123cad1 100644 --- a/main.lua +++ b/main.lua @@ -1,4 +1,5 @@ -- Main Module +ledPin=4 function startSetupMode() tmr.stop(0) @@ -11,9 +12,20 @@ function startSetupMode() cfg.ssid="wordclock" cfg.pwd="wordclock" wifi.ap.config(cfg) + + -- Write the buffer to the LEDs + local color=string.char(0,128,0) + local white=string.char(0,0,0) + local ledBuf= white:rep(6) .. color .. white:rep(7) .. color:rep(3) .. white:rep(44) .. color:rep(3) .. white:rep(50) + ws2812.write(ledPin, ledBuf) + color=nil + white=nil + ledBuf=nil + print("Waiting in access point >wordclock< for Clients") print("Please visit 192.168.4.1") startWebServer() + collectgarbage() end @@ -30,24 +42,30 @@ function syncTimeFromInternet() end function normalOperation() - ledPin=4 -- Color is defined as GREEN, RED, BLUE color=string.char(0,0,250) connect_counter=0 -- Wait to be connect to the WiFi access point. - tmr.alarm(0, 500, 1, function() + tmr.alarm(0, 1000, 1, function() connect_counter=connect_counter+1 if wifi.sta.status() ~= 5 then - print("Connecting to AP...") + print(connect_counter .. "/60 Connecting to AP...") if (connect_counter % 2 == 0) then - ws2812.write(ledPin, string.char(255,0,0):rep(114)) + ws2812.write(ledPin, string.char((connect_counter % 6)*20,(connect_counter % 5)*20,(connect_counter % 3)*20):rep(114)) else ws2812.write(ledPin, string.char(0,0,0):rep(114)) end else tmr.stop(0) print('IP: ',wifi.sta.getip()) + -- Here the WLAN is found, and something is done + print("Solving dependencies") + local dependModules = { "timecore" , "wordclock", "displayword" } + for _,mod in pairs(dependModules) do + print("Loading " .. mod) + mydofile(mod) + end tmr.alarm(2, 500, 0 ,function() syncTimeFromInternet() @@ -57,51 +75,47 @@ function normalOperation() mydofile("webserver") startWebServer() end) - + + -- Start the time Thread + tmr.alarm(1, 20000, 1 ,function() + sec, usec = rtctime.get() + + time = getTime(sec, timezoneoffset) + print("Local time : " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second) + words = display_timestat(time.hour, time.minute) + ledBuf = generateLEDs(words, color) + -- Write the buffer to the LEDs + ws2812.write(ledPin, ledBuf) + + -- Used for debugging + if (clockdebug ~= nil) then + for key,value in pairs(words) do + if (value > 0) then + print(key,value) + end + end + end + -- cleanup + ledBuf=nil + words=nil + time=nil + collectgarbage() + end) end -- when no wifi available, open an accesspoint and ask the user - if (connect_counter == 300) then -- 300 is 30 sec in 100ms cycle - startSetupmode() + if (connect_counter >= 60) then -- 300 is 30 sec in 100ms cycle + startSetupMode() end end) - tmr.alarm(1, 15000, 1 ,function() - sec, usec = rtctime.get() - - time = getTime(sec, timezoneoffset) - print("Local time : " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second) - words = display_timestat(time.hour, time.minute) - ledBuf = generateLEDs(words, color) - -- Write the buffer to the LEDs - ws2812.write(ledPin, ledBuf) - -- Used for debugging - if (clockdebug ~= nil) then - for key,value in pairs(words) do - if (value > 0) then - print(key,value) - end - end - end - -- cleanup - ledBuf=nil - words=nil - time=nil - collectgarbage() - end) end if ( file.open("config.lua") ) then --- Normal operation - print("Solving dependencies") - dependModules = { "timecore" , "wordclock", "displayword" } - for _,mod in pairs(dependModules) do - print("Loading " .. mod) - mydofile(mod) - end wifi.setmode(wifi.STATION) dofile("config.lua") normalOperation() diff --git a/webpage.lua b/webpage.lua index 222c4db..99a8116 100644 --- a/webpage.lua +++ b/webpage.lua @@ -1,7 +1,6 @@ ------------- --- The webpage for the Webserver function sendWebPage(conn,answertype) - collectgarbage() if (ssid == nil) then ssid="Not set" end diff --git a/webserver.lua b/webserver.lua index 980bfa6..20cc558 100644 --- a/webserver.lua +++ b/webserver.lua @@ -10,8 +10,11 @@ function startWebServer() if (payload:find("GET /") ~= nil) then --here is code for handling http request from a web-browser - -- Load the webcontent - mydofile("webpage") + if (sendWebPage == nil) then + print("Loading webpage ...") + -- Load the webcontent + mydofile("webpage") + end ssid, password, bssid_set, bssid = wifi.sta.getconfig() sendWebPage(conn,1) @@ -20,6 +23,7 @@ function startWebServer() -- Clear the webpage generation sendWebPage=nil print("Clean webpage from RAM") + collectgarbage() end) @@ -65,9 +69,7 @@ function startWebServer() if (file.rename(configFile .. ".new", configFile)) then print("Successfully") dofile(configFile) -- load the new values - print("New Config loaded") sendWebPage(conn,2) -- success - else print("Error") sendWebPage(conn,3) -- error @@ -79,6 +81,7 @@ function startWebServer() end else + print("Hello via telnet") --here is code, if the connection is not from a webbrowser, i.e. telnet or nc global_c=conn function s_output(str)