-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesp.c++
More file actions
332 lines (302 loc) · 9.59 KB
/
esp.c++
File metadata and controls
332 lines (302 loc) · 9.59 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <ArduinoJson.h>
#define GREEN_LED D1
#define RED_LED D2
#define BLUE_LED D3
#define BUZZER D4
#define LM35_PIN A0
const char* ssid = "ResQTemp";
const char* password = "11223344";
ESP8266WebServer server(80);
const float REAL_TEMP_THRESHOLD = 33.0;
const unsigned long HOLD_TIME_REAL = 5000UL;
const unsigned long HOLD_TIME_DEMO = 10000UL;
const unsigned long LOOP_PERIOD_MS = 500UL;
float temperature = REAL_TEMP_THRESHOLD - 5;
bool demoMode = false;
bool alertSent = false;
bool isGsmActive = false;
bool cancelCallFlag = false;
unsigned long tempStartTime = 0;
unsigned long lastLoopTime = 0;
const int EEPROM_DEMO_ADDRESS = 0;
const int EEPROM_LOG_COUNT_ADDRESS = 1;
String phoneNumber = "03218010098";
const char PAGE[] PROGMEM = R"====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ResQTemp</title>
<style>
body{margin:0;font-family:sans-serif;background:#0f2027;color:white;}
.container{max-width:420px;margin:auto;padding:20px}
.card{background:rgba(255,255,255,0.12);border-radius:18px;padding:25px;text-align:center;position:relative;box-shadow:0 4px 6px rgba(0,0,0,0.1);}
.temp{font-size:60px;font-weight:bold;margin-bottom:10px;}
.status{margin-top:12px;padding:10px;border-radius:10px;font-weight:600;}
.normal{background:#2ecc71;color:#000;}
.alert{background:#e74c3c}
.calling{background:#3498db}
.btn-group{margin-top:20px}
button{width:100%;padding:12px;border-radius:10px;border:0;background:#f1c40f;font-size:15px;margin-top:10px;cursor:pointer;transition:background 0.2s;}
button.reset-btn{background:#e74c3c;color:white;}
button.cancel{background:#e74c3c;color:white;}
.indicator{position:absolute;top:15px;right:15px;width:12px;height:12px;border-radius:50%;}
.indicator.connected{background:#2ecc71;}
.indicator.disconnected{background:#e74c3c;}
.mode-info{font-size:14px;color:rgba(255,255,255,0.7);margin-top:10px;}
</style>
</head>
<body>
<div class="container">
<h3>ResQTemp System</h3>
<div class="card">
<div class="indicator disconnected" id="conn"></div>
<div class="temp" id="temp">--</div>
<div class="status" id="status">Waiting...</div>
<div class="mode-info" id="modeInfo"></div>
<div class="mode-info" id="countdownInfo"></div>
<div class="btn-group">
<button id="demoBtn" onclick="toggleDemo()">Toggle Demo Mode</button>
<button onclick="window.location.reload()">Refresh</button>
<button class="reset-btn" onclick="resetEsp()">Reset ESP</button>
<button id="cancelBtn" class="cancel" onclick="cancelCall()" style="display:none;">Cancel Call</button>
</div>
<div style="font-size:13px;margin-top:15px">Location: NFC-IET Multan Lab 114 AI Depart</div>
</div>
</div>
<script>
let lastUpdate=0;
function updateUI(d){
lastUpdate=Date.now();
document.getElementById("conn").className="indicator connected";
document.getElementById("temp").innerHTML=d.temp+" °C";
let st=document.getElementById("status");
st.innerHTML=d.state;
st.className="status "+d.state.toLowerCase();
let demoBtn=document.getElementById("demoBtn");
let modeInfo=document.getElementById("modeInfo");
let countdownInfo=document.getElementById("countdownInfo");
let cancelBtn=document.getElementById("cancelBtn");
if(d.demo==="true"){
demoBtn.innerText="Disable Demo Mode";
modeInfo.innerHTML="Demo Mode Active (Simulation to "+d.limit+" °C)";
}else{
demoBtn.innerText="Enable Demo Mode";
modeInfo.innerHTML="Real Mode Active (Threshold: "+d.limit+" °C)";
}
if(d.state==="NORMAL") st.innerHTML="Normal";
if(d.state==="ALARM") st.innerHTML="Overheating Alert";
if(d.state==="CALLING") st.innerHTML="Emergency Alert Sent";
if(d.countdown>0 && d.state==="ALARM"){
countdownInfo.innerHTML="Alert in: "+d.countdown+" sec";
cancelBtn.style.display="block";
} else {
countdownInfo.innerHTML="";
cancelBtn.style.display="none";
}
}
function fetchData(){
fetch('/data').then(r=>r.json()).then(updateUI).catch(()=>document.getElementById("conn").className="indicator disconnected");
}
function toggleDemo(){ fetch('/demo').then(()=>fetchData()); }
function resetEsp(){ fetch('/reset').then(()=>setTimeout(()=>{window.location.reload();},2500)); }
function cancelCall(){ fetch('/cancelCall').then(()=>fetchData()); }
fetchData();
setInterval(fetchData,1500);
setInterval(()=>{ if(Date.now()-lastUpdate>3000) document.getElementById("conn").className="indicator disconnected"; },1000);
</script>
</body>
</html>
)====";
void runGsmSequence(){
static int gsmState=0;
static unsigned long stateStartTime=0;
if(cancelCallFlag){ // Stop GSM immediately
gsmState = 0;
cancelCallFlag = false;
isGsmActive = false;
return;
}
if(!isGsmActive) return;
unsigned long now = millis();
unsigned long dialDelay = demoMode ? HOLD_TIME_DEMO : HOLD_TIME_REAL;
switch(gsmState){
case 0:
stateStartTime = now;
gsmState = 1;
break;
case 1:
if(now - stateStartTime >= 500){
Serial.println("AT+CMGF=1");
stateStartTime = now;
gsmState = 2;
}
break;
case 2:
if(now - stateStartTime >= 500){
Serial.print("AT+CMGS=\"");
Serial.print(phoneNumber);
Serial.println("\"");
stateStartTime = now;
gsmState = 3;
}
break;
case 3:
if(now - stateStartTime >= 500){
Serial.println("ResQTemp Emergency: Temperature has crossed the safe limit!");
stateStartTime = now;
gsmState = 4;
}
break;
case 4:
if(now - stateStartTime >= 300){
Serial.write(26); // CTRL+Z
stateStartTime = now;
gsmState = 5;
}
break;
case 5:
if(now - stateStartTime >= dialDelay){
Serial.print("ATD");
Serial.print(phoneNumber);
Serial.println(";");
stateStartTime = now;
gsmState = 6;
}
break;
case 6:
if(now - stateStartTime >= 15000){
Serial.println("ATH");
isGsmActive = false;
gsmState = 0;
}
break;
default:
gsmState = 0;
break;
}
server.handleClient();
}
void startGsmAlert(){
if(!isGsmActive){
isGsmActive=true;
cancelCallFlag=false;
unsigned int count;
EEPROM.get(EEPROM_LOG_COUNT_ADDRESS,count);
count++;
EEPROM.put(EEPROM_LOG_COUNT_ADDRESS,count);
EEPROM.commit();
}
}
void handleRoot(){ server.send_P(200,"text/html",PAGE); }
void handleResetEsp(){ server.send(200,"text/plain","Restarting..."); delay(500); ESP.restart(); }
void handleData(){
StaticJsonDocument<200> doc;
float limit = REAL_TEMP_THRESHOLD;
String state = "NORMAL";
unsigned long holdTime = demoMode ? HOLD_TIME_DEMO : HOLD_TIME_REAL;
unsigned long remainingSec = 0;
if(isGsmActive) state = "CALLING";
else if(temperature>=limit){
state = alertSent ? "CALLING" : "ALARM";
if(tempStartTime>0) remainingSec = max(0UL,(holdTime-(millis()-tempStartTime))/1000UL);
}
doc["temp"] = String(temperature,1);
doc["demo"] = demoMode ? "true" : "false";
doc["state"] = state;
doc["limit"] = String(limit,1);
doc["countdown"] = remainingSec;
String json; serializeJson(doc,json);
server.send(200,"application/json",json);
}
void handleDemo(){
demoMode = !demoMode;
EEPROM.write(EEPROM_DEMO_ADDRESS, demoMode?1:0);
EEPROM.commit();
alertSent=false;
tempStartTime=0;
isGsmActive=false;
cancelCallFlag=false;
temperature = REAL_TEMP_THRESHOLD - 5;
server.send(200,"text/plain", demoMode?"DEMO_ON":"DEMO_OFF");
}
void handleCancelCall(){
if(isGsmActive || alertSent){
cancelCallFlag = true;
alertSent = false;
isGsmActive = false;
}
server.send(200,"text/plain","CALL_CANCELED");
}
void setup(){
Serial.begin(9600);
delay(1000);
EEPROM.begin(32);
demoMode = (EEPROM.read(EEPROM_DEMO_ADDRESS)==1);
pinMode(GREEN_LED,OUTPUT);
pinMode(RED_LED,OUTPUT);
pinMode(BLUE_LED,OUTPUT);
pinMode(BUZZER,OUTPUT);
digitalWrite(GREEN_LED,LOW);
digitalWrite(RED_LED,LOW);
digitalWrite(BLUE_LED,LOW);
digitalWrite(BUZZER,LOW);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
server.on("/", handleRoot);
server.on("/data", handleData);
server.on("/demo", handleDemo);
server.on("/reset", handleResetEsp);
server.on("/cancelCall", handleCancelCall);
server.begin();
}
void readTemperature(){
if(!demoMode) temperature = analogRead(LM35_PIN)*(3.3/1024.0)*100.0;
}
void simulateTempRise(){
if(demoMode) temperature = min(temperature + 0.5 * (LOOP_PERIOD_MS/1000.0), REAL_TEMP_THRESHOLD + 5.0);
}
void handleAlert(float limit, unsigned long hold){
unsigned long now = millis();
if(temperature < limit){
alertSent=false;
tempStartTime=0;
isGsmActive=false;
cancelCallFlag=false;
digitalWrite(GREEN_LED,HIGH);
digitalWrite(RED_LED,LOW);
digitalWrite(BLUE_LED,LOW);
digitalWrite(BUZZER,LOW);
} else {
digitalWrite(GREEN_LED,LOW);
digitalWrite(RED_LED,HIGH);
digitalWrite(BLUE_LED,LOW);
digitalWrite(BUZZER,HIGH);
if(tempStartTime==0) tempStartTime=now;
if(now-tempStartTime>=hold && !alertSent && !isGsmActive && !cancelCallFlag){
alertSent=true;
startGsmAlert();
}
if(alertSent){
digitalWrite(GREEN_LED,LOW);
digitalWrite(RED_LED,LOW);
digitalWrite(BLUE_LED,HIGH);
digitalWrite(BUZZER,LOW);
}
}
}
void loop(){
runGsmSequence();
server.handleClient();
unsigned long now = millis();
if(now - lastLoopTime >= LOOP_PERIOD_MS){
lastLoopTime = now;
readTemperature();
simulateTempRise();
unsigned long hold = demoMode ? HOLD_TIME_DEMO : HOLD_TIME_REAL;
handleAlert(REAL_TEMP_THRESHOLD, hold);
}
}