本帖最后由 maidoo 于 2016-2-2 23:19 编辑
软件附加在这里。
init.lua
[pre lang="LUA" line="1" file="init.lua"]
print("ower on, Setting wifi client connection....")
wifi.setmode(wifi.STATION)
wifi.sta.config("Chinanet","88888888")
wifi.sta.connect()
local cnt = 1
tmr.alarm(1, 1000, 1, function()
if (wifi.sta.getip() == nil) and (cnt <= 20) then
print("["..cnt.."] IP invalid, waitting...")
cnt = cnt + 1
else
if (cnt <= 20) then print("Config done, IP is "..wifi.sta.getip())
tmr.stop(1)
dofile("netCaliLM8562.lua")
else print("!!!! Wifi setup time more than 20s, reconnect it again....")
cnt = 1
wifi.sta.connect()
end
end
end)
[/code]
netCaliLM8562.lua
[pre lang="LUA" line="1" file="netCaliLM8562.lua"]
-- host = 'emouze.com'
host = 'baidu.com'
port = 80
--url = "/img/baidu_jgylogo1.gif"
url = '/'
httpPKG = "HEAD " .. url .. " HTTP/1.1\r\nHost: " .. host .. "\r\nConnection: close\r\n\r\n"
curGMT = ""
bConnected = false
calibratedAfterPowerOn = false
tzone = 8 -- 东8区时区
sentCnt = 0
t2cnt = 1
t2target = 0
tmrBtn = 2
tmrCycled = 4
ioPWR = 1
ioCali = 2
gpio.mode (ioCali, gpio.OUTPUT)
gpio.write(ioCali, gpio.LOW)
-----------------------------
local function loopButton()
if t2cnt <= t2target then
if 1 == (t2cnt % 2) then
gpio.write(ioCali, gpio.HIGH)
else
gpio.write(ioCali, gpio.LOW)
end
t2cnt = t2cnt + 1
else
tmr.stop(tmrBtn)
gpio.write(ioCali, gpio.LOW)
end
end
-----------------------------
function adjustClock(hour, minute)
local cc
hour = hour % 12
minute = minute % 60
cc = hour * 60 + minute
print("========== Calibrated by Internet ================")
print(" Adjust LM8562 clock to " .. hour .. ":" .. minute)
gpio.mode (ioPWR, gpio.OUTPUT)
gpio.write(ioPWR, gpio.LOW) -- Clock power off
tmr.delay(202000)
gpio.mode (ioPWR, gpio.INPUT) -- Power it on again, with LM8562 it start from 12:00 AM
tmr.delay(955000)
tmr.wdclr()
gpio.mode (ioCali, gpio.OUTPUT)
t2cnt = 1
if cc == 0 then cc = 1 end -- At least press button once to calibrated clock
t2target = cc * 2
tmr.alarm(tmrBtn,50,1,function() loopButton() end)
end
function constructConnection()
if bConnected == false then
socket = nil
socket = net.createConnection(net.TCP, 0)
socketn("connection", function(sck, response)
bConnected = true
sentCnt = sentCnt + 1
print("\r\n#### Conneted!\t\t&socket:", socket)
socket:send(httpPKG)
end)
socketn("disconnection", function(sck, response)
bConnected = false
socket:close()
socket = nil
print("#### Socket disconneted, @Count=" .. sentCnt ..", Heap=" .. node.heap())
end)
socketn("receive", function(sck, response)
--print("[Recv] " .. response)
hh = nil
curGMT = string.sub(response,string.find(response,"Date: "),string.find(response,"Date: ")+35)
-- print(curGMT) -- Date: Sat, 23 Jan 2016 09:10:20 GMT
hh,mm,ss = string.match(curGMT, "(%d+)%d*%d+)%d*%d+)%d* GMT")
if hh ~= nil then
print("#### Get Internet time from " .. host, (hh+tzone)%24, mm, ss)
--刚上电需要校准一次,之后仅在半夜12点到1点之间执行校准,这样按钮的次数少
if (calibratedAfterPowerOn == false) or (hh + tzone == 24) then
calibratedAfterPowerOn = true
adjustClock(hh+tzone, mm)
end
end
end)
socket:connect(port, host)
end
end
-- +++++++++++++++++++++++++++++++
constructConnection()
tmr.alarm(tmrCycled,3660000,1,function() constructConnection() end)
[/code] |