diff --git a/main.lua b/main.lua index 5295ecc..574809f 100644 --- a/main.lua +++ b/main.lua @@ -1,14 +1,18 @@ -- Main Module -function startSetupMode() - tmr.stop(0) - tmr.stop(1) - -- start the webserver module - mod="webserver" + +function mydofile(mod) if (file.open(mod .. ".lua")) then dofile( mod .. ".lua") else dofile(mod .. ".lc") end +end + +function startSetupMode() + tmr.stop(0) + tmr.stop(1) + -- start the webserver module + mydofile("webserver") wifi.setmode(wifi.SOFTAP) cfg={} @@ -20,20 +24,6 @@ function startSetupMode() startWebServer() end -print("Solving dependencies") -dependModules = { "timecore" , "wordclock", "displayword" } -for _,mod in pairs(dependModules) do - print("Loading " .. mod) - if (file.open(mod .. ".lua")) then - dofile( mod .. ".lua") - else - dofile(mod .. ".lc") - end -end - -ledPin=4 --- Color is defined as GREEN, RED, BLUE -color=string.char(0,0,250) function syncTimeFromInternet() --ptbtime1.ptb.de @@ -47,66 +37,85 @@ function syncTimeFromInternet() ) end -connect_counter=0 --- Wait to be connect to the WiFi access point. -tmr.alarm(0, 500, 1, function() - connect_counter=connect_counter+1 - if wifi.sta.status() ~= 5 then - print("Connecting to AP...") - if (connect_counter % 2 == 0) then - ws2812.write(ledPin, string.char(255,0,0):rep(114)) - else - ws2812.write(ledPin, string.char(0,0,0):rep(114)) - end - else - tmr.stop(0) - print('IP: ',wifi.sta.getip()) - - print("Start webserver...") - tmr.alarm(2, 2000, 0 ,function() - dofile("webserver.lua") - startWebServer() +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() + connect_counter=connect_counter+1 + if wifi.sta.status() ~= 5 then + print("Connecting to AP...") + if (connect_counter % 2 == 0) then + ws2812.write(ledPin, string.char(255,0,0):rep(114)) + else + ws2812.write(ledPin, string.char(0,0,0):rep(114)) + end + else + tmr.stop(0) + print('IP: ',wifi.sta.getip()) + + tmr.alarm(2, 500, 0 ,function() + syncTimeFromInternet() + end) + print("Start webserver...") + tmr.alarm(3, 2000, 0 ,function() + mydofile("webserver") + startWebServer() + 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() + end end) + tmr.alarm(1, 15000, 1 ,function() + sec, usec = rtctime.get() + -- Include the timezone + sec = sec + (timezoneoffset * 3600) + + 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) - 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() - end -end) - -tmr.alarm(1, 15000, 1 ,function() - sec, usec = rtctime.get() - -- Include the timezone - sec = sec + (timezoneoffset * 3600) - - 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 + -- Used for debugging + if (clockdebug ~= nil) then + for key,value in pairs(words) do + if (value > 0) then + print(key,value) + end + end end - end - -- cleanup - ledBuf=nil - words=nil - time=nil - collectgarbage() -end) + -- cleanup + ledBuf=nil + words=nil + time=nil + collectgarbage() + end) +end + + --- Logic 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() else + -- Logic for inital setup startSetupMode() end \ No newline at end of file diff --git a/webserver.lua b/webserver.lua index b90925f..6962431 100644 --- a/webserver.lua +++ b/webserver.lua @@ -32,7 +32,7 @@ function sendWebPage(conn,answertype) buf = buf .."" if answertype==2 then buf = buf .. "

New configuration saved" - elseif answertype==3 then + elseif answertype==3 then buf = buf .. "

ERROR" elseif answertype==4 then buf = buf .. "

Not all parameters set"