EMBEDDED TWEAKS

Embedded Electronics Tutorials, Coding and lot more

Controlling electrical Appliances via SMS

This device can enable you to control your home appliances by just an SMS message. You can also monitor some data like temperature, gas leakage, door status etc.  I am using a PIC microcontroller PIC16f887 for this purpose. I written the code in Mplab X IDE and XC8 compiler. Here for demo I have attached a gas, light, door, temperature sensor, Rain sensor. LM35 is used as temperature sensor.

App

SIM900 GSM modem:

 I am decoding the incoming SMS from SIM900 GSM modem. GSM/GPRS Modem-RS232 is built with Dual Band GSM/GPRS engine- SIM900A, works on frequencies 900/ 1800 MHz. The Modem is coming with RS232 interface, which allows you connect PC as well as microcontroller with RS232 Chip(MAX232). The baud rate is configurable from 9600-115200 through AT command. So many vendors are providing GSM modems. I am showing one of that a Quectel GSM modem below:

GSM-GPRS-MMS

 Here I am explaining incoming AT command for SMS. Whenever new SMS is received a notification code comes such as:

+CMTI: “SM”,3                                  //means Indicates that new message has been received and saved to  <index>=3 of “SM”

Then if we want to read sms, then we need to send AT command below:

AT+CMGR=3                                   // Read message

Then the GSM modem replies as follows:

+CMGR: “REC UNREAD”,”+8615021012496″,””,”2010/09/25 15:06:37+32″,145,4,0,241,”+8613800210500″,145,27

This is a test SMS. You text is displayed here

OK

Here we need to decode the 2nd line only to decode the text data. Hence we write code to flush the bytes till the first line. Means till getting 1st CR and LF we don’t store the data. After that we start saving data.

Sensors:

GAS Sensor: Now Gas sensor modules are available from so many vendors in the market at electronics shops or online. Here i am showing one GASof them. You can use any one. but once you need to check if it is working on +ve logic or -ve logic. I am showing one +ve logic sensor that is availabe here from parallax. Click here . Gas Sensor Module is designed to detect the presence of a dangerous LPG leak in your Home, car or in a service station, storage tank environment by interfacing with Microcontroller without ADC Channels and programming. In this version of LPG Gas sensor module two pots are included, one for trigger level setting and the other for setting sensitivity of the sensor. It allows to determine when a preset LPG gas level has been reached or exceeded. The module uses MQ-6 sensor to sense LPG leak. The MQ-6 can detect gas concentrations anywhere from 200 to 10000 ppm.

A high on OUT pin indicates  LPG presence. This pin can be  connected to the Digital I/O pin of the  microcontroller.

Light sensor: This is an easy to use Light sensor module based on the LDR. Capable to also vary the range of detection. Sample light sensor is shown below.xbhgiw1350351695996

Pin outs:  External 3.3V-5V: VCC

                   External GND: GND

                   DO: digital output interface, a small plate (0 and 1)

                   AO: small board analog output interface

Snow-Raindrops-Detection-Sensor-Module-Rain-Weather-Module-Humidity-For-ArduinoRain sensor: In the rain sensor shown below, when connected to 5V power supply, power light is on, when there is no water droplets on the induction plate, DO output is high, the switch light is off, and drops on a drop of water, DO for low-level output, switch light is on, Brush away water drops, but it returned to output high level State.

        Pin outs:       VCC: supply cathode (3-5V)

                               GND: connect to power negative

                                DO:TTL switch signal output

                                AO: analog signal output

Reed switch as a door sensor:u2s5l3_01

An ordinary switch has two electrical contacts in it that join together when you push a button and spring apart when you release it. Same way we can turn on or off the reed switch via keeping a magnet near to it.

How does a reed switch work?

In a reed switch, the two contacts (which look like metal reeds) are made from magnetic material and housed inside a thin glass envelope. (You can see this quite clearly in our top photo.) As you bring a magnet up to a reed switch, it magnetizes the contacts in opposite ways so they attract and spring together and a current flows through them. A reed switch like this is normally open (NO) (normally off), unless a magnet is positioned right next to it, when it switches on:

reed-switch

Take the magnet away and the contacts—made from fairly stiff and springy metal—push apart again and return back to their original positions.

LM35 Sensor:

