ESP8266-Lua开发(NodeMCU)

mtain 2024年11月29日 57次浏览

一、开发环境

文档:
nodemcu固件生成:https://nodemcu-build.com/
nodemcu使用文档:https://nodemcu.readthedocs.io/en/release/search.html

工具:
乐鑫Flash下载工 https://www.espressif.com.cn/zh-hans/support/download/other-tools
Lua开发工具ESPlorer(依赖于java):https://esp8266.ru/esplorer/

安信可开发工具:
https://docs.ai-thinker.com/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B72

二、刷入nodemcu固件

1. 生成固件

https://nodemcu-build.com/中设置所需组件,填写邮箱生成固件,固件下载地址会发送到邮箱

微信截图_20241129145459.png

integer和float两个固件的区别在于,integer无法进行浮点数运算。

2. 刷写固件

微信截图_20241129144027.png

刷写完成
微信截图_20241129144125.png

三、编辑运行代码

使用开发工具ESPlorer,波特率115200,选择esp8266的com口
微信截图_20241129145420.png

输出提示,需要按esp8266的重置按钮

微信截图_20241129145429.png

新建lua文件,写代码运行

print("hello!");

文件命名为init.lua,默认加载执行init.lua

微信截图_20241129145717.png

写入esp8266
image.png

四、连接WiFi

代码

wificonf={}
wificonf.ssid="esp8266"
wificonf.pwd="esp82660123456"
wifi.setmode(wifi.STATION)
wifi.sta.config(wificonf)
wifi.sta.autoconnect(1)
wifi.sta.connect()
 
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    printip = 0
    print("Wifi disconnect!")
end)
 
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
   if printip == 0 then
      print("Wifi connected!")
      print("IP:"..T.IP)
   end
   printip = 1
end)

微信截图_20241129151832.png

参考文章:https://blog.csdn.net/2401_84103060/article/details/138635740

五、HTTP-Server

ESP8266创建一个ap,启动http-server

wifi.setmode(wifi.SOFTAP)

ap_cfg =
{
  ssid="ESP8266",
  pwd="0123456789"
}
wifi.ap.config(ap_cfg)

dhcp_config ={}
dhcp_config.start = "192.168.8.2"
wifi.ap.dhcp.config(dhcp_config)

ap_ip_cfg =
{
    ip="192.168.8.1",
    netmask="255.255.255.0",
    gateway="192.168.8.1"
}
wifi.ap.setip(ap_ip_cfg)
print('AP start...')
print("Config done, IP is "..wifi.ap.getip())


srv=net.createServer(net.TCP) 
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload) 
    print(payload) 
    conn:send("<h1> Hello, NodeMcu.</h1>")
    end) 
end)
           

浏览器访问
image.png

NodeMCU代码笔记

wifi.setmode(wifi.SOFTAP)

ap_cfg =
{
  ssid="ESP8266",
  pwd="helloesp8266"
}
wifi.ap.config(ap_cfg)

dhcp_config ={}
dhcp_config.start = "192.168.8.2"
wifi.ap.dhcp.config(dhcp_config)

ap_ip_cfg =
{
    ip="192.168.8.1",
    netmask="255.255.255.0",
    gateway="192.168.8.1"
}
wifi.ap.setip(ap_ip_cfg)
print('AP start...')
print("Config done, IP is "..wifi.ap.getip())


print("Starting web server...")
srv = net.createServer(net.TCP, 30)
srv:listen(80, function(conn)
    conn:on("receive", function(conn, payload)
        local sLine, sEmpty, method, path, vars = string.match(payload, "([A-Z]+)%s-(.-)%s+HTTP/%d%.%d\r\n(.-)\r\n\r\n(.-)\r\n")
        if method == "GET" then
            if path == "/" then
                local response = "<html><body><h1>Hello, NodeMCU!</h1></body></html>"
                conn:send("HTTP/1.1 200 OK\r\n")
                conn:send("Content-Type: text/html\r\n")
                conn:send("Content-Length: " .. response:len() .. "\r\n\r\n")
                conn:send(response)
            else
                conn:send("HTTP/1.1 404 Not Found\r\n")
                conn:send("Content-Type: text/html\r\n")
                conn:send("Content-Length: 0\r\n\r\n")
            end
        end
        collectgarbage()
    end)
    conn:on("sent", function(conn) conn:close() end)
end)
           
------------------------------------------------------------------------------------------------------------------------------------------------