Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help how can i send sms... i really dnt knw the routine....

Status
Not open for further replies.

mldt28

Newbie level 6
Newbie level 6
Joined
Feb 20, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,401
guys pls help me with my code... i can't send sms through gsm modem using a PIC18f4550



Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
 
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
//End of LCD Initialization
 
//GSM Modem AT Commands
const unsigned char AT_CMGF1[]                   = "AT+CMGF=1\r";
const unsigned char AT_CMGD[]                    = "AT+CMGDA=1\r";
const unsigned char AT_CSQ[]                     = "AT+CSQ\r";
const unsigned char AT_CMGF[]                    = "AT+CMGF=0\r";
const unsigned char AT_CMGL[]                    = "AT+CMGL\r";
const unsigned char AT[]                         = "AT\r";
const unsigned char ATE0[]                       = "ATE0\r";                         //!< Echo off
const unsigned char AT_CNMI[]                    = "AT+CNMI=1,1,,,1\r";              //!< Identification of new sms
const unsigned char AT_CPMS[]                    = "AT+CPMS=\"ME\",\"ME\",\"ME\"\r"; //!< Preferred storage
const unsigned char AT_CMGS[]                    = "AT+CMGS=\"09184637272\"\r";
 
void           Init();
 
void main() {
  char dat, err, ptr, extra[10], kwdata[10], kwhdata[10], kwsumEEP;
  unsigned char mybuff[54], mypbuff = 0;
  float wattsec_ = 0;
  float total = 0;
  float ave = 0;
  float ave2 = 0;
  float kw =0;
  float kwh =0;
  float kwhsum = 0;
  int count = 0;
  int count2 =0;
 
  /*===========Declarations============*/
  TRISD=0;
  PORTB=0;
  PORTD=0;
  TRISB = 0;
  ADCON1 = 0x0F;
  /*====================================*/
  /*===========Initialization and Main Display===========*/
  Init();
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,2, "Energy Monitoring");
  Lcd_Out(2,8, "System");
  Lcd_Out(4,1, "AROMAMI");
  delay_ms(5000);
  /*======================================================*/
 
  /*==Executions of Data Fetching, Calculations, Sending==*/
  for(;;)
   {
      while(1)
     {
          do{ dat = Soft_Uart_Read(&err); }while(err);
          if(dat == 'O'){ break; }
          if((mypbuff == 0) && (dat != 'I')){ continue; }
          mybuff[mypbuff++] = dat;
          if(mypbuff == 53)
          {
               ptr = strtok(mybuff, ",");     //mybuff here contains the string from power analyzer module
               ptr = strtok(0, ",");          // ptr now contains string of watts
               count++;
               total += wattsec_;
               wattsec_ = atof(ptr);
               kw = wattsec_/1000;
               kwh = kw/3600;
               kwhsum += kwh;
               sprintf(kwdata, "%.2f", kw);
               //FloatToStr(kw,kwdata);
               sprintf(kwhdata, "%.2f", kwhsum);
               //FloatToStr(kwhsum,kwhdata);
 
               if((count % 5) == 0){
                  ave = total / 1000;
                  ave2 = ave  / 3600;           //to kph /30 secs         
                  sprintf(extra, "%.10f", ave2); // to GSM modem
                  //FloatToStr(ave2, extra);
                                                   
 
 
THIS IS THE AREA WHERE I DONT REALLY KNOW WHAT TO DO... pls help me guys
 // send to gsm program*************************************************************
                                              uart1_write(ATE0);
                                              uart1_write(0x0A);
                                              uart1_write(0x1A);
                                              delay_ms(500);
 
                                              UART1_Write(AT_CMGF1);
                                              UART1_Write(13); //Enter key = CF + LF
                                              UART1_Write(10);
                                              delay_ms(500);
 
                                              UART1_Write(AT_CMGS);
                                              UART1_Write(13); //Enter key = CF + LF
                                              UART1_Write(10);
                                              delay_ms(500);
 
                                              UART1_Write(extra);
                                              UART1_Write(0x1A); // <ctrl-z>
                                              delay_ms(3000);
                                              
                                              Lcd_Cmd(_LCD_CLEAR);
                                              Lcd_Out(1,1, "SENT");
 
 
                  ave   = 0;
                  ave2  = 0;
                  total = 0;
                  count = 0;
                  delay_ms(2000);
               }
 
               Lcd_Cmd(_LCD_CLEAR);
               Lcd_Out(1,1, "KW: ");
               Lcd_Out(1,5, kwdata);
 
               Lcd_Out(2,1, "KW/hour: ");
               Lcd_Out(2,10, kwhdata);
 
               Lcd_Out(3,1, "Ave/30s: ");
               Lcd_Out(3,10, extra);
               Uart1_Write_Text(kwdata);
               mypbuff = 0;
               break;
          }
     }
   }
}
/*=====================================================*/
 
 
void COM_puts(const unsigned char *cmd)
{
     while(*cmd)
         UART1_Write(*cmd++);
}
 
void Init()
{
        Soft_UART_Init(&PORTD,1,0,9600,0);     // initialize serial extra port
        UART1_Init(2500);
                                               // initializes GSM
        Delay_ms(1000);
        COM_puts(ATE0);                      // gsm mode: echo off
        Delay_ms(350);
        COM_puts(AT_CMGF1);
        Delay_ms(350);
        COM_puts(AT_CMGS);
        Delay_ms(350);
        //COM_puts(AT_CPMS);
        //Delay_ms(350);
        //COM_puts(AT_CNMI);
        //Delay_ms(1000);
        //COM_puts(AT_CMGD);
        //Delay_ms(2000);
}

 
Last edited by a moderator:

You are not posting the uart1_write() function, maybe it is an in-built compiler function, I am not sure about that.
But in any case it does not seem to be right. Either you send only one byte instead of a string, or there must be an error thrown by the compiler on those lines.
The information provided is really poor and only assumptions could be made. You are not even mentioning compiler. Is the program running not correctly, or cannot be compiled at all?
Are you using an osciloscope or a logic analyzer to see the data? Or maybe a terminal software?
Without proper tooling, GSM development is really difficult if not impossible. You need to use all the available tools. You could use a free terminal software for instance, I think putty is for free, search the web.
 

yes it does work...
actually...
that system is for AC power analyzer...
and it mimics the ENERGY METER....
and it is working....
it displays the value of
KW
KWH
and
KWH/30 secs.

and im using mikroC for this project....
it has no errors....
the only prob that im having is how am i going to send the data which is displayed on KWH/30s...
 

I am not sure if you must include '\r' character inside string declaration. You send strings via UART and then <CR><LF> as well in your transmissions, so it looks like there is a duplicated sent charactrer in the sequence. Check it just in case. Also I cannot understand why this 0x1A character is sent in the top lines of the gsm code. I don't know which gsm you are using, but this doesn't look right as well. This character is used in the sms sequence.

Have you found out where the problem lies? You send proper data and the sms is not sent? Have you verified serial communication?
 

i am using a sim900d gsm modem now...
and
and my problem now is my routine on sending a msg...
i know my code sequence there is wrong..
that is why im asking for help..
can you help me with this sir??
 

The important thing is to first locate the problem in the hardware level.
Do you send data but no answer is returned? Do you send data at all?

I would suggest not to touch the code if you don't first find out what the problem is.
As already pointed out, a terminal software is what you really need right now.
You will connect the communication lines to the PC, and watch what is really happening (or not happening :smile:).
 

hehehe....
ahhh the hardware is functioning already...
it reads data in KW, KWH and kwh/30sec..

what am i trying now is how may i send the kwh/30sec.
through sms...
coz i dont know how to use gsm modem yet...
i only wanted the routine on sending it in mikroC
using UART1 etc what to type at commands...
 

