stack traceback:
C: in function 'assert'
blynkmon.lua:95: in function 'connectBlynk'
blynkmon.lua:121: in function 'fun'
./blynk.lua:54: in function 'emit'
./blynk.lua:107: in function 'disconnect'
./blynk/socket.lua:31: in function 'run'
blynkmon.lua:179: in main chunk
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
Here is the whole script
!/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")
local blynk = Blynk.new(auth, {
heartbeat = 30, -- default h-beat is 50
--log = print,
})
function exec_out(cmd)
local file = io.popen(cmd)
if not file then return nil end
local output = file:read('all')
file:close()
print("Run: "..cmd.." -> "..output)
return output
end
function read_file(path)
local file = io.open(path, "rb")
if not file then return nil end
local content = file:read "a"
file:close()
print("Read: "..path.." -> "..content)
return content
end
function getArpClients()
return tonumber(exec_out("cat /proc/net/arp | grep br-lan | grep 0x2 | wc -l"))
end
function getUptime()
return tonumber(exec_out("cat /proc/uptime | awk '{print $1}'"))
end
function getWanIP()
return exec_out("ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'")
end
function getWirelessIP()
return exec_out("ifconfig 3g-modem_1_1_2 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'")
end
function getCpuLoad()
return tonumber(exec_out("top -bn1 | grep 'CPU:' | head -n1 | awk '{print $2+$4}'"))
end
function getRamUsage()
return tonumber(exec_out("free | grep Mem | awk '{print ($3-$7)/$2 * 100.0}'"))
end
function getWanRxBytes()
return tonumber(read_file("/sys/class/net/eth0/statistics/rx_bytes"))
end
function getWanTxBytes()
return tonumber(read_file("/sys/class/net/eth0/statistics/tx_bytes"))
end
function getWirelessRxBytes()
return tonumber(read_file("/sys/class/net/3g-modem_1_1_2/statistics/rx_bytes"))
end
function getWirelessTxBytes()
return tonumber(read_file("/sys/class/net/3g-modem_1_1_2/statistics/tx_bytes"))
end
function getWirelessSINR()
return tonumber(exec_out("gl_modem cells | jq .[0].sinr | awk '{ print $1 }' | tr -d \'\"\'"))
end
function getWirelessRSSI()
return tonumber(exec_out("gl_modem cells | jq .[0].rssi | awk '{ print $1 }' | tr -d \'\"\'"))
end
function getWirelessRSRQ()
return tonumber(exec_out("gl_modem cells | jq .[0].rsrq | awk '{ print $1 }' | tr -d \'\"\'"))
end
function getWirelessRSRP()
return tonumber(exec_out("gl_modem cells | jq .[0].rsrp | awk '{ print $1 }' | tr -d \'\"\'"))
end
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")
blynk:virtualWrite(12, getWanIP())
blynk:virtualWrite(13, getWirelessIP())
blynk:virtualWrite(40, getWirelessSINR())
blynk:virtualWrite(41, getWirelessRSSI())
blynk:virtualWrite(42, getWirelessRSRQ())
blynk:virtualWrite(43, getWirelessRSRP())
-- whenever we connect, request an update of V1
blynk:syncVirtual(1)
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)
local prevWanTx, prevWanRx, prevWirelessTx, prevWirelessRx
-- create a timer to update widget property
local tmr1 = Timer:new{interval = 5000, func = function()
blynk:setProperty(2, "label", os.time())
blynk:virtualWrite(5, getCpuLoad())
blynk:virtualWrite(6, getRamUsage())
local wantx = getWanTxBytes()
local wanrx = getWanRxBytes()
local wirelesstx = getWirelessTxBytes()
local wirelessrx = getWirelessRxBytes()
if prevWanTx and prevWanTx ~= wantx then
blynk:virtualWrite(30, wantx - prevWanTx)
print(wantx - prevWanTx)
end
if prevWanRx and prevWanRx ~= wanrx then
blynk:virtualWrite(31, wanrx - prevWanRx)
print(wanrx - prevWanRx)
end
if prevWirelessTx and prevWirelessTx ~= wirelesstx then
blynk:virtualWrite(32, wirelesstx - prevWirelessTx)
print(wirelesstx - prevWirelessTx)
end
if prevWirelessRx and prevWirelessRx ~= wirelessrx then
blynk:virtualWrite(33, wirelessrx - prevWirelessRx)
print(wirelessrx - prevWirelessRx)
end
prevWanTx = wantx
prevWanRx = wanrx
prevWirelessTx = wirelesstx
prevWirelessRx = wirelessrx
end}
stack traceback: C: in function 'assert' blynkmon.lua:95: in function 'connectBlynk' blynkmon.lua:121: in function 'fun' ./blynk.lua:54: in function 'emit' ./blynk.lua:107: in function 'disconnect' ./blynk/socket.lua:31: in function 'run' blynkmon.lua:179: in main chunk
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
Here is the whole script
!/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, })
function exec_out(cmd) local file = io.popen(cmd) if not file then return nil end local output = file:read('all') file:close() print("Run: "..cmd.." -> "..output) return output end function read_file(path) local file = io.open(path, "rb") if not file then return nil end local content = file:read "a" file:close() print("Read: "..path.." -> "..content) return content end
function getArpClients() return tonumber(exec_out("cat /proc/net/arp | grep br-lan | grep 0x2 | wc -l")) end function getUptime() return tonumber(exec_out("cat /proc/uptime | awk '{print $1}'")) end function getWanIP() return exec_out("ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'") end function getWirelessIP() return exec_out("ifconfig 3g-modem_1_1_2 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'") end function getCpuLoad() return tonumber(exec_out("top -bn1 | grep 'CPU:' | head -n1 | awk '{print $2+$4}'")) end function getRamUsage() return tonumber(exec_out("free | grep Mem | awk '{print ($3-$7)/$2 * 100.0}'")) end
function getWanRxBytes() return tonumber(read_file("/sys/class/net/eth0/statistics/rx_bytes")) end function getWanTxBytes() return tonumber(read_file("/sys/class/net/eth0/statistics/tx_bytes")) end function getWirelessRxBytes()
return tonumber(read_file("/sys/class/net/3g-modem_1_1_2/statistics/rx_bytes"))
end
function getWirelessTxBytes()
return tonumber(read_file("/sys/class/net/3g-modem_1_1_2/statistics/tx_bytes"))
end
function getWirelessSINR() return tonumber(exec_out("gl_modem cells | jq .[0].sinr | awk '{ print $1 }' | tr -d \'\"\'")) end function getWirelessRSSI()
return tonumber(exec_out("gl_modem cells | jq .[0].rssi | awk '{ print $1 }' | tr -d \'\"\'")) end function getWirelessRSRQ()
return tonumber(exec_out("gl_modem cells | jq .[0].rsrq | awk '{ print $1 }' | tr -d \'\"\'")) end function getWirelessRSRP()
return tonumber(exec_out("gl_modem cells | jq .[0].rsrp | awk '{ print $1 }' | tr -d \'\"\'")) end 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") blynk:virtualWrite(12, getWanIP()) blynk:virtualWrite(13, getWirelessIP()) blynk:virtualWrite(40, getWirelessSINR()) blynk:virtualWrite(41, getWirelessRSSI()) blynk:virtualWrite(42, getWirelessRSRQ()) blynk:virtualWrite(43, getWirelessRSRP()) -- 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)
local prevWanTx, prevWanRx, prevWirelessTx, prevWirelessRx
-- create a timer to update widget property local tmr1 = Timer:new{interval = 5000, func = function() blynk:setProperty(2, "label", os.time()) blynk:virtualWrite(5, getCpuLoad()) blynk:virtualWrite(6, getRamUsage()) local wantx = getWanTxBytes() local wanrx = getWanRxBytes() local wirelesstx = getWirelessTxBytes()
local wirelessrx = getWirelessRxBytes() if prevWanTx and prevWanTx ~= wantx then blynk:virtualWrite(30, wantx - prevWanTx) print(wantx - prevWanTx) end if prevWanRx and prevWanRx ~= wanrx then blynk:virtualWrite(31, wanrx - prevWanRx) print(wanrx - prevWanRx) end if prevWirelessTx and prevWirelessTx ~= wirelesstx then
blynk:virtualWrite(32, wirelesstx - prevWirelessTx)
print(wirelesstx - prevWirelessTx) end
if prevWirelessRx and prevWirelessRx ~= wirelessrx then
blynk:virtualWrite(33, wirelessrx - prevWirelessRx)
print(wirelessrx - prevWirelessRx) end prevWanTx = wantx prevWanRx = wanrx prevWirelessTx = wirelesstx prevWirelessRx = wirelessrx end}
local tmr2 = Timer:new{interval = 5601000, func = function() blynk:virtualWrite(10, getArpClients()) blynk:virtualWrite(11, string.format("%.1f h", getUptime()/60/60)) blynk:virtualWrite(40, getWirelessSINR()) blynk:virtualWrite(41, getWirelessRSSI()) blynk:virtualWrite(42, getWirelessRSRQ())
blynk:virtualWrite(43, getWirelessRSRP()) end}
connectBlynk()
while true do blynk:run() tmr1:run() tmr2:run()
end