极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13786|回复: 2

NodeMCU资源帖,基础代码

[复制链接]
发表于 2016-12-10 13:13:50 | 显示全部楼层 |阅读模式
本帖最后由 wetnt 于 2016-12-10 13:19 编辑

NodeMCU资源帖




NodeMCU固件  http://docs.espush.cn/nodemcu.htm

UART Module   http://nodemcu.readthedocs.io/en/master/en/modules/uart/#uart-module

nodeMcu API说明   https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn#wifistartsmart

Lua中的string库     http://www.jb51.net/article/57613.htm

WiFi Module      http://nodemcu.readthedocs.io/en/dev/en/modules/wifi/


  1. --------------------------------------------------------
  2. print('Setting up WIFI...')
  3. --------------------------------------------------------
  4. wifi.startsmart()
  5. print("wifi.smart   = ".."wifi.startsmart()"                        )
  6. print("wifi.MAC     = "..wifi.sta.getmac()                          )
  7. print("wifi.STATION = "..wifi.setmode(wifi.STATION)                 )
  8. wifi.sta.config('xxxx', 'xxxx')
  9. print("wifi.MMMM    = ".."wifi.sta.config('xxxx', 'xxxx')" )
  10. wifi.sta.autoconnect(1)
  11. print("wifi.Connect = ".."wifi.sta.autoconnect(1)"                  )
  12. print("wifi.IP      = "..wifi.sta.getip()                           )
  13. --------------------------------------------------------
复制代码


  1. --------------------------------------------------------
  2. local httpA = "GET http://www.bbkgps.com/"
  3. local httpB = "t.php"
  4. local httpC = " HTTP/1.1\r\n"
  5. local httpD = "Host: www.bbkgps.com\r\n"
  6. local httpE = "Connection: close\r\n"
  7. local httpF = "Accept: */*\r\n\r\n"
  8. --print(httpA..httpB..httpC..httpD..httpE..httpF..httpG)
  9. --------------------------------------------------------
  10. conn=net.createConnection(net.TCP, false)
  11. conn:on("receive",
  12.     function(conn, pl)
  13.         print(pl)
  14.     end)
  15. conn:connect(80,"112.124.186.245")
  16. conn:send(httpA..httpB..httpC..httpD..httpE..httpF)
  17. --------------------------------------------------------
复制代码


  1. --------------------------------------------------------
  2. -- print ap list
  3. function listap(t)
  4.   for k,v in pairs(t) do
  5.     print(k.."\t\t:\t\t"..v.."\t\t"..string.gsub(v, ',', '\t'))
  6.   end
  7. end
  8. wifi.sta.getap(listap)
  9. --------------------------------------------------------
复制代码




点亮板子上的蓝牙灯

  1. wifi.setmode(wifi.STATION)  
  2. wifi.sta.config("xxxx","xxxx")  
  3. print(wifi.sta.getip())  
  4. led1 = 3  
  5. led2 = 4  
  6. gpio.mode(led1, gpio.OUTPUT)  
  7. gpio.mode(led2, gpio.OUTPUT)  
  8. srv=net.createServer(net.TCP)  
  9. srv:listen(80,function(conn)  
  10.     conn:on("receive", function(client,request)  
  11.         local buf = "";  
  12.         local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");  
  13.         if(method == nil)then  
  14.             _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");  
  15.         end  
  16.         local _GET = {}  
  17.         if (vars ~= nil)then  
  18.             for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do  
  19.                 _GET[k] = v  
  20.             end  
  21.         end  
  22.         buf = buf.."<h1> ESP8266 Web Server</h1>";  
  23.         buf = buf.."<p>GPIO0 <a href="?pin=ON1"><button>ON</button></a> <a href="?pin=OFF1"><button>OFF</button></a></p>";  
  24.         buf = buf.."<p>GPIO2 <a href="?pin=ON2"><button>ON</button></a> <a href="?pin=OFF2"><button>OFF</button></a></p>";  
  25.         local _on,_off = "",""  
  26.         if(_GET.pin == "ON1")then  
  27.               gpio.write(led1, gpio.HIGH);  
  28.         elseif(_GET.pin == "OFF1")then  
  29.               gpio.write(led1, gpio.LOW);  
  30.         elseif(_GET.pin == "ON2")then  
  31.               gpio.write(led2, gpio.LOW);  
  32.         elseif(_GET.pin == "OFF2")then  
  33.               gpio.write(led2, gpio.HIGH);  
  34.         end  
  35.         client:send(buf);  
  36.         client:close();  
  37.         collectgarbage();  
  38.     end)  
  39. end)
复制代码
回复

使用道具 举报

发表于 2017-1-4 19:35:56 | 显示全部楼层
经过实战,使用Lua在NodeMCU上编程,发现不稳定,容易死机、重启。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 16:26 , Processed in 0.036638 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表