Last active
November 22, 2018 02:25
-
-
Save thienandangthanh/34a25c907b4d11dc6c6caa98b8ad05f9 to your computer and use it in GitHub Desktop.
A gas alert application using Arduino UNO R3, module SIM800A, MQ2 gas sensor, Buzzer and LED
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SIM800A-MQ2-Buzzer.ino | |
/* | |
Sơ đồ nối chân | |
SIM800A Arduino UNO | |
TX 2 | |
RX 3 | |
Power 4 | |
VCC VCC | |
GND GND | |
----------------------- | |
MQ2 Arduino UNO | |
AO A0 | |
VCC 5V | |
GND GND | |
----------------------- | |
Mạch còi Arduino UNO | |
I/O 11 | |
VCC 5V | |
GND GND | |
LED Arduino UNO | |
cực dương 10 | |
cực âm GND | |
*/ | |
// LED Cảnh báo | |
#define LED_pin 10 | |
// Còi Cảnh báo | |
#define BUZZER 11 | |
// Chân analog của cảm biến MQ2 | |
#define MQ2_Apin A0 | |
// Chân điều khiển bật tắt Module SIM800A | |
#define PWR_KEY 4 | |
// Giá trị ngưỡng để cảnh báo | |
#define WARNING 1500 | |
// Thoi gian gian cach giua cac lan nhan tin | |
#define SMS_INTERVAL 30000 // 5*60*1000 (ms) - 5 phut | |
unsigned long previousMillis = 0; | |
unsigned long currentMillis = 0; | |
// Số điện thoại cần cảnh báo | |
// thay đổi số điện thoại nhận tin nhắn tại đây | |
const String phone_number = "0123456789"; | |
// Biến chứa giá trị nồng độ đọc từ cảm biến MQ2 | |
int MQ2_val = 0; | |
void Alert_LED_BUZZER(); | |
void Gsm_Power_On(); // Bật module SIM800A | |
void Gsm_Init(); // Cấu hình module SIM800A | |
void Gsm_Call(); // Hàm gọi điện | |
void Gsm_sendSMS(); // Hàm nhắn tin cảnh báo | |
void setup() { | |
pinMode(MQ2_Apin, INPUT); | |
pinMode(LED_pin, OUTPUT); | |
// Cấu hình UART để giao tiếp module SIM800A | |
Serial.begin(9600); | |
// Khai báo chân PWR_KEY để bật/tắt module SIM800A | |
digitalWrite(PWR_KEY, LOW); | |
pinMode(PWR_KEY, OUTPUT); | |
delay(1000); | |
Gsm_Power_On(); // Bật Module SIM800A | |
Gsm_Init(); // Cấu hình module SIM800A | |
Gsm_Call(); // Test cuộc gọi | |
// chờ khoảng 20s để cảm biến gas ổn định | |
delay(19000); | |
} | |
void loop() { | |
// ĐỌC giá trị Analog của cảm biến MQ2 | |
MQ2_val = map(analogRead(MQ2_Apin), 0, 1023, 300, 10000); | |
if (MQ2_val >= WARNING) { | |
// nếu vượt ngưỡng báo động | |
// báo động bằng buzzer, LED | |
Alert_LED_BUZZER(); | |
currentMillis = millis(); | |
// Neu thoi gian phat hien co GAS giua 2 lan lien tiep >= SMS_INTERVAL | |
// thi tiep tuc gui tin nhan bao dong | |
if (currentMillis - previousMillis >= SMS_INTERVAL) { | |
// luu thoi diem nhan SMS | |
previousMillis = currentMillis; | |
// gửi tin nhắn SMS báo động | |
Gsm_sendSMS(); | |
} | |
} else { | |
// tắt LED, còi, tạm nghỉ 1.5s | |
analogWrite(LED_pin, LOW); | |
analogWrite(BUZZER, LOW); | |
delay(10); | |
} | |
} | |
void Alert_LED_BUZZER() { | |
// báo động bằng còi buzzer, LED | |
for (int i = 0; i < 255; i = i + 2) { | |
analogWrite(LED_pin, i); | |
analogWrite(BUZZER, i); | |
delay(10); | |
} | |
for (int i = 255; i > 1; i = i - 2) { | |
analogWrite(LED_pin, i); | |
analogWrite(BUZZER, i); | |
delay(5); | |
} | |
for (int i = 1; i <= 10; i++) { | |
analogWrite(LED_pin, 255); | |
analogWrite(BUZZER, 200); | |
delay(100); | |
analogWrite(LED_pin, 0); | |
analogWrite(BUZZER, 25); | |
delay(100); | |
} | |
analogWrite(LED_pin, LOW); | |
analogWrite(BUZZER, LOW); | |
} | |
void Gsm_Power_On() { | |
digitalWrite(PWR_KEY, HIGH); // Du chan PWR_KEY len cao it nhat 1s | |
delay(1500); // o day ta de 1,5s | |
digitalWrite(PWR_KEY, LOW); // Du chan PWR_KEY xuong thap | |
delay(10000); // xem trong Hardware designed SIM800A de hieu ro hon | |
} | |
void Gsm_Init() { | |
Serial.println("ATE0"); // Tat che do phan hoi (Echo mode) | |
delay(2000); | |
Serial.println("AT+IPR=9600"); // Dat toc do truyen nhan du lieu 9600 bps | |
delay(2000); | |
Serial.println("AT+CMGF=1"); // Chon che do TEXT Mode | |
delay(2000); | |
Serial.println("AT+CLIP=1"); // Hien thi thong tin nguoi goi den | |
delay(2000); | |
Serial.println("AT+CNMI=2,2"); // Hien thi truc tiep noi dung tin nhan | |
delay(2000); | |
} | |
void Gsm_Call() { | |
Serial.println("ATD" + phone_number + ";"); // Goi dien | |
delay(15000); // Sau 15s | |
Serial.println("ATH"); // Ngat cuoc goi | |
delay(2000); | |
} | |
void Gsm_sendSMS() { | |
Serial.println("AT+CMGS=\"" + phone_number + "\""); // Lenh gui tin nhan | |
delay(5000); // Cho ky tu '>' phan hoi ve | |
Serial.print("Phat hien co khi gas: "); // Gui noi dung | |
Serial.print(map(MQ2_val, 0, 1024, 300, 10000)); // gửi nồng độ gas | |
Serial.println(" ppm"); // đơn vị nồng độ | |
// Gui Ctrl+Z hay 26 de ket thuc noi dung tin nhan va gui tin di | |
Serial.print((char)26); | |
delay(5000); // delay 5s | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment