#include <WiFiClientSecure.h>   
#include <ESP8266WiFi.h>      
#include <ESP8266WiFiMulti.h>
#include <Nextion.h>

ESP8266WiFiMulti wifiMulti;    

const char* host = "mail.google.com"; 
const char* url = "/mail/feed/atom"; 
const int httpsPort = 443;

                                      // The SHA-1 fingerprint of the SSL certificate for the Gmail server (see below)
const char* fingerprint = "D3 90 FC 82 07 E6 0D C2 CE F9 9D 79 7F EC F6 E6 3E CB 8B B3";

                                      // The Base64 encoded version of your Gmail login credentials (see below)
const char* credentials = "ZW1haWwuYWRkcmVzc0BnbWFpbC5jb206cGFzc3dvcmQ=";
NexText tmail = NexText(0, 2, "t0");
const byte led = D3;

void setup() {
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');
  nexInit();
  pinMode(led, OUTPUT);

  wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");   // add Wi-Fi networks you want to connect to

  Serial.println("Connecting ...");
  int i = 0;
  while (wifiMulti.run() != WL_CONNECTED) {
    delay(250);
    Serial.print('.');
  }
  Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());           
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP()); 
  Serial.println('\n');
}

void loop() {
  int unread = getUnread();
  if (unread == 0) {
    tmail.setText("You've got no unread emails");
    digitalWrite(led, LOW);
  } else if (unread > 0) {
    tmail.setText("You've got %d new messages", unread);
    digitalWrite(led, HIGH);
  } else {
    tmail.setText("Could not get unread mails");
  }
  Serial.println('\n');
  delay(5000);
}

int getUnread() {    // a function to get the number of unread emails in your Gmail inbox
  WiFiClientSecure client; 
  Serial.printf("Connecting to %s:%d ... \r\n", host, httpsPort);
  if (!client.connect(host, httpsPort)) {  
    Serial.println("Connection failed");   
    return -1;
  }

  if (client.verify(fingerprint, host)) {   // Check the SHA-1 fingerprint of the SSL certificate
    Serial.println("Certificate matches");
  } else {                           
    Serial.println("Certificate doesn't match");
    return -1;
  }

  Serial.print("Requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" + 
               "Host: " + host + "\r\n" +
               "Authorization: Basic " + credentials + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n"); // Send the HTTP request headers

  Serial.println("Request sent");

  int unread = -1;

  while (client.connected()) {                         
    client.readStringUntil('<');                       
    String tagname = client.readStringUntil('>');       
    if (tagname == "fullcount") {                      
      String unreadStr = client.readStringUntil('<'); 
      unread = unreadStr.toInt();                    
      break;                                          
    }                                             
  }
  Serial.println("Connection closed");

  return unread;                                        // Return the number of unread emails
}
