Arduino Projects

DTMF Based Home Automation Using Arduino

Today we’re going to learn how to make DTMF Based Home Automation Using Arduino this is very intresting project As we know, the demand for home Automantion devices has increased over the past few years. There are so many products in our market.

DTMF Home Automation (Dual-tone multi-frequency signaling)  you can Control your home electronics appliances like bulb and water pump or any electronics devices from anywhere.  if you forgot to turn off the Bulb or other electronics appliances while going out, this DTMF Home Automation helps you to turn off the appliance with your phone call.

Required Material

  1. GSM Module 800l
  2. Arduino UNO
  3. Switch Button
  4. Relay
  5. Wires
  6. Bulb
  7. Holder

DTMF Based Home Automation Using Arduino Schematic Diagram

The Schematic diagram is very straightforward. You have to connect the SIM800L GSM module and Relay pins with Arduino pins as per the schematic diagram. The schematic diagram is shown below. DTMF Based Home Automation Using Arduino

Programming

First, download the code attached below. Then download the following libraries and install them.

  1. Adafruit Fona Library

After installation, all the libraries, set the correct board and COM port, then upload the below code.

Source Code

#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"

#define FONA_RX    2
#define FONA_TX    3
#define FONA_RST   4


const String PHONE = "+91xxxxxxxxxx"; //sender phone number here with country code

String smsStatus, senderNumber, receivedDate, msg, buff;
boolean isReply = false;
String dtmf_cmd;
boolean is_call = false;


#define FONA_RI_INTERRUPT  0
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);



#define button1_pin 4
#define button2_pin 5
#define button3_pin 6
#define button4_pin 7

#define relay1_pin 8
#define relay2_pin 9
#define relay3_pin 10
#define relay4_pin 11

boolean relay1_state = 1;
boolean relay2_state = 1;
boolean relay3_state = 1;
boolean relay4_state = 1;

void setup()
{
  pinMode(button1_pin, INPUT);
  pinMode(button2_pin, INPUT);
  pinMode(button3_pin, INPUT);
  pinMode(button4_pin, INPUT);

  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  pinMode(relay3_pin, OUTPUT);
  pinMode(relay4_pin, OUTPUT);


  digitalWrite(relay1_pin, relay1_state);
  digitalWrite(relay2_pin, relay2_state);
  digitalWrite(relay3_pin, relay3_state);
  digitalWrite(relay4_pin, relay4_state);
  Serial.begin(115200);


  fonaSS.begin(9600);
  if (! fona.begin(fonaSS)) {
    Serial.println(F("Couldn't find FONA"));
    while (1);
  }
  Serial.println(F("FONA is OK"));

  fona.println("AT");
  delay(500);
  fona.print ("AT+CSMP=17,167,0,0\r");
  fona.println("AT+DDET=1");
  delay(500);

  smsStatus = "";
  senderNumber = "";
  receivedDate = "";
  msg = "";

}

void loop()
{
  while (fona.available()) {
    buff = fona.readString();
    handle_sim800_response();
  }
  while (Serial.available())  {
    fona.println(Serial.readString());
  }

  // listen_iir();

  listen_push_buttons();

}

void handle_sim800_response()
{
  Serial.println(buff);
  if (is_call == true) {
    if (int index = buff.indexOf("+DTMF:") > -1 ) {
      index = buff.indexOf(":");
      dtmf_cmd = buff.substring(index + 1, buff.length());
      dtmf_cmd.trim();
      Serial.println("dtmf_cmd: " + dtmf_cmd);

      if (dtmf_cmd == "1")      control_relay(1);
      else if (dtmf_cmd == "2") control_relay(2);
      else if (dtmf_cmd == "3") control_relay(3);
      else if (dtmf_cmd == "4") control_relay(4);
    }
    if (buff.indexOf("NO CARRIER") > -1) {
      fona.println("ATH");
      is_call = false;
    }
  }
  else if (buff.indexOf("RING") > -1)
  {
    delay(2000);
    fona.println("ATA");
    is_call = true;
  }
  else if (buff.indexOf("+CMTI") > -1)
  {
    unsigned int index = buff.indexOf(",");
    String temp = buff.substring(index + 1, buff.length());
    temp = "AT+CMGR=" + temp + "\r";
    fona.println(temp);
  }
  else if (buff.indexOf("+CMGR") > -1) {
    extractSms();

    if (msg == "1")      control_relay(1);
    else if (msg == "2") control_relay(2);
    else if (msg == "3") control_relay(3);
    else if (msg == "4") control_relay(4);
    else if (msg.indexOf(".status") > -1) send_relay_status(msg.substring(0, 1));
    else if (msg.indexOf("del all") > -1) delete_all_sms();
  }
}

