Wednesday 6 July 2016

GMail notifier with ESP8266 / NodeMCU


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.






15 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. No, this is scripted in lua. And I use "esplorer" as an IDE. But it probably can be ported to C.

      Delete
    2. This comment has been removed by the author.

      Delete
  2. is this need any other files or firmware ?

    ReplyDelete
  3. Do you have outbound sending from ESP8266 to Gmail anywhere please.

    ReplyDelete
    Replies
    1. Yes, I have it here:
      http://www.areresearch.net/2016/04/how-to-send-emails-via-gmail-from.html
      The code is not very elegant or reliable, but does work ok.

      Delete
  4. can u please share arduino IDE code for this, please?

    ReplyDelete
  5. can u upload code for arduino IDE, please?

    ReplyDelete
  6. Hi, would this still work if it were flashed with the ESP8266 Deauther?

    https://github.com/spacehuhn/esp8266_deauther/releases

    Thanks

    ReplyDelete
    Replies
    1. You have to re-flash to switch between NodeMCU and the Deauther firmware (built in Arduino IDE).
      This is usually painless and you can reflash as often as you like. (In theory it wears out, but that is rather academic in this case.)

      Delete
  7. How do you make it so it checks every 5 minutes or so?

    ReplyDelete
  8. How can you make it so it checks the gmail every 5 minutes or so?

    ReplyDelete
    Replies
    1. The last line in the code listing triggers the check. You can adjust the timer value.

      Delete
  9. I get this error:

    sketch_jul09e:1:1: error: 'wifi' does not name a type
    1 | wifi.setmode(wifi.STATION)
    | ^~~~
    exit status 1
    'wifi' does not name a type

    ReplyDelete
  10. I get this error:
    sketch_jul09e:1:1: error: 'wifi' does not name a type
    1 | wifi.setmode(wifi.STATION)
    | ^~~~
    exit status 1
    'wifi' does not name a type

    ReplyDelete