esp8266--nodemcu--lua

esp8266--nodemcu--lua

開發主要是兩個網站:node

  1. nodemcu lua文檔NodeMCU Documentation
  2. nodemcu 固件編譯nodemcu-build

GPIO

pin=4
gpio.mode(pin,gpio.OUTPUT) 
--開啓pin4--
gpio.write(pin,gpio.LOW)
--置端口輸出低電位,燈亮--
gpio.write(pin,gpio.HIGH) 
--置端口輸出高電位,燈滅--

基本語法

編譯時不能有中文函數

單行註釋 --網站

多行註釋ui

--[[
 多行註釋
 多行註釋
 --]]
--函數
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)

定時器模式lua

  • tmr.ALARM_SINGLE 一次性警報(不須要調用unregister())
  • tmr.ALARM_SEMI ALARM_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的限制code

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息