#include <TinyGPS++.h>
#include <TinyGPSPlus.h>
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
SoftwareSerial Gsm(2, 3);
TinyGPSPlus gps;
char phone_no[] = "9869118755";
#define trigPin 12
#define echoPin 13
#define Led_Green 8
#define Led_Yellow 9
#define Led_Red 10
void sendSMS();
void sendLocationSMS(float lat, float lon);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Gsm.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Led_Green, OUTPUT);
pinMode(Led_Yellow, OUTPUT);
pinMode(Led_Red, OUTPUT);
}
void loop() {
long duration, distance;
int max = 80;
float diff, perc;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
diff = max - distance;
perc = (diff / max) * 100;
if (perc <= 25) {
digitalWrite(Led_Green, HIGH);
delay(1000);
digitalWrite(Led_Green, LOW);
}
if (perc >= 50 && perc < 90) {
digitalWrite(Led_Yellow, HIGH);
delay(1000);
digitalWrite(Led_Yellow, LOW);
}
if (perc >= 90) {
Serial.println("Garbage Bin is FULL.");
delay(1000);
digitalWrite(Led_Red, HIGH);
delay(1000);
digitalWrite(Led_Red, LOW);
sendSMS();
} else {
Serial.print("Garbage Bin is Filled ");
Serial.print(perc);
Serial.println(" %.");
}
delay(500);
// GPS Data Parsing
bool newData = false;
for (unsigned long start = millis(); millis() - start < 1000;) {
while (mySerial.available()) {
char c = mySerial.read();
if (gps.encode(c))
newData = true;
}
}
if (newData && gps.location.isUpdated()) {
float flat = gps.location.lat();
float flon = gps.location.lng();
sendLocationSMS(flat, flon);
}
}
void sendSMS() {
Gsm.print("AT+CMGF=1\r");
delay(400);
Gsm.print("AT+CMGS=\"");
Gsm.print(phone_no);
Gsm.println("\"");
delay(300);
Gsm.print("The Garbage Bin is FULL.");
delay(200);
Gsm.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
}
void sendLocationSMS(float lat, float lon) {
Gsm.print("AT+CMGF=1\r");
delay(400);
Gsm.print("AT+CMGS=\"");
Gsm.print(phone_no);
Gsm.println("\"");
delay(300);
Gsm.print("Location: http://maps.google.com/maps?q=");
Gsm.print(lat, 6);
Gsm.print(",");
Gsm.print(lon, 6);
delay(200);
Gsm.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(20000);
}