我有一个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 回答
来自NodeMCU参考的一点引用:
请注意第二行 . :)
不确定你的问题是什么,但为什么你首先使用while循环?为什么不定期使用定时器事件轮询ADC?
也许看门狗是因为你的饲料由于某种原因迟到而被触发的 . 在if的情况下,你根本不喂它,因为你离开了循环 .
即使它可能不是明确的答案,我也会这样说,因为评论输入太小了 .
首先,我建议您使用我为上一个问题发布的脚本 . 这个没有正确处理WiFi设置 . 您需要在计时器中等待,直到设备获得IP才能继续 . 请记住,wifi.sta.config是非阻塞的 . 并且由于它使用
auto-connect=true
如果没有明确设置它'll try to connect to the AP immediately. That'也是为什么wifi.sta.autoconnect(1)
是多余的 .我不明白你发布的ADC阅读代码 .
对我来说似乎不必要因为a)你没有对GPIO 0做任何事情而b)adc.read只支持0 .
我建议您使用基于间隔的计时器,而不是使用繁忙的循环并不断地给看门狗喂食which is a very bad sign . 此外,我想你不想在第一次遇到这种情况时打破循环并且永远不会回来?所以,你需要保持循环并继续触发发送邮件,不是吗?这样的事可能(未经测试):