-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPS-PushButton-LED-EmailAlert.CPP
150 lines (130 loc) · 3.98 KB
/
GPS-PushButton-LED-EmailAlert.CPP
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
#include <EMailSender.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
static const int RXPin = 12 , TXPin = 13;
static const uint32_t GPSBaud = 9600;
const char* ssid = " "; //enter wifi name
const char* password = " "; //enter wifi password
unsigned long myChannelNumber = ; //enter thingspeak channel number
const char * myWriteAPIKey = " "; //enter thingspeak API KEY
int ledpin = 5;
int button = 4;
int buttonState=0;
TinyGPSPlus gps;
WiFiClient client;
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
Serial.println(F("DeviceExample.ino"));
Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
Serial.print(F("Testing TinyGPS++ library v. "));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
ThingSpeak.begin(client);
pinMode(ledpin, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
buttonState=digitalRead(button); // put your main code here, to run repeatedly:
if (buttonState == 1)
{
digitalWrite(ledpin, HIGH);
EMailSender emailSend(" ", " "); //enter email address and password of that email account respectively
EMailSender::EMailMessage message;
message.subject = "Danger Alert !!!HELP!!!";
message.message = "Danger!!!<br> Go save her!!";
EMailSender::Response resp = emailSend.send(" ", message); //enter email address to which you want email alert to be send
Serial.println("Sending status: ");
Serial.println(resp.code);
Serial.println(resp.desc);
Serial.println(resp.status);
}
if (buttonState==0)
{
digitalWrite(ledpin, LOW);
}
}
void displayInfo()
{
if (gps.location.isValid())
{
double latitude = (gps.location.lat());
double longitude = (gps.location.lng());
String latbuf;
latbuf += (String(latitude, 6));
Serial.println(latbuf);
String lonbuf;
lonbuf += (String(longitude, 6));
Serial.println(lonbuf);
ThingSpeak.setField(1, latbuf);
ThingSpeak.setField(2, lonbuf);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" \n Date/Time: "));
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F("."));
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}