Problems with Microcontroller Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you the Codes for Registered Members of this site only, at free service...Follow Me.

ATmega16 SMS controlled Relay Siemens M55 AVR Studio WinAVR microcontroller project remote wireless

The first project I am posting in this blog is a microcontroller project that communicates with a Siemens M55 mobile phone through the serial interface using AT commands and toggles 4 relays connected to a standard IO port of the microcontroller according to the information contained in a SMS.
Restrictions imposed by my coordinator were that the microcontroller had to be in system programmable. That's about it. I have chosen the ATmega16A microcontroller because I've got a good deal on them (great discount when I bought 10 ... even though I only needed 1 :/)

Before I begin with the project description I consider it pretty important that you get to know how de communication with the mobile phone is done. For this you should check out the following links first.
The first link is a link to the wikipedia site that helps you udnerstand the basics of the AT (Hayes) Command Set:
http://en.wikipedia.org/wiki/Hayes_command_set
The following link is a .pdf file that lists the AT commands that you need to send to the mobile phone through the serial interface. The AT commands are basically the same for most GSM modules/phones with slight variations.
http://ebookbrowse.com/s35i-c35i-m35i-atc-commandset-v01-pdf-d29637636
Finally I added this link that explains in detail how to send and receive SMS messages with the help of AT commands and how exactly those messages are encoded (PDU format):
http://www.dreamfabric.com/sms/

1. Block Diagram


2. Description of the individual blocks

As mentioned above I have chosen the ATmega16A microcontroller. The program I have written takes up less then 8kB so an ATmega8A would be just as good. You can look for microcontrollers from other manufacturers as well. All this project needs is a standard IO port with 8 pins and an asynchronous serial interface (RX and TX pins). Also for ease of programming ISP capability would be good.

The ISP interface is a simple 6 pin (3x2) header connected to the corresponding pins of the microcontroller. I have designed mine to work with the AVRISP mkII that connects to the USB port of the PC. There are many different possibilities to make your own in-system-programmer but most of them are either designed for a parallel or serial port or require a programmed microcontroller. Also the AVRISP mkII is very easy to use with AVR Studio and not too expensive.

Since I use the data cable of the Siemens M55 to communicate with it and didn't want to destroy it I had to translate my TTL level serial communication from the microcontroller to RS-232 levels. If you have a GSM module that works with TTL levels (or connect directly to the corresponding pins of the phone, that also operate at TTL levels) you don't need a level converter and can connect the module directly to the RX and TX pins of the microcontroller. If you use a data-cable that operates at RS-232 levels (designed to communicate with PC) then you need a MAX232 or similar IC. The MAX232 is very easily implemented. It only needs some capacitors and you can connect the RX and TX pins of the TTL levels on one side and get RS-232 levels on the other. The following image should give you a better idea of what I am talking about if you didn't use this IC before. The picture is from the MAX232 datasheet:

The capacitors are of 1uF each.

I will describe the relay driving circuit for 1 relay. It is the same for the other 3 relays. The relays that I have used are latching type relays with 2 coils. Both coils can be sued to set or reset the relay. To keep the command simple I have used one IO pin to set and one IO pin to reset the relay, commanding the two coils individually. To be able to supply the necessary current for the relay to latch, I have used npn transistors with a current limiting resistor in the base of the transistor, that is connected to the microcontroller IO pin. Since I use 2 IO pins to command each relay and those lines are always at different logic values (either the set or the reset line are at +5V) I have mounted a bicolor led and a current limiting resistor between the two IO lines to visualize the position of the relays. For a better understanding of the relay driving circuit look at the schematic further down.

