This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
nodemcu [2015/04/01 17:27] vowstar Add description. |
nodemcu [2015/06/21 22:36] terrye [NodeMCU] fix link to correct dokuwiki format |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== NodeMCU ====== | ====== NodeMCU ====== | ||
+ | |||
+ | * See the [[nodemcu-unofficial-faq|nodeMCU Unofficial FAQ]] for answers to your "Frequently Asked Questions". | ||
+ | |||
+ | ===== Hardware ===== | ||
+ | NodeMCU devkit is a development kit for NodeMCU firmware.\\ It will make NodeMCU more easy. With a micro USB cable, you can connect NodeMCU devkit to your laptop and flash it without any trouble, just like Arduino.\\ It is an open hardware, with ESP-12 core with 32Mbits(4MBytes) flash. | ||
+ | |||
+ | ==== Devkit 0.8 ==== | ||
+ | It is the original design of NodeMCU devkit. Very old and never sell on shop. It can't automatic flash firmware. | ||
+ | |||
+ | {{:devkit_0.8.jpg?600|}} | ||
+ | |||
+ | ==== Devkit 0.9 ==== | ||
+ | It is the second design of NodeMCU devkit. It uses CH340G as UART bridge, and can flash firmware automatically by using nodemcu-flasher. | ||
+ | |||
+ | {{:devkit_0.9.jpg?300|}} | ||
+ | {{:devkit_0.9_B.jpg?300|}} | ||
+ | |||
+ | ==== Devkit 1.0 ==== | ||
+ | It is the 5th design of NodeMCU devkit. It uses CP2102 as UART bridge, and can flash firmware automatically by using nodemcu-flasher. It support apple's MAC OS. | ||
+ | |||
+ | {{:devkit_1.0.jpg?600|}} | ||
===== Introduction ===== | ===== Introduction ===== | ||
Line 41: | Line 62: | ||
=== Connect to your ap === | === Connect to your ap === | ||
- | <source lang="lua"> | + | <code lang="lua"> |
- | ip = wifi.sta.getip() | + | ip = wifi.sta.getip() |
- | print(ip) | + | print(ip) |
- | --nil | + | -- nil |
- | wifi.setmode(wifi.STATION) | + | wifi.setmode(wifi.STATION) |
- | wifi.sta.config("SSID","password") | + | wifi.sta.config("SSID","password") |
- | ip = wifi.sta.getip() | + | ip = wifi.sta.getip() |
- | print(ip) | + | print(ip) |
- | --192.168.18.110 | + | -- 192.168.18.110 |
- | </source> | + | </code> |
=== Manipulate hardware like a arduino === | === Manipulate hardware like a arduino === | ||
- | <source lang="lua"> | + | <code lang="lua"> |
- | pin = 1 | + | pin = 1 |
- | gpio.mode(pin,gpio.OUTPUT) | + | gpio.mode(pin,gpio.OUTPUT) |
- | gpio.write(pin,gpio.HIGH) | + | gpio.write(pin,gpio.HIGH) |
- | print(gpio.read(pin)) | + | print(gpio.read(pin)) |
- | </source> | + | </code> |
=== Write network application in nodejs style === | === Write network application in nodejs style === | ||
- | <source lang="lua"> | + | <code lang="lua"> |
- | --A simple http client | + | -- A simple http client |
- | conn=net.createConnection(net.TCP, 0) | + | conn=net.createConnection(net.TCP, 0) |
- | conn:on("receive", function(conn, payload) print(payload) end ) | + | conn:on("receive", function(conn, payload) print(payload) end ) |
- | conn:connect(80,"115.239.210.27") | + | conn:connect(80,"115.239.210.27") |
- | conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n" | + | conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n" |
- | .."Connection: keep-alive\r\nAccept: */*\r\n\r\n") | + | .."Connection: keep-alive\r\nAccept: */*\r\n\r\n") |
- | </source> | + | </code> |
=== Or a simple http server === | === Or a simple http server === | ||
- | <source lang="lua"> | + | <code lang="lua"> |
- | --A simple http server | + | -- A simple http server |
- | srv=net.createServer(net.TCP) | + | srv=net.createServer(net.TCP) |
- | srv:listen(80,function(conn) | + | srv:listen(80,function(conn) |
- | conn:on("receive",function(conn,payload) | + | conn:on("receive",function(conn,payload) |
- | print(payload) | + | print(payload) |
- | conn:send("<h1> Hello, NodeMcu.</h1>") | + | conn:send("<h1> Hello, NodeMcu.</h1>") |
- | end) | + | end) |
- | conn:on("sent",function(conn) conn:close() end) | + | conn:on("sent",function(conn) conn:close() end) |
- | end) | + | end) |
- | </source> | + | </code> |
=== Connect to MQTT Broker === | === Connect to MQTT Broker === | ||
- | <source lang="lua"> | + | <code lang="lua"> |
- | --init mqtt client with keepalive timer 120sec | + | -- init mqtt client with keepalive timer 120sec |
- | m = mqtt.Client("clientid", 120, "user", "password") | + | m = mqtt.Client("clientid", 120, "user", "password") |
- | --setup Last Will and Testament (optional) | + | -- setup Last Will and Testament (optional) |
- | --Broker will publish a message with qos = 0, retain = 0, data = "offline" | + | -- Broker will publish a message with qos = 0, retain = 0, data = "offline" |
- | --to topic "/lwt" if client don't send keepalive packet | + | -- to topic "/lwt" if client don't send keepalive packet |
- | m:lwt("/lwt", "offline", 0, 0) | + | m:lwt("/lwt", "offline", 0, 0) |
- | m:on("connect", function(con) print ("connected") end) | + | m:on("connect", function(con) print ("connected") end) |
- | m:on("offline", function(con) print ("offline") end) | + | m:on("offline", function(con) print ("offline") end) |
- | --on publish message receive event | + | -- on publish message receive event |
- | m:on("message", function(conn, topic, data) | + | m:on("message", function(conn, topic, data) |
- | print(topic .. ":" ) | + | print(topic .. ":" ) |
- | if data ~= nil then | + | if data ~= nil then |
- | print(data) | + | print(data) |
- | end | + | end |
- | end) | + | end) |
- | --for secure: m:connect("192.168.11.118", 1880, 1) | + | -- for secure: m:connect("192.168.11.118", 1880, 1) |
- | m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end) | + | m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end) |
- | --subscribe topic with qos = 0 | + | -- subscribe topic with qos = 0 |
- | m:subscribe("/topic",0, function(conn) print("subscribe success") end) | + | 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) | + | -- 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) | + | -- 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 | + | -- publish a message with data = hello, QoS = 0, retain = 0 |
- | m:publish("/topic","hello",0,0, function(conn) print("sent") end) | + | m:publish("/topic","hello",0,0, function(conn) print("sent") end) |
- | m:close(); | + | m:close(); |
- | --you can call m:connect again | + | -- you can call m:connect again |
- | </source> | + | </code> |
=== UDP client and server === | === UDP client and server === | ||