-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathESP8266_HTTP_GET_传感器示例.init.lua
69 lines (63 loc) · 2.21 KB
/
ESP8266_HTTP_GET_传感器示例.init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
print("Coolpy5 V1.0 ESP8266_SDK HTTP GET")
server = "192.168.1.105"--服务器域名或IP地址
port = 6543--酷痞平台服务端口
ukey = "51dd40af-135f-4733-89ff-a07615282812"--UserKey用户密钥(必改项)
hub = 5--Hub ID(必改项)
cnode = 8--Node ID(必改项)
--(让芯片连接互联网)
wifiPoint = "YMS_805_1"--本地路由器热点名
wifiPwd = "yms*****"--本地路由器密码
wifiok = 0;
tmr.stop(0);
tmr.stop(1);
local str=wifi.ap.getmac();
local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
wifi.setmode(wifi.STATIONAP)
local cfg={}
cfg.ssid="ESP8266_"..ssidTemp;
cfg.pwd="12345678"
wifi.ap.config(cfg)
cfg={}
cfg.ip="192.168.4.1";
cfg.netmask="255.255.255.0";
cfg.gateway="192.168.4.1";
wifi.ap.setip(cfg);
wifi.sta.config(wifiPoint,wifiPwd)
wifi.sta.connect()
local cnt = 0
gpio.mode(0,gpio.OUTPUT);
tmr.alarm(0, 1000, 1, function()
if (wifi.sta.getip() == nil) and (cnt < 20) then
print("Trying Connect to Router, Waiting...")
cnt = cnt + 1
if cnt%2==1 then gpio.write(0,gpio.HIGH);
else gpio.write(0,gpio.LOW); end
else
tmr.stop(0);
wifiok =1;
print("Soft AP started")
print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());
cnt = nil;cfg=nil;str=nil;ssidTemp=nil;
collectgarbage()
end
end)
--每隔10秒向服务器请求一个数据
tmr.alarm(1, 10000, 1, function()
if(wifiok == 1) then --判断wifi是否已经成功连接主路由器连接互联网
sk=net.createConnection(net.TCP, 0)
sk:on("connection", function(conn) toget() end)
sk:on("disconnection", function(conn, pl) print("disconnection") sk:close() end)
sk:on("receive", function(conn, pl)
sk:close()
--显示所有已提取回来的消息
print(pl)
end)
sk:connect(port,server)
end
function toget()
sk:send("GET /api/hub/"..hub.."/node/"..cnode.."/datapoint HTTP/1.1\r\n"
.."Host: "..server.."\r\n"
.."U-ApiKey:"..ukey.."\r\n"
.."Cache-Control: no-cache\r\n\r\n")
end
end)