Gentilissimi, sto cercando di ricevere sms con una shield di questo tipo:
e questa la libreria:
ho cercato di lasciar perdere la libreria e di provare l’utilizzo degli AT command ma credo di poter riconoscere onestamente che non sono all’altezza.
Sono tornato all’uso della libreria, ma ancora ho il problema di prima cioè Ricevo il primo sms ma non il secondo.
Mi farebbe davvero piacere se deste uno sguardo al mio codice e alla libreria che sto usando.
Dubito che la libreria sia sbagliata perchè è molto usata in rete e a quanto pare nessuno ha questo mio problema.
In pratica la funzione isSMSPresent(SMS_UNREAD) mi torna 0 anche quando la sim dovrebbe aver ricevuto il “Secondo SMS”
La cosa strana è che il primo sms lo ricevo correttamente, da quel momento, questa funzione torna il valore 0 non trovando la presenza di un altro “sms non letto” da mostrare.
Ti ringrazio infinitamente per il vostro aiuto sempre prezioso.
Questo il codice
#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"
//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;
//To change pins for Software Serial, use the two lines in GSM.cpp.
//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.
//Simple sketch to send and receive SMS.
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
char sms_position;
char phone_number[20];
char sms_text[100];
int i;
void setup()
{
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)) {
Serial.println("\nstatus=READY");
started=true;
} else Serial.println("\nstatus=IDLE");
if(started)
{
for (i=1;i<=20;i++)
{
sms.DeleteSMS(i);
}
}
};
void loop()
{
if(started) {
sms_position=sms.IsSMSPresent(SMS_UNREAD); // SMS.cpp Method finds out if there is present at least one SMS with specified status
Serial.print("SMS postion:");
Serial.println(sms_position,DEC);
if (sms_position)
{
Serial.print("SMS postion:");
Serial.println(sms_position,DEC);
sms.GetSMS(sms_position, phone_number, 11, sms_text, 100); //Arguments to GET SMS. See SMS.h line12:(byte position, char *phone_number,byte max_phone_len, char *SMS_text, byte max_SMS_len)
Serial.println(phone_number);
Serial.println(sms_text);
sms.DeleteSMS(sms_position);
}
delay(5000);
}
};
Libreria:
*********************************************************/
char SMSGSM::IsSMSPresent(byte required_status)
{
char ret_val = -1;
char *p_char;
byte status;
if (CLS_FREE != gsm.GetCommLineStatus()) return (ret_val);
gsm.SetCommLineStatus(CLS_ATCMD);
ret_val = 0; // still not present
switch (required_status) {
case SMS_UNREAD:
gsm.SimpleWriteln(F("AT+CMGL=\"REC UNREAD\""));
break;
case SMS_READ:
gsm.SimpleWriteln(F("AT+CMGL=\"REC READ\""));
break;
case SMS_ALL:
gsm.SimpleWriteln(F("AT+CMGL=\"ALL\""));
break;
}
// 5 sec. for initial comm tmout
// and max. 1500 msec. for inter character timeout
gsm.RxInit(5000, 1500);
// wait response is finished
do {
if (gsm.IsStringReceived("OK")) {
// perfect - we have some response, but what:
// there is either NO SMS:
// <CR><LF>OK<CR><LF>
// or there is at least 1 SMS
// +CMGL: <index>,<stat>,<oa/da>,,[,<tooa/toda>,<length>]
// <CR><LF> <data> <CR><LF>OK<CR><LF>
status = RX_FINISHED;
break; // so finish receiving immediately and let's go to
// to check response
}
status = gsm.IsRxFinished();
} while (status == RX_NOT_FINISHED);
switch (status) {
case RX_TMOUT_ERR:
// response was not received in specific time
ret_val = -2;
break;
case RX_FINISHED:
// something was received but what was received?
// ---------------------------------------------
if(gsm.IsStringReceived("+CMGL:")) {
// there is some SMS with status => get its position
// response is:
// +CMGL: <index>,<stat>,<oa/da>,,[,<tooa/toda>,<length>]
// <CR><LF> <data> <CR><LF>OK<CR><LF>
p_char = strchr((char *)gsm.comm_buf,':');
if (p_char != NULL) {
ret_val = atoi(p_char+1);
}
} else {
// other response like OK or ERROR
ret_val = 0;
}
// here we have gsm.WaitResp() just for generation tmout 20msec. in case OK was detected
// not due to receiving
gsm.WaitResp(20, 20);
break;
}
gsm.SetCommLineStatus(CLS_FREE);
return (ret_val);
}