Nội Dung Bài Viết
Giao tiếp hai Arduino qua sóng rf, thực hiện cuộc gọi với Module Sim
Hệ thống hoạt động cơ bản dựa trên sự truyền nhận tín hiệu giữa các trạm quan trắc 1, 2, 3, 4 với trạm trung tâm. Cảm biến tại các trạm quan trắc sẽ đo nhiệt độ môi trường, phát hiện khói có trong không khí, cập nhật liên tục trạng thái về trạm trung tâm. trạm trung tâm sẽ xử lý tín hiệu và đưa ra biện pháp tương ứng.
- Mức 0 : tương ứng với báo cháy, module sim sẽ thực hiện cuộc gọi, còi báo được kích hoạt.
- Mức 1 : tương ứng với nhiệt độ cao, còi báo được bật và hiển thị trạng thái lên lcd.
- Mức 2: tương ứng với có khói, còi báo được bật và hiển thị trạng thái lên lcd.
- Mức 3: tương ứng với an toàn, hiển thị trạng thái lên lcd.
Sơ đồ mạch kết nối trạm thu
Sơ đồ mạch kết nối trạm phát
Chương trình Arduino cho trạm phát và trạm thu
Code trạm phát:
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 | #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <SPI.h> #include <RF24.h> #include <nRF24L01.h> int relay = 4; int CB1 = 6; int khuA[1]; #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); LiquidCrystal_I2C lcd(0x27, 16, 2); RF24 radio(9,10); const uint64_t pipes[2] = { 0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL }; void setup() { Serial.begin(9600); lcd.begin(); lcd.backlight(); pinMode(CB1, INPUT); pinMode(relay, OUTPUT); dht.begin(); radio.begin(); radio.setAutoAck(1); radio.setRetries(1,1); radio.setDataRate(RF24_250KBPS); radio.setPALevel(RF24_PA_MAX); radio.setChannel(10); radio.openWritingPipe(pipes[0]); } void loop() { lcd.clear(); int khoi = digitalRead(CB1); float t = dht.readTemperature(); digitalWrite(relay, HIGH); if (khoi == HIGH) { if (t < 38 ) { khuA[0] = 2; // CO KHOI 2 radio.write(&khuA, sizeof(khuA)); digitalWrite(relay, LOW); } else { khuA[0] = 0; // CHAY 0 radio.write(&khuA, sizeof(khuA)); digitalWrite(relay, LOW); } } if (t > 38 ) { if (khoi == HIGH) { khuA[0] = 0; // CHAY 0 radio.write(&khuA, sizeof(khuA)); digitalWrite(relay, LOW); } else { khuA[0] = 1; // NHIET DO CAO 1 radio.write(&khuA, sizeof(khuA)); } } if ((t < 38 )&&(khoi == LOW)) { khuA[0] = 3; // AN TOAN 3 radio.write(&khuA, sizeof(khuA)); } lcd.setCursor(2, 0); lcd.print("Temp:"); lcd.setCursor(8, 0); lcd.print(t); lcd.setCursor(2, 1); lcd.print("Khoi:"); lcd.setCursor(9, 1); lcd.print(khoi); /*lcd.println(khuA[0]); Serial.println(khoi); Serial.println(t); */ delay(600); } |
Code trạm thu:
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 | #include <SPI.h> #include "RF24.h" #include "nRF24L01.h" #include <Wire.h> const int coi = 2; #include <SoftwareSerial.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x3f, 16, 2); SoftwareSerial serialSIM800(4, 5); int khuA[1]; //,temp2; RF24 radio(9,10); const uint64_t pipes[2] = { 0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL }; void setup() { Serial.begin(9600); serialSIM800.begin(9600); delay(1000); pinMode(coi, OUTPUT); radio.begin(); radio.setAutoAck(1); radio.setDataRate(RF24_250KBPS); radio.setChannel(10); radio.openReadingPipe(1, pipes[0]); radio.startListening(); lcd.begin(); lcd.backlight(); lcd.clear(); lcd.print("nRF24L01+ Temp"); delay(1000); lcd.clear(); lcd.print("Connecting....."); delay(1000); } void loop() { lcd.clear(); delay(500); if ( radio.available()) { Serial.println(khuA[0]); radio.read(&khuA, sizeof(khuA)); lcd.setCursor(3, 0); lcd.print("TINH TRANG"); if (khuA[0] == 0) // CHAY { lcd.setCursor(6, 1); lcd.print("CHAY"); digitalWrite(coi, HIGH); Gsm_MakeCall("0972648387"); } if (khuA[0] == 2) //CO KHOI { lcd.setCursor(5, 1); lcd.print("CO KHOI"); digitalWrite(coi, HIGH); } if (khuA[0] == 1) // NHIET DO CAO { lcd.setCursor(5, 1); lcd.print("NHIET DO CAO"); digitalWrite(coi, HIGH); } if (khuA[0] == 3) //AN TOAN { lcd.setCursor(5, 1); lcd.print("AN TOAN"); digitalWrite(coi, LOW); } } else { lcd.println("No radio available"); digitalWrite(coi, LOW); } delay(1000); } void Gsm_MakeCall(String phone) { serialSIM800.println("ATD" + phone + ";"); // Goi dien delay(10000); // Sau 10s serialSIM800.println("ATH"); // Ngat cuoc goi delay(1000); } |
Download source code và thư viện
Bài viết các bạn có thể tham khảo thêm: