NodeMCU quick start

什麼是NodeMCU?

NodeMCU,是一個開源的物聯網平臺。 它使用Lua腳本語言編程。該平臺基於eLua]開源項目,底層使用ESP8266 sdk 0.9.5版本。該平臺使用了很多開源項目, 例如 lua-cjson, spiffs. NodeMCU包含了可以運行在 esp8266 Wi-Fi SoC芯片之上的固件,以及基於ESP-12模組的硬件。

它以lua語言爲基礎,同時提供了封裝esp8266硬件操作的高級API,可以讓開發者以類似於arduino的方式與底層硬件打交道,使軟件開發人員輕鬆操作硬件設備;同時NodeMcu還提供了事件驅動型的網絡API,Nodejs風格的編程方式更是讓互聯網開發人員如魚得水。

目前NodeMcu推出的第一代開發板NodeMcu Dev Kit,對安信可科技推出的esp8266封裝模塊ESP12進行擴展,包括了:

  • D1~D10:均可複用爲GPIO,PWM,I2C,1-Wire
  • A0:1路ADC
  • USB供電
  • USB轉串口調試接口

可用RAM爲20Kbyte;目前採用512K FLash,用戶可用存儲空間150Kbyte。同時使用NodeMcu Flash Programmer自動燒寫固件(詳見後續文章)。

低成本的ESP8266硬件、高度抽象NodeMcu API將助推衆多開發者們的創造性想法,讓您的原型開發快速推進!

軟硬件開源,lua高級語言編程,支持wifi,TCP/IP,UDP,HTTP,MQTT等協議,開發板39元


History

NodeMCU is started after ESP8266 come out. In December 30, 2013, Espressif systems begin production of ESP8266.[9] ESP8266 is an Wi-Fi SoC and integrated with LX106 core, widely used in IoT applications(See related projects[10][11][12]). In 13 Oct 2014, Hong committed first file of nodemcu-firmware to github,[13] 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,[14] 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,[15] 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[16] to NodeMCU project,[17] and NodeMCU can drive LCD, Screen, OLED, even VGA display module easily.

相關網站

硬件購買:http://item.taobao.com/item.htm?spm=a230r.1.14.1.ogDtJv&id=43189493943&ns=1&abbucket=9#detail

維基百科:http://en.wikipedia.org/wiki/NodeMCU

官網:http://nodemcu.com/index_cn.html

論壇:http://bbs.nodemcu.com/

源代碼:https://github.com/nodemcu

固件下載:http://bbs.nodemcu.com/t/nodemcu-firmware-download-build-20150318-new-location/27

中文API素查手冊CHM格式:http://bbs.nodemcu.com/t/nodemcu-de-apisu-cha-shou-ce/74

NodeMCU硬件介紹:

原理圖,硬件接口定義

http://bbs.nodemcu.com/t/nodemcu-devkit-v0-dot-9-ying-jian-shuo-ming-shu-yin-jiao-shuo-ming-yu-zhu-yi-shi-xiang/49/1

NodeMCU上手

NodeMcu介紹:(一) 概述

http://bbs.nodemcu.com/t/nodemcujie-shao-gai-shu/25

NodeMcu介紹:(二)固件燒寫

http://bbs.nodemcu.com/t/nodemcu/22/8

NodeMcu介紹:(三)啓動文件init.lua

http://bbs.nodemcu.com/t/nodemcujie-shao-san-qi-dong-wen-jian-init-dot-lua/24

NodeMcu介紹:(四)下載*.lua文件

http://bbs.nodemcu.com/t/nodemcu-lua/26

NodeMcu三小時入門 (介紹如何鏈接yeelink)

http://bbs.nodemcu.com/t/nodemcusan-xiao-shi-ru-men/104

示例代碼

Connect to an AP

    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

Control GPIO

    pin = 1
    gpio.mode(pin,gpio.OUTPUT)
    gpio.write(pin,gpio.HIGH)
    print(gpio.read(pin))

HTTP request

    -- 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 server

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

Connect to 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 client and server

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

NodeMCU進階

NodeMCU硬件基於ESP8266,軟件是基於elua。如果希望改進NodeMCU固件,需要掌握ESP8266和elua

ESP8266相關資源

ESP8266 Quick Start

http://www.benlo.com/esp8266/esp8266QuickStart.html

論壇

http://www.esp8266.com/

 Getting Started with ESP8266  on seeedstudio

http://www.seeedstudio.com/blog/2014/09/11/getting-started-with-esp8266/

elua相關資源

http://www.eluaproject.net/

官方網站的quickstart 在交叉編譯的說明上存在問題,我在親自實驗後寫了說明,請參考我的博客:

學習elua(一)——簡介
http://blog.csdn.net/coolwaterld/article/details/39007115

 學習elua(二)--編譯和燒寫

http://blog.csdn.net/coolwaterld/article/details/39007587

 學習elua(三)--定製elua的編譯選項

http://blog.csdn.net/coolwaterld/article/details/39049239

學習elua(四)--使用elua 

http://blog.csdn.net/coolwaterld/article/details/39050387

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章