Pages: [1]   Go Down
Author Topic: Can not connect to the server and send a SMS  (Read 1199 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear All,

I ma very frustreted because I spent so many time to understand why my server does not answer. The script can not connect.
In the pasted it worked.

Some can tell me how I can debug it?
Do you see something wrong?
(My fire wall is open to the port81)

Many thank fro your help

My bauds are like this:
 Serial.begin(4800);
    cell.begin(9600);

Quote
void loop() {
  /* If called for the first time, loop until GPRS and AT is ready */
  if(firstTimeInLoop) {
    firstTimeInLoop = 0;
    while (GPRS_registered == 0 || GPRS_AT_ready == 0) {
      readATString();
      ProcessATString();
    }

    if(POWERPIN) {
      pinMode(POWERPIN, OUTPUT);
    }

    pinMode(13, OUTPUT);
   
    Serial.println("GPS Parser Initialized");
    digitalWrite(POWERPIN, LOW);
   
    //Serial.println("Setting Band for 1800 frenquency");
    //cell.println("AT+SBAND=4");
   
    delay(1500);

    Serial.println("Setting up APN Context");
    cell.println("AT+CGDCONT=1,\"IP\",\"internet\"");

    delay(1500);
    Serial.println("Activating PDP Context");
    cell.println("AT+CGACT=1,1");

    delay(1500);

    Serial.println("Configuring TCP connection to TCP Server");
    cell.println("AT+SDATACONF=1,\"TCP\",\"xx.xxx.xx.xxx\",81");

    delay(1500);

    Serial.println("Starting TCP Connection\n");
    cell.println("AT+SDATASTART=1,1");
   
    delay(2000);
   
   
    //Send SMS
    cell.println("AT+SBAND=4"); // for 900&1800
    cell.println("AT+CMGF=1"); // set SMS mode to text
    cell.println("AT+CMGS=");  // now send message...
    cell.write(34); // ASCII equivalent of "
    cell.println("0041762233440"); // wrong number to not publish it (0041=Country code, 76=areacode)
    cell.write(34);  // ASCII equivalent of "
    delay(500); // give the module some thinking time
    cell.println("Bipme Alarm");   // our message to send
    cell.write(26);  // ASCII equivalent of Ctrl-Z
    delay(15000); // the SMS module needs time to return to OK status
   
   
    Serial.println("SMS sent");

    onLed(redLedPin);

« Last Edit: January 12, 2013, 03:51:56 pm by pierrot10 » Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8842
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your sketch never checks the response from the cellphone (Serial.write(cell.read())) so there is no way to know which commands are working and which are failing.  Add a function like this:
Code:
void check_result() {
    int character;
    while (character = cell.read()) {
       if (character == '\n') {
           Serial.write(character);
           return;  // Response complete
       }
      if (character != -1)
          Serial.write(character);
     }
}

Call after each AT command is sent.  You may also want to print the AT commands being sent so you can associate the reply with the request.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear John

Before trying, I thank for this proposition.
How many delay should I considere between the command and the answer?
I was thinking about 1sec

Cheers
« Last Edit: January 13, 2013, 06:14:32 am by pierrot10 » Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok then I tried,

Between the command and the answer, I delayed it with 1sed, then 2sec, then 5sec and lastely 10sec. In order to make sure that the command have time to sent.

In all case, I only got an "OK" for the other commande, it return a empty line like \n\r.
 I just would like to mentionned, I put the SIM card to my smartphone, and I could go the the web, send message.

Quote
Setting up APN Context

Activating PDP Context

Configuring TCP connection to TCP Server
OK
Starting TCP Connection



I supposed an empty line is not good?

Cheers
Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's really creasy. If you can help me I will really appreciate.

Yesterday evening, my code was working, excepted the TCP connection. But the GPS part, was working very well. The loop() return me the gps position very well.

Now, this morning I past your code and nothing is working. I can compile well, I have no message error. Then I remove your code to have my code bask as yesterday. But it still not working.

It already happen once in the same situation. Working the previous evening and not the next day !!!!!!!

And as the last time it that part wich make trouble. This condition return now false
  if (gps.encode(c)) {

Code:
while(Serial.available()) {
      int c = Serial.read();
      Serial.println("1");
      if (gps.encode(c)) {
        Serial.println("2");
        onLed(blueLedPin);
        float flat, flon, fspeed, falt;
        unsigned long fix_age;
        gps.f_get_position(&flat,&flon,&fix_age);
       
        fspeed = gps.f_speed_kmph();
        falt = gps.f_altitude();

        //fcourse = gps.f_course();

        if(fix_age == TinyGPS::GPS_INVALID_AGE)

          Serial.println(F("No fix detected"));

        else if (fix_age > 5000)

          Serial.println(F("WARNING: Possible Stale Data!"));

        else {

          myString.print("AT+SSTRSEND=1,\"");

          myString.print(flat,DEC);

          myString.print(",");

          myString.print(flon,DEC);
         
          myString.print(",");
         
          myString.print(fspeed,2);
         
          myString.print(",");
         
          myString.print(falt,2);
         
          //myString.print(",");
         
          //myString.print(fcourse,DEC);
         
          myString.print(",");
         
          myString.print(String(count));

          myString.print("\"");

          Serial.println(myString);

          cell.println(myString);

          myString.begin();

          offLed(blueLedPin);
         
          count++;
         

        }
      }
    }

I could not understand what happen during the night  smiley-mr-green

Do you have any idea? It will really help
Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I could not understand what happen during the night
Code rot, probably. Nasty stuff. Your whole computer might be infected.

Of course, since you have not posted all your code, that might be complete nonsense.

You have two choices, the way I see it. One is to post ALL of your code. The other is to head down the internet to http://snippets-r-us.com to see if they can help you based only on incomplete snippets.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How many delay should I considere between the command and the answer?

The sample code given did not use a fixed delay - it waited (printing out any response it received) until it received an end-of-line character. If the modem always sends back a one line response every time you give it a command, this this approach will wait for the response to be received and show it to you which is what you want to debug the commands.

In an ideal world your sketch would also validate the response to each command to check whether the command succeeded. To do that, rather than just print the response to the serial port the code would need to buffer it until a complete response had been received and then process the response to find what the command status was.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear PeterH and PaulS

Thank again for helping. Below you will find my full code.
I really thank you for your help because I am really lost in my understanding  smiley

Just a couple of Question.
PeterH, I do not undertand well your answer
Quote
If the modem always sends back a one line response every time you give it a command
Then we are agree that is not normal to have an empty line? We should have an ok or a message?

Initialy, it use the library NewSoftSerial.h but I use SoftwareSerial.h
Ma libraries are in Document/Arduiano/libraries/. I am on Mac
Code:
//Include the NewSoftSerial library to send serial commands to the cellular module.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <PString.h>

#define POWERPIN 4
#define GPSRATE 4800
#define BUFFSIZ 90

char at_buffer[BUFFSIZ];
char buffidx;

int firstTimeInLoop = 1;

int GPRS_registered;
int GPRS_AT_ready;
int count = 0;

//Will hold the incoming character from the Serial Port.

char incoming_char=0;     
char buffer[60];

PString myString(buffer,sizeof(buffer));

//Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.

SoftwareSerial cell(2,3); 

TinyGPS gps;

int redLedPin  = 11;
int blueLedPin = 12;

// Function to Blink a LED
// Parameters: lPin   - Pin of the LED
//             nBlink - Number of Times to Blink
//             msec   - Time in milliseconds between each blink

void blinkLed(int lPin, int nBlink, int msec) {
  if (nBlink) {
    for (int i = 0; i < nBlink; i++) {
      digitalWrite(lPin, HIGH);
      delay(msec);
      digitalWrite(lPin, LOW);
      delay(msec);
    }
  }
}

// Function to Switch on a LED
// Parameters: lPin - Pin of the LED

void onLed (int lPin) {
  digitalWrite(lPin, HIGH);
}

// Function to Switch off a LED
// Parameters: lPin - Pin of the LED

void offLed (int lPin) {
  digitalWrite(lPin, LOW);
}

void check_result() {
    int character;
    while (character = cell.read()) {
       if (character == '\n') {
           Serial.write(character);
           return;  // Response complete
       }
      if (character != -1)
          Serial.write(character);
     }
}

void freeRam (){
  extern int __heap_start, *__brkval;
  int v;
  int free = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  Serial.println(free);
}

// Do system wide initialization here in this function
void setup(){

// LED Pin are outputs. Switch the mode
  pinMode(redLedPin, OUTPUT);
  pinMode(blueLedPin, OUTPUT);

  /* Blink the Power LED */

  blinkLed(redLedPin,3,500);
 
  //Initialize serial ports for communication.

    Serial.begin(4800);
    cell.begin(9600);
 
  //Let's get started!

  Serial.println(F("Starting SM5100B Communication..."));

  delay(5000);

  /* Currently GPRS is not registered and AT is not ready */

  GPRS_registered = 0;
  GPRS_AT_ready = 0;
}

/* Reads AT String from the SM5100B GSM/GPRS Module */

void readATString(void) {
  char c;
  buffidx= 0; // start at begninning
  while (1) {
    if(cell.available() > 0) {
      c=cell.read();
      if (c == -1) {
        at_buffer[buffidx] = '\0';
        return;
      }
     
      if (c == '\n') {
        continue;
      }

      if ((buffidx == BUFFSIZ - 1) || (c == '\r')){
        at_buffer[buffidx] = '\0';
        return;
      }

      at_buffer[buffidx++]= c;
    }
  }
}

/* Processes the AT String to determine if GPRS is registered and AT is ready */

void ProcessATString() {
  if( strstr(at_buffer, "+SIND: 8") != 0 ) {
    GPRS_registered = 0;
    Serial.println(F("GPRS Network Not Available (0)"));
  }
 
  if (strstr(at_buffer, "+SIND: 10,\"SM\",0,\"FD\",0,\"LD\",0,\"MC\",0,\"RC\",0,\"ME\",0") != 0
    || strstr(at_buffer, "+SIND: 0") != 0) {
     Serial.println(F("SIM removed +SIND: 10+0"));
  }
 
  if (strstr(at_buffer, "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1") != 0) {
    Serial.println(F("SIM inserted +SIND:10+1"));
    //successLED();
  }
 
  if (strstr(at_buffer, "+CME ERROR: 29") != 0) {
    Serial.println(F("+CME ERROR: 2"));
    return;
  }

  if( strstr(at_buffer, "+SIND: 11") != 0 ) {
    GPRS_registered=1;
    Serial.println(F("GPRS Registered (11)"));
    //blinkLed(redLedPin,5,100);
  }
 
  if( strstr(at_buffer, "+SIND: 7") != 0 ) {
    Serial.println(F("Emergency call only (7)"));
  }
 
  if( strstr(at_buffer, "+SIND: 3") != 0 ) {
    GPRS_AT_ready=1;
    Serial.println(F("GPRS AT almost Ready (3)"));
  }
 
  if( strstr(at_buffer, "+SIND: 4") != 0 ) {
    GPRS_AT_ready=1;
    Serial.println(F("GPRS AT Ready (4)"));
  }
 
  if( strstr(at_buffer, "+SIND: 15") != 0 ) {
    GPRS_AT_ready=1;
    Serial.println(F("Host error (15)"));
  }
 
  if( strstr(at_buffer, "+SIND: 16") != 0 ) {
    GPRS_AT_ready=1;
    Serial.println(F("Unknow error (16)"));
  }
 
  if( strstr(at_buffer, "+SIND: 17") != 0 ) {
    GPRS_AT_ready=1;
    Serial.println(F("GSM Fail (17)"));
  }
}

void loop() {
  /* If called for the first time, loop until GPRS and AT is ready */
  if(firstTimeInLoop) {
    firstTimeInLoop = 0;
    while (GPRS_registered == 0 || GPRS_AT_ready == 0) {
      readATString();
      ProcessATString();
    }
   
    if(POWERPIN) {
      pinMode(POWERPIN, OUTPUT);
    }

    pinMode(13, OUTPUT);
   
    Serial.println("GPS Parser Initialized");
    digitalWrite(POWERPIN, LOW);
   
    //Serial.println("Setting Band for 1800 frenquency");
    //cell.println("AT+SBAND=4");
   
    delay(1000);

    Serial.println("Setting up APN Context");
    cell.println("AT+CGDCONT=1,\"IP\",\"internet\"");
    check_result();
    delay(1000);
   
    Serial.println("Activating PDP Context");
    cell.println("AT+CGACT=1,1");
check_result();
    delay(1000);

    Serial.println("Configuring TCP connection to TCP Server");
    cell.println("AT+SDATACONF=1,\"TCP\",\"xxx.xxx.xx.xxx\",81");
check_result();
    delay(1000);

    Serial.println("Starting TCP Connection\n");
    cell.println("AT+SDATASTART=1,1");
check_result();

    //Send SMS
    cell.println("AT+SBAND=4");
    cell.println("AT+CMGF=1"); // set SMS mode to text
    cell.println("AT+CMGS=");  // now send message...
    cell.write(34); // ASCII equivalent of "
    cell.println("0041761235465"); //0041=Country code, 76=Area code
    cell.write(34);  // ASCII equivalent of "
    delay(500); // give the module some thinking time
    cell.println("Bipme Alarm");   // our message to send
    cell.write(26);  // ASCII equivalent of Ctrl-Z
    delay(15000); // the SMS module needs time to return to OK status
   
   
    Serial.println("SMS sent");

    onLed(redLedPin);

  } else {
    while(Serial.available()) {
      int c = Serial.read();
      if (gps.encode(c)) {
        onLed(blueLedPin);
        float flat, flon, fspeed, falt;
        unsigned long fix_age;
        gps.f_get_position(&flat,&flon,&fix_age);
       
        fspeed = gps.f_speed_kmph();
        falt = gps.f_altitude();

        //fcourse = gps.f_course();

        if(fix_age == TinyGPS::GPS_INVALID_AGE)

          Serial.println("No fix detected");

        else if (fix_age > 5000)

          Serial.println("WARNING: Possible Stale Data!");

        else {

          myString.print("AT+SSTRSEND=1,\"");

          myString.print(flat,DEC);

          myString.print(",");

          myString.print(flon,DEC);
         
          myString.print(",");
         
          myString.print(fspeed,2);
         
          myString.print(",");
         
          myString.print(falt,2);
         
          //myString.print(",");
         
          //myString.print(fcourse,DEC);
         
          myString.print(",");
         
          myString.print(String(count));

          myString.print("\"");

          Serial.println(myString);

          cell.println(myString);

          myString.begin();

          offLed(blueLedPin);
         
          count++;
         
        }
      }
    }
  }
}
« Last Edit: January 13, 2013, 02:28:29 pm by pierrot10 » Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
There is nothing fake about a software serial port.

Code:
PString myString(buffer,sizeof(buffer));
Stupid. Get rid of the PString class. Use the buffer you are wrapping here.

Your readATString() doesn't. It reads whatever has arrived so far. If that is nothing, it returns. You need to make it wait until a complete response has been received, or until some amount of time with no response has elapsed. The return code needs to reflect that timeout.

Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear PaulS,

Thank for helping.
Really it really creasy. Now it work. I got the GPS position. I am totaly confused,  it's like if it only works on evening   smiley
Quote
//Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
Ok I removed that comment
Quote
PString myString(buffer,sizeof(buffer));
For this, I am a bit confused. To resume, I should remove that line and replace all ma myString...
Code:
myString.print("AT+SSTRSEND=1,\"");
by something like this?
Code:
char index;
char gps_buffer[BUFFSIZ];
gps_buffer[index++] = 'AT+SSTRSEND=1,"';
gps_buffer[index++] = flat;
//myString.print(flat,DEC);
gps_buffer[index++] = ',';
gps_buffer[index++] = flon;

I am sorry if my question seams to be stupid, but I am not used to program with Sketch. I spend a lot of time to understand that code, specialy the readATString()

For the last point, I do not undertand what I should do.
Quote
You need to make it wait until a complete response has been received
OK, I understand that but Is not the case? The while loop run until to read a \r caracter. Is not?
Quote
The return code needs to reflect that timeout
Ok but where to definy the the timeout and how the know the amount of delay regarding the return code

Thank thank

« Last Edit: January 13, 2013, 04:02:50 pm by pierrot10 » Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Definitively, it creasy. Yesterday evening, it was working, I switched all off, go to sleep, and this morning, the position are not display.
« Last Edit: January 14, 2013, 10:56:42 am by pierrot10 » Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Offline Offline
Sr. Member
****
Karma: 0
Posts: 399
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am still around my problem and I look around the web.
Until now, I only powered my module with USB cable. May it be a cause? Should I power with 7-9V?

@johnwasser
Could you let me know a bit more about your function. I think I did not fully understand what you state, May be my english is not good enouhg ?

I supposed that an empty line is not good, but I did not understand where I should add code to consider the timeout?

How many delay should I considere between the command and the answer?

The sample code given did not use a fixed delay - it waited (printing out any response it received) until it received an end-of-line character. If the modem always sends back a one line response every time you give it a command, this this approach will wait for the response to be received and show it to you which is what you want to debug the commands.

In an ideal world your sketch would also validate the response to each command to check whether the command succeeded. To do that, rather than just print the response to the serial port the code would need to buffer it until a complete response had been received and then process the response to find what the command status was.


Concerning my GPS podition display, when it does not display, the error come from that condition  "if (gps.encode(c)) {" which return false.
Code:
while(Serial.available()) { //or cell.available? I think it's the same?
      int c = Serial.read(); //or cell.available? I think it's the same?
    [b]  if (gps.encode(c)) {[/b]
        onLed(blueLedPin);
        float flat, flon, fspeed, falt;
        unsigned long fix_age;
        gps.f_get_position(&flat,&flon,&fix_age);

...

Thaousand thanks
Logged

Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

Pages: [1]   Go Up
Jump to: