【公网远程】网页控制nodemcu的LED

id = '4567'-自己设置自己的id替换4567 ssid = 'wifi名' ssidpwd = 'wifi密码' wifi.setmode(wifi.STATION) wifi.sta.config(ssid,ssidpwd)--set your ap info !!!!!! wifi.sta.autoconnect(1) led1 = 3 led2 = 4 gpio.mode(led1, gpio.OUTPUT) gpio.mode(led2, gpio.OUTPUT) function startServer() conn=net.createConnection(net.TCP, 0) conn:on("connection", function(conn, c) conn:send(id) tmr.alarm(2, 30000, 1, function() conn:send('') end) end) conn:on("receive", function(conn, pl) local _, _, method, path, vars = string.find(pl, "([A-Z]+) (.+)?(.+) HTTP"); local buf = ""; local _GET = {} if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end buf = buf.." ESP8266 Web Server"; buf = buf.."GPIO0
"; buf = buf.."GPIO2
"..'Raw Data To MCU:

'..pl; local _on,_off = "","" if(_GET.pin == "ON1")then gpio.write(led1, gpio.LOW); print('on led1') elseif(_GET.pin == "OFF1")then gpio.write(led1, gpio.HIGH); print('off led1') elseif(_GET.pin == "ON2")then gpio.write(led2, gpio.LOW); print('on led2') elseif(_GET.pin == "OFF2")then gpio.write(led2, gpio.HIGH); print('off led2') end conn:send(buf); collectgarbage(); end) conn:connect(8001,"eiot.club") end tmr.alarm(1, 1000, 1, function() if wifi.sta.getip()==nil then print("Connect AP, Waiting...") else tmr.stop(1) startServer()end end)

修改为自己的程序:1,2,3行分别改为自己的id(自定义),mcu可连接公网的路由器ssid和密码
如果id为4567,访问页面地址为:http://eiot.club/proxy/4567/index.html
上述页面已经停止分享,服务器需要自己部署:https://github.com/IoTServ/McuNode-server
其中4567可替换为自定义id,index.html可换为任意字符串,不影响连接,mcu可以获取此参数,建议为模拟文件名,至此,你就可以从公网随意访问nodemcu页面啦!(测试阶段,当前只支持Get方法通过url参数传递参数)


【公网远程】网页控制nodemcu的LED
文章图片


分别点击四个按钮,nodemcu的串口打印效果:
【公网远程】网页控制nodemcu的LED
文章图片


【【公网远程】网页控制nodemcu的LED】【公网远程】网页控制nodemcu的LED
文章图片


    推荐阅读