终端设备用的是ESP8266
运行固件为NodeMCU,即嵌入式Lua环境
服务端基于PHP。简单的获取后保存上传的数据
演示代码:
服务端
- <html>
- <head>
- <head>
- <title>数据收集器</title>
- <meta charset="utf-8">
- </head>
- </head>
- <body>
- <?php
- $user_name = isset($_GET['name'])?$_GET['name']:null;
- $user_pwd = isset($_GET['pwd'])?$_GET['pwd']:null;
- $message = array(
- "type" => 0,
- "data" => "name:".$user_name." pwd:".$user_pwd
- );
- echo json_encode($message);
- ?>
- <?php
- $file=fopen("test.csv","a+");
- fwrite($file, $_GET["name"]);
- fwrite($file, ",");
- fwrite($file, $_GET["pwd"]);
- fwrite($file, ",");
- fwrite($file, "\n");
- ?>
- </body>
- </html>
改程序是一个PHP格式的网页文件,在网页框架内包含了2个php代码段,第一个获取get提交上来的两个数据,name和pwd
第二个PHP段是保存这两个变量传送来的数据,写入到一个CSV文件里去。
————————————————————————————————————————————————————————————————————————
终端两个代码文件,一个是初始化联网用的init.lua,一个是test.lua
- --init.lua
- print("set up wifi mode")
- wifi.setmode(wifi.STATION)
- sta_cfg={}
- sta_cfg.ssid="360WiFi-SJB"
- sta_cfg.pwd="shejibu123"
- wifi.sta.config(sta_cfg)
- wifi.sta.connect()
- print("IP unavaiable, Waiting...")
- tmr.alarm(1, 1000, 1, function()
- if wifi.sta.getip()~= nil then
- tmr.stop(1)
- print("Config done, IP is "..wifi.sta.getip())
- dofile("test.lua")
- end
- end)
其实这个文件可以保持不变,只需要修改你的WIFI账号密码就行了,另外就是下面要跳转到什么文件里。这个文件名可以根据实际修改
终端我设定的是定时提交一组数据
- tmr.alarm(1, 10000, 1, function()
- http.get("http://192.168.1.229:8088/test.php?name=Hello&pwd=World", nil, function(code, data)
- if (code < 0) then
- print("HTTP request failed")
- else
- print(code,data)
- end
- end)
- print("----------------------------------------------\n")
- end)
所以我建立了个定时器函数,定时为10秒中断一次,然后回调函数是一个get提交,这里的URL其实可以使用变量处理的,这样就可以将你外挂传感器获取的内容通过它提交上去了
|