nodemcu教程

http://pastebin.com/
http://paste.ubuntu.com/

https://nodemcu.readthedocs.io/en/dev/en/modules/mqtt/

http://moxd.io/2015/10/public-mqtt-brokers/

http://www.hivemq.com/demos/websocket-client/

http://www.cnblogs.com/xiaowuyi/p/4850081.html

http://blog.csdn.net/leytton/article/details/51647663

http://nodemcu-build.com/

http://www.cnblogs.com/wangzexi/p/5696925.html

人体红外传感器控制led灯

led1=2
led2=1
gpio.mode(led1, gpio.INPUT);
print(gpio.read(led1));
tmr.alarm(1, 1000, 1, function()   
if gpio.read(led1)==1 then
         gpio.write(led2, gpio.HIGH);
else
         gpio.write(led2,gpio.LOW);             
end
tmr.stop(0)  
end) 

远程web控制led灯 http://blog.163.com/allegro_tyc/blog/static/3374376820166835845450/

wifi.setmode(wifi.STATION)  
wifi.sta.config("------","------")  
wifi.sta.connect()
print(wifi.sta.getip())  
led1 = 0  --板载led灯
led2 = 3  --正极接,负极接gnd
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=OFF1\"><button>ON</button></a> <a href=\"?pin=ON1\"><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.HIGH);  
        elseif(_GET.pin == "OFF2")then  
              gpio.write(led2, gpio.LOW);  
        end  
        client:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1> Hello, NodeMCU.</h1>"..buf)
        --client:send(buf);  
        client:close();  
        collectgarbage();  
    end)  
end) 

1、Connect Wifi

Before we make a http-get request, connecting Wifi must be done.The demo gave by official website is as Code Block-1. See http://nodemcu.com/index_cn.html

2、PHP Webserver

By using php,we can get the request arguments by _GET['xxx'] directly. Demo is as  Code Block-2.

3、NodeMcu Request

Demo is as Code Block-3. We used the cjson module to parse the Json data returned by php server and iterate over and print  the elements.

4、Chinese Garbled Solution

Since the url of NodeMcu http-get request supports Chinese badly,we can use the Base64 encoded url. For example,  the base64 code of '你好' is '5L2g5aW9Cg==', then the url will be 'login.php?name=5L2g5aW9Cg=='. The php server should decode the data such as the Code Block-4.

————————————————————————————————————————

上面是练英语写作的,欢迎吐槽吐舌头。中文如下:

1、连接 Wifi

在进行Http 的GET请求前,我们需要连接Wifi.官方给出了一个例子,代码如下

连接Wifi代码Code Block-1:

[javascript] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. print(wifi.sta.getip())  
  2. --nil  
  3. wifi.setmode(wifi.STATION)  
  4. wifi.sta.config("SSID","password")  
  5. print(wifi.sta.getip())  
  6. --192.168.18.110  

参考 http://nodemcu.com/index_cn.html

2、PHP服务端

PHP服务端通过_GET['xxx']即可获取到GET请求参数。代码如下:

PHP服务端代码Code Block-2:

[php] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. <?php  
  2. $user_name = isset($_GET['name'])?$_GET['name']:null;  
  3. $user_pwd = isset($_GET['pwd'])?$_GET['pwd']:null;  
  4. $message = array(  
  5.        "type"=>0,  
  6.        "name"=>$user_name,
  7.     "pwd"=>$user_pwd
  8. );  
  9. echo json_encode($message);   
  10. ?>  

3、NodeMCU请求

代码如下,我们通过cjson模块来解析PHP服务端返回的Json数据并且遍历输出。

NodeMcu进行php.get请求代码Code Block-2:

[javascript] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. http.get("http://192.168.1.106/login.php?name=aa&pwd=11", nil, function(code, data)  
  2.     if (code < 0) then  
  3.       print("HTTP request failed")  
  4.     else  
  5.       print(code,data)  
  6.       t = cjson.decode(data)  
  7.       for k,v in pairs(t) do print(k,v) end  
  8.     end  
  9. end)  

4、中文参数乱码解决

NodeMCU对GET请求的URL参数中文支持并不好,我们可以使用Base64编码后的参数。比如说"你好"的base64编码为 ”5L2g5aW9Cg==“,那么url则为 “login.php?name=5L2g5aW9Cg==”,PHP服务端对base64参数的解析代码如下:

PHP解析base64编码参数Code Block-4:

[php] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. if(preg_match("/==$/"$user_name)){  
  2.     $user_name=base64_decode($user_name);  
    A+
发布日期:2016年07月12日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: