-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDustbin_Monitoring_System.ino
189 lines (159 loc) · 4.19 KB
/
Dustbin_Monitoring_System.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
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
const int trigPin =D3;
const int echoPin = D4;
// defines variables
long duration;
int distance;
TinyGPSPlus gps; // The TinyGPS++ object
SoftwareSerial ss(D1, D2); // The serial connection to the GPS device
#define FIREBASE_HOST "example.firebaseio.com"
#define FIREBASE_AUTH "yourFirebaseaAuth"
const char* ssid = "SSID";
const char* password = "PASSWORD";
float latitude , longitude;
int year , month , date, hour , minute , second;
String date_str , time_str , lat_str , lng_str;
int pm;
WiFiServer server(80);
void setup()
{
Serial.begin(9600);
ss.begin(9600);
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");
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
{
if (gps.location.isValid())
{
latitude = gps.location.lat();
lat_str = String(latitude , 6);
longitude = gps.location.lng();
lng_str = String(longitude , 6);
}
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if (distance>50)
{
Serial.print("Empty");
Firebase.setString("message", "empty");
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
return;
}
}
else if(distance>40&&distance<50)
{
Serial.println("quater");
Firebase.setString("message", "quater");
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
return;
}
}
else if(distance>30&&distance<40)
{
Serial.println("half");
Firebase.setString("message", "half");
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
return;
}
}
else if(distance>20&&distance<30)
{
Serial.println("almost_full");
Firebase.setString("message", "almost_full");
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
return;
}
}
else if(distance<20)
{
Serial.println("full");
Firebase.setString("message", "full");
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
return;
}
}
delay(1000);
while (ss.available() > 0)
if (gps.encode(ss.read()))
{
if (gps.location.isValid())
{
latitude = gps.location.lat();
Serial.print("lat: ");
Serial.print(latitude,6);
Serial.print("\t");
lat_str = String(latitude,6);
longitude = gps.location.lng();
Serial.print(" long: ");
Serial.print(longitude,6);
Serial.print("\t");
lng_str = String(longitude,6);
}
}
// Check if a client has connected
// WiFiClient client = server.available();
// if(ss.available() == 0)
// {Serial.print ("no gps");
// delay(1000);
// Serial.print('\n');
// }
if (!client)
{
return;
}
delay(100);
}