NodeMCU quick start

柴晔
2023-12-01

什么是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

 类似资料:

相关阅读

相关文章

相关问答