The power supply consists of a 7805 voltage regulator. It can be supplied by any DC voltage between 7 and 35V (35V is the absolute maximum rating. I wouldn't go that high ;) ). Along with the 7805 I have added some filtering capacitors at the input and output of the IC.

3. Electrical schematic

I have designed this project in EAGLE. Since the dimensions are not that big the light (free) version of EAGLE was all I needed.
Also worth mentioning is that I have connected one of the pins of the ADC to a 3 pin header along with VCC and GND to be able to add a temperature sensor to the project in case I decide to modify the project. If you don't want this you can also leave the AREF and AVCC pins unconnected.


4. Board layout

I will only post the complete layout with both Top and Bottom sides. The EAGLE file is linked below.


The drill sizes are rounded to mm's. So if you're living in the stone age and don't use the metric system :P you will probably have to modify them.

I have uploaded the schematic and the board layout to a free file hosting server but I don't know how long it will stay up. If you want it and the link isn't working just send me a mail. The folder also contains the component library. Link: http://www.mediafire.com/?h6taq8dk9g2xq

4. Software

The software was written in AVR Studio with the WinAVR plugin installed. I have written it in c programming language since assembly is just a pain in the a**. :) Basically what the microcontroller is doing is initializing the serial interface and then the phone. Initialization of the phone means inserting the pin number (I have put it to 1234 in the program), selecting the storage for SMS messages (it selects the phone memory and not the SIM memory) and inserting the SMS service centre address. The current address is for a Vodafone Romania prepaid SIM. These parameters are at the beginning of the program so you shouldn't have a hard time changing them even if you have no clue of C programming. Below is the block diagram of the software. It is written in romanian but basically the idea is:
- you initialize the serial port, the phone and set the IO pins to output;
- you keep asking the phone if it has any unread messages (with the AT command "at+cmgl=0")
- if so you check whether the message starts with a 'R' or a 'r'. This is because the message to change the relay positions has to be "R xxxx" where x is either 1 or 0 according to what position the relay has to have. Also this allows us to ignore messages from other people.
- if the message starts accordingly you modify the relay positions so that they correspond to the SMS


.c file: http://www.mediafire.com/?g3nv4r1eql9zpeg

C CODE:




#include
#define F_CPU 8000000// Clock Speed
#include
#include
#include

#define NL 12
#define n1 10
#define n3 6

#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

unsigned char PIN[]="at+cpin=1234";
unsigned char SCC[]="at+csca=+40722004000";
unsigned char MEM[]="at+cpms=mt,mt,mt";
unsigned char POLL[]="at+cmgl=0";
unsigned char DEL[]="at+cmgd=1";
unsigned char CR=0x0D;
unsigned char Pnumber[12+1];

unsigned char R[4+1];
unsigned char relay_positions = 0x00;

int length;
unsigned char msg[256];
unsigned char tmp;

unsigned char antet[n1+1]="0011000B91";
unsigned char mijloc[n3+1]="0000AA";

//USART part
void USART_Transmit(unsigned char data){
while (!(UCSRA&(1<
;
UDR = data; // Put data into buffer, sends the data
}
void USART_Transmit_string(unsigned char *data, int l){
int i;
for(i=0;i
USART_Transmit(data[i]);
}
void USART_Flush( void ){
unsigned char dummy;
while ( UCSRA & (1<
}

//Init Part
void Init_USART(unsigned int ubrr){
UBRRH = (unsigned char)(ubrr>>8); //set BaudRate
UBRRL = (unsigned char)ubrr;
UCSRB = (1<
UCSRC = (1<
}
void Init_Relays(){
DDRC=0xFF;
PORTC=relay_positions;
}
void Init_Phone(){
USART_Transmit_string(PIN,12);
USART_Transmit(CR);
int j;
for(j=0;j<100;j++)
_delay_ms(100);
USART_Transmit_string(MEM,16);
USART_Transmit(CR);
_delay_ms(1000);
USART_Transmit_string(SCC,20);
USART_Transmit(CR);
_delay_ms(1000);
}
/*Rutina de descifrare a mesajelor SMS*/

void taiereantet(){
for(int j=0;j<(length-82);j++)
msg[j]=msg[j+82];
length-=82;
}
void taierecapat(){
msg[length-8]=0x00;
length-=8;
}

void makehex(){
unsigned char ch;
int j=0;
ch=msg[j];
while(ch!=0){
if((ch>='A')&&(ch<='F'))
ch=ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
ch=ch-'a'+10;
else if((ch>='0')&&(ch<='9'))
ch=ch-'0';
msg[j]=ch;
ch=msg[++j];
}
}
void rearrange(){
unsigned char tmp[2]={0,0};
for(int j=0;j<=(length-1)/4;j++){
tmp[0]=msg[2*j];
tmp[1]=msg[(2*j)+1];
msg[2*j]=msg[length-1-(2*j)-1];
msg[(2*j)+1]=msg[length-1-(2*j)];
msg[length-1-(2*j)-1]=tmp[0];
msg[length-1-(2*j)]=tmp[1];
}
}
void rearrange2(){
unsigned char tmp;
for(int j=0;j<=(length-1)/2;j++){
tmp=msg[j];
msg[j]=msg[length-1-j];
msg[length-1-j]=tmp;
}
}
void scriepe8biti(){
unsigned char tmp;
unsigned char c2[128];
for(int j=(length-1);j>0;j=j-2){
tmp=msg[j]|(msg[j-1]<<4);
c2[j/2]=tmp;
}
for(int j=0;j<128;j++)
msg[j]=c2[j];
}
void shiftarefrate(){
unsigned char c3[32];
unsigned char a,b;
int safetyspace=1+(length/7);
for(int k=0;k<(length+safetyspace);k++)
c3[k]=0;
for(int k=0;k
c3[k+safetyspace]=msg[k];
}
for(int z=(length+safetyspace-1);z>=0;z--){
b=0;
for(int j=z;j>=0;j--){
a=c3[j];
a=a>>7;
c3[j]=c3[j]<<1;
c3[j]=c3[j]+b;
b=a;
}
c3[z]=c3[z]>>1;
}
length=length+safetyspace;
for(int j=0;j<128;j++)
msg[j]=c3[j];
}
void descifrare_SMS(){
rearrange();
makehex();
scriepe8biti();
length/=2;
shiftarefrate();
rearrange2();
}
//Rutina de compunere a mesajelor SMS
/*void relaypos(){
if((relay_positions|0b00000001))
R[0]=1;
if((relay_positions|0b00000100)==0b00000100)
R[1]=1;
if((relay_positions|0b00010000)==0b00010000)
R[2]=1;
if((relay_positions|0b01000000)==0b01000000)
R[3]=1;
R[4]=0x00;
}*/
int main(void){
Init_USART(MYUBRR);
Init_Relays();
Init_Phone();
USART_Flush();
sei();
while(1){
length=0;
USART_Transmit_string(POLL,9);
USART_Transmit(CR);
_delay_ms(3000);
if(length>=13){
if(msg[12]==0x2B){
int j;
for(j=0;j<12;j++)
Pnumber[j]=msg[j+50];
Pnumber[12]=0x00;
taiereantet();
taierecapat();
descifrare_SMS();
if((msg[0]=='R')||(msg[0]=='r')){
/*if(msg[2]=='?'){
relaypos();
compose_Relaypos_msg();
send_SMS();
}*/
if((msg[2]=='0')||(msg[2]=='1')){
relay_positions=0;
if(msg[2]=='0')
relay_positions+=2;
if(msg[2]=='1')
relay_positions+=1;
if(msg[3]=='0')
relay_positions+=8;
if(msg[3]=='1')
relay_positions+=4;
if(msg[4]=='0')
relay_positions+=32;
if(msg[4]=='1')
relay_positions+=16;
if(msg[5]=='0')
relay_positions+=128;
if(msg[5]=='1')
relay_positions+=64;
PORTC=relay_positions;
}
}
USART_Transmit_string(DEL,9);
USART_Transmit(CR);
_delay_ms(1000);
}
}
}
}

ISR(_VECTOR(11)) {
length++;
msg[length-1]=UDR;
msg[length]=0x00;
return;
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Recent Comments

Popular Projects

Give Support

Give Support
Encourage Me through Comments

Microcontroller Projects

Total Pageviews