#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "ThingSpeak.h"
#include <SPI.h>
#include <Ethernet.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int total_height = 30; // garbage bin height in CM
const int hold_height = 25; // garbage holding capacity (height) in CM
const int full_threshold = 100; // Garbage level threshold for full (in percentage)
const int empty_threshold = 0; // Garbage level threshold for empty (in percentage)
int Minute = 1; // Data update in min.
unsigned long Channel_ID = 987654; // Channel ID
const char *WriteAPIKey = "XTS8MPCWWZZSJISU"; // Your write API Key
const int trigger = 7;
const int echo = 6;
long Time;
int x;
int i;
int distanceCM;
int resultCM;
int bin_lvl = 100;
int snsr_to_max = 90;
const int Field_number = 1;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Ethernet shield MAC address
IPAddress ip(192, 168, 1, 177); // Arduino IP address
EthernetClient client;
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IoT Garbage lvl");
lcd.setCursor(0, 1);
lcd.print("Monitoring Sys.");
// Start the Serial for debugging
Serial.begin(9600);
// Initialize Ethernet and ThingSpeak
Ethernet.begin(mac, ip);
ThingSpeak.begin(client);
// Set pin modes
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
// Calculate the sensor to max distance
snsr_to_max = total_height - hold_height;
// Delay for initialization
delay(2500);
}
void loop() {
// Measure the garbage level
measure();
// Display the garbage level on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Garbage Level:");
// Check if the garbage level is full or empty
if (bin_lvl >= full_threshold) {
Serial.println("Garbage Level: Full");
lcd.setCursor(0, 1);
lcd.print("Full"); // Display full garbage level on LCD
} else if (bin_lvl <= empty_threshold) {
Serial.println("Garbage Level: Empty");
lcd.setCursor(0, 1);
lcd.print("Empty"); // Display empty garbage level on LCD
} else {
Serial.println("Garbage Level: ");
lcd.setCursor(5, 1);
lcd.print(bin_lvl);
lcd.print('%');
}
// Debug print to Serial
Serial.print("Garbage Level: ");
Serial.print(bin_lvl);
Serial.println("%");
// Upload the data to ThingSpeak
upload();
// Standby loop
for (i = 0; i < Minute; i++) {
Serial.println("-------------------------");
Serial.println("System Standby....");
Serial.print(i);
Serial.println(" Minutes elapsed.");
delay(60000); // 1 minute delay
}
}
void upload() {
// Write the data to ThingSpeak
x = ThingSpeak.writeField(Channel_ID, Field_number, bin_lvl, WriteAPIKey);
if (x == 200) {
Serial.println("Data Updated.");
} else {
Serial.println("Data upload failed, retrying....");
delay(15000);
static int retry_count = 0;
if (retry_count < 3) { // limit retries to 3 times
retry_count++;
upload();
} else {
Serial.println("Failed to upload data after multiple attempts.");
retry_count = 0; // reset retry counter
}
}
}
void measure() {
delay(100);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
Time = pulseIn(echo, HIGH);
distanceCM = (Time / 2) * 0.0343; // corrected distance calculation
resultCM = distanceCM;
bin_lvl = map(resultCM, snsr_to_max, total_height, 100, 0);
bin_lvl = constrain(bin_lvl, 0, 100);
}