NodeMCU
使用Lua脚本语言为基础的物联网开源项目。 来自维基百科,自由的百科全书
NodeMCU是一個開源[4]的物聯網平台。 它使用Lua腳本語言編程[4]。該平台基於eLua [5]開源項目,底層使用ESP8266 sdk 0.9.5版本。該平台使用了很多開源項目, 例如 lua-cjson[6], spiffs[7]. NodeMCU包含了可以運行在 esp8266 Wi-Fi SoC芯片之上的固件,以及基於ESP-12模組的硬件。
已隱藏部分未翻譯內容,歡迎參與翻譯。
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.
![]() | 此條目包含指南或教學內容。 (2015年4月2日) |
![]() | 此條目需要精通或熟悉相關主題的編者參與及協助編輯。 (2015年4月2日) |
Pins腳
NodeMCU 提供對 GPIO(通用輸入/輸出)的存取控制,並且引腳映射表是 API 文件的一部分。 [17]
I/O index | ESP8266 pin |
---|---|
0 [*] | GPIO16 |
1 | GPIO5 |
2 | GPIO4 |
3 | GPIO0 |
4 | GPIO2 |
5 | GPIO14 |
6 | GPIO12 |
7 | GPIO13 |
8 | GPIO15 |
9 | GPIO3 |
10 | GPIO1 |
11 | GPIO9 |
12 | GPIO10 |
[*] D0 (GPIO16) 只能用於GPIO讀/寫。它不支援open-drain/interrupt/PWM/I²C 或 1-Wire。
硬體連接
如果使用 NodeMCU devkit, 只需要安裝 CH340G 驅動程式[18], 並且將開發模組使用micro-usb 線連接到電腦。 然後,將波特率設為9600, 並打開序列埠。若使用普通的esp8266模組, 則必須準備usb-ttl 轉接器。
固件燒寫
nodemcu_latest.bin: 0x00000
對於大多數 esp8266 模塊, 直接拉低 GPIO0 引腳的電平,並且重新上電或重啟。
可以使用 nodemcu-flasher(頁面存檔備份,存於網際網路檔案館) 燒寫固件[18]。
特別的,如果你想編譯/構建自己的固件,需要注意以下燒寫地址:
0x00000.bin: 0x00000
0x10000.bin: 0x10000
注意,在燒寫之後,最好執行 file.format()
程式舉例
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
如同arduino般操作硬體
pin = 1
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.HIGH)
print(gpio.read(pin))
-- 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")
-- 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)
-- 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
-- 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")
引用文獻
Wikiwand - on
Seamless Wikipedia browsing. On steroids.