Skip to content

Commit a039648

Browse files
Update Testing.ino: Expand sensor data storage to 120 iterations and enhance web endpoint for latest readings
1 parent 1f86154 commit a039648

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

Testing.ino

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const char* password = "DT1234dt";
2424
WebServer server(80); // Using ESP32 WebServer
2525

2626
// Sensor iteration variables
27-
String iterationData = "";
28-
float pressures[60]; // changed from 10 to 60 for 1 minute aggregation
29-
float temperatures[60]; // changed from 10 to 60 for 1 minute aggregation
27+
float pressures[120]; // changed size: now stores 120 iterations
28+
float temperatures[120]; // changed size: now stores 120 iterations
3029
int sensorIdx = 0;
30+
int iterationCount = 0; // new counter to track total iterations
3131

3232
// Stepper motor pins and settings with connection labels for ESP32
3333
const int dirPin = 5; // Connect Stepper Direction to GPIO5
@@ -41,9 +41,16 @@ int motorSpeed = 600; // Delay in microseconds between steps
4141
#define ULTRASONIC_ECHO_PIN 0 // Connect Ultrasonic Echo to GPIO0 (use caution on ESP32)
4242
const float ULTRASONIC_DIST_THRESHOLD = 10.0; // Threshold in cm
4343

44-
// Web endpoint handler for sensor data (/data)
44+
// Updated web endpoint handler for /data to always display the latest readings
4545
void handleData() {
46-
server.send(200, "text/plain", iterationData);
46+
int count = iterationCount < 120 ? iterationCount : 120;
47+
String data = "";
48+
// Starting at sensorIdx yields the oldest reading in the circular buffer.
49+
for (int i = 0; i < count; i++) {
50+
int idx = (sensorIdx + i) % 120;
51+
data += String(pressures[idx]) + "," + String(temperatures[idx]) + "\n";
52+
}
53+
server.send(200, "text/plain", data);
4754
}
4855

4956
// New function to measure distance with the ultrasonic sensor
@@ -148,14 +155,9 @@ void loop() {
148155
sensor.read();
149156
pressures[sensorIdx] = sensor.pressure();
150157
temperatures[sensorIdx] = sensor.temperature();
151-
sensorIdx++;
152-
if (sensorIdx >= 60) { // changed condition: now aggregates readings for 1 minute
153-
iterationData = "";
154-
for (int i = 0; i < 60; i++) {
155-
iterationData += String(pressures[i]) + "," + String(temperatures[i]) + "\n";
156-
}
157-
sensorIdx = 0;
158-
}
158+
159+
sensorIdx = (sensorIdx + 1) % 120; // update the circular buffer index
160+
iterationCount++; // track total iterations
159161

160162
// Handle incoming client requests.
161163
server.handleClient();

0 commit comments

Comments
 (0)