gsm based home automation

94
Chapter- 1 INTRODUCTION 1

Upload: shashi-kant-saini

Post on 26-Oct-2014

336 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GSM BASED HOME AUTOMATION

Chapter-1INTRODUCTION

1

Page 2: GSM BASED HOME AUTOMATION

Chapter-1INTRODUCTION

CHAPTER 1

INTRODUCTION

The aim of this project is to remote monitoring of domestic energy meter using

GSM. This system enables the electricity department to read the meter reading regularly

without the person visiting each house through GSM network. This can be achieved by

the use of microcontroller (89C51) unit.

The microcontroller based system continuously observes the reading of the live meter

and it sends the current reading of live meter. The microcontroller at the transmitter is

provided with GSM unit.

1.1 The main features of this project are:-

1) Monitoring of energy meter

2) Dynamic update of meter reading

1.2 The device provides learning’s on following advancement :-

GSM modem interfacing to controller

Energy meter interfacing

PCB design

LCD interfacing

Embedded C programming

2

Page 3: GSM BASED HOME AUTOMATION

Chapter-1INTRODUCTION

1.3 The major building blocks of this project are:-

Microcontroller based control unit with regulated power supply.

GSM modem

Energy meter

LCD display with driver circuit

1.4 Simplified Block Diagram of Project

Fig.1

3

Page 4: GSM BASED HOME AUTOMATION

Chapter-2STRUCTURAL LAYOUT

4

Page 5: GSM BASED HOME AUTOMATION

Chapter-2STRUCTURAL LAYOUT

CHAPTER 2

STRUCTURAL LAYOUT

Figure 2.1

As shown in above circuit diagram IC CS5460 is low cost Analog to Digital

converter. So the reference voltage & current values are taken in the form of analog signal

from the main source.

Reference Voltage are taken on across of R1(as shown in above diagram) &

Reference Current are taken on across of Rs (as shown in above diagram).

The regulated power supply constantly feds supply to pin no. 14 as positive pin

no. 3 as negative. Now as a clock source crystal is connected between pin no. 1 & 24.

And remaining pins are connected as shown in above circuit diagram with Controller

89S52.

5

Page 6: GSM BASED HOME AUTOMATION

Chapter-2STRUCTURAL LAYOUT

Figure 2.2

Above diagram shows detailing of connections. Here we use 5 volts DC regulated

power supply. A 12 volts, 1 ampere transformer is connected with diode bride. The output

of diode bridge is connected with regulate power IC (7805) to provide 5 volt regulated

power supply. The two capacitors are used to filtered the DC into pure form. This power

supply is supplied to the whole circuit as shown in above diagram.

6

Page 7: GSM BASED HOME AUTOMATION

Chapter-2STRUCTURAL LAYOUT

Crystal is connected between pin no. 18 & 19. To reset the IC RC circuit is

connect on pin no.9.

The value acquired by controller 89S52 is stored in EPROM 24C02C every

second. Pin no. 5 (SDA) of EPROM is connected on port no. P 1.0 & Pin no. 6 (SCK) is

connected with port no. P 1.1 So we can get the previous data from EPROM at the time

of power failure.

LCD (16x2) is connected to port no. 2 of controller. And the RS pin of LCD is

connected with port no. P 3.6 (WR). And the E pin of LCD is connected wiyh port no.

P 3.5 (T1). Vee Pin of LCD is used for contrast setting. And 5 volt supply is given to

LCD & RW is grounded.

GSM MODULE SIM 300 is used to transmit & receive the data from controller.

Now Rx & Tx of GSM MODULE SIM 300 are crossly connected with controller. And 12

volt supply is given to GSM module.

Relay is connected with port no P 0.7 through transistor BC547. This relay can

used to operate the ON & OFF function of energy meter output.

7

Page 8: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

8

Page 9: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

CHAPTER 3

PROGRAMMING DESCRIPTION

3.1 PROGRAMMING OF IC 89S52 USING uKEIL

STEP 1

Open the uKEIL application software.

9

Page 10: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

STEP 2

Crate a project as shown below

10

Page 11: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

STEP 3

Select required device from the list as given below

11

Page 12: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

STEP 4

Now right click on TARGET and ADD a new (.c format) file.

12

Page 13: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

STEP 6

Now compile the program & analysize the error. It automatically hex file in particular folder.

13

Page 14: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

STEP 7

Right click on target & click on option for target window to set parameters of particular controllers.

14

Page 15: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

3.2 Programming of IC 89S52 using C programming Language

#include<reg52.h>

#include"lcd.h"

#include"e2p.h"

#include"spi.h"

#include<string.h>

long double i = 0;

long int Power=0;

char send=0;

unsigned long int Energy = 0;

unsigned long int TEnergy=0,PrevEnergy=0;

char a=0,t=0;

char p=0;

char TeH=0,TeL=0;

unsigned long int Vrms=0,count1000=0,Irms=0;

char sec=0;

char resp0[]="info";

char resp1[]="off9";

char resp2[]="on99";

int y=0;

char v=0,b=0;

char temp[10];

15

Page 16: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

sbit RLY = P0^7;

void calP(void);

void timer0(void) interrupt 1 using 1

{

TR0 = 0;

TF0 = 0;

TH0=0xfc;

TL0=0x64;

count1000++;

if(count1000>=999)

{

count1000=0;

sec=1;

}

TR0 = 1;

}

void putchar(char x)

{

SBUF = x;

TR1=1;

while(!TI);

TF1= 0;

16

Page 17: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

TI = 0;

}

void puts(char*a)

{

putchar(0x0d);

putchar(0x0a);

while(*a)

putchar(*a++);

putchar(0x0d);

putchar(0x0a);

}

void putss(char*new)

{

while(*new)

putchar(*new++);

}

void putsn(char*str)

{

char i=0;

for(i=0;i<=5;i++)

{

17

Page 18: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

putchar(str[i]+48);

}

}

void serial_init(void)

{

TMOD = 0x21; // 8-bit auto reload for timer1,timer0 16-bit mode

TH1 = -3;

TR1 = 1;

SCON = 0x50; // 8-bit uart

REN = 1;

TI=1;

RI=0;

}

void serial(void) interrupt 4 using 1

{

if(RI)

{

a++;

if(a>=51 && a<=54)

{

temp[t]=SBUF;

t++;

18

Page 19: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

if(t>=4)

{

t=0;

send=1;

}

}

if(a>=55)

{

a =0;

}

RI=0;

TI=0;

TF1=0;

}

}

void main(void)

{

char v=0;

RLY=0;

P1 = 0x40;

P2=0x00;

19

Page 20: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

TH0=0xfc;

TL0=0x64;

TR0=1;

lcd_init();

spi_init();

TR1= 1;

lcd_print(2,0,"please wait.");

ms_delay(1000);

serial_init();

ms_delay(100);

while(v<3)

{

putchar('A');

putchar('T');

putchar('+');

putchar('C');

putchar('N');

putchar('M');

putchar('I');

putchar('=');

putchar('1');

putchar(',');

20

Page 21: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

putchar('2');

putchar(',');

putchar('0');

putchar(',');

putchar('0');

putchar(',');

putchar('0');

ms_delay(10);

putchar(13);

ms_delay(100);

v++;

};

i2c_start();

i2c_write(0xa0); // eeprom I2C address with R/W bit clear

i2c_write(0x10); // data word register address

i2c_start();

i2c_write(0xa1);

TeH = i2c_read(0);

i2c_stop();

i2c_start();

i2c_write(0xa0); // eeprom I2C address with R/W bit clear

i2c_write(0x11); // data word register address

21

Page 22: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

i2c_start();

i2c_write(0xa1);

TeL = i2c_read(0);

i2c_stop();

PrevEnergy =(((int)TeH*100)+TeL)*1000;

spi_write(8,0xa0);

spi_write(32,0x4a0016e3);

spi_write(8,0xe8);

spi_write(8,0x18);

i=adc_read();

i=(i/16777216)*250*(1.38);

Vrms=(int)i;

clearlcd();

CursorOff();

spi_write(8,0x16);

i=adc_read();

i=(i/16777216)*5*1000;

i=(int)i;

IE = 0x92;

while(1)

{

if(send==1)

22

Page 23: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

{

if(!(strcmp(temp,resp0)))

{

send=0;

putchar('A');

putchar('T');

putchar('+');

putchar('C');

putchar('M');

putchar('G');

putchar('S');

putchar('=');

putchar('"');

putchar('+');

putchar('9');

putchar('1');

putchar('9');

putchar('9');

putchar('2');

putchar('5');

putchar('8');

putchar('8');

23

Page 24: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

putchar('7');

putchar('6');

putchar('0');

putchar('3');

putchar('"');

putchar(13);

ms_delay(100);

putsn(digit);

ms_delay(10);

putchar(26);

}

}

if(!strcmp(temp,resp1))

{

RLY=1;

}

if(!strcmp(temp,resp2))

{

RLY=0;

}

spi_write(8,0x18);

i=adc_read();

24

Page 25: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

i=(i/16777216)*250*(1.38);

Vrms=(int)i;

spi_write(8,0x16);

i=adc_read();

i=(i/16777216)*5*1000;

Irms=(int)i;

if(sec==1)

{

calP();

sec=0;

}

}

}

void calP(void)

{

Power =( Vrms * Irms * 1.41 *(0.735));

Energy = (unsigned long int)(Power);

Energy=Energy/3600;

TEnergy = PrevEnergy+Energy;

PrevEnergy=TEnergy;

TEnergy = TEnergy/1000;

TeH = TEnergy/100;

25

Page 26: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

TeL = TEnergy%100;

e2pWrite(0x10,TeH);

e2pWrite(0x11,TeL);

binbcd(TEnergy);

lcd_print(7,0,"Wh");

lcd_print1(5,1,digit);

}

26

Page 27: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

3.3 INCLUDED FILES

3.3.1 REG52.H

#ifndef __REG52_H__

#define __REG52_H__

/* BYTE Registers */

sfr P0 = 0x80;

sfr P1 = 0x90;

sfr P2 = 0xA0;

sfr P3 = 0xB0;

sfr PSW = 0xD0;

sfr ACC = 0xE0;

sfr B = 0xF0;

sfr SP = 0x81;

sfr DPL = 0x82;

sfr DPH = 0x83;

sfr PCON = 0x87;

sfr TCON = 0x88;

sfr TMOD = 0x89;

sfr TL0 = 0x8A;

sfr TL1 = 0x8B;

27

Page 28: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

sfr TH0 = 0x8C;

sfr TH1 = 0x8D;

sfr IE = 0xA8;

