【问题标题】:NodeMCU timeout when using while loop使用while循环时NodeMCU超时
【发布时间】:2016-07-07 04:24:23
【问题描述】:

我有一个通过 SMTP 向自己发送电子邮件的 Lua 脚本。上传到 NodeMCU 并说dofile("sendemail.lua") 时一切正常。

-- sendmail.lua    

-- The email and password from the account you want to send emails from
    MY_EMAIL = "REDACTED"

EMAIL_PASSWORD = "REDACTED"

-- The SMTP server and port of your email provider.
-- If you don't know it google [my email provider] SMTP settings
SMTP_SERVER = "isp.smtp.server"
SMTP_PORT = 25

-- The account you want to send email to
mail_to = "REDACTED"

-- Your access point's SSID and password
SSID = "REDACTED"
SSID_PASSWORD = "REDACTED"

-- configure ESP as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)

email_subject = ""
email_body = ""
count = 0


local smtp_socket = nil -- will be used as socket to email server

-- The display() function will be used to print the SMTP server's response
function display(sck,response)
     print(response)
end

-- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence.
-- I was going to use socket callbacks but the code would not run callbacks after the first 3.
function do_next()
            if(count == 0)then
                count = count+1
                IP_ADDRESS = wifi.sta.getip()
                smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
            elseif(count==1) then
                count = count+1
                smtp_socket:send("AUTH LOGIN\r\n")
            elseif(count == 2) then
                count = count + 1
                smtp_socket:send("REDACTED".."\r\n")
            elseif(count == 3) then
                count = count + 1
                smtp_socket:send("REDACTED".."\r\n")
            elseif(count==4) then
                count = count+1
               smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
            elseif(count==5) then
                count = count+1
               smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
            elseif(count==6) then
                count = count+1
               smtp_socket:send("DATA\r\n")
            elseif(count==7) then
                count = count+1
                local message = string.gsub(
                "From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" ..
                "To: \"".. mail_to .. "\"<".. mail_to..">\r\n"..
                "Subject: ".. email_subject .. "\r\n\r\n"  ..
                email_body,"\r\n.\r\n","")

                smtp_socket:send(message.."\r\n.\r\n")
            elseif(count==8) then
               count = count+1
                 tmr.stop(0)
                 smtp_socket:send("QUIT\r\n")
                 print("msg sent")
            else
               smtp_socket:close()
            end
            print(count)
end

-- The connectted() function is executed when the SMTP socket is connected to the SMTP server.
-- This function will create a timer to call the do_next function which will send the SMTP commands
-- in sequence, one by one, every 5000 seconds. 
-- You can change the time to be smaller if that works for you, I used 5000ms just because.
function connected(sck)
    tmr.alarm(0,5000,1,do_next)
end

-- @name send_email
-- @description Will initiated a socket connection to the SMTP server and trigger the connected() function
-- @param subject The email's subject
-- @param body The email's body
function send_email(subject,body)

     count = 0
     email_subject = subject
     email_body = body
     smtp_socket = net.createConnection(net.TCP,0)
     smtp_socket:on("connection",connected)
     smtp_socket:on("receive",display)
    smtp_socket:connect(SMTP_PORT, SMTP_SERVER)     
end
-- Send an email
send_email("ESP8266", "[[Hi, How are your IoT projects coming along? Best Wishes,ESP8266]]")

但是,我想使用循环来监控模拟输入值,并且仅在检测到某些模拟输入值时发送电子邮件。因此,我在脚本末尾添加了这段代码,在 sendemail() 函数定义之后和函数 sendmail('subject', 'body') 被调用之前

vp = 0
gpio.mode(vp, gpio.INPUT)

while true do

    local v = adc.read(vp)
    if v < 840 or v > 870 then
        print(v)
        break
    end
    tmr.wdclr()
end
sendmail('subject', 'body')

while 循环完美运行,无限期地等待来自模拟引脚的输入。一旦找到该输入,它就会正确中断并调用 sendmail 函数。但是,一旦调用该函数,NodeMCU 最终会重置。有时它会成功地通过服务器验证 SMTP 凭据,有时它甚至不会在关闭之前生成 HELO。这可能是什么原因造成的?为什么sendmail.lua 脚本可以正常工作,然后在添加这个看起来完全可以正常工作的小while 循环时突然决定不工作?

【问题讨论】:

    标签: lua esp8266 nodemcu


    【解决方案1】:

    来自 NodeMCU 参考的一点引用:

    tmr.wdclr() 输入系统看门狗。

    一般来说,如果你需要使用这个功能,你正在做 错了。

    NodeMCU的事件驱动模型意味着不需要 坐在硬循环中等待事情发生。相反,只需使用 当事情发生时得到通知的回调。有了这个 方法,永远不需要手动输入系统 看门狗。

    请注意第二行。 :)

    不确定你的问题是什么,但你为什么首先使用while循环?为什么不使用计时器事件定期轮询您的 ADC?

    可能由于某种原因您的提要迟到了,因此触发了看门狗。在 if 情况下,您在离开循环之前根本不喂它。

    【讨论】:

      【解决方案2】:

      即使它可能不是明确的答案,我也照此发布,因为评论输入太少了。

      首先,我建议您使用我为您之前的问题发布的脚本。这个没有正确处理 WiFi 设置。您需要在计时器中等待,直到设备获得 IP 才能继续。请记住,wifi.sta.config 是非阻塞的。由于它使用auto-connect=true,如果没有明确设置,它会立即尝试连接到AP。这也是为什么wifi.sta.autoconnect(1)是多余的原因。

      我不明白你发布的 ADC 读取代码。

      vp = 0
      gpio.mode(vp, gpio.INPUT)
      

      对我来说似乎没有必要,因为 a) 你不使用 GPIO 0 和 b)adc.read 只支持 0。

      我建议您使用基于间隔的计时器,而不是使用繁忙的循环并不断地喂看门狗which is a very bad sign。此外,我猜你不想在第一次满足条件时打破循环并且永远不会回来?所以,你需要保持循环并不断触发发送邮件,不是吗?可能是这样的(未经测试):

      tmr.alarm(0, 200, tmr.ALARM_AUTO, function()
        local v = adc.read(0)
        if v < 840 or v > 870 then
          node.task.post(function()
            send_email("ESP8266", "[[Hi, How are your IoT projects coming along? Best Wishes,ESP8266]]")
          end)
        end
      end)
      

      【讨论】:

      • 所以这仍然会重置大约十秒到定时器警报。任何想法为什么会发生这种情况?
      • 实际上,当我尝试在我的 NodeMCU 上运行脚本时,这似乎是随机发生的。各个部分似乎工作正常,所以我想我要么是硬件不好,要么是固件出了问题。
      猜你喜欢
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      相关资源
      最近更新 更多