Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client crashed line 95 #3

Open
mfoxworthy opened this issue Sep 21, 2021 · 1 comment
Open

Client crashed line 95 #3

mfoxworthy opened this issue Sep 21, 2021 · 1 comment

Comments

@mfoxworthy
Copy link

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
[C]: ?

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

@reeett
Copy link

reeett commented Jan 2, 2025

This topic is already very old but in case someone stumbles across it again i'll show my solution for the problem here. The problem seems to occur when the device has no connection to the network or internet. If the connect blynk function is slightly extended the program can run and does not crash when there is no network connection. As soon as there is a network connection again the blynk client reconnects to the server.

New "connect_blynk()" function

local function connect_blynk()
    local host = "blynk.cloud"
    local sock, err

    local function safe_connect(sock, host, port)
        local success, connect_err = pcall(function()
            sock:connect(host, port)
        end)
        return success, connect_err
    end

    local function handle_error(message, err)
        print(message .. ": " .. (err or "Unknown error"))
        return false
    end

    -- Try to create the socket
    sock, err = socket.tcp()
    if not sock then
        return handle_error("Error creating the socket", err)
    end

    sock:setoption("tcp-nodelay", true)

    if use_ssl then
        print("Connecting Blynk (secure)...")
        local success, connect_err = safe_connect(sock, host, 443)
        if not success then
            return handle_error("Error when connecting via SSL", connect_err)
        end

        local opts = {
            mode = "client",
            protocol = "tlsv1_2"
        }

        local ssl_wrap_success
        ssl_wrap_success, sock = pcall(ssl.wrap, sock, opts)
        if not ssl_wrap_success then
            return handle_error("SSL wrap error")
        end

        local handshake_success, handshake_err = pcall(function()
            sock:dohandshake()
        end)
        if not handshake_success then
            return handle_error("SSL handshake error", handshake_err)
        end
    else
        print("Connecting Blynk...")
        local success, connect_err = safe_connect(sock, host, 80)
        if not success then
            return handle_error("Error with unsecured connection", connect_err)
        end
    end`

Then the function can be called as follows

local success = connect_blynk()
if not success then
    print("Blynk connection failed")
end
blynk:on("disconnected",
	function()
		print("Disconnected")
		-- auto-reconnect after 5 seconds
		socket.sleep(5)
		-- connect_blynk()
		local success = connect_blynk()
		if not success then
			print("Blynk connection failed")
		end
	end
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants