Sie sind auf Seite 1von 37

SMS Controlling Electrical devices

SMS Controlling Electrical devices

Content s
..

SMS Controlling Electrical devices

Introduction
Over the last few years, the technologies of Remote control and wireless control spreads extremely fast and play a main role in our life . for example in our houses there are many devices work with remote control like TV , Video set , DVD player .. etc. in addition to that you can control your factory machines and observe them where they are working although you live in other country . Some of these important technologies are : 1- Telephone (DTMF) control system. 2- SMS control system. 3- Internet control system. 4- RF control system . This project explains one of these technologies (SMS control system) and shows how to apply it to your home ,company or factory to control any electrical or electronic devices (like motor , fan ,wash machine , TV , air conditioner , etc) and use a computer software to make a small database or history to show when these devices work and when they stop ? and who given these orders to make them ON or OFF ? .

SMS Controlling Electrical devices

Abstract
This project designed to control over 8 devices remotely by using SMS short message technology, so that we can control our devices from any place of the world. To make any devices ON or OFF we send a specific SMS message which contains the device name or number and the state which we want (ON , OFF). The message would be like this make a fan ON .make device 3 OFF. This SMS message we sent is received by another mobile(or gsm modem) which connected to a computer and microcontroller circuit. The Computer has software we designed it to store all received SMS messages and its time and date on a database. The microcontroller circuit controls our devices, make them ON or OFF according to SMS message, and can observe any device at any time. If we leave the home and we want to ensure if the air conditioner or other device is ON or OFF we can send a SMS message contain for exampleair conditioner state ? and we will receive another SMS message from our system contains the state of this device like this air conditioner is OFF . This system contains some security features which described in details in security part . One of these features is that if a thief entered your house or company the sensor which connected to microcontroller circuit can detect the thief and send a SMS message to your mobile and the message will be like this your house in dangerous. there is a thief . this sensor is an IR detector or receiver and IR led transmitter . they described in hardware description.
3

SMS Controlling Electrical devices

Project maps
1- The mechanism to control any device :

sending sms message from any mobile to control a certain device

microcontroller controlling the device and make it on or off

receving the message by another mobile

PC sending an order to microcontroller to control a device

sending this message to PC

SMS Controlling Electrical devices


2-The mechanism to know the state of device (is it ON or OFF ? )

send a message from any mobile to know the state of any device
receivng this message by anoter mobile (this mobile attatched to our system)

sending this message to a PC

PC sending an order to microcontroller to know the state of the device. the microcontroller check the device state and send this inofrmation to computer (PC) pc make a mobile send a sms contians the state of device

message received to the user and contains the state of the device
5

SMS Controlling Electrical devices

2- The mechanism to alarm if a thief attacks the house or company.

attacking from a theif

sensor detect him and microcontroller send signal to computer

computer send message to a house owner " your house is attacing from a theif"

SMS Controlling Electrical devices

Hardware description
1- Microcontroller : A microcontroller is a single-chip computer. Micro suggests that the device is small, and controller suggests that it is used in control applications. Another term for microcontroller is embedded controller, since most of the microcontrollers are built into (or embedded in) the devices they control. The function of a microcontroller is not constant like other ICs . the function of a microcontroller can be change by a programming process. So, if we have two microcontroller (e.g. pic16f877a) each one may be has a different function although they are same. All microcontrollers operate on a set of instructions (or the user program) stored in their memory. A microcontroller fetches the instructions from its program memory one by one, decodes these instructions, and then carries out the required operations. Microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast, it has several disadvantages. An assembly program consists of mnemonics, which makes learning and maintaining a program written using the assembly language difficult. Also, microcontrollers manufactured by different firms have different assembly languages, so the user must learn a new language with every new microcontroller he orshe uses. Microcontrollers can also be programmed using a high-level language, such as BASIC,PASCAL, or C. High-level languages are much easier to learn than assembly languages. They also facilitate the development of large and complex programs.

SMS Controlling Electrical devices


In this project we use a pic16f877a microcontroller because it has many features like serial communications , 33 I/O pins and more (see pic16f877a datasheet appendix A ) .

Pic16f877a 2-GSM modem : GSM modem is a kind of modems that work with SIM card and we can connect it to computer via serial communication or USB . The modem can send SMS and receive it and other operations, but the most important operation to us is send and receive sms messages. We can read the forward message and send message by giving order to gsm modem these order which a gsm modem can understand it are AT commands.

GSM modem

SMS Controlling Electrical devices


3- Relay switch circuit : a microcontroller output pins cant make a fan ON or OFF directly , Because microcontroller pins voltage can be +5 or 0 DC volt where a fan works By 220 AC volt another reason is the current supplied from microcontroller is very small (20 mA) where we want to control devices work with 1 A or 2 A or more. A relay switch circuit is appended to microcontroller circuit to make a microcontroller able to control a fan or any electrical device which work with AC volt. Relay switch circuit is consists of : 1- Resistor and transistor. 2- Relay. 3- Diode. 4- Power supply to relay and another to an electrical device . Microcontroller cant operate a relay directly because a relay need more current (greater than 20 mA which can be supplied by microcontroller) and the relay may be work with 12 V or more where microcontroller pins can be +5 volt or 0 volt only. So , we must use an electronic component between microcontroller pins and relay . this component is a transistor . transistor has many applications like amplify the voltage and current. We use a transistor as a switch to control a relay . A resistor should be attached to transistor base.

SMS Controlling Electrical devices


Transistors has many packages (or shapes) as shown in this figure :

Transostors

Resistors

10

SMS Controlling Electrical devices


Here we can connect any electrical device like fan or lambetc.

Any i/o pin from microcontroller

How relay works? A relay is an electrically operated switch. Current flowing through the coil of the relay creates a magnetic field which attracts a lever and changes the switch contacts. The coil current can be on or off so relays have two switch positions and most have double throw (changeover) switch contacts as shown in the diagram.

Circuit symbol for a relay


11

SMS Controlling Electrical devices

Relays allow one circuit to switch a second circuit which can be completely separate from the first. For example a low voltage battery circuit can use a relay to switch a 230V AC mains circuit. There is no electrical connection inside the relay between the two circuits, the link is magnetic and mechanical. The coil of a relay passes a relatively large current, typically 30mA for a 12V relay, but it can be as much as 100mA for relays designed to operate from lower voltages. Most ICs (chips) cannot provide this current and a transistor is usually used to amplify the small IC current to the larger value required for the relay coil. The maximum output current for the popular 555 timer IC is 200mA so these devices can supply relay coils directly without amplification.

some shapes of relays Protection diodes for relays : Transistors and ICs must be protected from the brief high voltage produced when a relay coil is switched off. The diagram shows how a signal diode (eg 1N4148) is connected 'backwards' across the relay coil to provide this protection. Current flowing through a relay coil creates a magnetic field which collapses suddenly when the current is switched off. The sudden collapse of the magnetic field induces a brief high voltage across the relay coil which is very likely to
12

SMS Controlling Electrical devices


damage transistors and ICs. The protection diode allows the induced voltage to drive a brief current through the coil (and diode) so the magnetic field dies away quickly rather than instantly. This prevents the induced voltage becoming high enough to cause damage to transistors and ICs.

Some shapes of diodes


13

SMS Controlling Electrical devices


4-max232 chip: Our control circuit is connected to a computer by RS232 serial communication. So, microcontroller and pc connected to each other . but we cant connect them directly ,because microcontroller logic signal levels are +5 v for logic one and 0 volt for logic zero . but a RS232 in a PC are are at +12V and 12V. Thus the output signal from the microcontroller has to be converted to _12V, and the input from an RS232 device must be converted into 0 to 5V before it can be connected to a microcontroller. Using Max232 chip is an easy way to do this conversion operation.

The device requires four external 1uF capacitors for its operation.

14

SMS Controlling Electrical devices

Communication Protocols
1- RS232 serial communication protocol : the RS-232 serial communication protocol is a standard protocol used in asynchronous serial communication. It is the primary protocol used over modem lines and many devices .

Serial communication is basically the transmission or reception of data one bit at a time. Today's computers generally address data in bytes or some multiple thereof. A byte contains 8 bits. A bit is basically either a logical 1 or zero. Every character on this page is actually expressed internally as one byte. The serial port is used to convert each byte to a stream of ones and zeroes as well as to convert a streams of ones and zeroes to bytes. The serial port contains a electronic chip called a Universal Asynchronous Receiver/Transmitter (UART) that actually does the conversion.
15

SMS Controlling Electrical devices


The serial port has many pins. We will discuss the transmit and receive pin first. Electrically speaking, whenever the serial port sends a logical one (1) a negative voltage is effected on the transmit pin. Whenever the serial port sends a logical zero (0) a positive voltage is effected. When no data is being sent, the serial port's transmit pin's voltage is negative (1) and is said to be in a MARK state. Note that the serial port can also be forced to keep the transmit pin at a positive voltage (0) and is said to be the SPACE or BREAK state. (The terms MARK and SPACE are also used to simply denote a negative voltage (1) or a positive voltage(0) at the transmit pin respectively). When transmitting a byte, the UART (serial port) first sends a START BIT which is a positive voltage (0), followed by the data (general 8 bits, but could be 5, 6, 7, or 8 bits) followed by one or two STOP BITs which is a negative(1) voltage. The sequence is repeated for each byte sent. Figure 1 shows a diagram of a what a byte transmission would look like.

Figure 1 At this point you may want to know what is the duration of a bit. In other words, how long does the signal stay in a particular state to define a bit. The answer is simple. It is dependent on the baud rate. The baud rate is the number of times the signal can switch states in one second. Therefore, if the line is operating at 9600 baud, the line can switch states 9,600 times per second. This means each bit has the duration of 1/9600 of a second or about 100 sec. When transmitting a character there are other characteristics other than the baud rate that must be known or that must be setup. These characteristics define the entire interpretation of the data stream.
16

SMS Controlling Electrical devices


The first characteristic is the length of the byte that will be transmitted. This length in general can be anywhere from 5 to 8 bits. The second characteristic is parity. The parity characteristic can be even, odd, mark, space, or none. If even parity, then the last data bit transmitted will be a logical 1 if the data transmitted had an even amount of 0 bits. If odd parity, then the last data bit transmitted will be a logical 1 if the data transmitted had an odd amount of 0 bits. If MARK parity, then the last transmitted data bit will always be a logical 1. If SPACE parity, then the last transmitted data bit will always be a logical 0. If no parity then there is no parity bit transmitted. The third characteristic is the amount of stop bits. This value in general is 1 or 2. Assume we want to send the letter 'A' over the serial port. The binary representation of the letter 'A' is 01000001. Remembering that bits are transmitted from least significant bit (LSB) to most significant bit (MSB), the bit stream transmitted would be as follows for the line characteristics 8 bits, no parity, 1 stop bit, 9600 baud. LSB (0 1 0 0 0 0 0 1 0 1) MSB The above represents (Start Bit) (Data Bits) (Stop Bit) To calculate the actual byte transfer rate simply divide the baud rate by the number of bits that must be transferred for each byte of data. In the case of the above example, each character requires 10 bits to be transmitted for each character. As such, at 9600 baud, up to 960 bytes can be transferred in one second. The above discussion was concerned with the "electrical/logical" characteristics of the data stream. We will expand the discussion to line protocol. Serial communication can be half duplex or full duplex. Full duplex communication means that a device can receive and transmit data at the same
17

SMS Controlling Electrical devices


time. Half duplex means that the device cannot send and receive at the same time. It can do them both, but not at the same time. Half duplex communication is all but outdated except for a very small focused set of applications. Half duplex serial communication needs at a minimum two wires, signal ground and the data line. Full duplex serial communication needs at a minimum three wires, signal ground, transmit data line, and receive data line. The RS232 specification governs the physical and electrical characteristics of serial communications. This specification defines several additional signals that are asserted (set to logical 1) for information and control beyond the data signals and signal ground. These signals are the Carrier Detect Signal (CD), asserted by modems to signal a successful connection to another modem, Ring Indicator (RI), asserted by modems to signal the phone ringing, Data Set Ready (DSR), asserted by modems to show their presence, Clear To Send (CTS), asserted by modems if they can receive data, Data Terminal Ready (DTR), asserted by terminals to show their presence, Request To Send (RTS), asserted by terminals if they can receive data. The section RS232 Cabling describes these signals and how they are connected. The above paragraph eluded to hardware flow control. Hardware flow control is a method that two connected devices use to tell each other electronically when to send or when not to send data. A modem in general drops (logical 0) its CTS line when it can no longer receive characters. It reasserts it when it can receive again. A terminal does the same thing instead with the RTS signal. Another method of hardware flow control in practice is to perform the same procedure in the previous paragraph except that the DSR and DTR signals are used for the handshake. Note that hardware flow control requires the use of additional wires. The benefit to this however is crisp and reliable flow control. Another method of flow control used is known as software flow control. This method requires a
18

SMS Controlling Electrical devices


simple 3 wire serial communication link, transmit data, receive data, and signal ground. If using this method, when a device can no longer receive, it will transmit a character that the two devices agreed on. This character is known as the XOFF character. This character is generally a hexadecimal 13. When a device can receive again it transmits an XON character that both devices agreed to. This character is generally a hexadecimal 11.

19

SMS Controlling Electrical devices


RS232 connector : on many PC we can find this connector .

What can I do if I dont find the RS232 connector ? Some laptops and modern desktop computer dont have RS232 connector . The solution of this problem is to use (RS232 to USB converter). This converter Plugs directly into your computers USB port for a simple easy way to connect RS232 output devices. The steps to use this converter are : 1- Connect it to computer . 2- Install the driver (attached with it CD ). 3- Now we can use RS232 serial communication and its name may be com4 or COM5 , COM6 ,,,,,,, etc . determined by driver software.

20

SMS Controlling Electrical devices

Some shapes of (RS232 to USB converter)

21

SMS Controlling Electrical devices


2-AT commands : The AT command set is the fundamental interface with the modem. An AT command is simply a string of characters preceded by the AT prefix that is sent to the modem. The commands typically instruct the modem to perform some action or set some characteristic within the modem. The modem has two states: command state and on-line state. In command state, the modem will accept and respond to AT commands. In the on-line state, the modem will transmit data, but ignore AT commands. Typically the modem is in the on-line state after dialing. AT commands is easy to sent by serial communication (RS232) so, we can send the commands to gsm modem by using a computer via RS232. AT commands has the following format: * The command is prefixed with AT (Attention) * The commands can be entered in upper case or lower case. * The AT prefix can be in upper case or lower case, but both the A and the T must be the same case. * Characters that precede the AT prefix are ignored. * Multiple commands can be strung together on a single line and spaces may be included between commands but are not necessary. The command line interpretation begins upon receipt of the carriage return. These commands are used for request information about the current configuration or operational status of the mobile phone/modem and test

22

SMS Controlling Electrical devices


availability and request the range of valid parameters, when applicable, for an AT command.

Important AT command used to Test and Design: 1) ATD To dial a voice call from the modem. 2) AT+IPR To set the baud rate for the modem ( here for our application the baud rate is set as 9600) 3) ATA To answer an incoming call. 4) AT+CHUP To hang up the initiated call. 5) AT+CFUN To set the phone functionality. Set to 0 to deactivate the modem. 6) AT+CLIP To identify caller number this command is set to 1 7) AT+CLIR For calling line Identification Restriction. 8) AT+CNUM To identify the subscriber number. 9) AT+CMGR To read the message at particular location .The location number is given as index. 10) AT+CMGD To delete the received message 11) AT+CMGS To send the message. 12) AT+CMGF To change the message format to PDU or Text mode. 13) AT+CMGL To see all the list of messages. 14) ATE To enable and disable command echo.
23

SMS Controlling Electrical devices

Microcontroller Programming
1234567Steps to program a microcontroller : Write a code by any microcontroller compilers like MikroC . Build the project to get a hex file. Put a microcontroller in a programmer ( burner) circuit . Connect a burner to your computer . Open a software which can write hex file to microcontroller like picpgm,winpic. Etc. Choose the hex file from browse buttom and burn . Now, your microcontroller is ready to make it in your electronic circuit to do the determined job.

How to build your owner programmer ( Burner ) ? You can buy the programmer from microchip or from other company but it cost will be very cheep if you build it yourself.

This programmer name is JDM programmer (shown in figure) and its circuit is not complex and it has a little of compnenets. The circuit diagram of the programmer is shown on the next figure.

24

SMS Controlling Electrical devices

JDM programmer

How to connect the programmer to your PC ? We need a serial cable (male female ) or ( female female) depended on your connector in a programmer .

25

SMS Controlling Electrical devices


Using MikroC Compiler : The next step after Building a programmer is using any microcontroller compiler. MikroC is one of high level compilers so, it has many advantages : 1234The code is not very long like assembly . It is easy to make applications with small code . Easy to mange big projects. There are many libraries make the interface between microcontroller and other devices (snsors,LCD,keypad,RS232, etc) very easy . 5- There is no need to understand all details of operations happen inside the microcontroller like assembly. mikroC is a powerful, feature rich development tool for PICmicros. It is designed to provide the programmer with the easiest possible solution for developing applications for embedded systems.

26

SMS Controlling Electrical devices


Features : mikroC allows you to quickly develop and deploy complex applications:

Write your C source code using the built-in Code Editor (Code and Parameter Assistants, Syntax Highlighting, Auto Correct, Code Templates, andmore) Use the included mikroC libraries to dramatically speed up the development: data acquisition, memory, displays, conversions, communicationsPracticallyallP12,P16,andP18chipsaresupported. Monitor your program structure, variables, and functions in the Code Explorer. Generate commented, human-readable assembly, and standard HEX compatible with all programmers. Inspect program flow and debug executable logic with the integrated Debugger. Get detailed reports and graphs: RAM and ROM map, code statistics, assemblylisting,callingtree,andmore provided plenty of examples for users to expand, develop, and use as building bricks in your projects. Copy them entirely if you deem fit thats why we included them with the compiler.

Mikroc Libraries : mikroC provides a set of libraries which simplify the initialization and use of PIC MCU and its modules: Hardware/PIC-specific Libraries

ADC Library CAN Library CANSPI Library Compact Flash Library EEPROM Library Ethernet PIC18FxxJ60 Library SPI Ethernet Library Flash Memory Library Graphic LCD Library T6963C Graphic LCD Library IC Library
27

SMS Controlling Electrical devices


Keypad Library LCD Library LCD Custom Library LCD8 Library Manchester Code Library Multi Media Card Library OneWire Library PS/2 Library PWM Library RS-485 Library Software IC Library Software SPI Library Software UART Library Sound Library SPI Library USART Library USB HID Library Util Library SPI Graphic LCD Library Port Expander Library SPI LCD Library SPI LCD8 Library SPI T6963C Graphic LCD Library

Standard ANSI C Libraries


ANSI C Ctype Library ANSI C Math Library ANSI C Stdlib Library ANSI C String Library

Miscellaneous Libraries

Conversions Library Trigonometry Library sprint Library Setjmp Library Time Library

28

SMS Controlling Electrical devices


This is an example to show how to deal with RS232 communication between microcontroller and computer. The function of this code to make a device ON (this device connected to B0 pin (portb.f0) of microcontroller) when a microcontroller receive a character G from computer.

