Anda di halaman 1dari 45

1.

INTRODUCTION

1.1 INTRODUCTION

GSM and GPRS based Designs have developed another innovative and public utility

product for mass communication .The purpose of this project is to remote monitoring

and control of the Domestic Energy meter. This system enables the Electricity

Department to read the meter readings regularly without the person visiting each house.

This can be achieved by the use of micro controller unit that continuously monitors and

records the Energy Meter readings in its permanent (non-volatile) memory location.

This system also makes use of a GSM modem for remote monitoring and control of

Energy Meter.

The Microcontroller based system continuously records the readings and the live

meter reading can be sent to the Electricity department on request. This system also can

be used to disconnect the power supply to the house in case of non-payment of

electricity bills. A dedicated GSM modem with SIM card is required for each energy

meter.

The main aim of the project will be to design a SMS electronic ENERGY

METER toolkit which can replace the traditional ENERGY METER. The toolkit send

SMS to Electricity Department number, the system is made efficient by SIMs so that the

SMS can be received by number of devices boards in a locality using techniques of time

division multiple access.

The main components of the toolkit include microcontroller, GSM modem.

These components are integrated with the device board and thus incorporate the

1|Page
Wireless features. The GSM modem receives the SMS. The AT commands are serially

transferred to the modem. In return the modem transmits the stored message through the

wireless link. The microcontroller used in this case is ATMEL AT89S52. SIMCOM 300

is used as the GSM modem. In this prototype model, LCD display is used for simulation

purpose. The results presented in the thesis support the proper functionalities and

working of the system. The timing diagram suggests the response of the modem to

various AT (attention) commands.

1.2 METHODOLOGY

The method used to carry out this project is the principle of serial communication in

collaboration with embedded systems. As the analog energy meter shows load reading

while microcontroller kit with GSM modem reads actual reading where theft taken into

account and that reading will be send by SMS to EB officer . The latest technology used

for communication between the mobile and the embedded devices.

System will work like when the user wants to receive a SMS on the energy

consumption in houses and offices; the modem sent a message through the subscriber

identity module (SIM) which is inserted in the display system MODEM.

2|Page
1.3 SCOPE OF WORK

In this project, liquid crystal display is used for displaying the message; and also use

GSM modem (Motorola W220) as an interface between mobile and microcontroller. It

will send message to any phone irrespective of the GSM network through the modem

connected to the programmable device.

1.4 AIM AND OBEJECTIVE

 The proposed project work aims on the design and development of energy theft

prevention aspect.

 The concept of this project to prevent the illegal usage of electrical power this

system would provide a simple way to detect an electrical power theft without

any human interface.

3|Page
1.5 BLOCK DIAGRAM

REGULATED POWER
SUPPLY (5 V)

ANALOG

ENERGY METER

LCD

MICRO
RELAY CONTROLLER

89S52 GSM MODEM

(12 V)

BUZZER

MOBILE PHONES

Fig (1.1) block diagram of microcontroller based power theft identifier

4|Page
1.6 LIST OF COMPONENTS

Name Capacity Quantity Code

Regulator 7805 1 U1

Regulator 7812 1 U3

Capacitor (25 V ) 1000µf 1 C1

Capacitor ( 63V) 10µf 1 C2

Ceramic Capacitor 22pf 2 C3,C4

Diode(INL0074) 4 D1,D2,D3,D4

Push Button 1

Mobile Phone 1

Relay (230 V /10A) 1 RL1

LCD 16*2 1

40 Pin Base 1 U2

16 Pin Base 1 U5

Load (60 W )

5|Page
8051(AT89S52) 1

Oscillator 11.0592mhz 1 X1

LED 2 D5,D7

Resistance 220Ω 3 R1,R4,R8

Resistance 1k 1 R3

Resistance 10k 2 R2,R5

Buzzer 1 BUZ1

BC547(Transistor)(NPN) 1 Q1

Energy meter(240V,5-30A) 1

6|Page
2. THEORETICAL BACKGROUND AND
LITERATURE REVIEW
2.1 THEORETICAL BACKGROUND

GSM (Global System for Mobile communications: originally from GROUPE Special

Mobile) is the most popular standard for mobile phones in the world. Its promoter, the

GSM Association, estimates that 80% of the global mobile market uses the standard.

GSM is used by over 3 billion people across more than 212 countries. Its ubiquity

makes international roaming very common between mobile phone operators enabling

subscribers to use their phones in many parts of the world. GSM differs from its

predecessors in that both signaling and speech channels are digital, and thus is

considered a second generation (2G) mobile phone system. This has also meant that data

communication was easy to build into the system.

2.2 LITERATURE REVIEW

This project is an implementation to the idea of the wireless communication between a

mobile phone and a microcontroller. Currently the main work that has been done on this

proposed system is through serial port to the computer but not wireless. If they want to

switch on and off the appliance, they have to go to the remote area and one /off the

appliance. But in this new design, the systems need not be reprogrammed to control

another home appliance without changing the programming of microcontroller. The user

will send SMS from his phone and he will be able to control the appliance.

7|Page
2.3 GSM ARCHITECTURE

GSM is a complex system and difficult to understand. The Mobile Station (MS) refers to

the mobile equipment. The Base Station Subsystem controls the radio link with the

Mobile Station. The Network Subsystem performs main functions such as switching of

calls between mobile users, mobility management operations, and proper operation and

setup of a network. These functions are controlled by the Mobile Services Switching

Center (MSC).

2.4 TECHNICAL DETAILS

GSM is a cellular network, which means that mobile phones connect to it by searching

for cells in the immediate vicinity.

2.5 GSM FREQUENCIES

GSM networks operate in a number of different frequency ranges (separated into GSM

frequency ranges for 2G and UMTS frequency bands for 3G). Most 2G GSM networks

Operate in the 900 MHz or 1800 MHz bands. Some countries in the Americas

(including Canada and the United States) use the 850 MHz and 1900 MHz bands

because the 900 and 1800 MHz frequency bands were already allocated. Most 3G GSM

networks in Europe operate in the 2100 MHz frequency band.

8|Page
2.6 NETWORK STRUCTURE

The network behind the GSM seen by the customer is large and complicated in order to

provide all of the services which are required.

 The Base Station Subsystem (the base stations and their controllers).

 The Network and Switching Subsystem (the part of the network most similar to a

fixed network). This is sometimes also just called the core network.

 The GPRS Core Network (the optional part which allows packet based Internet

connections).

2.7 SUBSCRIBER IDENTITY MODULE (SIM)

One of the key features of GSM is the Subscriber Identity Module, commonly known

as a SIM card. The SIM is a detachable smart card containing the user's subscription

information and phone book. This allows the user to retain his or her information after

switching handsets. Alternatively, the user can also change operators while retaining the

handset simply by changing the SIM. Some operators will block this by allowing the

phone to use only a single SIM, or only a SIM issued by them; this practice is known as

SIM locking, and is illegal in some countries.

2.8 GSM SECURITY

GSM was designed with a moderate level of security. The system was designed to

authenticate the subscriber using a pre-shared key and challenge-response.

Communications between the subscriber and the base station can be encrypted.

9|Page
3. SIMULATOR

KEIL Micro Vision is an integrated development environment used to create software to

be run on embedded systems (like a microcontroller). It allows for such software to be

written either in assembly or C programming languages and for that software to be

simulated on a computer before being loaded onto the microcontroller. The software

used is c programming

10 | P a g e
μVision3 is an IDE (Integrated Development Environment) that helps write,

compile, and debug embedded programs. It encapsulates the following components:

 A project manager.

 A make facility.

 Tool configuration.

 Editor.

 A powerful debugger.

To create a GSM ENOTICE board project in uVision3:

1. Select Project - New Project.

2. Select a directory and enter the name of the project file.

3. Select Project –Select Device and select a device from Device Database.

4. Create source files to add to the project

5. Select Project - Targets, Groups, and Files. Add/Files, select Source Group1, and

add the

Source files to the project.

6. Select Project - Options and set the tool options. Note that when the target device

is selected from the Device Database all-special options are set automatically.

Default memory model settings are optimal for most applications.

7. Select Project - Rebuild all target files or Build target.

11 | P a g e
To create a new project, simply start micro vision and select “Project”=>”New

Project” from the pull–down menus. In the file dialog that appears, a filename and

directory was chosen for the project. It is recommended that a new directory be

created for each project, as several files will be generated. Once the project has been

named, the dialog shown in the figure below will appear, prompting the user to

select a target device. The chip being used is the “AT89S52,” which is listed under

the heading “Atmel”.

Next, Micro Vision was instructed to generate a HEX file upon program

compilation. A HEX file is a standard file format for storing executable code that is to

be loaded onto the microcontroller. In the “Project Workspace” pane at the left, right–

click on “Target 1” and select “Options for ‘Target 1’ ”.Under the “Output” tab of the

resulting options dialog, ensure that both the “Create Executable” and “Create HEX

File” options are checked. Then click “OK”.

Next, a file must be added to the project that will contain the project code. To do

this, expand the “Target 1” heading, right–click on the “Source Group 1” folder, and

select “Add files…” Create a new blank file (the file name should end in “.c”), select it,

and click “Add.” The new file should now appear in the “Project Workspace” pane

under the “Source Group 1” folder. Double-click on the newly created file to open it in

the editor. To compile the program, first save all source files by clicking on the “Save

All” button, and then click on the “Rebuild All Target Files” to compile the program as

shown in the figure below. If any errors or warnings occur during compilation, they will

12 | P a g e
be displayed in the output window at the bottom of the screen. All errors and warnings

will reference the line and column number in which they occur along with a description

of the problem so that they can be easily located.

When the program has been successfully compiled, it can be simulated using the

integrated debugger in KEIL Micro Vision. To start the debugger, select

“Debug”=>”Start/Stop Debug Session” from the pull–down menus.

At the left side of the debugger window, a table is displayed containing several

key parameters about the simulated microcontroller, most notably the elapsed time

(circled in the figure below). Just above that, there are several buttons that control code

execution. The “Run” button will cause the program to run continuously until a

breakpoint is reached, whereas the “Step Into” button will execute the next line of code

and then pause (the current position in the program is indicated by a yellow arrow to the

left of the code).

13 | P a g e
PRO51 BURNER SOFTWARE

PRO51 BURNER provides you with software burning tools for 8051 based
Microcontrollers in their Flash memory. The 51 BURNER tools, you can burn
AT89C/SXXXX series of ATMEL microcontrollers.

14 | P a g e
4. SIMULATION AND ITS RESULTS

4.1 Bridge rectifier (230V to 9V)

Output

15 | P a g e
4.2 Power supply for microcontroller (230 V to 5 V)

Output

16 | P a g e
4.3 Power supply for GSM module (230 V to 12 V)

Output

17 | P a g e
5 HARDWARE PROTOTYPE AND RESULTS

5.1 POWER SUPPLY:

Power supply is a reference to a source of electrical power. A device or system that

supplies electrical or other types of energy to an output load or group of loads is called a

power supply unit or PSU. The term is most commonly applied to electrical energy

supplies, less often to mechanical ones, and rarely to others.

Fig 5.1 hardware prototype

18 | P a g e
Here in our application we need a 5v DC power supply for all electronics involved in the

project. This requires step down transformer, rectifier, voltage regulator, and filter

circuit for generation of 5v DC power. Here a brief description of all the components are

given as follows

5.2 TRANSFORMER:

A transformer is a device that transfers electrical energy from one circuit to another

through inductively coupled conductors — the transformer's coils or "windings". Except

for air-core transformers, the conductors are commonly wound around a single iron-rich

