2016-07-06 104 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