-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWater_flow_sen.ino
188 lines (154 loc) · 5.74 KB
/
Water_flow_sen.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
<<<<<<< HEAD
#include <WiFi.h>
#include <WebServer.h>
#define FLOW_SENSOR_PIN 2 // GPIO2 for flow sensor input
WebServer server(80);
volatile unsigned int pulseCount;
float flowRate;
float totalVolume;
const char* ssid = "Rithish";
const char* password = "88888888";
unsigned long prevMillis = 0;
const long interval = 2000; // interval to update readings (in milliseconds)
void pulseCounter() {
static unsigned long lastPulseTime = 0;
unsigned long currentMillis = millis();
if (currentMillis - lastPulseTime > 30) { // Debounce time increased to 30 milliseconds
pulseCount++;
lastPulseTime = currentMillis;
}
}
void updateFlowRateAndVolume() {
detachInterrupt(FLOW_SENSOR_PIN);
flowRate = pulseCount / 7.5; // Adjust this according to your sensor
totalVolume += (flowRate / 60.0) * (interval / 1000.0); // L/min * (min/interval) = L
pulseCount = 0;
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, FALLING);
}
void handleRoot() {
String message = "<!DOCTYPE html><html><head><title>Water Flow and Volume Sensor</title>";
message += "<script>function updateValues() {";
message += "var xhttp = new XMLHttpRequest();";
message += "xhttp.onreadystatechange = function() {";
message += "if (this.readyState == 4 && this.status == 200) {";
message += "var response = this.responseText.split(',');";
message += "document.getElementById('flowRate').innerHTML = response[0];";
message += "document.getElementById('totalVolume').innerHTML = response[1];";
message += "}};";
message += "xhttp.open('GET', '/data', true);";
message += "xhttp.send();";
message += "}";
message += "setInterval(updateValues, 1000);</script></head>";
message += "<body onload='updateValues()'>";
message += "<h1>Water Flow and Volume Sensor</h1>";
message += "<p>Flow Rate: <span id='flowRate'></span> L/min</p>";
message += "<p>Total Volume: <span id='totalVolume'></span> L</p>";
message += "</body></html>";
server.send(200, "text/html", message);
}
void setup() {
Serial.begin(115200);
pinMode(FLOW_SENSOR_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, FALLING);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/data", HTTP_GET, []() {
updateFlowRateAndVolume();
String responseData = String(flowRate, 2) + "," + String(totalVolume, 2);
server.send(200, "text/plain", responseData);
});
server.begin();
}
void loop() {
server.handleClient();
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= interval) {
updateFlowRateAndVolume();
prevMillis = currentMillis;
}
=======
#include <WiFi.h>
#include <WebServer.h>
#define FLOW_SENSOR_PIN 2 // GPIO2 for flow sensor input
WebServer server(80);
volatile unsigned int pulseCount;
float flowRate;
float totalVolume;
const char* ssid = "Rithish";
const char* password = "88888888";
unsigned long prevMillis = 0;
const long interval = 2000; // interval to update readings (in milliseconds)
void pulseCounter() {
static unsigned long lastPulseTime = 0;
unsigned long currentMillis = millis();
if (currentMillis - lastPulseTime > 30) { // Debounce time increased to 30 milliseconds
pulseCount++;
lastPulseTime = currentMillis;
}
}
void updateFlowRateAndVolume() {
detachInterrupt(FLOW_SENSOR_PIN);
flowRate = pulseCount / 7.5; // Adjust this according to your sensor
totalVolume += (flowRate / 60.0) * (interval / 1000.0); // L/min * (min/interval) = L
pulseCount = 0;
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, FALLING);
}
void handleRoot() {
String message = "<!DOCTYPE html><html><head><title>Water Flow and Volume Sensor</title>";
message += "<script>function updateValues() {";
message += "var xhttp = new XMLHttpRequest();";
message += "xhttp.onreadystatechange = function() {";
message += "if (this.readyState == 4 && this.status == 200) {";
message += "var response = this.responseText.split(',');";
message += "document.getElementById('flowRate').innerHTML = response[0];";
message += "document.getElementById('totalVolume').innerHTML = response[1];";
message += "}};";
message += "xhttp.open('GET', '/data', true);";
message += "xhttp.send();";
message += "}";
message += "setInterval(updateValues, 1000);</script></head>";
message += "<body onload='updateValues()'>";
message += "<h1>Water Flow and Volume Sensor</h1>";
message += "<p>Flow Rate: <span id='flowRate'></span> L/min</p>";
message += "<p>Total Volume: <span id='totalVolume'></span> L</p>";
message += "</body></html>";
server.send(200, "text/html", message);
}
void setup() {
Serial.begin(115200);
pinMode(FLOW_SENSOR_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(FLOW_SENSOR_PIN), pulseCounter, FALLING);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/data", HTTP_GET, []() {
updateFlowRateAndVolume();
String responseData = String(flowRate, 2) + "," + String(totalVolume, 2);
server.send(200, "text/plain", responseData);
});
server.begin();
}
void loop() {
server.handleClient();
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= interval) {
updateFlowRateAndVolume();
prevMillis = currentMillis;
}
>>>>>>> fecdb4e548eb5376a7f0a3354dad4b2ab7dbb154
}