core, or around separate but magnetically-coupled cores. A varying current in the first or

"primary" winding creates a varying magnetic field in the core (or cores) of the

transformer. This varying magnetic field induces a varying electromotive force (EMF)

or "voltage" in the "secondary" winding. This effect is called mutual induction.

Fig5.2 Transformer

19 | P a g e
If a load is connected to the secondary circuit, electric charge will flow in the secondary

winding of the transformer and transfer energy from the primary circuit to the load

connected in the secondary circuit.

The secondary induced voltage VS, of an ideal transformer, is scaled from the primary

VP by a factor equal to the ratio of the number of turns of wire in their respective

windings:

VS/VP=NNS/NP

By appropriate selection of the numbers of turns, a transformer thus allows an

alternating voltage to be stepped up — by making NS more than NP — or stepped down,

by making it

5.3 BRIDGE RECTIFIER

Fig 5.3 Circuit diagram of bridge rectifier

A bridge rectifier makes use of four diodes in a bridge arrangement to achieve full-wave
rectification. This is a widely used configuration, both with individual diodes wired as
shown and with single component bridges where the diode bridge is wired internally.

20 | P a g e
5.4 REGULATOR IC (7805)

It is a three pin IC used as a voltage regulator. It converts unregulated DC current into

regulated DC current.

Fig 5.4 circuit diagram of regulator IC with Bridge rectifier

Normally we get fixed output by connecting the voltage regulator at the output of the

filtered DC (see in above diagram). It can also be used in circuits to get a low DC

voltage from a high DC voltage (for example we use 7805 to get 5V from 12V). There

are two types of voltage regulators 1. Fixed voltage regulators (78xx, 79xx) 2.Variable

voltage regulators (LM317) in fixed voltage regulators there is another classification

+ve voltage regulators and -ve voltage regulators. Positive voltage regulator this include

78xx voltage regulators. The most commonly used ones are 7805 and 7812. 7805 gives

fixed 5V DC voltage if input voltage is in (7.5V, 20V).

21 | P a g e
5.5 THE CAPACITOR FILTER

The simple capacitor filter is the most basic type of power supply filter. The application

of the simple capacitor filter is very limited. It is sometimes used on extremely high-

voltage, low-current power supplies for cathode ray and similar electron tubes, which

the power-supply ripple frequency is not critical; this frequency can be relatively high.

The capacitor (C1) shown in figure is a simple filter connected across the output of the

rectifier in parallel with the load.

Fig 5.5 circuit diagram of capacitor filter

Full-wave rectifier with a capacitor filter

When this filter is used, the RC charge time of the filter capacitor (C1) must be short

and the RC discharge time must be long to eliminate ripple action. In other words, the

capacitor must charge up fast, preferably with no discharge at all. Better filtering also

results when the input frequency is high; therefore, the full-wave rectifier output is

easier to filter than that of the half-wave rectifier because of its higher frequency.

22 | P a g e
5.6 RELAY

The relay takes advantage of the fact that when electricity flows through a coil, it

becomes an electromagnet. The electromagnetic coil attracts a steel plate, which is

attached to a switch. So the switch's motion (ON and OFF) is controlled by the current

flowing to the coil, or not, respectively. A very useful feature of a relay is that it can be

used to electrically isolate different parts of a circuit. It will allow a low voltage circuit

(e.g. 5VDC) to switch the power in a high voltage circuit (e.g. 100 VAC or more).The

relay operates mechanically, so it cannot operate at high speed.

Fig 5.6 relay diagram

23 | P a g e
6. DESIGN AND CONSTRUCTION

6.1GSM MODEM

A GSM modem is a wireless modem that works with a GSM wireless network. A

wireless modem behaves like a dial-up modem. The main difference between them is

that a dial-up modem sends and receives data through a fixed telephone line while a

wireless modem sends and receives data through radio waves. Like a GSM mobile

phone, a GSM modem requires a SIM card from a wireless carrier in order to operate.