sfr IP = 0xB8;

sfr SCON = 0x98;

sfr SBUF = 0x99;

/* 8052 Extensions */

sfr T2CON = 0xC8;

sfr RCAP2L = 0xCA;

sfr RCAP2H = 0xCB;

sfr TL2 = 0xCC;

sfr TH2 = 0xCD;

/* BIT Registers */

/* PSW */

sbit CY = PSW^7;

sbit AC = PSW^6;

sbit F0 = PSW^5;

sbit RS1 = PSW^4;

sbit RS0 = PSW^3;

28

Page 29: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

sbit OV = PSW^2;

sbit P = PSW^0; //8052 only

/* TCON */

sbit TF1 = TCON^7;

sbit TR1 = TCON^6;

sbit TF0 = TCON^5;

sbit TR0 = TCON^4;

sbit IE1 = TCON^3;

sbit IT1 = TCON^2;

sbit IE0 = TCON^1;

sbit IT0 = TCON^0;

/* IE */

sbit EA = IE^7;

sbit ET2 = IE^5; //8052 only

sbit ES = IE^4;

sbit ET1 = IE^3;

sbit EX1 = IE^2;

sbit ET0 = IE^1;

sbit EX0 = IE^0;

29

Page 30: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

/* IP */

sbit PT2 = IP^5;

sbit PS = IP^4;

sbit PT1 = IP^3;

sbit PX1 = IP^2;

sbit PT0 = IP^1;

sbit PX0 = IP^0;

/* P3 */

sbit RD = P3^7;

sbit WR = P3^6;

sbit T1 = P3^5;

sbit T0 = P3^4;

sbit INT1 = P3^3;

sbit INT0 = P3^2;

sbit TXD = P3^1;

sbit RXD = P3^0;

/* SCON */

sbit SM0 = SCON^7;

sbit SM1 = SCON^6;

sbit SM2 = SCON^5;

30

Page 31: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

sbit REN = SCON^4;

sbit TB8 = SCON^3;

sbit RB8 = SCON^2;

sbit TI = SCON^1;

sbit RI = SCON^0;

/* P1 */

sbit T2EX = P1^1; // 8052 only

sbit T2 = P1^0; // 8052 only

/* T2CON */

sbit TF2 = T2CON^7;

sbit EXF2 = T2CON^6;

sbit RCLK = T2CON^5;

sbit TCLK = T2CON^4;

sbit EXEN2 = T2CON^3;

sbit TR2 = T2CON^2;

sbit C_T2 = T2CON^1;

sbit CP_RL2 = T2CON^0;

#endif

31

Page 32: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

3.3.2 LCD.H

sbit RS = P3^6;

sbit EN = P3^5;

static unsigned char base_y[4] = {0x80,0xc0};

void us_delay(unsigned int a)

{

int i;

for(i=0;i<a;i++);

}

void ms_delay(unsigned int a)

{

int i,j;

for(i=0;i<a;i++)

for(j=0;j<i;j++);

}

void sendcommand(unsigned char datt)

{

RS=0;

P2=datt;

EN=1;

us_delay(10);

32

Page 33: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

EN=0;

}

void senddata(unsigned char dat)

{

RS=1;

P2=dat;

EN=1;

us_delay(10);

EN=0;

}

void lcd_init(void)

{

RS= 0;

EN= 0;

sendcommand(0x01);

us_delay(100);

sendcommand(0x38);

us_delay(100);

sendcommand(0x38);

us_delay(100);

sendcommand(0x0f);

us_delay(100);

33

Page 34: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

sendcommand(0x0f);

us_delay(100);

}

/* void lcd_xy(char a, char b)

{

sendcommand(base_y[b]+a);

} */

void lcd_print(char x, char y, char code *string) // send string of characters to LCD

{

int i;

int c;

for (i=0;string[i]!=0;i++)

{

c = string[i]; // convert ASCII to LCD char address

if (c<0) c=0;

sendcommand(base_y[y]+x+i);

us_delay(100);

senddata(c);

us_delay(100);

}

}

34

Page 35: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

void lcd_print1(char x, char y, char* strg)

{

int j;

int b;

for (j=0;j<=5;j++)

{

b = strg[j]+0x30; // convert ASCII to LCD char address

if (b<0) b=0;

sendcommand(base_y[y]+x+j);

us_delay(100);

senddata(b);

us_delay(100);

}

}

/*void lcd_char(char x, char y, char stg)

{

int j;

int b;

b = stg; // convert ASCII to LCD char address

if (b<0) b=0;

sendcommand(base_y[y]+x+j);

us_delay(100);

35

Page 36: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

senddata(b);

us_delay(100);

} */

void clearlcd(void)

{

sendcommand(0x01);

us_delay(100);

us_delay(100);

}

void CursorOn(void)

{

sendcommand(0x0E);

us_delay(100);

us_delay(100);

}

void CursorOff(void)

{

sendcommand(0x0c);

us_delay(100);

us_delay(100);

}

void CursorBlink(void)

36

Page 37: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

{

sendcommand(0x0f);

us_delay(100);

us_delay(100);

}

3.3.3 E2P.H

sbit SCL = P1^0;

sbit SDA = P1^1;

void uss_delay(void)

{

int i;

for(i=0;i<=200;i++);

}

unsigned char i2c_read(char ack)

{

char i, dat=0;

SDA = 1;

for(i=0; i<8; i++)

{

37

Page 38: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

dat <<= 1;

do {

SCL = 1;

}

while(SCL==0); // wait for any SCL clock stretching

uss_delay();

if(SDA) dat |= 1;

SCL = 0;

}

if(ack) SDA = 0;

else SDA = 1;

SCL = 1;

uss_delay(); // send (N)ACK bit

SCL = 0;

SDA = 1;

return dat;

}

void i2c_stop(void)

{

SDA = 0;

uss_delay();

SCL = 1;

38

Page 39: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

uss_delay();

SDA = 1;

uss_delay();

}

void i2c_write(unsigned char DAT)

{

unsigned char i;

for(i=0;i<8;i++)

{

SDA=(DAT&0x80);

DAT=DAT<<1;

uss_delay();

SCL=1;

uss_delay();

SCL=0;

uss_delay();

}

SDA=1;

uss_delay();

SCL=1;

39

Page 40: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

uss_delay();

SCL=0;

uss_delay();

} void i2c_start(void)

{

SDA = 1;

uss_delay();

SCL = 1;

uss_delay();

SDA = 0;

uss_delay();

SCL = 0;

uss_delay();

}

void e2pWrite(unsigned char address, unsigned char dat)

{

i2c_start(); // send start sequence

i2c_write(0xa0); // eeprom I2C address with R/W bit clear

i2c_write(address); // data word register address

i2c_write(dat); // data to be sent(Seconds)

i2c_stop();

uss_delay();

40

Page 41: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

}

unsigned char e2pRead(unsigned char addr)

{

i2c_start();

i2c_write(0xa0); // eeprom I2C address with R/W bit clear

i2c_write(addr); // data word register address

i2c_start();

i2c_write(0xa1);

return (i2c_read(1));

}

3.3.4 SPI.H

#include<intrins.h>

sbit ADC_SDI = P1^5;

sbit ADC_SCLK = P1^7;

sbit ADC_SDO = P1^6;

sbit ADC_CS = P1^4;

sbit ADC_RST = P1^3;

41

Page 42: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

#define TRUE 1

#define FALSE 0

char digit[6];

void u_delay(void)

{

int a;

for(a=0;a<100;a++);

}

void spi_cmd(unsigned char cmd)

{

char i=8;

while(i>0)

{

ADC_SCLK=1;

if(cmd & 0x80)

ADC_SDI = 1;

else

ADC_SDI = 0;

ADC_SCLK = 0;

i--;

cmd<<=1;

42

Page 43: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

}

ADC_SDI=TRUE;

}

unsigned char unithb,tenhb,hundhb,thoushb,thous10hb,lachb;

void spi_write(unsigned char length, long int value)

{

char i;

ADC_CS=FALSE;

i = 32 - length;

while ( i > 0)

{

i -= 1;

value = value << 1;

}

// ADC_CS =FALSE;

while(length > 0)

{

if(value & 0x80000000)

ADC_SDI = TRUE;

else

ADC_SDI = FALSE;

43

Page 44: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

ADC_SCLK = TRUE;

u_delay();

length -= 1;

value <<= 1;

ADC_SCLK = FALSE;

}

}

long adc_read(void)

{

char i=24;

unsigned long readed = 0x00;

ADC_SDI=TRUE;

while( i > 0)

{

ADC_SCLK = TRUE;

u_delay();

readed <<= 1;

readed = readed | (ADC_SDO);

ADC_SCLK = FALSE;

i-- ;

}

44

Page 45: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

return readed;

}

void binbcd(unsigned long int x)

{

tenhb = 0;

hundhb = 0;

thoushb = 0;

thous10hb = 0;

lachb =0;

while(x >= 100000)

{

++lachb;

x = x - 100000;

}

while(x >= 10000)

{

++thous10hb;

x = x - 10000;

}

while(x >= 1000)

{

45

Page 46: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

++thoushb;

x = x - 1000;

}

while(x >= 100)

{

++hundhb;

x = x - 100;

}

while(x >= 10)

{

++tenhb;

x = x - 10;

}

unithb = x;

digit[5]=unithb;

digit[4]=tenhb;

digit[3]=hundhb;

digit[2]=thoushb;

digit[1]=thous10hb;

digit[0]=lachb;

46

Page 47: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

}

void spi_init(void)

{

char j=0;

// ADC_CS = TRUE;

ADC_RST = FALSE;

u_delay();

ADC_RST = TRUE;

ADC_CS = FALSE;

/* for(j=0;j<4;j++)

{

spi_cmd(0xff);

}

// spi_cmd(0xfe);

// u_delay(); */

spi_write(32,0xFFFFFFFE); // sends 3 SYNC1 (0xFF)command and 1

SYNC0(0XFE)command

/* adc_write(8,0x40); // command to write configuration register

adc_write(32,0x20000000); // set rs bit of configureation register

adc_write(8,0x40); // command to write configuration register

adc_write(32,0x00000000); // set rs bit of configureation register

47

Page 48: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

adc_write(8,0x40); // command to write configuration register

adc_write(32,0x02000000); // set VRS bit of configureation register

while(1)

{

binbcd(85);

delay1();

adc_write(8,0x0B); // command to read configuration

register

i = adc_read();

binbcd(i);

//display_adcvalue(i,0); // displays the readed value on

display

}

// adc_write(8,0x05); // command to write in channel set up

register

// adc_write(32,0x00400840); // define channel set up reg 1,and reg 2

adc_write(8,0x81); // command for self offset calibration

adc_write(8,0X82); // command for self gain calibration

*/

}

