-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_on_esp.cpp
More file actions
118 lines (101 loc) · 2.73 KB
/
code_on_esp.cpp
File metadata and controls
118 lines (101 loc) · 2.73 KB
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
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#include <Arduino.h> //If executing from non-arduino IDE
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600; //GPS on 9600 baud rate
// repace your wifi username and password
const char* ssid = "ssid";
const char* password = "password";
unsigned long myChannelNumber = channel number;
const char* myWriteAPIKey = "Thingspeak write api key";
// The TinyGPS++ object
TinyGPSPlus gps;
WiFiClient client;
// The serial connection to the GPS device
SoftwareSerial gpsSerial(RXPin, TXPin);
void setup()
{
Serial.begin(115200);
gpsSerial.begin(GPSBaud);
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);
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
displayInfo();
}
void displayInfo()
{
// Serial.print(F("Location: "));
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(" 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();
}