char data; void main() { trisc=0; // all portc pins are output (C0,C1,.C7) . Usart_Init(9600); // Baudrate of serial communication is 9600 while(1){ if(usart_data_ready()==1) // when receiving data { data=usart_read(); // read data and store it in variable if(data=='G') portc.f0=1;// make a device ON } } }
In our project the computer receive sms messages through GSM modem like open the door then the computer send a character or data to microcontroller via RS232 this char may be like the last example G or anything we want after that the microcontroller make one pin high, this pin connected to Relay switch circuit which connected to a motor of the door . the motor will be on and the door will be opened. So our microcontroller code is very simple it just contains if statements and some variables and using of USART library (which make serial communication very easy) .

29

SMS Controlling Electrical devices


USART Library USART hardware module is available with a number of PICmicros. mikroC USART Library provides comfortable work with the Asynchronous (full duplex) mode. You can easily communicate with other devices via RS232 protocol (for example with PC, see the figure at the end of the topic RS232 HW connection). You need a PIC MCU with hardware integrated USART, for example PIC16F877. Then, simply use the functions listed below.

30

SMS Controlling Electrical devices


Library Routines

Usart_Init Usart_Data_Ready Usart_Read Usart_Write

Certain PICmicros with two USART modules, such as P18F8520, require you to specify the module you want to use. Simply append the number 1 or 2 to a function name. For example, Usart_Write2(); Also, for the sake of backward compabitility with previous compiler versions and easier code management, MCU's with multiple USART modules have USART library which is identical to USART1 (i.e. you can use Usart_Init() instead of Usart_Init1() for Usart operations). Usart_Init Prototype void Usart_Init(const unsigned long baud_rate); Returns Nothing. Description Initializes hardware USART module with the desired baud rate. Refer to the device data sheet for baud rates allowed for specific Fosc. If you specify the unsupported baud rate, compiler will report an error. Requires You need PIC MCU with hardware USART. Usart_Init needs to be called before using other functions from USART Library. Example This will initialize hardware USART and establish the communication at 2400 bps: Usart_Init(2400); Usart_Data_Ready Prototype unsigned short Usart_Data_Ready(void); Returns Function returns 1 if data is ready or 0 if there is no data. Description Use the function to test if data in receive buffer is ready for reading. Requires USART HW module must be initialized and communication established before using this function. See Usart_Init.

31

SMS Controlling Electrical devices


Example If data is ready, read it: int receive; ... if (Usart_Data_Ready()) receive = Usart_Read; Usart_Read Prototype unsigned short Usart_Read(void); Returns Returns the received byte. If byte is not received, returns 0. Description Function receives a byte via USART. Use the function Usart_Data_Ready to test if data is ready first. Requires USART HW module must be initialized and communication established before using this function. See Usart_Init. Example If data is ready, read it: int receive; ... if (Usart_Data_Ready()) receive = Usart_Read(); Usart_Write Prototype void Usart_Write(unsigned short data); Returns Nothing. Description Function transmits a byte (data) via USART. Requires USART HW module must be initialized and communication established before using this function. See Usart_Init. Example int chunk = 0x1E; Usart_Write(chunk); /* send chunk via USART */

32

SMS Controlling Electrical devices

Software description
In our project we designed software by using C# .net, This software has many jobs : 1- Deal with GSM modem by using serial communication via AT commands. 2- It is an interface between GSM modem and microcontroller ciruit. 3- Store all sms messages and its time and date. 4- Manage a security operations and permissions manage a security operation to our system by using password to all users . each user has his own password and this password must be written on the beginning of his sms messages. And there are permissions given to user.

Visual studio .net framework (shown in the picture) we use it to design our software .
33

SMS Controlling Electrical devices

This is the main screen of our software which supervise the sms messages and show who send this message ,the time of send , the date and the text of SMS. There is a button allow us to go to User and Permissons.

34

SMS Controlling Electrical devices

This screen control the user accounts and permissions of each user . these permissions are : 1- make a device ON. 2- make a device OFF. 3- Ask about the state of the device .
35

SMS Controlling Electrical devices

Refrences
1- Advanced PIC Microcontroller Projects in C ,Dogan Ibrahim , Newnes. 2- MikroC help. 3- pic16f877a datasheet . 4- max232 datasheet . 5- the fastest way to master programming microcontroller ,Ahmad Fayed , Free EBook. http://qariya.com/vb/showthread.php?t=40760 6- wavecom AT commands manual for wavecom GSM modems.

36

Das könnte Ihnen auch gefallen