esp8266--nodemcu--lua
開發主要是兩個網站:
- nodemcu lua檔案NodeMCU Documentation
- nodemcu 韌體編譯nodemcu-build
GPIO
pin=4
gpio.mode(pin,gpio.OUTPUT)
--開啟pin4--
gpio.write(pin,gpio.LOW)
--置埠輸出低電位,燈亮--
gpio.write(pin,gpio.HIGH)
--置埠輸出高電位,燈滅--
基本語法
編譯時不能有中文
單行注釋 --
多行注釋
--[[
多行注釋
多行注釋
--]]
--函式
function test()
end
--判斷
if wifi.sta.getip() ==nil then
else
end
--串口輸出
print("disconnect..")
定時器
timer =tmr.create()
--定時回圈函式
function test()
end
timer:alarm(1000,tmr.ALARM_AUTO,test)
定時器模式
tmr.ALARM_SINGLE一次性警報(不需要呼叫unregister())tmr.ALARM_SEMIALARM_SEMI手動重復報警(call start()重新啟動)tmr.ALARM_AUTO自動重復告警
串口
timer =tmr.create()
function serial()
uart.on("data", 4,
function(data)
print("receive from uart:", data)
uart.write(0,data)
end, 0)
end
timer:alarm(2000,tmr.ALARM_AUTO,serial)
連接熱點
timer =tmr.create()
cfg = {}
cfg.ssid = "username" -- wifi username
cfg.pwd = "passwd" -- wifi passwd
wifi.sta.config(cfg)
wifi.sta.connect()
function reconnect()
if wifi.sta.getip() ==nil then
print("disconnect..")
else
timer:stop()
print("connect success")
print(wifi.sta.getip())
end
end
timer:alarm(1000,tmr.ALARM_AUTO,reconnect)
wifi-station
--connect to Access Point (DO NOT save config to flash)
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.save=false
wifi.sta.config(station_cfg)
--connect to Access Point (DO save config to flash)
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.save=true
wifi.sta.config(station_cfg)
--connect to Access Point with specific MAC address (DO save config to flash)
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.bssid="AA:BB:CC:DD:EE:FF"
wifi.sta.config(station_cfg)
--configure station but don't connect to Access point (DO save config to flash)
station_cfg={}
station_cfg.ssid="NODE-AABBCC"
station_cfg.pwd="password"
station_cfg.auto=false
wifi.sta.config(station_cfg)
dht
dht_pin =5
function AcDht()
status, temp, humi, temp_dec, humi_dec = dht.read11(dht_pin)
print("temp is:"..temp,"humi is:"..humi)
end
timer1:alarm(1000,tmr.ALARM_AUTO,AcDht)
ADC
pin =5
function readADC()
val = adc.read(pin)
print("val:"..val)
end
timer1:alarm(1000,tmr.ALARM_AUTO,readADC)
如果ESP8266被配置為使用ADC讀取系統電壓,該函式將始侄訓傳65535,這是硬體和/或SDK的限制
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/251321.html
標籤:嵌入式
上一篇:安卓音頻學習
