Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.5 kB
2
Indexable
Never
#include "DHT.h"
#include <SPI.h>
#include <MFRC522.h>

DHT dht(2, DHT22); // temperature and humidity pin
MFRC522 mfrc522(10, 9); // ss pin and rst pin for rfid

//bool one_time_sms;
String sms_data;

String dht_state = "IDLE";
String gas_state = "IDLE";
String motion_state = "IDLE";
bool is_armed = false;

void setup()
{
  pinMode(8, INPUT); // motion pin
  pinMode(5, OUTPUT); // buzzer pin
  pinMode(A1, OUTPUT); //red pin
  pinMode(A2, OUTPUT); // green pin
  pinMode(A3, OUTPUT); // blue pin
  dht.begin();
  Serial.begin(9600);
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  //one_time_sms = true;

  analogWrite(A2, 255); // green color
  analogWrite(A1, 0);
  analogWrite(A3, 0);
}

void loop()
{
  checkRFIDAccess();

  if(is_armed == true)
  {
    if(dht_state == "ALARM")
    {
      buzzerOn();
    } 
    else
    {
      readDHTdata();
      if(dht_state == "WARNING")
      {
        sendSMS();
        dht_state == "ALARM";
      }
    }

    if(motion_state == "ALARM")
    {
      buzzerOn();
    }
    else 
    {
      readMotiondata();
      if(motion_state == "WARNING")
      {
        sendSMS();
        motion_state == "ALARM";
      }
    }
  }
}

void sendSMS()
{
  Serial.println("Sending SMS...");               //Show this message on serial monitor
  Serial.print("AT+CMGF=1\r");                   //Set the module to SMS mode
  delay(100);
  Serial.print("AT+CMGS=\"+48783905878\"\r");
  delay(500);
  Serial.print(sms_data);
  delay(500);
  Serial.print((char)26);// (required according to the datasheet)
  delay(500);
  Serial.println();
  Serial.println("Text Sent.");
  delay(500);

}

bool readDHTdata()
{
  float temperature;
  float humidity;
  // to be configured
  float max_temp = 35.0;
  float min_temp = 0.0;
  float max_hum = 99.0;

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  temperature = dht.computeHeatIndex(temperature,humidity,false);

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return false;
  }

  if((temperature > max_temp || temperature < min_temp || humidity > max_hum) && dht_state == "IDLE")
  {
    dht_state = "WARNING";
    Serial.println("DHT sensor changes state to WARNING!");
  }
  
  sms_data = ("Temperature: " + String(temperature) + "*C " + " Humidity: " + String(humidity) + "%");

  // For testing purpose
  Serial.print("Humidity: "); 
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(temperature);
  Serial.print(" *C ");

  return true;
}

bool readMotiondata()
{
  int motion_value;
  motion_value=digitalRead(8); // motion pin
  
  if (isnan(motion_value)) {
    Serial.println("Failed to read from Gas sensor!");
    return false;
  }

  if(motion_value == HIGH && motion_state == "IDLE")
  {
    sms_data = ("Motion detected!");
    motion_state = "WARNING";
    Serial.println("Motion sensor changes state to WARNING!");
  }

  return true;
}


void checkRFIDAccess()
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  if (content.substring(1) == "A9 ED 0A B3") //card UID
  {
    is_armed = true;
    Serial.println("Alarm uzbrojony");
    Serial.println();
    analogWrite(A3, 255); // blue color
    analogWrite(A1, 0);
    analogWrite(A2, 0);
  }
  else if (content.substring(1) == "D9 6D 98 B8") //brelok UID
  {
    is_armed = false;
    dht_state = "IDLE";
    gas_state = "IDLE";
    motion_state = "IDLE";
    Serial.println("Alarm rozbrojony");
    Serial.println();
    analogWrite(A2, 255); // green color
    analogWrite(A1, 0);
    analogWrite(A3, 0);
  }
  else   
  {
    Serial.println(" Access denied");
  }
}

void buzzerOn()
{
  analogWrite(A1, 255); // red color
  analogWrite(A2, 0);
  analogWrite(A3, 0);
  digitalWrite(5,HIGH); // buzzer pin
  delayMicroseconds((1000000/2700)/2); // 2700 is a frequency in Hz
  digitalWrite(5,LOW); // buzzer pin
  delayMicroseconds((1000000/2700)/2); // 2700 is a frequency in Hz
}

//TODO kontrakton BT