Fig 6.1 GSM Modem

24 | P a g e
6.2 Microcontroller – Modem Interfacing
DTE and DCE
The terms DTE and DCE are very common in the data communications market. DTE is

short for Data Terminal Equipment and DCE stands for Data Communications

Equipment. As the full DTE name indicates this is a piece of device that ends a

communication line, whereas the DCE provides a path for communication. Let's say we

have a computer on which we want to communicate with the Internet through a modem

and a dial-up connection. To get to the Internet we tell our modem to dial the number of

our provider. After our modem has dialed the number, the modem of the provider will

answer our call and we will hear a lot of noise. Then it becomes quiet and we see our

login prompt or our dialing program tells us the connection is established. Now we have

a connection with the server from our provider and we can surf the Internet.

25 | P a g e
6.3 MICROCONTROLLER – LCD INTERFACING

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.

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

had 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.

We used a power supply of 5volt. The user may select whether the LCD is to operate

with a 4-bit data bus or an 8- bit data bus. If a 4-bit data bus is used, the LCD will

require a total of 7 data lines. If an 8-bit data bus is used, the LCD will require a total of

11 data lines. LCD with 8-bit data bus is used for this design. The three control lines are

26 | P a g e
EN, RS, and RW. EN line must be raised/lowered before/after each instruction sent to

the LCD regardless of whether that instruction is read or write text or instruction. In

short, we manipulate EN when communicating with the LCD.

DISPLAY

After validity check the control flow goes into the LCD program module to display the

valid message stored in the memory. In case of multiple valid numbers all invalid stored

messages are deleted by proper branching in the code to the “delete-message” module.

Fir 6.2 block diagram of LCD display

27 | P a g e
6.4. PROGRAMMER

When we have to learn about a new computer we have to familiarize about the machine

capability we are using, and we can do it by studying the internal hardware design

(devices architecture), and also to know about the size, number and the size of the

registers.

A microcontroller is a single chip that contains the processor (the CPU), non-volatile

memory for the program (ROM or flash), volatile memory for input and output (RAM),

a clock and an I/O control unit. Also called a "computer on a chip," billions of

microcontroller units (MCUs) are embedded each year in a myriad of products from

toys to appliances to automobiles. For example, a single vehicle can use 70 or more

microcontrollers. The following picture describes a general block diagram of

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 pin out. The on-chip Flash allows the

program memory to be reprogrammed in-system or by a conventional nonvolatile

memory pro-grammar. 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, Watchdog timer, two data pointers,

28 | P a g e
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. The Power-

down mode saves the RAM con-tents but freezes the oscillator, disabling all other chip

functions until the next interrupt. The hardware is driven by a set of program

instructions, or software. Once familiar with hardware and software, the user can then

apply the microcontroller to the problems easily. The pin diagram of the 8051 shows

all of the input/output pins unique to microcontrollers:

Fig 6.3 Microcontroller 40 pin IC (89S52)

29 | P a g e
The following are some of the capabilities of 8051 microcontroller.

 Internal ROM and RAM

 I/O ports with programmable pins

 Timers and counters

 Serial data communication

The 8051 architecture consists of these specific features:

 16 bit PC &data pointer (DPTR)

 8 bit program status word (PSW)

 8 bit stack pointer (SP)

 Internal ROM 4k

 Internal RAM of 128 bytes.

 4 register banks, each containing 8 registers

 80 bits of general purpose data memory

 32 input/output pins arranged as four 8 bit ports: P0-P3

 Two 16 bit timer/counters: T0-T1

 Two external and three internal interrupt sources Oscillator and clock

circuits.

30 | P a g e
Circuit diagram

Fig 6.4 circuit digram of microcontroller based power theft identifier

31 | P a g e
Result

Fig 6.5 hardware prototype


model

Fig 6.6 hardware result

32 | P a g e
7 CONCLUSION

The power theft identifier using microcontroller and GSM is very effective topology to

control electricity theft percentage. The hardware prototype is built by using microcontroller

