-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_StatisticalAnalysis.ino
More file actions
143 lines (128 loc) Β· 3.64 KB
/
extra_StatisticalAnalysis.ino
File metadata and controls
143 lines (128 loc) Β· 3.64 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Import required libraries
#include "WiFi.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Replace with your network credentials
const char* ssid = "My-2G";
const char* password = "Qwerty12";
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 4 // Digital pin connected to the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
const int numReadings = 1000; // Number of readings to collect
float readingsTemp[numReadings],readingsHumidity[numReadings]; // Arrays to store readings
float sum = 0;
float mean = 0,variance = 0,standardDeviation = 0,median = 0,mode_s = 0;
int i=0,count=1;
String readDHTTemperature() {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
//float t = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return "--";
}
else
return String(t);
}
String readDHTHumidity() {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return "--";
}
else
return String(h);
}
// Replaces placeholder with DHT values
String processor(const String& var){
if(var == "TEMPERATURE"){
return readDHTTemperature();
}
else if(var == "HUMIDITY"){
return readDHTHumidity();
}
return String();
}
void performStatisticalAnalysis(float arr[]) {
int len=sizeof(arr) / sizeof(float);
// Calculate the mean
sum = 0;
for (int j = 0; j < len; j++) {
sum += arr[j];
}
mean = sum / len;
// Calculate the variance
variance = 0;
for (int j = 0; j < len; j++) {
variance += pow((arr[j] - mean), 2);
}
variance /= len;
// Calculate the standard deviation
standardDeviation = sqrt(variance);
for (int j = 0; j < len - 1; j++) {
for (int k = j+1; k < len; k++) {
if (arr[j] > arr[k]) {
float temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
}
}
median = (len % 2 == 0) ? (arr[(len/ 2) - 1] + arr[len/ 2]) / 2 : arr[(int)(len/ 2)];
// Calculate the mode
int maxCount = 0;
for (int i = 0; i < len; i++) {
int c = 0;
for (int j = 0; j < len; j++) {
if (arr[j] == arr[i])
c++;
}
if (c > maxCount) {
maxCount = c;
mode_s = arr[i];
}
}
Serial.print("Mean: ");
Serial.print(mean);
Serial.print(", Variance: ");
Serial.print(variance);
Serial.print(", Standard Deviation: ");
Serial.print(standardDeviation);
Serial.print(", Median: ");
Serial.print(median);
Serial.print(", Mode: ");
Serial.println(mode_s);
}
void setup(){
Serial.begin(115200);
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
}
void loop(){
readingsTemp[i]=readDHTTemperature().toFloat();
readingsHumidity[i]=readDHTHumidity().toFloat();
Serial.print("count= ");
Serial.println(count);
i++;
count++;
delay(500);
if(i==numReadings)
{
Serial.println("Analysis of Temperature: ---");
performStatisticalAnalysis(readingsTemp);
Serial.println("Analysis of Humidity: ---");
performStatisticalAnalysis(readingsHumidity);
while(1);
}
}