View previous topic :: View next topic |
Author |
Message |
farazwan
Joined: 26 Apr 2012 Posts: 13
|
Need help receive sms from gsm modem! |
Posted: Thu Apr 26, 2012 1:24 am |
|
|
I'm using gsm modem MOD-9001D from Cytron to interface with PIC18F4550. I don't know how to write a coding using ccs compiler to receive sms from gsm modem and display the sms at LCD. Please help me!! |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Apr 26, 2012 1:48 am |
|
|
You have to read the AT command manual of that product and then issue those commands from microcontroller to read or write some messages. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Apr 26, 2012 6:43 am |
|
|
Do you know how to do it using any other compiler?
Do you need help with CCS or with SMS and AT commands? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
farazwan
Joined: 26 Apr 2012 Posts: 13
|
|
Posted: Thu Apr 26, 2012 7:49 am |
|
|
to hayee:i already read the AT command manual. i can sent SMS to phone but i can't display the sms that receive by GSM modem on LCD.do you have any suggestions??
to SherpaDoug: i only know the basic of CCS. i have no problem with AT command,i only cant display the receive sms on lcd..how we can sure that gsm modem has receive sms from phone?? |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Apr 26, 2012 8:12 am |
|
|
farazwan,
There is at least one complete project documented here on the forum by user 'Gabriel' that shows how to communicate with an GSM modem. This project is intended to control 4 relays via SMS control, but could be easily modified to do what you want. Did you even bother to do a search of the forum archives?
John |
|
|
farazwan
Joined: 26 Apr 2012 Posts: 13
|
|
Posted: Fri Apr 27, 2012 6:40 am |
|
|
i have try the coding that gabriel provided but there is an error when i want to compile it.. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri Apr 27, 2012 7:55 am |
|
|
Hi farazwan,
You certainly don't need to compile the program written by Gabriel in order to get an idea how to do such a thing yourself. Further, if you don't tell us the error you got when you try to compile it, we'll never be able to help you!
John |
|
|
farazwan
Joined: 26 Apr 2012 Posts: 13
|
|
Posted: Mon May 07, 2012 8:03 am |
|
|
I can receive sms from gsm modem but when I reply sms, gsm modem cannot receive my sms. Is there any problem with my interrupt? Here is my coding. Please have a look.
Code: |
#include <18F4550.h>
#include <stdlib.h>
#use delay(clock=20M)
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, stream=GSM)
#include <lcd.c>
#use fast_io(C)
#use fast_io(B)
#define LCD_E PIN_D0
#define LCD_RS PIN_D1
#define LCD_RW PIN_D2
#define LCD_D4 PIN_D4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7
#define LED1 PIN_B0 // LED 1
#define LED2 PIN_B1 // LED 2
#define LED3 PIN_B2 // LED 3
#define LED4 PIN_B3 // LED 4
#define LED5 PIN_B4 // LED 5
#define LED6 PIN_B5 // LED 6
#define LED7 PIN_B6 // LED 7
#define LED8 PIN_B7 // LED 8
#define BUTTON1 PIN_C0
#define BUTTON2 PIN_C1
#define BUTTON3 PIN_C2
void initialize();
void LOCK();
void UNLOCK();
void BREACH();
int DM();
void read_sms();
int get_cmd();
void do_cmd();
void del_sms();
short sms_mode=0;
volatile int counter_search=0; //counter to traverse the array
volatile int counter_read=0; //counter to fill the array
Volatile int HitCounter=0; // for string comparison
Volatile int Next_CMD=0;
volatile Char Recieve_String[70]; //buffer for incoming data
void main()
{
lcd_init();
set_tris_b(0x00);
output_b(0x00);
set_tris_c(0xFF);
output_c(0xFF);
initialize();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(true)
{
if(!input(BUTTON1))
{
UNLOCK();
}
if(!input(BUTTON2))
{
LOCK();
}
if(!input(BUTTON3))
{
BREACH();
}
if(sms_mode==1)
{
lcd_putc("\f");
lcd_gotoxy(4,1);
printf(lcd_putc,"%c",Recieve_String[counter_read]);
delay_ms(3000);
DM();
read_sms();
get_cmd();
do_cmd();
del_sms();
sms_mode=0;
counter_read=0;
}
lcd_putc("\f");
}
}
void initialize()
{
printf("AT\n\r");
delay_ms(500);
printf("AT+CMGF=1\n\r"); //It inhabits the text mode sms
delay_ms(500);
lcd_putc("\f");
lcd_gotoxy(4,1);
lcd_putc("GSM MODEM");
lcd_gotoxy(4,2);
lcd_putc("INITIALIZED");
delay_ms(1000);
}
void UNLOCK()
{
output_high(LED1);
output_low(LED2);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("CAR DOOR UNLOCK");
delay_ms(1000);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("SEND MESSAGE");
delay_ms(1000);
fprintf(GSM,"at+cmgs=\"0142205381\"\r\n");
delay_ms(300);
fprintf(GSM, "Warning!The car door is unlock\n"); // Text message
putc(0x1A);// Print (Ctr+Z) to send message
delay_ms(300);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("MESSAGE SEND");
lcd_gotoxy(0,2);
lcd_putc("COMPLETE");
delay_ms(1500);
}
void LOCK()
{
output_high(LED2);
output_low(LED1);
output_low(LED3);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("CAR DOOR LOCK");
delay_ms(1000);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("SEND MESSAGE");
delay_ms(1000);
fprintf(GSM, "at+cmgs=\"0142205381\"\r\n");
delay_ms(300);
fprintf(GSM, "The car door is lock\n"); // Text message
putc(0x1A);// Print (Ctr+Z) to send message
delay_ms(300);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("MESSAGE SEND");
lcd_gotoxy(0,2);
lcd_putc("COMPLETE");
delay_ms(1500);
}
void BREACH()
{
output_high(LED3);
output_high(LED1);
output_low(LED2);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("CAR IS BREACH");
delay_ms(1000);
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("SEND MESSAGE");
delay_ms(1000);
fprintf(GSM, "at+cmgs=\"0142205381\"\r\n");
delay_ms(300);
fprintf(GSM, "The car is been breach\n"); // Text message
putc(0x1A);// Print (Ctr+Z) to send message
delay_ms(300);
lcd_gotoxy(0,1);
lcd_putc("MESSAGE SEND");
lcd_gotoxy(0,2);
lcd_putc("COMPLETE");
delay_ms(1500);
}
int DM()
{
const char StrCMTI[]={"+CMTI"};
counter_read=0;
While(counter_read<=16) // read all data
{
output_high(PIN_B1); // signal ready to receive sms
}
counter_read=0;
Delay_ms(500); // ignore all remaining characters recieved..if any.
output_low(PIN_B1); // off receive ready light.
counter_search=2;
HitCounter=0;
while((HitCounter!=5)&&(counter_search<8))
{
if(Recieve_String[counter_search]==StrCMTI[counter_search-2])
HitCounter++;
counter_search++;
}
if(HitCounter==0x05)
Return(1);
else return(0);
}
void read_sms()
{
counter_read=0;
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("READ SMS");
delay_ms(1000);
fprintf(GSM, "AT+CMGR=1\r\n"); // send command, MEMORY LOCATION OF SMS IS ALWAYS ONE, SINCE I DELETE THEM AFTER PROSCESING
delay_ms(3000);
}
int get_cmd()
{
const char CMD1[8][7]={"ON1","ON2","ON3","ON4","ON5","ON6","ON7","ON8"};
int offset=0;
int store_counter=0;
counter_search=0;
while((Recieve_String[counter_search]!='!')&&(counter_search<69)) // wait till command indicator is found '!'
{
counter_search++;
}
counter_search=counter_search+1; // increment one to actual first letter of command
store_Counter=counter_search; // store current position for multiple searches
NEXT_CMD=0; // NEXT_CMD keeps track of the command being read, thus used
while((HitCounter!=6)&&(NEXT_CMD<8)) // compare to all commands in list.
{
counter_search=store_Counter; // initialize counter search with stored counter value.
offset=0; // since value of counter is unknown use a separate counter for the const array
HitCounter=0; // counts number of equal letters found.
while((HitCounter!=6)&&(offset<=6)) // keeps the counters in check...to not overshoot. and compares the strings
{
if(Recieve_String[counter_search]==CMD1[NEXT_CMD][offset]) // if letter is found
{HitCounter++;
offset++;
counter_search++;
}
}
if(HitCounter==6) // if 6 chars found...exit..
{
Return(1);
}
NEXT_CMD++; // if increase to search next command.
}
Return(0);
}
void do_cmd()
{
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("do command");
delay_ms(1000);
if(NEXT_CMD==0)output_low(PIN_B4);
if(NEXT_CMD==1)output_high(PIN_B4);
if(NEXT_CMD==2)output_low(PIN_B5);
if(NEXT_CMD==3)output_high(PIN_B5);
if(NEXT_CMD==4)output_low(PIN_B6);
if(NEXT_CMD==5)output_high(PIN_B6);
if(NEXT_CMD==6)output_low(PIN_B7);
if(NEXT_CMD==7)output_high(PIN_B7);
}
void del_sms()
{
counter_read=0;
printf("AT+CMGD=1"); //send command
putchar(0x0D); //send return
While(counter_read<=15) // get all returned text
{
}
counter_read=0;
Delay_ms(500); // ignore all remaining characters received
}
#INT_RDA
void SerialInt()
{
while (kbhit())
{
Recieve_String[counter_read]=getchar();
if(Recieve_String[counter_read]=='+'){//first character of phone number
counter_read=0;//to begin to write the var buffer from the 1 st position
output_high(LED8);
sms_mode=1;
}
counter_read++;
}
return;
}
|
|
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon May 07, 2012 11:30 am |
|
|
Farazwan,
OK, so you've got a problem receiving an SMS message with your code, right? Effective troubleshooting would reduce the problem to it's barest essentials (ie. strip all the non-pertinent crap out of your code), and actually examine what the GSM modem is sending out the serial port when an SMS is received. At this point, maybe nothing is happening, or maybe what is being send is not what is expected, so your code gets lost. The point is that to effectively troubleshoot a problem like this, you've got to strip the code to the simplest building block and then get that working 100%. So, my recommendation would be to wait for a SMS, and then examine the contents of the receive buffer and see what you've actually got. For there it should become obvious why your complete code is not working!
It's NOT a "black box", so dig in and see what's happening under the hood!
John |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue May 08, 2012 11:18 am |
|
|
Hi farazwan,
I took another brief look at your code, and your use of the INT_RDA interrupt handler is totally wrong. This interrupt should be used to receive a single character only from the hardware UART, stuff it into a buffer, and then exit quickly. Look at the example program 'Ex_SISR.c' to see how this is done.
My earlier recommendation still stands, however, that you need to break your code down into it's absolute essentials, and get your SMS receive routine working reliably before you proceed!
Here is a hint: Inside Main(), I use bkbhit to determine when the UART interrupt has put another character into my receive buffer. I then use a state machine to look for this group of characters '+CMTI\r' which is sent from the GSM model when a SMS is received. Once received, I jump into the routine that actually reads the SMS message.
I did a LOT of playing with the GSM modem while it was attached to my PC serial port via a MAX232 IC to learn exactly the response of the GSM modem under all sorts of conditions. Once I had this data, I was able to go off and write code for the PIC with a lot more confidence!!
John |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue May 08, 2012 12:35 pm |
|
|
WOAH Nelly!!!!
Did you get the original code running before you started to modify it?
The code was made for a 16F877A.
You need to change the appropriate code to make it for your specific PIC... that is, set the right fuses etc...
Once you got that working... and you are actually controlling the 4 LEDs as the code was intended, you can start to modify the code... provided you _understand_ what is going on...
As i say on the post... its a _Starting point_ not a complete SMS solution.... it was more of a "start here" kinda thing.... and its definitely not bug free....
Furthermore... in that same post in the comment section i went ahead and posted some common problems/solutions to getting started with GSM modems... as i relived some of them recently...
you should check that out...
I'm glad people like you are using the code to get started... that was the idea.. and I'll try to help as much as i can... but understand that the posted code was my first SMS working code... and its tailored for the responses of the specific module i was using.... and more importantly the settings the module had (i.e. local echo on... etc).
to be more precise...when i wait for responses i wait for a ~finite amount of characters... if you code responds with less than the hardcoded character quantities... you will be stuck in those loops...
Case in point: (from SMS delete Function)
Code: | While(counter_read<=15) // get all returned text
{
} |
Also... at the time i had never used (in CCS) string functions... so i did all string searches and comparisons manually.... like when i search for "OK" or "+CMTI".... STRSTR() does all this and its much easier to understand...
my point is... you can use my code however you want... but i recomend you use it as a reference and a starting point.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed May 09, 2012 10:34 pm |
|
|
Gabriel wrote: | WOAH Nelly!!!! |
snx _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Thu May 10, 2012 12:45 pm |
|
|
snx..... On a Plane? _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
farazwan
Joined: 26 Apr 2012 Posts: 13
|
|
Posted: Sun Jun 03, 2012 10:27 pm |
|
|
To gabriel and those who have help me, my project is finally working well. thanks.. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jun 04, 2012 7:11 am |
|
|
Good for you man!
Post your code on the library for others!... SMS related projects are common here so your code may be very useful for others..
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|