-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduino Code
More file actions
104 lines (81 loc) · 2.55 KB
/
Arduino Code
File metadata and controls
104 lines (81 loc) · 2.55 KB
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
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
// WiFi Credentials
#define WIFI_SSID "Aditi"
#define WIFI_PASSWORD "adii2004"
// ThingSpeak API
#define CHANNEL_ID 2832905
#define API_KEY "NAHQQXA23U3KG9FM"
// Ultrasonic Sensor Pins
#define TRIG_PIN D5
#define ECHO_PIN D6
#define GREEN_LED_PIN D7
#define RED_LED_PIN D8
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
// Initialize sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nConnected to WiFi");
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
float distance = getBinLevel(); // Measure distance
if (distance == -1) {
Serial.println("Sensor Error: No echo received.");
}
else
{
float binHeight = 30.0;
float fillPercentage = ((binHeight - distance) / binHeight) * 100.0;
fillPercentage = constrain(fillPercentage, 0, 100);
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Fill Level: ");
Serial.print(fillPercentage);
Serial.println("%");
int response = ThingSpeak.writeField(CHANNEL_ID, 1, fillPercentage, API_KEY);
if (response == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.print("Error sending data: ");
Serial.println(response);
}
if (fillPercentage > 75) {
// Dustbin is almost full
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED
} else {
// Dustbin is not full
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green LED
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
}
}
delay(15000);
}
// Function to measure bin level using Ultrasonic Sensor
float getBinLevel() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 60000); // Increased timeout
if (duration == 0) {
return -1; // Return error value
}
float distance = duration * 0.034 / 2; // Convert time to distance (cm)
return distance;
}