Giao tiếp hai Arduino qua sóng rf, thực hiện cuộc gọi với Module Sim

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

Giao tiếp hai Arduino qua sóng rf

Sơ đồ mạch kết nối trạm phát

Sơ đồ mạch kết nối trạm phát RFID

Chương trình Arduino cho trạm phát và trạm thu

Code trạm phát:

#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:

#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

Code master & slave
Thư viện

Bài viết các bạn có thể tham khảo thêm:

Viết một bình luận

This site uses Akismet to reduce spam. Learn how your comment data is processed.