-
Notifications
You must be signed in to change notification settings - Fork 0
/
BusOnTime.ino
169 lines (133 loc) · 3.88 KB
/
BusOnTime.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
169
#include <HTTPSRedirect.h>
#include <ESP8266WiFi.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 5, TXPin = 4;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
String latitude, longitude;
String lat_str, lng_str;
String location_tuple;
const char* ssid = "baba";
const char* password = "poiuytrewq";
// The ID below comes from Google Sheets, where the data will be posted
const char *GScriptId = "AKfycby0PSqsjIJskJucORycdCq-PkzTZS7Q9bv3giw5dxpt7xQymcQ";
// Push data on this interval
const int dataPostDelay = 2000; // 1 minutes = 1 * 60 * 1000;
const char* host = "script.google.com";
const char* googleRedirHost = "script.googleusercontent.com";
const int httpsPort = 443;
HTTPSRedirect client(httpsPort);
String bus_no = "CH-04-AG-8512";
// Prepare the url (without the varying data)
String url = String("/macros/s/") + GScriptId + "/exec?";
void setup()
{
Serial.begin(115200);
ss.begin(GPSBaud);
Serial.println("BUS ON TIME");
connectToWifi();
}
void connectToWifi(){
Serial.println("Connecting to wifi: ");
Serial.println(ssid);
Serial.flush();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" IP address: ");
Serial.println(WiFi.localIP());
Serial.print(String("Connecting to "));
Serial.println(host);
bool flag = false;
for (int i=0; i<5; i++){
int retval = client.connect(host, httpsPort);
if (retval == 1) {
flag = true;
break;
}
else
Serial.println("Connection failed. Retrying...");
}
// Connection Status, 1 = Connected, 0 is not.
Serial.println("Connection Status: " + String(client.connected()));
Serial.flush();
if (!flag){
Serial.print("Could not connect to server: ");
Serial.println(host);
Serial.println("Exiting...");
Serial.flush();
return;
}
}
// This is the main method where data gets pushed to the Google sheet
void postData(String tag, String Value)
//String tag, float value
{
//float Latitude, float Longitude, float PPM, float Temperature, float Humidity
if (!client.connected()){
Serial.println("Connecting to client again...");
client.connect(host, httpsPort);
}
String urlFinal = url + "&tag=" + String(bus_no) + "&Value=" + String(Value);
client.printRedir(urlFinal, host, googleRedirHost);
Serial.print("URL generated:-");
Serial.println(urlFinal);
}
void collect_gps_data()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
latitude = String(gps.location.lat(),4);
longitude = String(gps.location.lng(),4);
location_tuple = "(" + String(latitude) + "," + String(longitude) + ")";
Serial.print(latitude);
Serial.print(F(","));
Serial.print(longitude);
Serial.println(location_tuple);
postData(bus_no,location_tuple);
dataPostDelay;
}
else
{
Serial.print(F("INVALID"));
}
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(ss.read()))
collect_gps_data();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
/*
latitude = 36.75;
longitude = 76.76;
location_tuple = "(" + String(latitude) + "," + String(longitude) + ")";
Serial.println(location_tuple);
Serial.println(latitude);
Serial.println(longitude);
Serial.println(lat_str);
Serial.println(lng_str);
*/
/*
Serial.println(location_tuple);
postData(bus_no,location_tuple);
dataPostDelay;
*/
}