本帖最后由 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/
- --------------------------------------------------------
- print('Setting up WIFI...')
- --------------------------------------------------------
- wifi.startsmart()
- print("wifi.smart = ".."wifi.startsmart()" )
- print("wifi.MAC = "..wifi.sta.getmac() )
- print("wifi.STATION = "..wifi.setmode(wifi.STATION) )
- wifi.sta.config('xxxx', 'xxxx')
- print("wifi.MMMM = ".."wifi.sta.config('xxxx', 'xxxx')" )
- wifi.sta.autoconnect(1)
- print("wifi.Connect = ".."wifi.sta.autoconnect(1)" )
- print("wifi.IP = "..wifi.sta.getip() )
- --------------------------------------------------------
复制代码
- --------------------------------------------------------
- local httpA = "GET http://www.bbkgps.com/"
- local httpB = "t.php"
- local httpC = " HTTP/1.1\r\n"
- local httpD = "Host: www.bbkgps.com\r\n"
- local httpE = "Connection: close\r\n"
- local httpF = "Accept: */*\r\n\r\n"
- --print(httpA..httpB..httpC..httpD..httpE..httpF..httpG)
- --------------------------------------------------------
- conn=net.createConnection(net.TCP, false)
- conn:on("receive",
- function(conn, pl)
- print(pl)
- end)
- conn:connect(80,"112.124.186.245")
- conn:send(httpA..httpB..httpC..httpD..httpE..httpF)
- --------------------------------------------------------
复制代码
- --------------------------------------------------------
- -- print ap list
- function listap(t)
- for k,v in pairs(t) do
- print(k.."\t\t:\t\t"..v.."\t\t"..string.gsub(v, ',', '\t'))
- end
- end
- wifi.sta.getap(listap)
- --------------------------------------------------------
复制代码
点亮板子上的蓝牙灯
- wifi.setmode(wifi.STATION)
- wifi.sta.config("xxxx","xxxx")
- print(wifi.sta.getip())
- led1 = 3
- led2 = 4
- gpio.mode(led1, gpio.OUTPUT)
- gpio.mode(led2, gpio.OUTPUT)
- srv=net.createServer(net.TCP)
- srv:listen(80,function(conn)
- conn:on("receive", function(client,request)
- local buf = "";
- local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
- if(method == nil)then
- _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
- end
- local _GET = {}
- if (vars ~= nil)then
- for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
- _GET[k] = v
- end
- end
- buf = buf.."<h1> ESP8266 Web Server</h1>";
- buf = buf.."<p>GPIO0 <a href="?pin=ON1"><button>ON</button></a> <a href="?pin=OFF1"><button>OFF</button></a></p>";
- buf = buf.."<p>GPIO2 <a href="?pin=ON2"><button>ON</button></a> <a href="?pin=OFF2"><button>OFF</button></a></p>";
- local _on,_off = "",""
- if(_GET.pin == "ON1")then
- gpio.write(led1, gpio.HIGH);
- elseif(_GET.pin == "OFF1")then
- gpio.write(led1, gpio.LOW);
- elseif(_GET.pin == "ON2")then
- gpio.write(led2, gpio.LOW);
- elseif(_GET.pin == "OFF2")then
- gpio.write(led2, gpio.HIGH);
- end
- client:send(buf);
- client:close();
- collectgarbage();
- end)
- end)
复制代码 |