Until there's support for the new OAuth2 protocol on the ESP, we'll have to use the old, less secure method.
Hardware
Connect an LED (+ resistor) to pin 13, as an unread email indicator.
The Code
#include
<
WiFiClientSecure
.
h
>
// Include the HTTPS library
#include
<
ESP8266WiFi
.
h
>
// Include the Wi-Fi library
#include
<
ESP8266WiFiMulti
.
h
>
// Include the Wi-Fi-Multi library
ESP8266WiFiMulti wifiMulti;
// Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
const
char
*
host
=
"mail.google.com"
;
// the Gmail server
const
char
*
url
=
"/mail/feed/atom"
;
// the Gmail feed url
const
int
httpsPort
=
443;
// the port to connect to the email server
// 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="
;
const
byte
led
=
13;
void
setup
() {
Serial
.
begin
(115200);
// Start the Serial communication to send messages to the computer
delay
(10);
Serial
.
println
(
'\n'
);
pinMode
(led
,
OUTPUT
);
wifiMulti
.
addAP(
"ssid_from_AP_1"
,
"your_password_for_AP_1"
);
// add Wi-Fi networks you want to connect to
wifiMulti
.
addAP(
"ssid_from_AP_2"
,
"your_password_for_AP_2"
);
wifiMulti
.
addAP(
"ssid_from_AP_3"
,
"your_password_for_AP_3"
);
Serial
.
println
(
"Connecting ..."
);
int
i
=
0;
while
(wifiMulti
.
run
()
!=
WL_CONNECTED) {
// Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of
the networks above
delay
(250);
Serial
.
(
'.'
);
}
Serial
.
println
(
'\n'
);
Serial
.
(
"Connected to "
);
Serial
.
println
(
WiFi
.
SSID
());
// Tell us what network we're connected to
Serial
.
(
"IP address:\t"
);
Serial
.
println
(
WiFi
.
localIP
());
// Send the IP address of the ESP8266 to the computer
Serial
.
println
(
'\n'
);
}
void
loop
() {
int
unread
=
getUnread();
if
(unread
==
0) {
Serial
.
println
(
"\r\nYou've got no unread emails"
);
digitalWrite
(led
,
LOW
);
}
else
if
(unread
>
0) {
Serial
.
printf
(
"\r\nYou've got %d new messages\r\n"
,
unread);
digitalWrite
(led
,
HIGH
);
}
else
{
Serial
.
println
(
"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
;
// Use WiFiClientSecure class to create TLS (HTTPS) connection
Serial
.
printf
(
"Connecting to %s:%d ... \r\n"
,
host
,
httpsPort);
if
(
!
client
.
connect
(host
,
httpsPort)) {
// Connect to the Gmail server, on port 443
Serial
.
println
(
"Connection failed"
);
// If the connection fails, stop and return
return
-
1;
}
if
(
client
.
verify(fingerprint
,
host)) {
// Check the SHA-1 fingerprint of the SSL certificate
Serial
.
println
(
"Certificate matches"
);