twn39 / code

:memo: 代码笔记,通过 issue 的方式记录日常遇到的问题和学习笔记
13 stars 1 forks source link

NodeMCU lua 连接 wifi #344

Open twn39 opened 3 years ago

twn39 commented 3 years ago

使用 esptool 刷入固件:

esptool.py --chip esp8266 --port /dev/cu.SLAB_USBtoUART --baud 115200 write_flash -fm dio 0x00000 nodemcu-release-20-modules-2021-01-05-12-09-56-float.bin

init.lua

status = wifi.sta.status()
if(status == wifi.STA_FAIL) then
    wifi.setmode(wifi.STATION)
    sta_config = {}
    sta_config.ssid="CMCC-PebQ"
    sta_config.pwd="xxxxxx"
    sta_config.save=true

    wifi.sta.config(sta_config)
else 
    print("---- wifi connect success ----")    
end
twn39 commented 3 years ago

Led 闪烁:

local pin = 4
local led_timer = tmr.create()
local led_status = 0

gpio.mode(pin,gpio.OUTPUT)

led_timer:alarm(1000, tmr.ALARM_AUTO, function()
    if (led_status == 0) then
        gpio.write(pin,gpio.HIGH)
        led_status = 1
    else   
        gpio.write(pin,gpio.LOW)
        led_status = 0
    end
end)