/
ESP_Mailbox_Sketch.ino
54 lines (41 loc) · 1.95 KB
/
ESP_Mailbox_Sketch.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
#include <ESP8266WiFi.h>
#include <ESP8266SMTP.h>
const int sensor = 4; // pin for reed switch sensor... actually pin 2 but nodemcu is mislabeled ¯\_(ツ)_/¯
int state; // 0 (close) or 1 (open) reed switch
void setup()
{
pinMode(sensor, INPUT_PULLUP);
Serial.begin(74880);
delay(2000);
}
void loop()
{
state = digitalRead(sensor); // read sensor
if (state == HIGH) { // someone opened your mailbox
const char* ssID = "****your_network_name****"; // WIFI network name
const char* password = "****your_network_password****"; // WIFI network password (not very secure, but you can encrypt)
WiFi.begin(ssID, password);
delay(8000);
Serial.print(F("Connection Status is now: "));
Serial.println(WiFi.status());
SMTP.setEmail("****your_gmail_address****") // your outgoing gmail email address (for added security, use a dummy gmail account)(will need to turn on "use less secure apps" in gmail settings)
.setPassword("****your_gmail_password****") // your outgoing gmail password (not very secure, but you can encrypt)
.Subject("You've Got Mail!")
.setFrom("ESP8266_SMTP")
.setForGmail(); // simply sets port to 465 and setServer("smtp.gmail.com");
if(SMTP.Send("****your_receiving_address****", "You need to go get your mail now.")) { // email address you want message sent to
Serial.println(F("Message sent"));
} else {
Serial.print(F("Error sending message: "));
Serial.println(SMTP.getError());
}
delay(30000);
WiFi.disconnect(true); // disconnect WIFI to save battery
delay(30000);
Serial.print(F("Connection Status is now: "));
Serial.println(WiFi.status());
}
else {
// nothing to see here
}
}