A while back I investigated the use of NodeMCU with GMail. One result was
this script to send mails over GMail. The other aspect I initially didn't fully investigate was
the atom feed offered by GMail.
|
Looks like we have unread mail |
If you haven't watched the video yet,
here it is.
In the code below, I use that feed to retrieve the number of unread elements from the inbox.
Apart from the Lua code, you also need to place the two files with the mailbox icons on NodeMCU's file system:
Mailoff-file:
here
Mailon-file:
here
That is what it looks like in action:
I recommend "esplorer" to copy the files to the ESP8266 module.
-- ESP8266 NodeMCU
-- GMail Notifier
-- 2016/07 Andy Reischle
-- www.AReResearch.net
-- Graphics handling and conversion
-- adapted from Daniel Eichhorns blog
-- http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html
--
-- To see this script in action, see:
-- https://youtu.be/IVxJosLZCXs
wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR-SSID","YOUR-WIFIPASS")
wifi.sta.connect()
-- setup I2c and connect display
function init_i2c_display()
-- SDA and SCL can be assigned freely to available GPIOs
sda = 5 -- GPIO14
scl = 6 -- GPIO12
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_128x64_i2c(sla)
end
function xbm_picture()
disp:setFont(u8g.font_6x10)
disp:drawStr( 0, 62, "Google Mail Notifier")
disp:drawXBM( 10, 5, 32, 32, xbm_data )
disp:drawStr (65,30, unread .. " unread")
end
function bitmap_mailon(delay)
file.open("mailon", "r")
xbm_data = file.read()
file.close()
disp:firstPage()
repeat
xbm_picture()
until disp:nextPage() == false
tmr.wdclr()
end
function bitmap_mailoff(delay)
file.open("mailoff", "r")
xbm_data = file.read()
file.close()
disp:firstPage()
repeat
xbm_picture()
until disp:nextPage() == false
tmr.wdclr()
end
init_i2c_display()
function checkmail()
user="YOURADDRESS@GOOGLEMAIL.COM"
pass="YOURGMAILPASSWD"
b64 = crypto.toBase64(user .. ":" .. pass)
-- print (b64)
local LED_PIN1 = 4
gpio.mode(LED_PIN1, gpio.OUTPUT)
conn=net.createConnection(net.TCP, 1)
conn:on("receive", function(sck, c)
-- print(c)
start1,stop1=string.find(c,"<fullcount>")
start2,stop2=string.find(c,"</fullcount>")
if start1 then
unread=string.sub(c,stop1+1,start2-1)
print ("Found " .. unread .. " unread Mails.")
if tonumber(unread) > 0 then
gpio.write(LED_PIN1, gpio.LOW)
conn:close() -- we got what we came for, so close
bitmap_mailon()
else
gpio.write(LED_PIN1, gpio.HIGH)
conn:close() -- no Mail, so close
bitmap_mailoff()
end
end
end )
conn:on("connection", function(conn)
print("connected")
conn:send("GET https://mail.google.com/mail/feed/atom/ HTTP/1.1\r\n" ..
"Host: mail.google.com\r\n"..
"Authorization: Basic " .. b64 .. "\r\n" ..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n")
end )
conn:on("disconnection", function(conn) print("disconnected") end )
conn:connect(443,"mail.google.com")
end
tmr.alarm(0,30000,tmr.ALARM_AUTO,checkmail)
Not much stuff is needed for that little project:
Assembly is done in no time at all. Just connect power and I2C leads. (For me, this works without pull-up resistors.)
|
Not a lot to do. |