使用while循环时NodeMCU超时

时间:2016-07-06 19:18:42

标签: lua esp8266 nodemcu

我有一个Lua脚本,可以通过SMTP向自己发送电子邮件。上传到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循环时突然决定不工作呢?

2 个答案:

答案 0 :(得分:1)

来自NodeMCU参考的一点引用:

  

tmr.wdclr()输入系统监视程序。

     

通常,如果您需要使用此功能,那么您正在这样做   错。

     

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

请注意第二行。 :)

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

也许看门狗被触发了,因为你的饲料由于某种原因而迟到了。在if的情况下,你根本不喂它,因为你离开了循环。

答案 1 :(得分:1)

即使它可能不是明确的答案,我也会这样说,因为评论输入太小了。

首先,我建议您使用我为上一个问题发布的脚本。这个没有正确处理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)