2016-07-06 101 views
0

我有一个Lua脚本通过SMTP向我发送电子邮件。上传到NodeMCU并说dofile("sendemail.lua")时一切正常。使用while循环时NodeMCU超时

-- 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脚本能够正常工作,然后突然决定在添加这个看起来完全正常工作的小循环时不工作?

回答

1

从NodeMCU参考小报价:

tmr.wdclr()进料系统看门狗。

一般来说,如果您曾经需要使用这个功能,那么您在做 错误。

NodeMCU的事件驱动模型意味着不需要在硬环路中等待事件发生。相反,只需使用 回调函数在有些事情发生时得到通知。采用这种方法,应该永远不需要手动给系统提供看门狗。

请注意第二行。 :)

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

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

1

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

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

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

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

似乎没有必要对我来说,因为)你不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) 
+0

所以这仍然会重置约10秒进入计时器闹钟。任何想法为什么可能会发生? – user1173922

+0

实际上,当我尝试在NodeMCU上运行脚本时,这似乎是随机发生的。个别部分似乎工作正常,所以我想我要么有一些不好的硬件,要么固件搞砸了。 – user1173922