• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ESP8266的几个代码加深对LUA的理解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
--some functions
dofile("functions.lua")
lighton1=0
lighton2=0
lighton3=0
lighton4=0

pin1 = 0
pin2 = 1
pin3 = 4
pin4 = 5
gpio.mode(pin1,gpio.OUTPUT)
gpio.mode(pin2,gpio.OUTPUT)
gpio.mode(pin3,gpio.OUTPUT)
gpio.mode(pin4,gpio.OUTPUT)

--pin1
tmr.alarm(0,1000,1,function()
if lighton1==0 then 
    lighton1=1 
    gpio.write(pin1,gpio.LOW)
    -- 512/1024, 50% duty cycle
else 
    lighton1=0 
    gpio.write(pin1,gpio.HIGH)
end 
end)
--pin2
tmr.alarm(1,1000,1,function()
if lighton2==0 then 
    lighton2=1 
    gpio.write(pin2,gpio.LOW)
    -- 512/1024, 50% duty cycle
else 
    lighton2=0 
    gpio.write(pin2,gpio.HIGH)
end 
end)
--pin3
tmr.alarm(2,1000,1,function()
if lighton3==0 then 
    lighton3=1 
    gpio.write(pin3,gpio.LOW)
    -- 512/1024, 50% duty cycle
else 
    lighton3=0 
    gpio.write(pin3,gpio.HIGH)
end 
end)
--pin4
tmr.alarm(3,1000,1,function()
if lighton4==0 then 
    lighton4=1 
    gpio.write(pin4,gpio.LOW)
    -- 512/1024, 50% duty cycle
else 
    lighton4=0 
    gpio.write(pin4,gpio.HIGH)
end 
end)
dofile("sjson.lua")
--inti.lua
--sjson.lua
name = "lxx" print("Hello world!") print(name .. ",nihao!")
--functions.lua
function
startup() if file.open("init.lua") == nil then print("init.lua deleted or renamed") else print("Running") file.close("init.lua") -- the actual application is stored in 'application.lua' -- dofile("application.lua") end end -- Define WiFi station event callbacks wifi_connect_event = function(T) print("Connection to AP("..T.SSID..") established!") print("Waiting for IP address...") if disconnect_ct ~= nil then disconnect_ct = nil end end wifi_got_ip_event = function(T) -- Note: Having an IP address does not mean there is internet access! -- Internet connectivity can be determined with net.dns.resolve(). print("Wifi connection is ready! IP address is: "..T.IP) print("Startup will resume momentarily, you have 3 seconds to abort.") print("Waiting...") tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup) end wifi_disconnect_event = function(T) if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then --the station has disassociated from a previously connected AP return end -- total_tries: how many times the station will attempt to connect to the AP. Should consider AP reboot duration. local total_tries = 75 print("\nWiFi connection to AP("..T.SSID..") has failed!") --There are many possible disconnect reasons, the following iterates through --the list and returns the string corresponding to the disconnect reason. for key,val in pairs(wifi.eventmon.reason) do if val == T.reason then print("Disconnect reason: "..val.."("..key..")") break end end if disconnect_ct == nil then disconnect_ct = 1 else disconnect_ct = disconnect_ct + 1 end if disconnect_ct < total_tries then print("Retrying connection...(attempt "..(disconnect_ct+1).." of "..total_tries..")") else wifi.sta.disconnect() print("Aborting connection to AP!") disconnect_ct = nil end end

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua简介发布时间:2022-07-22
下一篇:
Lua &amp; C++发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap