NodeMCU

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書
NodeMCU
NodeMCU DEVKIT 1.0 BETA
研發商ESP8266開源社區
類型片上系統
作業系統XTOS
電源USB
中央處理器ESP8266[1](LX106[2])
記憶體20kBytes
硬碟儲存空間4MBytes[3]
網站http://www.nodemcu.com
NodeMCU DEVKIT 1.0 BETA 背面

NodeMCU是一個開源[4]物聯網平台。 它使用Lua手稿語言編程[4]。該平台基於eLua [5]開源專案,底層使用ESP8266 sdk 0.9.5版本。該平台使用了很多開源專案, 例如 lua-cjson[6], spiffs[7]. NodeMCU包含了可以執行在 esp8266 Wi-Fi SoC晶片之上的韌體,以及基於ESP-12模組的硬件。

韌體燒寫[編輯]

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

如同arduino般操作硬體[編輯]

    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")

參照文獻[編輯]

  1. ^ Kumar, Abhijeet, and Apoorva Sharma. "Internet of Life (IOL)." (2015). ISBN 978-93-5156-328-0
  2. ^ Brian Benchoff. An SDK for the ESP8266 Wi-Fi chip. Hackaday. 
  3. ^ Vowstar. NodeMCU Devkit. Github. NodeMCU Team. [2 April 2015]. (原始內容存檔於2015-08-15). 
  4. ^ 4.0 4.1 存档副本. [2015-04-01]. (原始內容存檔於2015-08-12). 
  5. ^ 存档副本. [2015-04-01]. (原始內容存檔於2010-12-27). 
  6. ^ 存档副本. [2015-04-01]. (原始內容存檔於2015-12-31). 
  7. ^ 存档副本. [2015-04-01]. (原始內容存檔於2015-11-03). 
  8. ^ Espressif system. IoT Wi-Fi 802.11b/g/n integrated SoC implementation of volume production. 中國上海訊. December 30, 2013 [2 April 2015]. (原始內容存檔於2015-04-02). 
  9. ^ 參照錯誤:沒有為名為the button的參考文獻提供內容
  10. ^ 參照錯誤:沒有為名為node usb的參考文獻提供內容
  11. ^ 參照錯誤:沒有為名為ijwatch的參考文獻提供內容
  12. ^ Hong. First commit of NodeMCU Firmware. Github. [2 April 2015]. (原始內容存檔於2021-08-15). 
  13. ^ Huang R. Initial design of NodeMCU devkit. Github. [2 April 2015]. (原始內容存檔於2021-08-15). 
  14. ^ Tuan PM. MQTT client library for ESP8266. Github. [2 April 2015]. (原始內容存檔於2017-01-02). 
  15. ^ Olikraus; Daniel Sittig. Universal Graphics Library for 8 Bit Embedded Systems. Google code. [2 April 2015]. (原始內容存檔於2015-04-06). 
  16. ^ Devsaurus. U8glib for esp8266. Github. [2 April 2015]. (原始內容存檔於2021-08-15). 
  17. ^ 17.0 17.1 存档副本. [2015-04-01]. (原始內容存檔於2015-04-02).