NodeMCU
此条目包含指南或教学内容。 (2015年4月2日) |
此条目需要精通或熟悉相关主题的编者参与及协助编辑。 (2015年4月2日) |
NodeMCU是一个开源[4]的物联网平台。 它使用Lua脚本语言编程[4]。该平台基于eLua [5]开源项目,底层使用ESP8266 sdk 0.9.5版本。该平台使用了很多开源项目, 例如 lua-cjson[6], spiffs[7]. NodeMCU包含了可以运行在 esp8266 Wi-Fi SoC芯片之上的固件,以及基于ESP-12模组的硬件。
研发商 | ESP8266开源社区 |
---|---|
类型 | 片上系统 |
作业系统 | XTOS |
电源 | USB |
中央处理器 | ESP8266[1](LX106[2]) |
记忆体 | 20kBytes |
硬盘储存空间 | 4MBytes[3] |
网站 | http://www.nodemcu.com |
History
编辑NodeMCU is started after ESP8266 come out. In December 30, 2013, Espressif systems begin production of ESP8266.[8] ESP8266 is an Wi-Fi SoC and integrated with LX106 core, widely used in IoT applications(See related projects[9][10][11]). In 13 Oct 2014, Hong committed first file of nodemcu-firmware to github,[12] NodeMCU project started. And then more and more developers from ESP8266 opensource community join in NodeMCU developer team. On 1 Dec 2014, Huang R commit the gerber file of an ESP8266 board, then NodeMCU project have the first open-hardware which named devkit 1.0,[13] thus NodeMCU is not only a firmware, it becomes a platform. In 31 Dec 2014, Tuan PM port MQTT client library from Contiki to ESP8266 SoC platform,[14] and commit to NodeMCU project, then NodeMCU can support MQTT IoT protocol, using Lua access MQTT broker, it is an important update of firmware. Another important update is in 30 Jan 2015, Devsaurus port u8glib[15] to NodeMCU project,[16] and NodeMCU can drive LCD, Screen, OLED, even VGA display module easily.
固件烧写
编辑nodemcu_latest.bin: 0x00000
对于大多数 esp8266 模块, 直接拉低 GPIO0 引脚的电平,并且重新上电或重启。
可以使用 nodemcu-flasher(页面存档备份,存于互联网档案馆) 烧写固件[17]。
特别的,如果你想编译/构建自己的固件,需要注意以下烧写地址:
0x00000.bin: 0x00000
0x10000.bin: 0x10000
注意,在烧写之后,最好执行 file.format()
硬体连接
编辑如果使用 NodeMCU devkit, 只需要安装 CH340G 驱动程式[17], 并且将开发模组使用micro-usb 线连接到电脑。 然后,将波特率设为9600, 并打开序列埠。若使用普通的esp8266模组, 则必须准备usb-ttl 转接器。
程式举例
编辑连接Wi-Fi热点
编辑 ip = wifi.sta.getip()
print(ip)
--nil
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
ip = wifi.sta.getip()
print(ip)
--192.168.18.110
pin = 1
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.HIGH)
print(gpio.read(pin))
使用 nodejs 风格网络编程
编辑 -- A simple http client
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end )
conn:connect(80,"115.239.210.27")
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
简易的HTTP伺服器
编辑 -- A simple http server
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)
conn:on("sent",function(conn) conn:close() end)
end)
连接 MQTT Broker
编辑-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- for secure: m:connect("192.168.11.118", 1880, 1)
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close();
-- you can call m:connect again
UDP 客户端与伺服器
编辑-- a udp server
s=net.createServer(net.UDP)
s:on("receive",function(s,c) print(c) end)
s:listen(5683)
-- a udp client
cu=net.createConnection(net.UDP)
cu:on("receive",function(cu,c) print(c) end)
cu:connect(5683,"192.168.18.101")
cu:send("hello")
引用文献
编辑- ^ Kumar, Abhijeet, and Apoorva Sharma. "Internet of Life (IOL)." (2015). ISBN 978-93-5156-328-0
- ^ Brian Benchoff. An SDK for the ESP8266 Wi-Fi chip. Hackaday.
- ^ Vowstar. NodeMCU Devkit. Github. NodeMCU Team. [2 April 2015]. (原始内容存档于2015-08-15).
- ^ 4.0 4.1 存档副本. [2015-04-01]. (原始内容存档于2015-08-12).
- ^ 存档副本. [2015-04-01]. (原始内容存档于2010-12-27).
- ^ 存档副本. [2015-04-01]. (原始内容存档于2015-12-31).
- ^ 存档副本. [2015-04-01]. (原始内容存档于2015-11-03).
- ^ Espressif system. IoT Wi-Fi 802.11b/g/n integrated SoC implementation of volume production. 中国上海讯. December 30, 2013 [2 April 2015]. (原始内容存档于2015-04-02).
- ^ 引用错误:没有为名为
node usb
的参考文献提供内容 - ^ 引用错误:没有为名为
ijwatch
的参考文献提供内容 - ^ Hong. First commit of NodeMCU Firmware. Github. [2 April 2015]. (原始内容存档于2021-08-15).
- ^ Huang R. Initial design of NodeMCU devkit. Github. [2 April 2015]. (原始内容存档于2021-08-15).
- ^ Tuan PM. MQTT client library for ESP8266. Github. [2 April 2015]. (原始内容存档于2017-01-02).
- ^ Olikraus; Daniel Sittig. Universal Graphics Library for 8 Bit Embedded Systems. Google code. [2 April 2015]. (原始内容存档于2015-04-06).
- ^ Devsaurus. U8glib for esp8266. Github. [2 April 2015]. (原始内容存档于2021-08-15).
- ^ 17.0 17.1 存档副本. [2015-04-01]. (原始内容存档于2015-04-02).