void extractSms() {
  unsigned int len, index;
  index = buff.indexOf("\r");
  buff.remove(0, index + 2);
  buff.trim();
  index = buff.indexOf(":");
  buff.substring(0, index);
  buff.remove(0, index + 2);
  index = buff.indexOf(",");
  smsStatus = buff.substring(1, index - 1);
  buff.remove(0, index + 2);

  senderNumber = buff.substring(0, 13);
  buff.remove(0, 19);

  receivedDate = buff.substring(0, 20);
  buff.remove(0, buff.indexOf("\r"));
  buff.trim();

  index = buff.indexOf("\n\r");
  buff = buff.substring(0, index);
  buff.trim();
  msg = buff;
  buff = "";
  msg.toLowerCase();
}

void listen_push_buttons() {
  if (digitalRead(button1_pin) == LOW) {
    delay(200);
    control_relay(1);
  }
  else if (digitalRead(button2_pin) == LOW) {
    delay(200);
    control_relay(2);
  }
  else if (digitalRead(button3_pin) == LOW) {
    delay(200);
    control_relay(3);
  }
  else if (digitalRead(button4_pin) == LOW) {
    delay(200);
    control_relay(4);
  }
}
void control_relay(int relay) {
  if (relay == 1) {
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, relay1_state);
    Serial.print("RelayState = ");
    Serial.println(relay1_state);
    delay(50);
  }
  else if (relay == 2) {
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, relay2_state);
    delay(50);
  }
  else if (relay == 3) {
    relay3_state = !relay3_state;
    digitalWrite(relay3_pin, relay3_state);
    delay(50);
  }
  else if (relay == 4) {
    relay4_state = !relay4_state;
    digitalWrite(relay4_pin, relay4_state);
    delay(50);
  }
}

void send_relay_status(String relay)
{
  Serial.println("Relay Number: " + relay);
  //(relay1_state):"ON"?"OFF"
  String sms_text = "";
  if (relay == "1") {
    sms_text = (relay1_state) ? "OFF" : "ON";
    sms_text = "Relay 1 is " + sms_text;
  }
  else if (relay == "2") {
    sms_text = (relay2_state) ? "OFF" : "ON";
    sms_text = "Relay 2 is " + sms_text;
  }
  else if (relay == "3") {
    sms_text = (relay3_state) ? "OFF" : "ON";
    sms_text = "Relay 3 is " + sms_text;
  }
  else if (relay == "4") {
    sms_text = (relay4_state) ? "OFF" : "ON";
    sms_text = "Relay 4 is " + sms_text;
  }
  Reply(sms_text);
}

void delete_all_sms()
{
  fona.println("AT+CMGD=1,4");
  delay(5000);
  while (fona.available()) {
    String response = fona.readString();
    if (response.indexOf("OK") > -1 ) {
      Reply("All sms are deleted");
    } else {
      Reply(response);
    }
  }
}


void Reply(String text)
{
  Serial.println(text);
  fona.print("AT+CMGF=1\r");
  delay(1000);
  fona.print ("AT+CSMP=17,167,0,0\r");
  fona.print("AT+CMGS=\"" + PHONE + "\"\r");
  delay(1000);
  fona.print(text);
  delay(100);
  fona.write(0x1A);
  //sim800.println((char)26);
  delay(1000);
  Serial.println("SMS Sent Successfully.");

  msg = "";
}

Final Testing

Now you will be able to Control Electronics appliance.

Ad

Related Articles

Leave a Reply

Your email address will not be published.

Ad
Back to top button