-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGmailNotifierWifi.ino
169 lines (128 loc) · 5.94 KB
/
GmailNotifierWifi.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright (C) 2018 Samuel Trassare (https://github.com/tiogaplanet)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This example sketch checks your Gmail account every minute and flashes
// MiP's chest LED to indicate new email.
#include <mip_esp8266.h>
// Include the HTTPS library.
#include <WiFiClientSecure.h>
// The following three variables must be configured by the user for the sketch to work. ////////////
// Enter the SSID for your wifi network.
const char* ssid = "..............";
// Enter your wifi password.
const char* password = "..............";
// The Base64 encoded version of your Gmail login credentials.
const char* credentials = "bWlwQGdtYWlsLmNvbTpBIHZlcnkgbG9uZyBwYXNzd29yZCwgaW5kZWVkLg==";
/////////////////////////////////////////////////////////////////////////////////////////////////////
// The hostname variable can be configured to the user's preference. ////////////////////////////////
const char* hostname = "MiP-0x01";
/////////////////////////////////////////////////////////////////////////////////////////////////////
// No other changes need to be made.
// Define some on and off times to flash the chest LED.
#define ON_TIME 1000
#define OFF_TIME 700
// MiP variables.
MiP mip;
bool connectResult;
// The Gmail server.
const char* host = "mail.google.com";
// The Gmail feed URL.
const char* url = "/mail/feed/atom";
// The port to connect to the email server.
const int httpsPort = 443;
// Keep track of new and older, unread emails.
int latestUnread;
int lastUnread;
// Store the last time Gmail was queried.
unsigned long previousMillis = 0;
// Check Gmail every minute (60000 milliseconds).
const long mailInterval = 60000;
// Don't let MiP disconnect from the esp8266. Send a read command every nine minutes.
const long keepAliveInterval = 540000;
void setup() {
// Establish the WiFi connection and connect to MiP.
connectResult = mip.begin(ssid, password, hostname);
if (!connectResult) {
Serial1.println(F("Failed connecting to MiP."));
return;
}
// Check email for the first time.
latestUnread = lastUnread = getUnread();
previousMillis = millis();
// Make sure the chest is solid before we loop.
mip.writeChestLED(0x00, 0xFF, 0x00);
}
void loop() {
ArduinoOTA.handle();
unsigned long currentMillis = millis();
unsigned long keepAliveMillis = currentMillis;
if (currentMillis - previousMillis >= mailInterval) {
latestUnread = getUnread();
if (latestUnread > lastUnread) {
Serial1.printf("You have %d new email%s and %d older, unread email%s.\n",
latestUnread - lastUnread, latestUnread - lastUnread == 1 ? "" : "s", lastUnread, lastUnread == 1 ? "" : "s");
MiPChestLED chestLED;
mip.readChestLED(chestLED);
// If the chest is already flashing, don't write to it again.
if (chestLED.offTime != OFF_TIME) {
// flash the chest to indicate new email.
mip.writeChestLED(0x00, 0xFF, 0x00, ON_TIME, OFF_TIME);
}
} else if (latestUnread < lastUnread) {
// User either read or deleted unread emails, so stop indicating new email.
Serial1.printf("You have %d unread message%s.\r\n", latestUnread, latestUnread == 1 ? "" : "s");
// Set the chest LED to solid green.
mip.writeChestLED(0x00, 0xFF, 0x00);
} else if (latestUnread == -1) {
Serial1.println(F("I could not access your email. I'll try again in a minute."));
}
lastUnread = latestUnread;
previousMillis = currentMillis;
}
// This sketch could possibly wait a long time before writing anything to MiP's chest LED. To keep
// MiP connected to the esp8266, read MiP's position every nine minutes.
if (currentMillis - keepAliveMillis >= keepAliveInterval) {
mip.readPosition();
keepAliveMillis = currentMillis;
}
}
// Get the number of unread emails in your Gmail inbox.
int getUnread() {
// Use WiFiClientSecure class to create a TLS (HTTPS) connection.
WiFiClientSecure client;
Serial1.printf("Connecting to %s:%d ... \r\n", host, httpsPort);
// Connect to the Gmail server on port 443.
if (!client.connect(host, httpsPort)) {
// If the connection fails, stop and return.
Serial1.println(F("Connection failed."));
return -1;
}
Serial1.printf("Requesting URL: %s%s\n", host, url);
// Send the HTTP request headers.
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");
Serial1.println(F("Request sent."));
int unread = -1;
while (client.connected()) { // Wait for the response. The response is in XML format
client.readStringUntil('<'); // read until the first XML tag
String tagname = client.readStringUntil('>'); // read until the end of this tag to get the tag name
if (tagname == "fullcount") { // if the tag is <fullcount>, the next string will be the number of unread emails
String unreadStr = client.readStringUntil('<'); // read until the closing tag (</fullcount>)
unread = unreadStr.toInt(); // convert from String to int
break; // stop reading
} // if the tag is not <fullcount>, repeat and read the next tag
}
Serial1.println(F("Connection closed."));
return unread;
}