LM35_500x500_1_The LM35 temperature sensor provides an output of 10mV per degree Celsius, with an accuracy of 0.5°C at 25°C. It can be powered by any DC voltage in the range 4 – 30v. The operating range is –55°C to +150°c.

A/D CONVERSION PROCEDURE :
This is an example procedure for using the ADC to perform an Analog-to-Digital conversion:
1. Configure Port:
• Disable pin output driver (See TRIS register)
• Configure pin as analog
2. Configure the ADC module:
• Select ADC conversion clock
• Configure voltage reference
• Select ADC input channel
• Select result format
• Turn on ADC module
3. Wait the required acquisition time(2).
4. Start conversion by setting the GO/DONE bit.
5. Wait for ADC conversion to complete by one of
the following:
• Polling the GO/DONE bit
6. Read ADC Result
ADC REGISTER DEFINITIONS: The following registers are used to control the operation of the ADC.

Block diagram & Circuit diagram:

 BLKDGM

ckt

 Programming section:

#include <xc.h>
#include <GenericTypeDefs.h>
#include “usart_pic16.h”
#include “LCD.h”
#include “ADC.h”
#include “lm35_pic16.h”

// Configuration Byte
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)

#define CheckCMTI 0
#define CheckCMGR 1
#define GAS_SENSOR RB0
#define PARKING_SENSOR RB1
#define RAIN_SENSOR RB2
#define LIGHT_SENSOR RB3
#define Light RC3
#define Plug RC4
#define Fan RC5
#define BUZZER RD0
#define LIGHT_LED RD1
#define PARKING_LED RC0
#define RAIN_LED RC1
#define MAIN_LOCK RC2
volatile unsigned char Data[17]={0};
volatile unsigned char Data1[78]={0};
unsigned char p = 0;
volatile bit GSMFuncFlag = 0;
volatile bit Notification1 = 0, Notification2 = 0;

