@@ -24,10 +24,10 @@ const char* password = "DT1234dt";
24
24
WebServer server (80 ); // Using ESP32 WebServer
25
25
26
26
// 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
30
29
int sensorIdx = 0 ;
30
+ int iterationCount = 0 ; // new counter to track total iterations
31
31
32
32
// Stepper motor pins and settings with connection labels for ESP32
33
33
const int dirPin = 5 ; // Connect Stepper Direction to GPIO5
@@ -41,9 +41,16 @@ int motorSpeed = 600; // Delay in microseconds between steps
41
41
#define ULTRASONIC_ECHO_PIN 0 // Connect Ultrasonic Echo to GPIO0 (use caution on ESP32)
42
42
const float ULTRASONIC_DIST_THRESHOLD = 10.0 ; // Threshold in cm
43
43
44
- // Web endpoint handler for sensor data ( /data)
44
+ // Updated web endpoint handler for /data to always display the latest readings
45
45
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);
47
54
}
48
55
49
56
// New function to measure distance with the ultrasonic sensor
@@ -148,14 +155,9 @@ void loop() {
148
155
sensor.read ();
149
156
pressures[sensorIdx] = sensor.pressure ();
150
157
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
159
161
160
162
// Handle incoming client requests.
161
163
server.handleClient ();
0 commit comments