-
Notifications
You must be signed in to change notification settings - Fork 4
/
smart.ino
339 lines (288 loc) · 11.3 KB
/
smart.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// #include <TinyGPS++.h>
// #include <SoftwareSerial.h>
// #include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
// #include <ESP8266HTTPClient.h>
// WiFiClient wifiClient;
// // WiFi credentials for hotspot (replace with desired name and password)
// const char* ssid = "MyServoControl";
// const char* password = "12345678";
// // Ultrasonic sensor setup (sensor 1)
// const int trigPin1 = D5;
// const int echoPin1 = D6;
// // Ultrasonic sensor setup (sensor 2)
// const int trigPin2 = D7; // Change pins as needed for sensor 2
// const int echoPin2 = D8; // Change pins as needed for sensor 2
// unsigned long currentTime = millis();
// unsigned long lastTime = currentTime;
// long duration[2]; // Array to store durations for both sensors
// int distance[2]; // Array to store distances for both sensors
// ESP8266WebServer server(80);
// TinyGPSPlus gps;
// SoftwareSerial SerialGPS(4, 5);
// float Latitude, Longitude;
// int year, month, date, hour, minute, second;
// String DateString, TimeString, LatitudeString, LongitudeString;
// void setup() {
// Serial.begin(115200);
// delay(10);
// // Configure WiFi hotspot
// WiFi.mode(WIFI_AP);
// WiFi.softAP(ssid, password);
// Serial.print("Connecting to hotspot... ");
// Serial.println(ssid);
// // Print hotspot IP address
// IPAddress ip = WiFi.softAPIP();
// Serial.print("IP address: ");
// Serial.println(ip);
// // Pin configuration for ultrasonic sensors and GPS
// pinMode(trigPin1, OUTPUT);
// pinMode(echoPin1, INPUT);
// pinMode(trigPin2, OUTPUT);
// pinMode(echoPin2, INPUT);
// SerialGPS.begin(9600); // Ensure correct baud rate for GPS module
// // Define server routes
// server.on("/", handleDistance);
// server.on("/gps", handleGPS); // New route for GPS data
// server.begin();
// }
// void loop() {
// server.handleClient(); // Handle incoming client requests
// // Read GPS data periodically (e.g., every second)
// if (millis() - lastTime >= 2000) { // Add a delay of 2 seconds
// lastTime = millis();
// if (gps.location.isUpdated()) {
// if (gps.location.isValid()) {
// // Process and format valid GPS data
// Latitude = gps.location.lat();
// LatitudeString = String(Latitude, 6);
// Longitude = gps.location.lng();
// LongitudeString = String(Longitude, 6);
// // ... (similar logic for date and time parsing)
// Serial.println("Valid GPS data received!"); // Print confirmation in serial monitor
// } else {
// Serial.println("Invalid GPS data received."); // Print error in serial monitor
// }
// } else {
// Serial.println("No new GPS data available.");
// }
// }
// }
// // Handle distance measurement and display on webpage
// void handleDistance() {
// // Measure distance for both sensors
// currentTime = millis();
// if (currentTime - lastTime >= 100) {
// lastTime = currentTime;
// // Sensor 1 measurement
// digitalWrite(trigPin1, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin1, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin1, LOW);
// duration[0] = pulseIn(echoPin1, HIGH);
// distance[0] = duration[0] * 0.034 / 2; // cm (adjust speed of sound if needed)
// // Sensor 2 measurement
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin2, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin2, LOW);
// duration[1] = pulseIn(echoPin2, HIGH);
// distance[1] = duration[1] * 0.034 / 2; // cm (adjust speed of sound if needed)
// // Send POST request with distance measurements
// if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
// HTTPClient http; //Declare object of class HTTPClient
// http.begin(wifiClient, "http://172.16.41.250/upload"); // Modify this line
// http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
// String httpRequestData = "distance1=" + String(distance[0]) + "&distance2=" + String(distance[1]); //Create the actual data string to be sent in the HTTP request body
// int httpResponseCode = http.POST(httpRequestData); //Send the actual POST request
// if(httpResponseCode>0){
// String response = http.getString(); //Get the response to the request
// Serial.println(httpResponseCode); //Print return code
// Serial.println(response); //Print request answer
// }else{
// Serial.print("Error on sending POST: ");
// Serial.println(httpResponseCode);
// }
// http.end(); //Free resources
// }else{
// Serial.println("Error in WiFi connection");
// }
// // Generate webpage content with distances
// String webpage = "<!DOCTYPE html><html><head><title>Ultrasonic Distances</title></head><body>";
// webpage += "<h1>Distance to Object (Sensor 1): ";
// webpage += String(distance[0]);
// webpage += " cm</h1>";
// webpage += "<h1>Distance to Object (Sensor 2): ";
// webpage += String(distance[1]);
// webpage += " cm</h1></body></html>";
// server.send(200, "text/html", webpage);
// }
// Serial.print("Sensor 1 Distance: ");
// Serial.println(distance[0]);
// Serial.print("Sensor 2 Distance: ");
// Serial.println(distance[1]);
// delay(100);
// }
// // Handle GPS data request and send formatted data
// void handleGPS() {
// if (gps.location.isValid()) {
// String gpsData = "{" // Send data as JSON
// "\"latitude\": " + LatitudeString + ","
// "\"longitude\": " + LongitudeString + ","
// "\"date\": \"" + DateString + "\","
// "\"time\": \"" + TimeString + "\""
// "}";
// server.send(200, "application/json", gpsData);
// } else {
// server.send(200, "text/plain", "No valid GPS data available.");
// }
// }
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
WiFiClient wifiClient;
// WiFi credentials for hotspot (replace with desired name and password)
const char* ssid = "MyServoControl";
const char* password = "12345678";
// Ultrasonic sensor setup (sensor 1)
const int trigPin1 = D5;
const int echoPin1 = D6;
// Ultrasonic sensor setup (sensor 2)
const int trigPin2 = D7; // Change pins as needed for sensor 2
const int echoPin2 = D8; // Change pins as needed for sensor 2
unsigned long currentTime = millis();
unsigned long lastTime = currentTime;
long duration[2]; // Array to store durations for both sensors
int distance[2]; // Array to store distances for both sensors
ESP8266WebServer server(80);
TinyGPSPlus gps;
SoftwareSerial SerialGPS(4, 5);
// Define fixed values for fallback (replace with your desired location)
const float defaultLatitude = 12.8396; // Example latitude (replace with yours)
const float defaultLongitude =80.1550; // Example longitude (replace with yours)
float Latitude, Longitude;
int year, month, date, hour, minute, second;
String DateString, TimeString, LatitudeString, LongitudeString;
void setup() {
Serial.begin(115200);
delay(10);
// Configure WiFi hotspot
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.print("Connecting to hotspot... ");
Serial.println(ssid);
// Print hotspot IP address
IPAddress ip = WiFi.softAPIP();
Serial.print("IP address: ");
Serial.println(ip);
// Pin configuration for ultrasonic sensors and GPS
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
SerialGPS.begin(9600); // Ensure correct baud rate for GPS module
// Define server routes
server.on("/", handleDistance);
// server.on("/gps", handleGPS); // New route for GPS data
server.begin();
}
void loop() {
server.handleClient(); // Handle incoming client requests
// Read GPS data periodically (e.g., every second)
if (millis() - lastTime >= 2000) { // Add a delay of 2 seconds
lastTime = millis();
if (gps.location.isUpdated()) {
if (gps.location.isValid()) {
// Process and format valid GPS data
Latitude = gps.location.lat();
LatitudeString = String(Latitude, 6);
Longitude = gps.location.lng();
LongitudeString = String(Longitude, 6);
// ... (similar logic for date and time parsing)
Serial.println("Valid GPS data received!"); // Print confirmation in serial monitor
} else {
// Use pre-defined values if no valid GPS data
Latitude = defaultLatitude;
LatitudeString = String(Latitude, 6);
Longitude = defaultLongitude;
LongitudeString = String(Longitude, 6);
}
} else {
// Use pre-defined values if no new data
Latitude = defaultLatitude;
LatitudeString = String(Latitude, 6);
Longitude = defaultLongitude;
LongitudeString = String(Longitude, 6);
}
}
}
// Handle distance measurement and display on webpage
void handleDistance() {
// Measure distance for both sensors
currentTime = millis();
if (currentTime - lastTime >= 100) {
lastTime = currentTime;
// Sensor 1 measurement
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration[0] = pulseIn(echoPin1, HIGH);
distance[0] = duration[0] * 0.034 / 2; // cm (adjust speed of sound if needed)
// Sensor 2 measurement
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration[1] = pulseIn(echoPin2, HIGH);
distance[1] = duration[1] * 0.034 / 2; // cm (adjust speed of sound if needed)
String response = "<!DOCTYPE html><html><head><title>Sensor Data</title></head><body><h1>Sensor Readings</h1>";
response += "<p>Distance (Sensor 1): ";
response += String(distance[0]);
response += " cm</p>";
response += "<p>Distance (Sensor 2): ";
response += String(distance[1]);
response += " cm</p>";
// Add GPS data
response += "<p>Latitude: ";
response += LatitudeString;
response += "</p>";
response += "<p>Longitude: ";
response += LongitudeString;
response += "</p>";
// ... (other sensor data or elements)
response += "</body></html>";
// Send webpage content to client
server.send(200, "text/html", response);
}
}
// Handle GPS data request and send formatted data
// void handleGPS() {
// if (gps.location.isValid()) {
// // Prepare JSON response with GPS data
// String response = "<!DOCTYPE html><html><head><title>GPS</title></head><body><h1>GPS Readings</h1>";
// response += "<p>Latitue: ";
// response += LatitudeString;
// response += "<p>Longitude: ";
// response += LongitudeString;
// response += "</body></html>";
// // Send webpage content to client
// server.send(200, "text/html", response);
// } else {
// String response = "<!DOCTYPE html><html><head><title>GPS</title></head><body><h1>GPS Readings</h1>";
// response += "<p>Latitue: ";
// response += LatitudeString;
// response += "<p>Longitude: ";
// response += LongitudeString;
// response += "</body></html>";
// // Send error message if no valid GPS data
// // server.send(500, "text/plain", "No valid GPS data available.");
// server.send(500, "text/html", response);
// }
// }