48

Page 49: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

long read_5460(void)

{

char i;

char j=0;

long temp;

temp=0;

while(j<3)

{

i=7;

ADC_SDI = 1;

while(i>0)

{

ADC_SCLK = 0;

temp <<= 1;

ADC_SCLK = 1;

_nop_();

if(ADC_SDO == 1)

temp |= 0x00000001;

i--;

}

49

Page 50: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

ADC_SDI = 0;

ADC_SCLK = 0;

temp <<= 1;

ADC_SCLK = 1;

_nop_();

if(ADC_SDO == 1)

temp |= 0x00000001;

j++;

}

return(temp);

}

3.3.5 STRING.H

#ifndef __STRING_H__

#define __STRING_H__

50

Page 51: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

#ifndef _SIZE_T

#define _SIZE_T

typedef unsigned int size_t;

#endif

#ifndef NULL

#define NULL ((void *) 0L)

#endif

#pragma SAVE

#pragma REGPARMS

extern char *strcat (char *s1, char *s2);

extern char *strncat (char *s1, char *s2, int n);

extern char strcmp (char *s1, char *s2);

extern char strncmp (char *s1, char *s2, int n);

extern char *strcpy (char *s1, char *s2);

extern char *strncpy (char *s1, char *s2, int n);

extern int strlen (char *);

extern char *strchr (const char *s, char c);

51

Page 52: GSM BASED HOME AUTOMATION

Chapter-3PROGRAMMING DESCRIPTION

extern int strpos (const char *s, char c);

extern char *strrchr (const char *s, char c);

extern int strrpos (const char *s, char c);

extern int strspn (char *s, char *set);

extern int strcspn (char *s, char *set);

extern char *strpbrk (char *s, char *set);

extern char *strrpbrk (char *s, char *set);

extern char *strstr (char *s, char *sub);

extern char *strtok (char *str, const char *set);

extern char memcmp (void *s1, void *s2, int n);

extern void *memcpy (void *s1, void *s2, int n);

extern void *memchr (void *s, char val, int n);

extern void *memccpy (void *s1, void *s2, char val, int n);

extern void *memmove (void *s1, void *s2, int n);

extern void *memset (void *s, char val, int n);

#pragma RESTORE

#endif

52

Page 53: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

53

Page 54: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

CHAPTER 4

HARDWARE DESCRIPTION

COMPONENTS USED:-

1) Microcontroller 89S52

2) EPROM 24C02

3) IC CS5460 (Analog to Digital converter)

4) LCD (16x2)

5) Relay

6) Crystal (11.0592 Mhz & 6 Mhz)

7) IC 7805

8) Resistors. Capacitors, Diodes, Transistor, Connecters & Transformer.

4.1 Microcontroller 89S52

The AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller

with 8K bytes of in-system programmable Flash memory.

The device is manufactured using Atmel’s high-density nonvolatile memory

technology and is compatible with the industry-standard 80C51 instruction set and pinout.

The on-chip Flash allows the program memory to be reprogrammed in-system or by a

conventional nonvolatile memory programmer.

54

Page 55: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

By combining a versatile 8-bit CPU with in-system programmable Flash on a

monolithic chip, the Atmel AT89S52 is a powerful microcontroller which provides a

highly-flexible and cost-effective solution to many embedded control applications.

The AT89S52 provides the following standard features: 8K bytes of Flash, 256

bytes of RAM, 32 I/O lines, timers, two data pointers, three 16-bit timer/counters, a

six-vector two-level interrupt architecture, a full duplex serial port, on-chip oscillator,

and clock circuitry.

In addition, the AT89S52 is designed with static logic for operation down to zero

frequency and supports two software selectable power saving modes. The Idle Mode

stops the CPU while allowing the RAM, timer/counters, serial port, and interrupt system

to continue functioning.

55

Page 56: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

The Power-down mode saves the RAM contents but freezes the oscillator,

disabling all other chip functions until the next interrupt or hardware reset.

Why we are using 89s52?

Microcontroller ROM(Code Memory) RAM Timer Ext.-Int. Sources

89s51 4,000 Bytes(4 kB) 128 B 2 6

89s52 8,000 Bytes(8 kB) 256 B 3 8

Table-4.1

Difference between S-type and C-type

Type Power

Consumption

Speed Crystal

Frequency

Sink Source

C-type Less Less 0-24 MHz 10 mA 10 mA

S-type More More 0-33 MHz 10 mA Very weak

Table-4.2

56

Page 57: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

Features

Compatible with MCS®-51 Products

8K Bytes of In-System Programmable (ISP) Flash Memory

Endurance: 10,000 Write/Erase Cycles

4.0V to 5.5V Operating Range

Fully Static Operation: 0 Hz to 33 MHz

Three-level Program Memory Lock

256 x 8-bit Internal RAM

32 Programmable I/O Lines

Three 16-bit Timer/Counters

Eight Interrupt Sources

Full Duplex UART Serial Channel

Low-power Idle and Power-down Modes

Interrupt Recovery from Power-down Mode

Watchdog Timer

Dual Data Pointer

Power-off Flag

Fast Programming Time

Flexible ISP Programming (Byte and Page Mode)

