-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32c3_main.ino
187 lines (153 loc) · 5.22 KB
/
esp32c3_main.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
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_AHTX0.h>
#include <Wire.h>
#include <WebSocketsServer.h>
#include <SPIFFS.h>
// Wi-Fi AP configuration
const char* apSSID = "IOT SmartEnviro";
const char* apPassword = "cuadra12345";
// Pins for motion detection
const int buzzerPin = 1;
const int ledPin = 4;
const int sensorPin = 5;
const unsigned long motionTimeout = 2000;
WebServer server(80);
WebSocketsServer webSocket(81);
Adafruit_AHTX0 aht;
bool sensorConnected = false;
String htmlContent;
// Timing constants
const unsigned long clientCheckInterval = 120000;
const unsigned long sensorPollInterval = 1000;
// Timing variables
unsigned long lastClientCheckMillis = 0;
unsigned long lastSensorCheck = 0;
unsigned long lastMotionTime = 0;
// Motion state
bool motionDetected = false;
bool apActive = true;
// Load HTML content once during setup
void loadHTMLContent() {
File file = SPIFFS.open("/index.html", "r");
if (file) {
htmlContent = file.readString();
file.close();
} else {
Serial.println("Failed to open index.html file");
}
}
// Handle root page request
void handle_OnConnect() {
String tempHtml = htmlContent;
tempHtml.replace("%TEMP%", "loading..");
tempHtml.replace("%HUMIDITY%", "loading..");
tempHtml.replace("%HEATINDEX%", "loading..");
server.send(200, "text/html", tempHtml);
lastClientCheckMillis = millis(); // Reset client check timer
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
// Calculate heat index
float calculateHeatIndex(float temperature, float humidity) {
if (humidity < 0 || humidity > 100 || temperature < -30 || temperature > 50) return NAN;
float T = (temperature * 9.0 / 5.0) + 32;
float HI = -42.379 + 2.04901523 * T + 10.14333127 * humidity
- 0.22475541 * T * humidity - 0.00683783 * T * T
- 0.05481717 * humidity * humidity
+ 0.00122874 * T * T * humidity
+ 0.00085282 * T * humidity * humidity
- 0.00000199 * T * T * humidity * humidity;
return (HI - 32) * 5.0 / 9.0;
}
// Send updates via WebSocket
void sendWebSocketUpdates(float temperature, float humidity) {
float heatIndex = calculateHeatIndex(temperature, humidity);
String json = "{\"temp\":\"" + String(temperature, 1) + "\","
"\"humidity\":\"" + String(humidity, 1) + "\","
"\"heatIndex\":\"" + String(heatIndex, 1) + "\"}";
webSocket.broadcastTXT(json);
}
// Monitor environment sensor
void checkEnvironmentSensor() {
if (millis() - lastSensorCheck < sensorPollInterval) return;
lastSensorCheck = millis();
if (!sensorConnected) return;
sensors_event_t humidityEvent, temperatureEvent;
aht.getEvent(&humidityEvent, &temperatureEvent);
if (isnan(temperatureEvent.temperature) || isnan(humidityEvent.relative_humidity)) {
Serial.println("Failed to read from AHT30 sensor!");
return;
}
static float lastTemperature = NAN;
static float lastHumidity = NAN;
if (temperatureEvent.temperature != lastTemperature || humidityEvent.relative_humidity != lastHumidity) {
lastTemperature = temperatureEvent.temperature;
lastHumidity = humidityEvent.relative_humidity;
sendWebSocketUpdates(lastTemperature, lastHumidity);
}
}
// Play buzzer pulse
void playBuzzerPulse() {
for (int i = 0; i < 5; i++) {
tone(buzzerPin, 3000);
delay(100);
noTone(buzzerPin);
delay(100);
}
}
// Check motion sensor
void checkMotionSensor() {
bool currentMotionState = digitalRead(sensorPin);
if (currentMotionState == HIGH && !motionDetected) {
Serial.println("Motion detected!");
motionDetected = true;
lastMotionTime = millis();
digitalWrite(ledPin, HIGH);
playBuzzerPulse();
}
if (motionDetected && millis() - lastMotionTime >= motionTimeout) {
Serial.println("Motion stopped!");
motionDetected = false;
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
WiFi.softAP(apSSID, apPassword);
Serial.print("AP IP Address: ");
Serial.println(WiFi.softAPIP());
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS mount failed");
return;
}
loadHTMLContent();
sensorConnected = aht.begin();
Serial.println(sensorConnected ? "AHT30 sensor initialized" : "AHT30 sensor not found");
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
webSocket.begin();
Serial.println("HTTP and WebSocket servers started");
}
void loop() {
unsigned long currentMillis = millis();
if (apActive) {
server.handleClient();
webSocket.loop();
checkEnvironmentSensor();
checkMotionSensor();
if (WiFi.softAPgetStationNum() > 0) {
lastClientCheckMillis = currentMillis;
} else if (currentMillis - lastClientCheckMillis >= clientCheckInterval) {
WiFi.softAPdisconnect(true);
Serial.println("No clients for 2 minutes. AP turned off.");
apActive = false;
}
}
}