Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 1   [ 2 posts ]
Author Message
 Post subject: 3G SHIELD : AT+CMGR=2 after AT+CMGR=1
PostPosted: Tue Apr 11, 2017 3:33 pm 

Joined: Thu Apr 06, 2017 9:53 am
Posts: 6
Hello,

Excuse my english... I'm french :-(

The code below allows to read the first sms in memory. If I want to read the second (../.. answer = sendATcommand("AT+CMGR=2", "+CMGR:", 2000); // reads the second SMS ../..), the response give to me the first sms again...

Is there a variable to initialize again ?

Thanks a lot for your response.
Adele


/*
* 3G + GPS shield
*
* Copyright (C) Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* a
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Version: 2.0
* Design: David Gascón
* Implementation: Alejandro Gallego & Victor Boria
*/

//Change here your data
const char pin[]="****";

int8_t answer;
int x;
int onModulePin= 2;
char SMS[200];
char aux_str[30];


void setup(){

pinMode(onModulePin, OUTPUT);
Serial.begin(115200);

Serial.println("Starting...");
power_on();

delay(3000);

//sets the PIN code
sprintf(aux_str, "AT+CPIN=%s", pin);
sendATcommand(aux_str, "OK", 2000);

delay(3000);

Serial.println("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000); // selects the memory

answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000); // reads the first SMS
if (answer == 1)
{
answer = 0;
while(Serial.available() == 0);
// this loop reads the data of the SMS
do{
// if there are data in the UART input buffer, reads it and checks for the answer
if(Serial.available() > 0){
SMS[x] = Serial.read();
x++;
// check if the desired answer (OK) is in the response of the module
if (strstr(SMS, "OK") != NULL)
{
answer = 1;
}
}
}
while(answer == 0); // Waits for the answer with time out

SMS[x] = '\0';

Serial.print(SMS);

}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}


}


void loop(){

}

void power_on(){

uint8_t answer=0;

// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);

// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}

}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

uint8_t x=0, answer=0;
char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialice the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial.println(ATcommand); // Send the AT command


x = 0;
previous = millis();

// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the answer
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the answer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}


Top
 Profile  
 
 Post subject: Re: 3G SHIELD : AT+CMGR=2 after AT+CMGR=1
PostPosted: Wed Apr 12, 2017 7:20 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hi AdeleI,

Maybe this post can help you:

viewtopic.php?f=43&t=5919

Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 2 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.