Green (Pb/Halide-free) Packaging Option

57

Page 58: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

Pin configuration

Fig 4.1

58

Page 59: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

4.2 EPROM 24C02

Features

Low-Voltage and Standard-Voltage Operation

– 5.0 (VCC = 4.5V to 5.5V)

– 2.7 (VCC = 2.7V to 5.5V)

– 2.5 (VCC = 2.5V to 5.5V)

– 1.8 (VCC = 1.8V to 5.5V)

Internally Organized 128 x 8 (1K), 256 x 8 (2K), 512 x 8 (4K),

1024 x 8 (8K) or 2048 x 8 (16K)

2-Wire Serial Interface

Schmitt Trigger, Filtered Inputs for Noise Suppression

Bidirectional Data Transfer Protocol

100 kHz (1.8V, 2.5V, 2.7V) and 400 kHz (5V) Compatibility

Write Protect Pin for Hardware Data Protection

8-Byte Page (1K, 2K), 16-Byte Page (4K, 8K, 16K) Write Modes

Partial Page Writes Are Allowed

Self-Timed Write Cycle (10 ms max)

High Reliability

– Endurance: 1 Million Write Cycles

– Data Retention: 100 Years

– ESD Protection: >3000V

Automotive Grade and Extended Temperature Devices Available

8-Pin and 14-Pin JEDEC SOIC, 8-Pin PDIP, 8-Pin MSOP, and 8-Pin TSSOP

Packages

59

Page 60: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

Description

The AT24C01A/02/04/08/16 provides 1024/2048/4096/8192/16384 bits of serial

electrically erasable and programmable read only memory (EEPROM) organized as

128/256/512/1024/2048 words of 8 bits each. The device is optimized for use in many

industrial and commercial applications where low power and low voltage operation are

essential. The AT24C01A/02/04/08/16 is available in space saving 8-pin PDIP,

(AT24C01A/02/04/08/16) , 8-Pin MSOP (AT24001A/02), 8-Pin TSSOP

(AT24C01A/02/04/08/16), and 8-Pin and 14-Pin JEDEC SOIC

(AT24C01A/02/04/08/16) packages and is accessed via a 2-wire serial interface. In

addition, the entire family is available in 5.0V (4.5V to 5.5V), 2.7V (2.7V to 5.5V), 2.5V

(2.5V to 5.5V) and 1.8V (1.8V to 5.5V) versions.

PIN CONFIGURATION

Fig 4.2

60

Page 61: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

4.3 IC CS5460 (Analog to Digital converter)

CS5460 Features

CS5460 is a single chip containing two one Σ analog-digital converter (ADC),

high-speed power calculation functions and a serial interface of highly integrated analog

Σ a digital converter. He can accurately measure the instantaneous voltage, instantaneous

current, instantaneous power, etc.; He also has two-way communication with the

microcontroller serial port. The CS5460 can be initialized after power and can perform all

functions, including procedures under the control of the user system calibration. 61

Page 62: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

Structure

CS5460 structure shown in Figure 1. Ten of them IN, IN A analog current input

channels; VIN +, VIN an output an analog voltage input channels; and different order and

magnitude of the input voltage matches the current channel also features a programmable

gain amplifier (PGA), the input signal full-scale range selectable; 2 △ Σ analog-digital

converter samples the signal on the system model of a digital conversion; two digital

high-speed filter to (MCLK / K) / 1024 output rate of output data word; 2 high-pass filter

can be calculated in the energy before the DC component of the input signal filter;

calibration unit to achieve the calibration of the analog input channels;-power computing

unit used to calculate the instantaneous power and voltage and current RMS and so on.

Serial Interface (Serial Interface) unit provides a CS5460 with external data

communication interface.

Works

CS5460 energy calculation is a monolithic CMOS cited Hajime power

measurement chip. Voltage analog signal from the VIN +, VIN A △ Σ after the analog

input of a digital converter to convert the converted signal by the high-speed filter and

high-pass filter given away after the power calculator. Current analog signal from the

VIN +, VIN A △ Σ after the input of a digital analog converter to convert the converted

signal by the high-speed filter and high-pass filter given away after the power calculator.

Power calculator for processing the two signals output measured after the operation

voltage, current, power and so on. All of these data by the serial interface and

microcontroller for data exchange.

Analog input

Measure the instantaneous sampling circuit is to achieve voltage, instantaneous

current, instantaneous power basis. The resistors and capacitors not only high precision,

but its role is not the same. Circuit RPl inrush current for the current channel when the

input pin for current limit protection; in VlN + feet do not need to protect the resistance

because of the introduction of the resistive divider as sensors, resistive voltage divider

62

Page 63: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

resistors in series have been be directed to the VIN + pin, if the voltage input channel

CS5460 negative side is not earthed (VIN + and VIN connected as a differential input

mode) plus the input signal is necessary to protect the resistance. CPl and CP2 is the role

of absorption coupled to the input line of high-frequency noise.

The resistance and capacitance, the voltage channel should be the ultimate

guarantee of full scale input signal amplitude is 150mV, the current channel input signal

amplitude of 30mV full scale or 150mV selectable (programmable gain amplifier by the

program through implementation).

Calibration

Calibration of the input channels, there are several software shown in Figure 3, the

calibration process.

Channel all of the calibration samples were calibrated by computer software,

should be the order from left to right in Figure 3 in order calibration. After calibration by