void main()
{
unsigned int t = 0;
static unsigned int lfcounter=0;
USARTInit(9600);
//Write intro text
LCDInitialize();
unsigned long ADC_value = 0;
unsigned int digit1, digit2, digit3, digit4;
ADC_Init();
__delay_ms(20);
ANSELH = 0x00;
TRISB = 0x0F;
PARKING_LED = 1;
Light = 1;
Plug = 1;
Fan = 1;
__delay_ms(5000);
PARKING_LED = 0;
Light = 0;
Plug = 0;
Fan = 0;
__delay_ms(5000);

LCDWriteString(“Initializing..”);
__delay_ms(300);
LCDRow2();
LCDWriteString(“Testing LCD”);
__delay_ms(300);

LCDClear();
LCDWriteString(“Testing Serial”);
LCDRow2();
LCDWriteString(“Port”);

LCDClear();
LCDWriteString(“Testing GSM”);
LCDRow2();
LCDWriteString(“Modem”);
__delay_ms(700);

LCDClear();
LCDWriteString(“Configuring GSM”);
LCDRow2();
LCDWriteString(“Modem”);

USARTWriteLine(“ATr”);
__delay_ms(300);
USARTWriteLine(“AT&Fr”);
__delay_ms(300);
USARTWriteLine(“AT+CMGF=1r”);
__delay_ms(300);
USARTWriteLine(“AT+CSCS=”GSM”r”);
__delay_ms(300);
USARTWriteString(“AT+CMGD=1,4r”);
__delay_ms(300);
USARTWriteString(“ATD8460491511;r”);
__delay_ms(6000);
USARTWriteString(“ATHr”);
__delay_ms(300);
LCDClear();
LCDWriteString(“All OK”);
LCDRow2();
LCDWriteString(“Ready…..”);
__delay_ms(700);

LCDClear();
LCDWriteString(” ***Welcome***”);
__delay_ms(700);

LCDClear();
LCDWriteString(“Waiting for SMS”);
LCDRow2();
LCDWriteString(“……”);
PARKING_LED = 0;
Light = 0;
Plug = 0;
Fan = 0;

while(1)
{
//Get the amount of data waiting in USART queue
uint8_t n= USARTDataAvailable();
//If we have some data
{
while(GAS_SENSOR)
{
BUZZER = 1;
LCDClear();
LCDWriteString(“GAS detected”);
}
// else
// BUZZER = 0;
}
{
if(!LIGHT_SENSOR)
LIGHT_LED = 1;
else
LIGHT_LED = 0;
}
{
if(PARKING_SENSOR)
PARKING_LED = 1;
else
PARKING_LED = 0;
}
{
if(RAIN_SENSOR)
RAIN_LED = 1;
else
RAIN_LED = 0;
}
if(n!=0)
{
//Read it
char ch=USARTReadData();
if(GSMFuncFlag == CheckCMTI)
{
if(ch == 0x0a)
{
Data[p] = ”;
Notification1 = 1;
p = 0;
}
else
{
Data[p] = ch;
p++;
}
}
else
{
Data1[p] = ch;
p++;
if(ch == 0x0a)
lfcounter++;
if(lfcounter == 4)
{
Data1[p] = ”;
p = 0;
Notification2 = 1;
lfcounter = 0;
}
}

}

if(Notification1)
{
if ((Data[0] == ‘+’) && (Data[1] == ‘C’) && (Data[2] == ‘M’) && (Data[3] == ‘T’) && (Data[4] == ‘I’))
{
LCDClear();
LCDWriteString(“New SMS”);
__delay_ms(500);
USARTWriteString(“AT+CMGR=1r”);
__delay_ms(1000);
GSMFuncFlag = CheckCMGR;
}
if ((Data[0] == ‘R’) && (Data[1] == ‘I’) && (Data[2] == ‘N’) && (Data[3] == ‘G’))
{
LCDClear();
LCDWriteString(“Incoming Call…”);
__delay_ms(1000);
USARTWriteString(“ATHr”);
__delay_ms(10);
ADC_value = (ADC_Read(0)/2)+5;
__delay_ms(500);
digit1 = (unsigned int)(ADC_value/1000); // Calculate digit1 of ADC_value
digit2 = (unsigned int)((ADC_value – digit1*1000)/100); // Calculate digit2 of ADC_value
digit3 = (unsigned int)((ADC_value – (digit1*1000+digit2*100))/10); // Calculate digit3 of ADC_value
digit4 = (unsigned int)(ADC_value – (digit1*1000+digit2*100+digit3*10)); // Calculate digit4 of ADC_value

/*Display on LCD*/

LCDClear();
LCDWriteString(“GAS Door Temp.”);
LCDRow2();
if (GAS_SENSOR == 1)
LCDWriteString(“Pr. “);
else
LCDWriteString(“Abs. “);

if (PARKING_SENSOR == 0)
LCDWriteString(“Open “);
else
LCDWriteString(“Close “);

LCDWriteData(digit3+0x30); // Display digit1 of ADC_value on LCD
__delay_ms(1);
LCDWriteData(digit4+0x30); // Display digit1 of ADC_value on LCD
__delay_ms(1);
LCDWriteData(0xdf); // Degree symbol value 0xDF in Hexadecinal
LCDWriteString(“C “);

/*Send via SMS*/

USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″rn”);
__delay_ms(1000);
USARTWriteString(“Gas “); // Display digit1 of ADC_value on LCD
__delay_ms(1);
if (GAS_SENSOR == 1)
USARTWriteString(“Present “);
else
USARTWriteString(“Absent “);
USARTWriteString(“nr Rain “);
if (RAIN_SENSOR == 1)
USARTWriteString(“Falling “);
else
USARTWriteString(“Not Falling “);

USARTWriteString(“nrTemperature “);
USARTWriteChar(digit3+0x30); // Display digit3 of ADC_value on UART
__delay_ms(1);
USARTWriteChar(digit4+0x30); // Display digit4 of ADC_value on UART
__delay_ms(10);
USARTWriteChar(0xBA); // Display Degree symbol on UART
__delay_ms(10);
USARTWriteString(“C”);
__delay_ms(10);

USARTWriteString(“nr External Light “);
if (LIGHT_SENSOR == 0)
USARTWriteString(“Dark “);
else
USARTWriteString(“Light “);

USARTWriteString(“nr Parking sensor “);
if (PARKING_SENSOR == 1)
USARTWriteString(“ON “);
else
USARTWriteString(“OFF “);

USARTWriteChar(0x1a);
__delay_ms(1);

__delay_ms(6000);

USARTWriteString(“AT+CMGD=1,4r”);
LCDClear();
LCDWriteString(“Waiting for SMS”);
LCDRow2();
LCDWriteString(“……”);
}
Notification1 = 0;
for(t = 0; t<15 ; t++)
Data[t] = 0x00;
t = 0;
}

if(Notification2)
{
Notification2 = 0;
GSMFuncFlag = CheckCMTI;
/*Command for GAS*/

/*Commands for Automation*/

if(Data1[55] == ‘L’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDClear();
LCDWriteString(“Light Turned ON”);
Light = 1;
USARTWriteString(“Ligth Turned ONrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);
}

if(Data1[55] == ‘l’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDClear();
LCDWriteString(“Light Turned OFF”);
Light = 0;
USARTWriteString(“Ligth Turned OFFrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);
}

if(Data1[55] == ‘P’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDClear();
LCDWriteString(“230V (AC)Plug ON”);
Plug = 1;
USARTWriteString(“Plug Turned ONrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);
}

if(Data1[55] == ‘p’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDClear();
LCDWriteString(“230V(AC) OFF”);
Plug = 0;
USARTWriteString(“Plug Turned OFFrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);
}

if(Data1[55] == ‘F’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDClear();
LCDWriteString(“Fan Turned ON”);
Fan = 1;
USARTWriteString(“Fan Turned ONrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);
}

if(Data1[55] == ‘f’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDRow2();
LCDWriteString(“Fan Turned FF”);
Fan = 0;
USARTWriteString(“Fan Turned OFFrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);

}

if(Data1[55] == ‘A’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(1000);
USARTWriteString(“”12345″r”);
__delay_ms(10);
LCDClear();
LCDWriteString(“All Turned ON”);
Light = 1;
Plug = 1;
Fan = 1;
USARTWriteString(“All Appliances Turned ONrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);
}

if(Data1[55] == ‘a’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(1000);
USARTWriteString(“”12345″r”);
__delay_ms(10);
LCDClear();
LCDWriteString(“All Turned OFF”);
Light = 0;
Plug = 0;
Fan = 0;
USARTWriteString(“Fan Turned OFFrn”);
__delay_ms(10);
USARTWriteChar(0x1a);
__delay_ms(1);

}

if(Data1[55] == ‘C’)
{
USARTWriteString(“AT+CMGS=”);
__delay_ms(10);
USARTWriteString(“”12345″r”);
__delay_ms(1000);
LCDClear();
LCDWriteString(“Light Plug Fan”);
LCDRow2();
LCDWriteData(Light+48);
LCDWriteString(” “);
LCDWriteData(Plug+48);
LCDWriteString(” “);
LCDWriteData(Fan+48);
USARTWriteString(“Light Plug Fanrn”);
__delay_ms(1);
USARTWriteChar(Light+48);
USARTWriteString(” “);
USARTWriteChar(Plug+48);
USARTWriteString(” “);
USARTWriteChar(Fan+48);
USARTWriteString(“rn”);
USARTWriteChar(0x1a);
__delay_ms(1);

}
USARTWriteString(“AT+CMGD=1,4r”);
__delay_ms(800);
LCDClear();
LCDWriteString(“Waiting for SMS”);
LCDRow2();
LCDWriteString(“……”);
}

}
}

Later on you can manually send SMS from your phone to hardware. So that the automation will work. You can also create an android app as shown in beginning of page.

You can also  create an Android app to make a GUI based SMS app. I am showing an easiest way to create android app in just 10 minutes even if you don’t know basic C programming.

You can see tutorial for creating Android app here.

Sharing is caring

9 Comments

  1. Great post. Is going to be back for more quality information.

  2. You can also simulate arduino in proteus. Just download arduino library for proteus.

    • first of all u did great brother .hmm..i got a problem with Mplab X IDE and XC8 compiler won’t run the program .would send the hex file pls ?

  3. Actually i am not so good with proteus and i am simulating my project in parts as i am usingarduino, lpg sensor , smoke sensor amd LM35 and controlling electrical appliances but lpg and smoke sensors are not available in proteus,And i am also trying to design LDR base security system but so far not so much successful so i thought u might help me

  4. Hi Taral
    i wish to make a project based on home security system along with gsm module and android app , can you help me develop the app and program the controller?

    you can charge me if required .

    regards
    Mohit Baid

Leave a Reply

Your email address will not be published.

*

© 2018 EMBEDDED TWEAKS

Theme by Anders NorenUp ↑

Follow Me

Get the latest posts right delivered in you mailbox

%d bloggers like this: