LUA初始化

--init.lua
if true then  --change to if true 
    g_mac=nil   
    print("set up wifi mode")
    wifi.setmode(wifi.STATION)
    --please config ssid and password according to settings of your wireless router.
    wifi.sta.config("jszzsj03","stanic2014")          --这里设置你的WIFI名字和密码
    wifi.sta.connect()
    cnt = 0
    tmr.alarm(1, 1000, tmr.ALARM_AUTO, function() 
        if (wifi.sta.getip() == nil) and (cnt < 20) then 
            print("IP unavaiable, Waiting...")
            cnt = cnt + 1 
        else 
            tmr.stop(1)
            if (cnt < 20) then print("IP:"..wifi.sta.getip())
                MAC=wifi.sta.getmac()
                mac=string.gsub(MAC,":","")
                g_mac = mac
                --print("MAC:"..mac)
                dofile("mqtt.lua")    --这里设置自动执行的程序,去掉减号就可以运行了。
                dofile("uart.lua")    --这里设置自动执行的程序,去掉减号就可以运行了。        
            else 
                print("No Wifi Connected.")    
            end
        end 
     end)
else
    print("\n")
    print("Please edit 'init.lua' first:")
    print("Step 1: Modify wifi.sta.config() function in line 5 according settings of your wireless router.")
    print("Step 2: Change the 'if false' statement in line 1 to 'if true'.")
end

--mqtt.lua
topicA = g_mac.."A"     --用于向手机上发送的信息
topicB = g_mac.."B"     --用于订阅手机上发来的信息
print("PUB:"..topicA)
print("SUB:"..topicB)

m = mqtt.Client(g_mac, 180, "tang", "modelsim")      --这里设置你的ClientID、用户名、密码,需要去www.difiot.com上注册获取

m:on("connect", function(client) print ("DiFi MQTT Server Connected1") end)
m:on("offline", function(client) print ("DiFi MQTT Server Offline") end)

m:on("message", function(client, topic, data) 
  if data ~= nil then
	gpio.write(4, gpio.LOW)
    print("receive "..data.." from "..topicB)
	gpio.write(4, gpio.HIGH)
  end
end)

while (m:connect("www.difiot.com", 1883, 0, 1, function(client) print("DiFi MQTT Server Connected") subscribe() end, 
    function(client, reason) print("failed reason: "..reason) end) == false) do 
    print("false")
end

function subscribe()
    m:subscribe(topicB, 0, function(client) print("subscribe "..topicB.." success") end)
end
--uart.lua
uart.alt(0)
uart.setup(0, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1, 0)

gpio.mode(4,gpio.OUTPUT,gpio.FLOAT)
gpio.write(4, gpio.LOW)

dataUart=nil

uart.on("data","\r",
  function(data)
    dataUart = data
    --print("receive from uart4:"..dataUart)
    sendPublish()
    if data=="\n" then
      uart.on("data") -- unregister callback function
  end
end, 0)

function sendPublish()
    gpio.write(4, gpio.LOW)
    --print("receive from uart5:", dataUart)
    m:publish(topicA, dataUart, 0, 0, function(client) print("sent "..dataUart.." to "..topicA) end)
end

i = 0
function sendPublishTimer()
    gpio.write(4, gpio.LOW)
    m:publish(topicA, i, 1, 0, function(client) print("sent "..i.." to "..topicA) i=i+1 if(i>=90) then i=0 end end)
	gpio.write(4, gpio.HIGH)
end

tmr.register(2, 2000, tmr.ALARM_AUTO, sendPublishTimer)
tmr.start(2)

 

转载于:https://my.oschina.net/u/2367008/blog/748507

你可能感兴趣的:(LUA初始化)