-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathimp.lua
More file actions
54 lines (46 loc) · 1.19 KB
/
imp.lua
File metadata and controls
54 lines (46 loc) · 1.19 KB
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
ssid = "myssid"
password = "mypassword"
host = '11.22.33.44'
port = '8000'
path = "/mypath?value="
ready = false
uart.setup(0, 9600, 8, 0, 1, 0)
uart.on("data", "\n", function(data)
data = string.gsub(data, "[\r\n]", " ")
call_server(data)
end, 0)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, password)
wifi.sta.autoconnect(1)
cnt = 1
tmr.alarm(3, 1000, 1, function()
if (wifi.sta.getip() == nil) and (cnt < 20) then
print("trying wifi AP..."..cnt.." sec")
cnt = cnt + 1
else
tmr.stop(3)
if (cnt < 20) then
ip = wifi.sta.getip();
print("got wifi="..ip..", status="..wifi.sta.status());
ready = true
else node.restart()
end
cnt = nil;
end
end)
function call_server(value)
print("value="..value)
if ready == false then return end
conn=net.createConnection(net.TCP, 0)
conn:on("connection",function(conn, payload)
conn:send("GET "..path..value.." HTTP/1.1\r\n"..
"Accept: */*\r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Kyuho Kim;)\r\n"..
"\r\n")
end)
conn:on("receive", function(conn, payload)
print("Received: "..payload)
conn:close();
end)
conn:connect(port, host)
end