• Система автоматизации с открытым исходным кодом на базе esp8266/esp32 микроконтроллеров и приложения IoT Manager. Наша группа в Telegram

Нужна помощь out of memory

ven0om

New member
Пишу скрипт для перезагрузки роутера. Но столкнулся с проблемой, скрипт выполняет проверку соединения 85 раз потом вызывает исключение:
Код:
PANIC: unprotected error in call to Lua API (stdin:13: out of memory)
Собственно скрипт:
Код:
watchdog = tmr.create()
checks_counter = 5 -- число неудачных проверок до перезагрузки роутера
result = 0 -- для результатов dns запросов
test_passed = 0 -- для подсчета выполненных dns запросов

function connected(a)
    print("connected to WiFi")
    print("SSID", a.SSID)
    print("BSSID", a.BSSID)
    print("channel", a.channel)
end

function got_ip_cb(a)
    print("ip recived")
    print("IP", a.IP)
    print("network", a.netmask)
    print("gateway", a.gateway)   
end

function reboot_router()
    print ("REBOOT!!!")
    checks_counter = 5
    watchdog:interval(120000)
    watchdog:start()
end

wdfunction2 = coroutine.create(function()
    while 1 do
        print ("RESULT ",result)
        if (result ~= 0) then
        print("connection OK")
        checks_counter = 5
        else
        print("connection lost")
        checks_counter = checks_counter - 1
        print(checks_counter)
        end
        if (checks_counter==0) then reboot_router() end
        watchdog:start()
        coroutine.yield()
   end
end)

wdfunction = function ()
    watchdog:stop()
    watchdog:interval(10000)
    print("Check connection")   -- Вылетает где-то после этой строки
    result = 0
    test_passed = 0
    net.dns.resolve("www.google.com", function(sk, ip)
        if (ip ~= nil) then result = result + 1        
        end
        test_passed = test_passed + 1
        if (test_passed==3) then coroutine.resume(wdfunction2) end
    end)
    net.dns.resolve("www.yandex.ru", function(sk, ip)
        if (ip ~= nil) then result = result + 1
        end
        test_passed = test_passed + 1
        if (test_passed==3) then coroutine.resume(wdfunction2) end
    end)
    net.dns.resolve("www.amazon.com", function(sk, ip)
        if (ip ~= nil) then result = result + 1
        end
        test_passed = test_passed + 1
        if (test_passed==3) then coroutine.resume(wdfunction2) end
    end)   
end
   
wifi.setmode(wifi.STATION)
cfg={}
cfg.ssid="MyWiFi"
cfg.pwd="secret"
cfg.auth=wifi.WPA2_PSK
cfg.connect_cb=connected
cfg.got_ip_cb=got_ip_cb
cfg.auto=true
wifi.sta.config(cfg)

watchdog:register(10000, tmr.ALARM_AUTO, function() wdfunction() end)
watchdog:start()
 

ven0om

New member
Код:
watchdog = tmr.create()
checks_counter = 5
result = 0
test_passed = 0

function connected(a)
    print("connected to WiFi")
    print("SSID", a.SSID)
    print("BSSID", a.BSSID)
    print("channel", a.channel)
end

function got_ip_cb(a)
    if (a~=nil)
        print("ip recived")
        print("IP", a.IP)
        print("network", a.netmask)
        print("gateway", a.gateway)
    end   
end

function reboot_router()
    print ("REBOOT!!!")
    checks_counter = 5
    watchdog:interval(120000)
    watchdog:start()
end

wdfunction2 = coroutine.create(function()
    while 1 do
        print ("RESULT ",result)
        if (result ~= 0) then
        print("connection OK")
        checks_counter = 5
        else
        print("connection lost")
        checks_counter = checks_counter - 1
        print(checks_counter)
        end
        if (checks_counter==0) then reboot_router() end
        watchdog:start()
        coroutine.yield()
   end
end)

wdfunction = coroutine.create(function ()
    while 1 do
        watchdog:stop()       
        print("Check connection1")
        print(node.heap())
        result = 0
        test_passed = 0
        net.dns.resolve("www.google.com", function(sk, ip)
        if (ip ~= nil) then result = result + 1        
        end
        test_passed = test_passed + 1
        if (test_passed==3) then coroutine.resume(wdfunction2) end
        end)
        net.dns.resolve("www.yandex.ru", function(sk, ip)
        if (ip ~= nil) then result = result + 1
        end
        test_passed = test_passed + 1
        if (test_passed==3) then coroutine.resume(wdfunction2) end
        end)
        net.dns.resolve("www.amazon.com", function(sk, ip)
        if (ip ~= nil) then result = result + 1
        end
        test_passed = test_passed + 1
        if (test_passed==3) then coroutine.resume(wdfunction2) end
        end)
        coroutine.yield()
        watchdog:interval(10000)
   end  
end)
   
wifi.setmode(wifi.STATION)
cfg={}
cfg.ssid="MyWiFi"
cfg.pwd="secret"
cfg.auth=wifi.WPA2_PSK
cfg.connect_cb=connected
cfg.got_ip_cb=got_ip_cb
cfg.auto=true
wifi.sta.config(cfg)
print("test")

watchdog:register(10000, tmr.ALARM_AUTO, function() coroutine.resume(wdfunction) end)
watchdog:start()
Вывод
Код:
connected to WiFi
SSID    MyWiFi
BSSID    9c:d6:53:71:de:37
channel    9
Check connection1
37936
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
RESULT     3
connection OK
Check connection1
37424
Дальше скрипт вылетает
 

ven0om

New member
вылетате он у вас потому что память закончилась
память закончилась потому что вы многократно создаете сураунт connection1
Не понял, что такое сураунт. В какой строке? Вывод node.heap() показывает что с памятью все норм.
Да и если убрать dns.resolve то все ок.
 
Последнее редактирование:
Сверху Снизу