-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathclient.lua
executable file
·78 lines (62 loc) · 1.63 KB
/
client.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
70
71
72
73
74
75
76
77
78
#!/usr/bin/env lua
--[[
This is the default example for Linux, Windows, OpenWrt
]]
local socket = require("socket")
local use_ssl, ssl = pcall(require, "ssl")
local Blynk = require("blynk.socket")
local Timer = require("timer")
assert(#arg >= 1, "Please specify Auth Token")
local auth = arg[1]
local blynk = Blynk.new(auth, {
heartbeat = 30, -- default h-beat is 50
--log = print,
})
local function connectBlynk()
local host = "blynk.cloud"
local sock = assert(socket.tcp())
sock:setoption("tcp-nodelay", true)
if use_ssl then
print("Connecting Blynk (secure)...")
sock:connect(host, 443)
local opts = {
mode = "client",
protocol = "tlsv1_2"
}
sock = assert(ssl.wrap(sock, opts))
assert(sock:dohandshake())
else
print("Connecting Blynk...")
sock:connect(host, 80)
end
-- tell Blynk to use this socket
blynk:connect(sock)
end
blynk:on("connected", function(ping)
print("Ready. Ping: "..math.floor(ping*1000).."ms")
-- whenever we connect, request an update of V1
blynk:syncVirtual(1)
end)
blynk:on("disconnected", function()
print("Disconnected.")
-- auto-reconnect after 5 seconds
socket.sleep(5)
connectBlynk()
end)
-- callback to run when V1 changes
blynk:on("V1", function(param)
print("V1:", tonumber(param[1]), tonumber(param[2]))
end)
-- callback to run when cloud requests V2 value
blynk:on("readV2", function(param)
blynk:virtualWrite(2, os.time())
end)
-- create a timer to update widget property
local tmr1 = Timer:new{interval = 5000, func = function()
blynk:setProperty(2, "label", os.time())
end}
connectBlynk()
while true do
blynk:run()
tmr1:run()
end