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
Just a couple of Question.
PeterH, I do not undertand well your answer
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
//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++;
}
}
}
}
}