So you say that you succesfully send proper data to the GSM modem, but no sms is sent.
Next step is to clarify if the modem responds succesfully or not.
That is, when you send AT_CMGS command (post #1, code line 106), what is the modem's answer?
 

ahmmm...
that is the prob ^^
i really don't know that much about GSM modems...
i can't really use gsm modem to its full extend im a newbie to it
 

OK, let us take this from the beggining...
Forget the modem, just focus on the UART line. This what I am requesting from my first post.
In order to have a functional GSM, you need to send data to it via UART line (RXD, TXD lines).

There are many possible reasons why the sms is not send. Before you look into your code, you must verify hardware. Common reasons for malfunctioning are bad power supply (you need a supply that can give 2A pulse current during GSM power on) and communication error.

Let us focus on communication error. You try to send data via UART to the GSM (uart1_write() function). Are you sure that those data are sent to the modem? Have you seen those data with a scope or logic analyzer or a terminal software? Maybe you have a cut track or bad UART initialization. Have you checked UART's tx line? Are data sent via this line?
 

im using proteus as a simulation tool...
i use a virtual terminal to determine if it is sending appropriate data.....
and yes it sends data to the exact line that i want..
this GSM modem is diff than the others bcoz it is ready for arduino programming..
meanning..
from PIC's TX to GSM's TX also...
that is what the vendor told me...
i just tested my GSM modem with my classmates arduino. it works....

- - - Updated - - -

but in MIKROC programming im still working on it i dnt know where to start... maybe its the baud rate ^^
 

I don't think it is the baudrate, modern GSMs are capable of autobaud.

So you tested PIC's UART in proteus and it is working OK...
Next step is to see if GSM is answering on PIC's rx line.
When you send those data:

UART1_Write(AT_CMGS);

Is there any responce returned?

Now about the code.
After each command you send the value 10 (LF character), but actually only CR character (value 13) is required. To be more accurate, I don't know if the CR character is required either, it seems that this character is included inside the declared strings (\r).
Furthermore after you send ATE0, you don't need to send 0x1A character, you can comment this line out.
Make sure you are supplying the modem from a source that is capable of 2A pulse current.

As a general comment, I would say that it is a bad practice not to read the modem's answer. Instead you seem to keep a delay and then go to the next command without any feedback from the modem.

Finally I still insist that you desperately need a terminal software. You send data and watch the PC monitor via terminal software if an answer is returned, simple as that. Proteus is good, but it is nothing more than simulation. You are using another board from your classmates (arduino uses AVR as far as I know), so you don't even know if YOUR hardware is OK.
You can spend an entire life debugging a code, but you will never find the solution inside the code if the hardware is not OK in the first place. And we can spend an entire life trying to figure out where the problem lies, when the problem could be a cut track, an unsoldered pin or who knows what else.
 

what the F!!!!! now that u mentioned it... i really checked my PCB.... and yeah i ddnt solder that two pins....
i'll get back to you bro later ^^...
damn.... ^^
 

is this code useful??

i edited it to meet my demand... but somehow...
im having a hardtime displaying the result on LCD...
can u help me dude???

Code:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
//End of LCD Initialization

// Set of AT commands
const char atc0[] = "AT";                        // Every AT command starts with "AT"
const char atc1[] = "ATE0";                      // Disable command echo
const char atc2[] = "AT+CMGF=1";                 // TXT messages
const char atc3[] = "AT+CMGS=\"+639184637272\"";     // Send message to cell number : 123456789 (Enter your number instead of 123456789!)

// Alarm message
const char msg1[] = "Warning: Motion detected!!!";
//

// Responses to parse
const GSM_OK = 0;
const GSM_Ready_To_Receive_Message = 1;

char gsm_state = 0;
char response_rcvd = 0;
short responseID = -1, response = -1;

// uart rx interrupt handler
void interrupt(){
char tmp;
  if (PIR1.RCIF == 1) {                          // Do we have uart rx interrupt request?
    tmp = UART1_Read();                          // Get received byte

// process reception through state machine
// we are parsing only "OK" and "> " responses
    switch (gsm_state) {
      case  0: {
                response = -1;                   // Clear response
                if (tmp == 'O')                  // We have 'O', it could be "OK"
                  gsm_state = 1;                 // Expecting 'K'
                if (tmp == '>')                  // We have '>', it could be "> "
                  gsm_state = 10;                // Expecting ' '
                break;
               }
      case  1: {
                if (tmp == 'K') {                // We have 'K' ->
                  response = GSM_OK;             // We have "OK" response
                  gsm_state = 20;                // Expecting CR+LF
                }
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 10: {
                if (tmp == ' ') {
                  response_rcvd = 1;             // We have "> " response
                  response = GSM_Ready_To_Receive_Message; // Set reception flag
                  responseID = response;         // Set response ID
                }
                gsm_state = 0;                   // Reset state machine
                break;
                }

      case 20: {
                if (tmp == 13)                   // We have 13, it could be CR+LF
                  gsm_state = 21;                // Expecting LF
                else
                  gsm_state = 0;                 // Reset state machine
                break;
               }
      case 21: {
                if (tmp == 10) {                 // We have LF, response is complete
                  response_rcvd = 1;             // Set reception flag
                  responseID = response;         // Set response ID
                }
                gsm_state = 0;                   // Reset state machine
                break;
               }
      default: {                                 // Unwanted character
                gsm_state = 0;                   // Reset state machine
                break;
               }
    }
  }
}

// Send command or data to the Telit GM862 Module
void GM862_Send(const char *s)
{
// Send command or data string
   while(*s) {
      UART1_Write(*s++);
   }
// Terminatation by CR
   UART1_Write(0x0D);
}

// Get GSM response, if there is any
short Get_response() {
  if (response_rcvd) {
    response_rcvd = 0;
    return responseID;
  }
  else
    return -1;
}


// Wait for GSM response
void Wait_response(char rspns) {                         
  while (Get_response() != rspns)
    ;
}

void Send_Alarm_Msg(){
       GM862_Send(atc0);                            // Send "AT" string until GSM862 sets up its baud rade
       Delay_ms(100);                               // and gets it correctly
       if (Get_response() == GSM_OK)                // If GSM862 says "OK" on our baud rate we program can continue
       break;
       }                               // and gets it correctly
       GM862_Send(atc2);
       Wait_response(GSM_OK);
       GM862_Send(atc3);
       Wait_response(GSM_Ready_To_Receive_Message);
       GM862_Send(msg1);
       UART1_Write(0x1A);
       UART1_Write(0x0D);
       Wait_response(GSM_OK);



}
void main() {

  char uart_rd,err,dat,mypbuff[10];
  TRISD=0;
  PORTB=0;
  PORTD=0;
  TRISB = 0;
  WDTCON = 0;
  ADCON1 |= 0x0F;                                // Configure all ports with analog function as digital
  CMCON  |= 7;                                   // Disable comparators
  UART1_Init(2400);                             // Initialize USART module
  Delay_ms(100);
  Lcd_Init();
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

          Lcd_Cmd(_LCD_CLEAR);               // Clear display
               Lcd_Out(2,1, "STATE: ");
               Lcd_Out(2,10, tmp);
               uart1_write_text(tmp);

               Lcd_Out(2,1, "STATE: ");
               Lcd_Out(2,10, gsm_state);
               uart1_write_text(gsm_state);


 while(1){


          Send_Alarm_Msg();     // I WANTED TO THIS DISPLAY THIS ON LCD AND ITS RESPONSE FROM THE GSM

}
  }
 
Last edited:

Send_Alarm_Msg() is running constantly inside while(1). I failed to spot LCD access in this function. Where is the LCD part of the code inside while(1)?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top