完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
嗨,我已经购买了 Wemos D1 mini 来构建邮件通知程序,以便在不打开计算机的情况下接收电子邮件通知。这是我的第一个程序,所以请对我温柔,欢迎提出改进建议。
我试着评论功能,但基本上: - 它连接到 Wi-Fi - 从阵列中选择更多网络,我在更多地方使用这个盒子 - 白色 LED 闪光灯 - 步骤开始 - 绿色 LED 闪光灯 - 从邮件服务器接收数据 - 红色 LED 亮起 - 服务器上存在未读邮件 - 通过 pcm 播放声音 - 每封新邮件仅通知一次,在收到新邮件通知后,红色 LED 灯不发出声音,直到新邮件到达 - 蓝色 LED 亮起 - 来自 VIP 电子邮件地址的邮件 IMAP 步骤 1 (LOGIN) 和 2 (LIST,不需要并且可以安全删除) 只处理一次,其他步骤在循环中重复。步骤时间应该长于播放的声音(否则声音会被切断),接收新电子邮件(FETCH)可以采取多个步骤(但程序会等到电子邮件完全接收),如果你愿意,你可以缩短收到的时间响应(我正在等待“OK Fetch completed”,这是最安全的方式,但如果您发现邮件的长度,那么它还包含“发件人:”字段,用于 VIP 邮件识别)。在第 5 步中,FETCH 是针对新邮件进行处理的,即使没有禁用 VIP 邮件检查,那么它确实不需要,您可以禁用它(但要小心包含 'unread_imaps.uns' 东西)。 有关电子邮件数量、最后播放的 pcm 通知和新电子邮件数量的信息(只是估计,因为 IMAP 不报告新电子邮件数量,所以它只是粗略地计算为 EXISTS - UNSEEN + 1),所以即使在重置后 ESP 也知道最后播放通知但不播放声音,如果它已经为相同的 UNSEEN、NEW 等播放过。程序循环运行,但有时会崩溃(一定要使用init.lua,如果你想使用它更长时间),如果有人能给我提示,为什么会崩溃,我会很高兴。 我还没有找到方法,如何检查证书(总是报告,证书不匹配),所以只是在端口 993 上建立到服务器的加密连接而不检查证书。可以将端口更改为不安全的 IMAP(IMAP_PORT 和 imap_socket = net.createConnection...),但我建议保持 SSL 开启(这可能需要包含 TLS 的固件)。 如果您不想循环检查电子邮件,而只想检查一次,只需取消注释第 8 步以关闭与服务器的连接,删除最后一个“else”并停止计时器 3。 代码:全选MY_EMAIL = "user@na.me" --IMAP username EMAIL_PASSWORD = "mailpassword" --IMAP password IMAP_SERVER = "mail.server.com" --IMAP server name IMAP_PORT = 993 --IMAP port (change to 143 for not secure connection, 993 for IMAPS) vipcheck = true -- check VIP mail and set blue LED (for common mails red LED is set) vipmail = nil letsplay = true pcmfile = "wavsound.u8" -- convert to wav "sox wavsound.wav -r 8000 -b 8 -c 1 wavsound.u8" (-r Supported sample rates are 1000, 2000, 4000, 5000, 8000, 10000, 12000, 16000) vip_sender = "my@lovely.gf" --VIP sender (just one, I don't have more girlfriends;-) APTABLE = { home = { "", "" },--AP array 'descriptor = {"SSID", "SSID_PASSWORD"'} work = { "", "" } } count = 0 atcount = 1 debug = false -- show at responses and other debug stuff, affects performance and functions processing, use with caution!!! response_processed = false response = "" lastplayed = nil ledgreen=1 -- AT notification ledred=2 -- unread mail notification ledblue=4 -- unread vip mail notification ledwhite=3 --step notification pcmpin=5 -- pcm output tmr.stop(1) tmr.stop(2) tmr.stop(3) gpio.mode(ledgreen, gpio.OUTPUT) gpio.mode(ledred, gpio.OUTPUT) gpio.mode(ledblue, gpio.OUTPUT) ----PCM thing starts here function cb_drained(d) file.seek("set", 0) end function cb_stopped(d) file.seek("set", 0) file.close() drv = nil end function cb_paused(d) -- end function play() file.open(pcmfile, "r") drv = pcm.new(pcm.SD, pcmpin) if file.open(pcmfile, "r") then drv:on("data", function(drv) return file.read() end) end drv:on("drained", cb_drained) drv:on("stopped", cb_stopped) drv:on("paused", cb_paused) drv:play(pcm.RATE_16K) -- Supported sample rates are pcm.RATE_1K, pcm.RATE_2K, pcm.RATE_4K, pcm.RATE_5K, pcm.RATE_8K, pcm.RATE_10K, pcm.RATE_12K, pcm.RATE_16K) end ---- PCM thing ends here function aplist(apfound) for o, d in pairs(APTABLE) do if d[1] == apfound then SSID = d[1] SSID_PASSWORD = d[2] end end --do nothing end function flash(led, delay) gpio.write(led, gpio.HIGH) i=0 while i < delay*100 do i = i + 1 end gpio.write(led, gpio.LOW) end function atdisplay(imap_socket, response) -- Correct responses are: -- "a1 OK [CAPABILITY..........] Logged in" -- "ax OK ............. completed." -- In step 5 (FETCH) whole ending of response 'OK Fetch completed' is checked to avoid detection of 'completed' in email body response_processed = false flash(ledgreen,100) if (debug==true) then print("atdisplay start "..count) end responseend = string.sub(response, -40) --just in case server supports lot of ptotocols if count == 0 and (string.match(responseend,'IMAP') ~= nil) then response_processed = true print("IMAP/IMAPS supported") elseif count == 1 and (string.match(responseend,'Logged') ~= nil) then response_processed = true elseif count ==5 and (string.match(responseend,'OK Fetch completed') ~= nil) then response_processed = true elseif count > 1 and (string.match(responseend,' completed') ~= nil) then response_processed = true end if (debug==true) then print("response = ", response) print("response processed = ", response_processed) print("atdisplay end ", count) end fromstart = string.find(response,'From: "') if fromstart ~= nil then fromcut = string.sub(response, fromstart, fromstart+200) vipmail = string.find(fromcut,vip_sender) end _G.response = response end function imapcheck() if (debug==true) then print (".") end if(response_processed == true) then if (debug==true) then print ("..") end flash(ledwhite,200) count = count + 1 if(count == 1) then imap_socket:send("a" ..atcount.. " LOGIN " ..MY_EMAIL.. " " ..EMAIL_PASSWORD.. "\r\n") imap_socket:on("receive", atdisplay) print(count.. " LOGIN") atcount = atcount + 1 elseif(count == 2) then imap_socket:send("a" ..atcount.. " LIST \"\" \"*\"\r\n") -- not really needed, every mail has INBOX imap_socket:on("receive", atdisplay) print(count.. " LIST - just for fun") atcount = atcount + 1 elseif(count == 3) then imap_socket:send("a" ..atcount.. " EXAMINE INBOX\r\n") imap_socket:on("receive", atdisplay) print(count.. " EXAMINE INBOX") atcount = atcount + 1 elseif(count == 4) then _, _, exists = string.find(_G.response,"([0-9]+) EXISTS(\.)") unseenfound = string.find(_G.response,"UNSEEN") print(count.. " looking for unread emails") if unseenfound ~= nil then unseenstart = unseenfound+7 unseenstring = string.sub(_G.response, unseenstart, unseenstart+7) unseenend = (string.find(unseenstring,"]")+unseenstart-2) firstunseen = tonumber(string.sub(_G.response, unseenstart, unseenend)) newcount = exists - firstunseen + 1 print (" you have " ..newcount.. " unread email/s (might be wrong due to IMAP limitations)") end elseif(count == 5) then print(count.. " seen some some unseen messages?") if firstunseen ~= nil then imap_socket:send("a" ..atcount.. " FETCH " ..firstunseen.. " BODY[]\r\n") imap_socket:on("receive", atdisplay) print(" FETCH " ..firstunseen) atcount = atcount + 1 if file.exists("unread_imaps.uns") then if file.open("unread_imaps.uns", "r+") then cont = file.read() oldexists=tonumber(string.sub(cont, 0,string.find(cont,",")-1)) oldfirstunseen=tonumber(string.sub(cont, string.find(cont,",")+1,string.find(cont,",")+string.find(string.sub(cont,(string.find(cont,",")+1)),",")-1)) oldlastplayed=tonumber(string.sub(cont, string.find(cont,",")+(string.find(string.sub(cont,(string.find(cont,",")+1)),",")+1) , string.len(cont) )) oldnewcount = oldexists - oldfirstunseen + 1 file.close() else lastplayed = 0 -- file doesn't exits end end end elseif(count== 6) then print(count.. " new mail notification") if firstunseen ~= nil then gpio.write(ledred, gpio.HIGH) if ((oldlastplayed ~= firstunseen) or (oldnewcount ~= newcount)) and letsplay == true then -- play the notification just once for each first UNSEEN play() -- PCM thing again, comment if no PCM used lastplayed = firstunseen end else gpio.write(ledred, gpio.LOW) end elseif(count == 7) then print(count.. " VIP mail notification") if vipcheck == true and vipmail ~= nil then gpio.write(ledblue, gpio.LOW) print (" you have new mail from " ..vip_sender.. " :-)") else gpio.write(ledblue, gpio.HIGH) end if letsplay == true and firstunseen ~= nil and lastplayed == firstunseen then if file.open("unread_imaps.uns", "w+") then file.write(exists.. "," ..firstunseen.. "," ..lastplayed) file.close() end end -- elseif(count == 8) then -- imap_socket:send("a"..atcount.." LOGOUT\r\n") -- imap_socket:on("receive",atdisplay) -- print(count.. " LOGOUT") -- atcount = atcount + 1 -- imap_socket:close() else print(count.. " Loop\n") gpio.write(ledred, gpio.LOW) gpio.write(ledgreen, gpio.LOW) gpio.write(ledwhite, gpio.LOW) gpio.write(ledblue, gpio.HIGH) if letsplay == true and drv ~= nil then drv:stop() ----PCM thing, release PCM driver, comment if no PCM used end count = 2 -- skip LOGIN and (useless) LIST vipmail = nil -- tmr.stop(3) end end end function connected(imap_socket) tmr.alarm(3, 15000, tmr.ALARM_AUTO, imapcheck) -- time should be longer than pcm notification file end gpio.write(ledred, gpio.LOW) gpio.write(ledgreen, gpio.LOW) gpio.write(ledwhite, gpio.LOW) gpio.write(ledblue, gpio.HIGH) --blue LED is inverted on my ESP, change for normal logic wifi.setmode(wifi.STATION) wifi.sta.getap(function(t) if t then for k,v in pairs(t) do aplist(k) end else --do nothing end end) tmr.alarm(1, 4000, tmr.ALARM_AUTO, function() if SSID == nil then print("Waiting for APs") else tmr.stop(1) print ("Connecting to "..SSID) wifi.sta.config(SSID,SSID_PASSWORD) end end) tmr.alarm(2, 5000, tmr.ALARM_AUTO, function() if wifi.sta.getip()== nil then print("IP unavaiable, waiting...") else tmr.stop(2) print("Config done, IP is "..wifi.sta.getip()) print("Connect to server "..IMAP_SERVER.." on port "..IMAP_PORT) imap_socket = net.createConnection(net.TCP,1) --connect to IMAP/IMAPS server, 0=IMAP, 1=IMAPS) imap_socket:on("connection",connected) flash(ledwhite,500) imap_socket:on("receive",atdisplay) -- to check if server supports IMAP, if you want to skip this check, comment this line and uncomment 'response_processed = true' 2 lines below imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- response_processed = true -- fake response if no IMAP support on server is checked end end) |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
524浏览 6评论
433浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
422浏览 5评论
414浏览 4评论
387浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-3 06:51 , Processed in 0.798382 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号