the calibration equipment used to do a higher level than the right, to ensure the accuracy

of the measurement data.

Computing

Voltage and current channel input signal is calibrated to the power calculation

unit, the measured voltage and current to calculate power. Also from the measured

instantaneous voltage and current to calculate the instantaneous voltage and current RMS.

Hardware Design

CS5460 implementation of the system by the instantaneous voltage, instantaneous

current, instantaneous power measurement, energy measurement achieved by the

microcontroller μPD75P3116 and other features, interface circuit shown in Figure 4

instructions.

CS5460s serial interface consists of four control lines that: CS, SDI, SDO, and

SCLK. CS is chip select signal, is allows access to the serial port of the control line,

63

Page 64: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

active low, high, high-impedance state when the SDO client; SDI as serial data input,

used to transfer data from the microcontroller to the CS5460; SDO is serial data output,

used to output data to the microcontroller; SCLK is to control the data input or output

serial bit clock, SCLK port level conversion can be identified before the CS must be set to

logic low. MCU is through the four control lines to achieve the data exchange with the

CS5460.

Microcontroller to read from the CS5460 the instantaneous voltage, instantaneous

current and instantaneous power exists and other data in memory after treatment, after

power microcontroller read the data from memory to memory. All of these data can be an

external communication interface from the host computer read out by the LCD display

can easily query.

Order to ensure safe and reliable system operation in the system by adding

"watchdog" circuit. HCF4060 CMOS components and parts from the discrete

components of the "watchdog" circuit, its low cost, simple to use, stable and reliable.

When the program runs into infinite loop error or when the system can ensure quick and

safe and reliable reset.

System software design

Program starts, first start the watchdog circuit, and then test the memory. If the

test is not successful then the re-testing, if successful, read CS5460. After the reading of

the data processing, the instantaneous voltage, instantaneous current, instantaneous

power, total power and voltage and current RMS displayed on the LCD display.

Conclusion

The CS5460 and t ~ PD75P3116 used to achieve power and energy measurement

approach to simple, easy to use, high cost performance characteristics by the majority of

design and development departments of all ages, a very good market prospects. At

present, our company has developed under this approach the single-phase electronic

meter, three phase and single-phase multi-site electronic form calibrator and other

products, has been widely used in the power sector.

64

Page 65: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

Pin Configuration

Fig 4.3

65

Page 66: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

4.4 LCD (16x2)

Description.

This is the first interfacing example for the Parallel Port. We will start with something

simple. This example doesn't use the Bi-directional feature found on newer ports, thus it should

work with most, if no all Parallel Ports. It however doesn't show the use of the Status Port as an

input. So what are we interfacing? A 16 Character x 2 Line LCD Module to the Parallel Port.

These LCD Modules are very common these days, and are quite simple to work with, as all the

logic required to run them is on board.

66

Page 67: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

Schematic

Fig4.4

Above is the quite simple schematic. The LCD panel's Enable and Register Select

is connected to the Control Port. The Control Port is an open collector / open drain output.

While most Parallel Ports have internal pull-up resistors, there are a few which don't.

Therefore by incorporating the two 10K external pull up resistors, the circuit is more

portable for a wider range of computers, some of which may have no internal pull up

resistors.

We make no effort to place the Data bus into reverse direction. Therefore we hard

wire the R/W line of the LCD panel, into write mode. This will cause no bus conflicts on

the data lines. As a result we cannot read back the LCD's internal Busy Flag which tells

us if the LCD has accepted and finished processing the last instruction. This problem is

overcome by inserting known delays into our program.

The 10k Potentiometer controls the contrast of the LCD panel. Nothing fancy

here. As with all the examples, I've left the power supply out. You can use a bench power

supply set to 5v or use a onboard +5 regulator. Remember a few de-coupling capacitors,

especially if you have trouble with the circuit working properly.

67

Page 68: GSM BASED HOME AUTOMATION

Chapter-4HARDWARE DESCRIPTION

The 2 line x 16 character LCD modules are available from a wide range of manufacturers

and should all be compatible with the HD44780. The one I used to test this circuit was a

Powertip PC-1602F and an old Philips LTN211F-10 which was extracted from a Poker

Machine! The diagram to the right, shows the pin numbers for these devices. When

viewed from the front, the left pin is pin 14 and the right pin is pin 1.

68

Page 69: GSM BASED HOME AUTOMATION

Chapter-5GSM

69

Page 70: GSM BASED HOME AUTOMATION

Chapter-5GSM

CHAPTER 5

GSM

This GSM wireless data module is the ready a solution for remote wireless

applications, machine to machine or user to machine and remote data communications in

all vertical market applications.

5.1 SIM300 AT Command Set

In application, controlling device controls the GSM engine by sending AT Command via its serial interface. The controlling device at the other end of the serial line is referred to as following term: 1) TE (Terminal Equipment); 2) DTE (Data Terminal Equipment)

Types of AT commands and responses

Test command AT+<x>=? The mobile equipment returns the list of parameters and value ranges set with the corresponding Write command or by internal processes.

Read command AT+<x>? This command returns the currently set value of the parameter or parameters.

Write command AT+<x>=<…> This command sets the user-definable parameter values.

Execution command AT+<x> The execution command reads non-variable parameters affected by internal processes in the GSM engine

Table 5.1

70

Page 71: GSM BASED HOME AUTOMATION

