-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino IDE Code.txt
More file actions
128 lines (106 loc) · 4.13 KB
/
Arduino IDE Code.txt
File metadata and controls
128 lines (106 loc) · 4.13 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//* USE THIS CODE BY YOUR OWN LOGIC AS IT HAS HARDWARE CONNCETED SO MIGHT BE PINS ARE DIFFRENT AND CHNAGED.
#include <WiFi.h>
#include <WebServer.h>
// WiFi credentials
const char* ssid = "//Your wifi id here";
const char* password = "//password here";
// Web server on port 80
WebServer server(80);
// Pin definitions
const int trigPin = 14; // Ultrasonic sensor trig pin (P14)
const int echoPin = 27; // Ultrasonic sensor echo pin (P27)
const int ledPin = 26; // Pin for LED (P26)
// Variables
long duration;
int waterLevel;
bool motorState = false; // Motor (LED) initially OFF
// Offset for sensor height above the glass (in cm)
const int sensorOffset = 2; // Adjust this value as needed (2-4 cm)
// Handle root request
void handleRoot() {
server.send(200, "text/plain", "Water Level System is Online");
}
// Handle motor control request (LED in place of motor)
void handleMotor() {
String state = server.arg("state");
if (state == "on") {
motorState = true;
digitalWrite(ledPin, HIGH); // Turn LED ON (motor ON)
server.send(200, "text/plain", "Motor ON");
}
else if (state == "off") {
motorState = false;
digitalWrite(ledPin, LOW); // Turn LED OFF (motor OFF)
server.send(200, "text/plain", "Motor OFF");
}
else if (state == "check") { // Handle 'check' state to return current motor status
server.send(200, "text/plain", motorState ? "on" : "off");
}
else {
server.send(400, "text/plain", "Invalid Motor State");
}
}
// Handle water level request
void handleWaterLevel() {
waterLevel = getWaterLevel(); // Get updated water level
String waterLevelStr = String(waterLevel); // Convert to string
server.send(200, "text/plain", waterLevelStr); // Send water level as response
}
// Function to get water level using ultrasonic sensor
int getWaterLevel() {
// Send a 10us pulse to the trig pin to start measurement
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin and calculate distance (in cm)
duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2; // Calculate distance in cm
// Set maximum distance based on the glass height (8.5 cm) and sensor offset
int glassHeight = 8.5; // Height of the glass in cm
int effectiveHeight = glassHeight + sensorOffset; // Adjusted height for sensor placement
int level = map(distance, 0, effectiveHeight, 100, 0); // Map distance to percentage (0% = empty)
return constrain(level, 0, 100); // Ensure result stays between 0% and 100%
}
void autoControlMotor() {
waterLevel = getWaterLevel(); // Get updated water level
if (waterLevel < 40 && !motorState) {
motorState = true;
digitalWrite(ledPin, HIGH); // Turn LED ON (motor ON)
Serial.println("Motor turned ON automatically (Water < 40%)");
}
else if (waterLevel > 75 && motorState) {
motorState = false;
digitalWrite(ledPin, LOW); // Turn LED OFF (motor OFF)
Serial.println("Motor turned OFF automatically (Water > 75%)");
}
}
void setup() {
Serial.begin(115200);
// Initialize the pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is initially off
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP()); // Print the IP address
// Setup web server routes
server.on("/", handleRoot); // Root route
server.on("/motor", handleMotor); // Motor control route
server.on("/waterlevel", handleWaterLevel); // Water level route
server.begin();
Serial.println("Server started");
}
void loop() {
server.handleClient(); // Handle incoming client requests
autoControlMotor(); // Check and auto control motor based on water level
delay(2000); // Delay to prevent overloading the sensor checks
}