2016-07-06 2 views
0

私はSMTP経由でメールを自分宛に送信するLuaスクリプトを持っています。 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つの小さなwhileループを追加するときに突然動作しないと決めると、それは完全に正常に動作するように見えますか? NodeMCU参照から

答えて

1

少し引用:

tmr.wdclr()システムウォッチドッグを養います。

通常、この機能を使用する必要がある場合は、 が間違っています。

NodeMCUのイベントドリブンモデルは、発生するのを待っているハードループ内に が存在する必要がないことを意味します。むしろ、ちょっとしたことが起きたときに通知を受けるには、単に コールバックを使用してください。この アプローチでは、システム ウォッチドッグに手動でフィードする必要はありません。

2行目にご注意ください。 :)

問題が何であるかはわかりませんが、最初にwhileループを使用する理由は何ですか?タイマーイベントを使用してADCを定期的にポーリングするのはなぜですか?

フィードが何らかの理由で遅れているため、ウォッチドッグが発生している可能性があります。 ifの場合、あなたはループを離れるので、あなたはそれをまったく食べていません。

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) 
+0

これはまだタイマアラームに約10秒をリセットします。それがなぜ起こっているのでしょうか? – user1173922

+0

実際には、NodeMCUでスクリプトを実行しようとすると、これはランダムに起こっているようです。個々の部品はうまく動作しているように見えるので、私はハードウェアが不足しているとか、何とかファームウェアが壊れていると思っています。 – user1173922

関連する問題