-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
200 lines (159 loc) · 4.34 KB
/
Copy pathmain.c
File metadata and controls
200 lines (159 loc) · 4.34 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
//Định vị địa chỉ blynk từ thiết bị
#define BLYNK_TEMPLATE_ID "TMPL63b_PJpak"
#define BLYNK_TEMPLATE_NAME "Home"
#define BLYNK_AUTH_TOKEN "eKImRijv-XwHGJSAadlcjjiEWZGuZ0Pz"
//lệnh tắt các tính năng và tiết kiệm dung lượng
#define BLYNK_PRINT Serial
//khai báo các thư viện cần có để dùng blynk
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <HardwareSerial.h>
// Định nghĩa chân
#define DHTPIN 14 // Chân tín hiệu DHT11
#define DHTTYPE DHT11 // Loại cảm biến DHT
#define KY026_PIN 27 // Chân tín hiệu KY-026
#define PIR_PIN 35 // Chân tín hiệu PIR
#define BUZZER_PIN 25 // Chân tín hiệu buzzer
#define simSerial Serial2
#define MCU_SIM_BAUDRATE 115200
#define MCU_SIM_TX_PIN 17
#define MCU_SIM_RX_PIN 16
#define MCU_SIM_EN_PIN 15
#define PHONE_NUMBER "+84908994920"
DHT dht(DHTPIN, DHTTYPE);
//địa chỉ wifi, có thể nhận wifi phát từ điện thoại vẫn được
char ssid[] = "HCMUT1";
char pass[] = "diachi_bklms";
int stateWarning = 0;
BLYNK_WRITE(V0)
{
int pinValue = param.asInt();
if (stateWarning == 0)
{
if(pinValue == 1)
{
digitalWrite(BUZZER_PIN, HIGH);
Serial.printf("bat coi");
}
if(pinValue == 0)
{
digitalWrite(BUZZER_PIN, LOW);
Serial.printf("tat coi");
}
}
else
{
Blynk.virtualWrite(V1, 0);
}
}
void sim_at_wait()
{
delay(100);
while (simSerial.available())
{
Serial.write(simSerial.read());
}
}
bool sim_at_cmd(String cmd)
{
simSerial.println(cmd);
sim_at_wait();
return true;
}
bool sim_at_send(char c)
{
simSerial.write(c);
return true;
}
void sent_sms()
{
sim_at_cmd("AT+CMGF=1");
String temp = "AT+CMGS=\"";
temp += (String)PHONE_NUMBER;
temp += "\"";
sim_at_cmd(temp);
sim_at_cmd("Canh bao nguy hiem"); //có thể thay thế bằng từng trường hợp cụ thể như cháy hoặc trộm
sim_at_send(0x1A);
delay(5000);
}
void setup()
{
pinMode(MCU_SIM_EN_PIN,OUTPUT);
digitalWrite(MCU_SIM_EN_PIN,LOW);
delay(20);
Serial.begin(115200);
Serial.println("\n\n\n\n-----------------------\nSystem started!!!!");
// tạo delay 8s để khơi động nguồn
delay(8000);
simSerial.begin(MCU_SIM_BAUDRATE, SERIAL_8N1, MCU_SIM_RX_PIN, MCU_SIM_TX_PIN);
// kiểm tra lệnh AT
sim_at_cmd("AT");
// thông tin sản phẩm
sim_at_cmd("ATI");
// kiểm tra thẻ sim
sim_at_cmd("AT+CPIN?");
// kiểm tra chất lượng tín hiệu
sim_at_cmd("AT+CSQ");
sim_at_cmd("AT+CIMI");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Khởi tạo DHT11
dht.begin();
// Khởi tạo các chân
pinMode(KY026_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Tắt còi buzzer ban đầu
digitalWrite(BUZZER_PIN, LOW);
Serial.println("Hệ thống giám sát bắt đầu!");
}
void loop()
{
Blynk.run();
// Đọc nhiệt độ và độ ẩm từ DHT11
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity))
{
Serial.println("Khhong doc duoc du lieu tu DHT11!");
}
else
{
Serial.print("Nhiet do: ");
Serial.print(temperature);
Serial.print("°C, Đo am: ");
Serial.print(humidity);
Serial.println("%");
}
// Kiểm tra cảm biến lửa (KY-026)
int fireDetected = digitalRead(KY026_PIN);
// Kiểm tra cảm biến chuyển động (PIR)
int motionDetected = digitalRead(PIR_PIN);
Serial.print("Cam bien chuyen dong: ");
Serial.println(motionDetected);
Serial.print("Cam bien lua: ");
Serial.println(fireDetected);
// Kiểm tra và bật còi nếu phát hiện chuyển động hoặc lửa
if (fireDetected == 1)
{
Serial.println("Phat hien lua!");
digitalWrite(BUZZER_PIN, HIGH); // Bật còi
stateWarning = 1;
sent_sms();
delay(5000);
}
else if (motionDetected == 1)
{
Serial.println("Phat hien chuyen dong!");
digitalWrite(BUZZER_PIN, HIGH); // Bật còi
stateWarning = 1;
sent_sms();
delay(5000);
}
else
{
digitalWrite(BUZZER_PIN, LOW); // Tắt còi nếu không có sự kiện
}
delay(1000); // Đọc lại sau 1 giây
}