(89S52) and GSM module. The hardware results are obtained for household load and

household load including with theft load. And from hardware result it concluded that, as the

analog energy meter shows load reading by using microcontroller reads actual reading

where theft taken into account and that reading will be send by SMS to EB officer by using

GSM modem. If there is difference between internal reading and energy meter reading,

immediately EB officer take action, make supply OFF by sending OFF command. It works

for all 24 hours. In commercial areas, municipal, Agriculture application and specially

for rural areas, it can be most applicable. By using this system, preventing the illegal

usage of the electrical power and tariff rate can be reduced. This method can make the

country economically strong, save electricity & removes the problem of load shedding.

Problem Encountered
 During soldering, many of the connection become short circuit. So we desolder the

connection and did soldering again.

 A leg of the crystal oscillator was broken during mounting. So it has been replaced.

 LED`s get damaged when we switched ON the supply so we replace it by the new

one.

 Care should be taken while soldering. There should be no shorting of joints.

 Proper power supply should maintain.

33 | P a g e
FUTURE SCOPE

 In this project, for sending and receiving sms GSM modem is used. This GSM

modem can be replace by GPS system for knowing exact location of

consumer. And how much load consumed by consumer.

 This system can also be utilized in distribution line.

 This system can be used by computer software for sms purpose in place of

mobile phone.

 We can use free sms service website for reducing sms charges.

34 | P a g e
REFERENCES

[1] T.B. Smith, “Electricity theft- comparative analysis,” Energy Policy, vol. 32, pp.
2067–2076, Aug. 2003.
[2] “Electricity thefts on the rise,” : http://www.wtsp.com/news
/local/story.aspx?storyid=109056.
[3] “Electricity theft: Dangerous trend that's costing honest Houstonians money,”
: http://www.khou.com/news/Electricity-theft-a-dangerous-trend-thats-costing-
honest-Houstonians-money-92450694.html.
[4] “Anti theft,” Maharashtra State Electricity Distribution Company, [Online].
Available: http://www.mahadiscom.in/anti-theft-photo/anti-theft-photo-01.jpg.
[5] V. J. Vibhute, “The 8051Microcontroller”.Chapter 4, 5, 6. Page 4.1-6.59
Second edition, publication-Tech Max.
[6] Muhammad Ali Mazidi, “The 8051 Microcontroller and Embedded Systems”.
Second edition, publication-prentice Hall.
[7] Vijay Garg, “Principles and Applications of GSM”, first edition. Publication-
prentice Hall.

35 | P a g e
APPENDICES
SOURCE CODE

#include<reg52.h>
//---------------------------------Defining Macros-------------

//

sbit RS=P3^7;
sbit EN=P3^6;
sbit RLY1=P1^0;

unsigned char pulse=0;

Void timer0 () interrupt 0


{
pulse++;
}

#define TIMEOUT 20000


#define NO_OF_RETRY 5

//---------------------------------------
// Forward function declaration
//---------------------------------------
void send_cmd(unsigned char *cmd);
void Txmsg(unsigned char *no,unsigned char *msg,unsigned char a1);
unsigned char Rxmsg(void);
void lcdinit(void);
void lcdclear(void);
void lcdData(unsigned char l);
void lcdcmd(unsigned char k);
void DelayMs(unsigned int count);
void InitModem(void);
void lcd_puts(unsigned char *p);
void uart_puts(unsigned char *p);
void uart_gets(unsigned char *p,unsigned char len);
void uart_putchar(unsigned char p);
void uart_getchar(unsigned char *p);
void wait();

36 | P a g e
void acknowledge();
void signal_strength(void);

unsigned char status=0,ret=0,mno[]="35791";

//---------------------------------------
// Main rotine
//---------------------------------------
void main()
{
TMOD=0x40; // Configure UART at 9600 baud rate
TH1=0xFa;
SCON=0x50;

TR1=1;

EA=1;
EX0=1;
IT0=0;

RLY1=0;

lcdinit(); // Initialize LCD


lcd_puts("INITIALIZING MODEM...PLZ WAIT");
DelayMs(10000);
InitModem(); // Initialize Modem

//Txmsg(mno,"GSM BASED ENERGY METER WITH TEMPER PROOF",pulse);


//DelayMs(1000);
EA=1;
while(1)
{
ret=Rxmsg();
if(ret==1)
Txmsg(mno,"POWER ON, METER READING=",pulse);
if(ret==2)
Txmsg(mno,"POWER OFF, METER READING=",pulse);
if(ret==3)
Txmsg(mno,"CURRENT REDING OF METER=",pulse);

}
}

//----------------------------------------------------
// send command subroutine to check the connectivity of modem

37 | P a g e
//----------------------------------------------------

void send_cmd(unsigned char *cmd)


{
unsigned char i,buff[6];
retry:
lcdclear();
uart_puts(cmd); // Sending Command
uart_putchar(0x0d); // Enter
uart_gets(buff,6); // Receive response
DelayMs(100);

for(i=0;i<6;i++) // Compare response


{
if(buff[i]=='a' || buff[i]=='b')
goto retry;
if(buff[i]=='e' || buff[i]=='j')
return;
}
}

//---------------------------------------
// Modem initialization subroutine
//---------------------------------------

void Txmsg(unsigned char *no, unsigned char *msg,unsigned char a1)


{
unsigned char i=0,j=0,buff[20],d1,d2,d3;

retry:
j++;
send_cmd("at"); // AT command sending to check
the connectivity
lcdclear();
uart_puts("at+cmgs=\"");
lcdcmd(0xC0);
uart_puts(no);
DelayMs(100);
uart_putchar('"');
uart_putchar(0x0d);
lcdclear();
uart_getchar(buff);
uart_puts(msg);
while(a1!=0)

38 | P a g e
{
d1=a1%10;
d1+=0x30;
//digits before desible point
a1=a1/10;
d2=a1%10;
d2+=0x30;

a1=a1/10;
d3=a1%10;
d3+=0x30;

a1=a1/10;
}
uart_putchar(d3);
uart_putchar(d2);
uart_putchar(d1);
uart_putchar(26);
lcdclear();
uart_gets(buff,20);
DelayMs(1000);

for(i=0;i<10;i++) //command to recv data


{
if(buff[i]=='+' && buff[i+1]=='C' && buff[i+2]=='M' && buff[i+3]=='G')
return;
}
if(j<NO_OF_RETRY)

//---------------------------------------
// Recieve message subroutine
//---------------------------------------
unsigned char Rxmsg(void)
{
unsigned char i=0,ret=0;
unsigned int j=0;
unsigned char c[84];

retry:
if(push==1)
{
return 4;
}

39 | P a g e
send_cmd("at"); // AT command sending to check
the connectivity
DelayMs(100);
lcdclear();
uart_puts("at+cmgr=1");
DelayMs(100);
uart_putchar(0x0d);

lcdcmd(0xC0);
EA=0;
for(i=0;i<84;i++)
{
j=0;
while(RI==0)
{
if(j>=TIMEOUT)
goto timeout;
wait();
j++;
}
c[i]=SBUF;
RI=0;
lcdData(c[i]);
}
//EA=1;
DelayMs(1000);

timeout:
for(i=0;i<5;i++) //command to recv data
{
if((c[i]=='O') || (c[i]=='K'))
return ret;
}

goto retry;

sucess1:
i=i+7;

for(j=0;j<13;j++)
{
mno[j]=c[i];
i++;
DelayMs(10);
}

40 | P a g e
mno[j]='\0';

for(;i<84;i++)
{
if((c[i]=='3') && (c[i+1]=='5') && (c[i+2]=='7') && (c[i+3]=='9') && (c[i+4]=='1'))
goto sucess;
}
goto delete;

sucess:
for(i=0;i<84;i++)
{
if((c[i]=='O') && (c[i+1]=='N'))
{
RLY1=1;
ret=1;
}

if((c[i]=='O') && (c[i+1]=='F') && (c[i+2]=='F'))


{
RLY1=0;
ret=2;
}
if((c[i]=='R') && (c[i+1]=='E') && (c[i+2]=='A') && (c[i+3]=='D'))
{
ret=3;
}
}
delete:
send_cmd("at+cmgd=1"); // sending AT+CMGD to delete message
return ret;
}

//---------------------------------------
// Lcd initialization subroutine
//---------------------------------------
void lcdinit(void)
{
lcdcmd(0x38);
DelayMs(250);
lcdcmd(0x0E);
DelayMs(250);
lcdcmd(0x01);
DelayMs(250);
lcdcmd(0x06);
DelayMs(250);

41 | P a g e
lcdcmd(0x80);
DelayMs(250);
}

//---------------------------------------
// Lcd initialization subroutine
//---------------------------------------
void lcdclear(void)
{
lcdcmd(0x01);
DelayMs(1);
lcdcmd(0x80);
DelayMs(1);
}

//---------------------------------------
// Lcd data display
//---------------------------------------
void lcdData(unsigned char l)
{
P2=l;
RS=1;
EN=1;
DelayMs(1);
EN=0;
}

//---------------------------------------
// Lcd command
//---------------------------------------
void lcdcmd(unsigned char k)
{
P2=k;
RS=0;
EN=1;
DelayMs(1);
EN=0;
}

//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count)
{
// mSec Delay 11.0592 Mhz

42 | P a g e
unsigned int i;
// Keil v7.5a
while(count) {
i = 115;
// 115 exact value
while(i>0) i--;
count--;
}
}

//---------------------------------------------------------------------------------------------------------
------------------
//--------------------------a small delay fxn to ensure the line settels down after transition---
----------------------------

void wait()
{
char i;
for(i=0;i<=10;i++)
i++;
return;
}

void lcd_puts(unsigned char *p)


{
unsigned char i=0;

while(p[i]!='\0')
{
lcdData(p[i]);
if(i==16)
lcdcmd(0xC0);
i++;
}
}

void uart_puts(unsigned char *p)


{
while(*p!='\0')
{
lcdData(*p);
uart_putchar(*p);
DelayMs(10);
p++;
}
}

43 | P a g e
void uart_putchar(unsigned char p)
{
SBUF=p;
while(TI==0);
TI=0;
}

void uart_gets(unsigned char *p,unsigned char len)


{
unsigned int i,j;

for(i=0;i<len;i++) //command to recv data


{
j=0;
while(RI==0)
{
if(j>=TIMEOUT)
break;
wait();
j++;
}
p[i]=SBUF;
RI=0;
lcdData(p[i]);
}
}

void uart_getchar(unsigned char *p)


{
unsigned int i,j;

i=0;
do
{
j=0;
while(RI==0)
{
if(j>=TIMEOUT)
break;
wait();
j++;
}
p[i]=SBUF;
RI=0;
lcdData(p[i]);

44 | P a g e
i++;
} while(p[i]!='>' && i<5);
}

void signal_strength(void)
{
unsigned char i,buff[12];
retry:
lcdclear();
uart_puts("at+csq"); // Sending Command
uart_putchar(0x0d); // Enter
lcdcmd(0xC0);
uart_gets(buff,12); // Receive response
DelayMs(1000);

for(i=0;i<12;i++) // Compare response


{
if(buff[i]=='E' || buff[i]=='R')
goto retry;
if(buff[i]=='+' && buff[i+1]=='C')
{
if(buff[i+6]>'3')
goto retry;
else
{
lcdclear();
lcd_puts("Signal Strength");
lcdcmd(0xC0);
lcdData(buff[i+6]);
lcdData(buff[i+7]);
DelayMs(1000);
return;
}
}
}
goto retry;
}

45 | P a g e

Anda mungkin juga menyukai