Chapter-5GSM

5.2 Product concept

Designed for global market, SIM300 is a Tri-band GSM/GPRS engine that works on frequencies EGSM 900 MHz, DCS 1800 MHz and PCS 1900 MHz. SIM300 features GPRS multi-slot class 10/ class 8 (optional) and supports the GPRS coding schemes CS-1, CS-2, CS-3 and CS-4.

71

Page 72: GSM BASED HOME AUTOMATION

Chapter-6MERITS & DEMERITS

72

Page 73: GSM BASED HOME AUTOMATION

Chapter-6MERITS & DEMERITS

CHAPTER 6

MERITS & DEMERITS

6.1 MERITS

Different Tariffs can be implemented.

Power thefts can be caught.

Power can be cut-off in case of non payement of bill.

For ON & OFF purpose security feature is provided.

Quick reponse.

It can Give every second update of energy meter value by SMS.

6.2 DEMERITS

The energy meter is of only 5 ampere current rating only (Domestic Use).

Maximum rating of meter is 10 KWh but it can be extended upto 10 MWh.

Costing of GSM is higher for regular use.

GSM module can be operated only proper network coverage is available.

It has to rely on particular SIM or service provider.

Accuracy of energy meter is only in watt hour.

The relay used has switching rating of 6 ampere so we can perform upto that only.

The SIM card must have a balance in order to send message.

73

Page 74: GSM BASED HOME AUTOMATION

Chapter-7FUTURE DEVELOPMENT

74

Page 75: GSM BASED HOME AUTOMATION

Chapter-7FUTURE DEVELOPMENT

CHAPTER 7

FUTURE DEVELOPMENT

Power theft can be caught using this device.

This device can perform as reminder for bills or tariffs.

Using this device new tariffs can be implemented.

Prepaid tariffs can also be available using this device.

Electric consumption can be improved if once gets regular update of its energy

consumption.

Any fault in power system can be detected using this device.

The capacity of energy meter can be increased upto 10 MWh.

75

Page 76: GSM BASED HOME AUTOMATION

Chapter-8CONCLUSION

76

Page 77: GSM BASED HOME AUTOMATION

Chapter-8CONCLUSION

CHAPTER 8

CONCLUSION

While preparing this project we learn the operating of microcontroller

89S52. And also understand the performance of IC CS5460. We learn interfacing

of LCD (16x2), EPROM 24C02C with microcontroller 89S52.And also learn

programming of 89S52 to operate remaining components.

Along with controller this device provide learning about GSM module and

also interfacing GSM module with controller. We also learn about the usage of

AT Command & how it can used to send SMS. Finally by preparing this device

we can understand how the device can be useful in present as well as future.

77

Page 78: GSM BASED HOME AUTOMATION

REFERENCES

REFERENCES

[1] Electric Power Distribution By A. S. Pabla

[2] A Textbook of Electrical Technology By R.K. Rajput

[3] Principles and Applications of GSM By Garg

[4] Exploring C for microcontrollers: a hands on approach  By Jivan S. Parab, Vinod

G. Shelake, Rajanish K. Kamat

[5] Microprocessors & Microcontroller Systems By D.A.Godse A.P.Godse

[6] http://www.keil.com/dd/docs/datashts/atmel/at89s52_ds.pdf

[7] http://www.atmel.com/dyn/resources/prod_documents/doc1919.pdf

[8] http://www.alldatasheet.com/datasheet-pdf/pdf/230848/CIRRUS/CS5460.html

[9] http://www.datasheetcatalog.org/datasheet/Cirrus_Logic/mXxtrxu.pdf

[10] http://catalog.gaw.ru/project/download.php?id=2834

[11] http://www.datasheetcatalog.com/datasheets_pdf/2/4/C/0/24C02.shtml

[12]http://microchip.ua/simcom/GSM-GPRS-GPS/AppNotes%20-%20doc/_ Module%20FAQ-2.pdf

[13] http://wm.sim.com/Sim/FrontShow_en/default.aspx

[14] http://www.datasheetcatalog.org/datasheet/philips/BC546_547_3.pdf

[15] http://www.datasheetcatalog.com/datasheets_pdf/B/C/5/4/BC547.shtml

[16] http://www.autoshop101.com/forms/hweb2.pdf

[17] http://electronics.howstuffworks.com/relay.htm

[18] http://www.engineersgarage.com/sites/default/files/LCD%2016x2.pdf

[19]http://www.bioenabletech.com/technical_introduction_to_gsm_modem_technology.htm

78

Page 79: GSM BASED HOME AUTOMATION

REFERENCES

[20] http://www.sourcecodester.com/visual-basic/sending-sms-using-commands-gsm-modemgsm-phone-receiving-sms-updated.html

[21] http://myrobobazaar.com/store/images/lcd16x2.jpg

[22] http://www.byvac.com/bv3/image/data/Displays/LCD/LCD16x2_physical.gif

[23] http://microcontrollerkits.com/Spare%20Parts/IC%2089C51.jpg

[24] http://www.dongfangic.com/UploadFiles/14/39b1d12b21aae80d88898f2bbfdfb448.jpg

[25] http://www.crodnet.co.uk/images/24c02.jpg

79

Page 80: GSM BASED HOME AUTOMATION

APPENDIX

80

Page 81: GSM BASED HOME AUTOMATION

APPENDIX

APPENDIX A

Cost Of Project

81