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

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息