Boa noite garagistas estou com um projeto, de um bloqueador via GPRS consegui esse esbolso abaixo na net testei alguns SMS e o shield Sim900 recebe transmite ao Arduíno e o mesmo nao interpreta corretamente segue resposta obtida na software serial.

Starting
ProcessGprsMsg();AT+CMGF=1
ProcessGprsMsg();ProcessGprsMsg();
OK
ProcessGprsMsg();ProcessGprsMsg();
+CMTI: "SM",6

 

Si alguem poder me da uma força agradeço..

segue esbolso usado.

ch = strstr((char *)comm_buf, compare_string);
if (ch != NULL) {
ret_val = 1;
}
else
{
/*#ifdef DEBUG_PRINT
DebugPrint("\r\nDEBUG: expected string was NOT received\r\n", 0);
#endif
*/
}
}

return (ret_val);
}


byte WaitResp(uint16_t start_comm_tmout, uint16_t max_interchar_tmout)
{
byte status;//programmer gudjon@undri.com 

//todo:sorry about the messy coding (not enough time to fix)

#include <SoftwareSerial.h>

//FOR MEGA decomment line below. On the EFCom shield DIS-connect the jumpers and put jumperwires from SHIELD RX to MEGA 51 and from SHIELD TX to MEGA 50
SoftwareSerial mySerial(50, 51);

//FOR UNO decomment line below. On the EFCom shield connect the jumers, TX to D2 and RX to D3.
//SoftwareSerial mySerial(2, 3);

/*
EFcom/GPRS Shield Light meaning.
------------------------------------------------------------------------
| LED STATUS DESCRIPTION |
| --- ----------------- ---------------------------------------------|
| PWR ON Power on the EFcom |
| PWR OFF Power off the EFcom |
| STA ON Power on the SIM900 OFF Power Off the SIM900 |
| NET OFF SIM900 is not running |
| NET 64ms ON/800ms OFF SIM900 not register the Network |
| NET 64ms ON/3000ms OFF SIM900 registered to the network |
| NET 64ms ON/300ms OFF GPRS communication is established |
--- -------------------------------------------------------------------
*/


///////////////////////////////////////////////////////////////////////////////////////

//SELECT HERE WICH PINS YOU WANT TO CONTROL ON THE ARDUINO (THEY must be in sequence)
#define OPIN_FIRST 30
#define OPIN_LAST 33
//when program starts turn all pins on or off. allowed values are either HIGH or LOW
#define OPIN_STARTSTATE LOW
//The controlling phonenumber;
#define PHONENUMBER "meu numero"

///////////////////////////////////////////////////////////////////////////////////////


String msg = String("");
int SmsContentFlag = 0;

#define GSM_POWER 6 /*EFCom power pin*/
#define GSM_RESET 5 /*EFCom reset pin**/

// S T A R T S T A R T Borrowed from GSMSHIELD Library S T A R T S T A R T //
// some constants for the IsRxFinished() method
// length for the internal communication buffer
#define COMM_BUF_LEN 300
#define RX_NOT_STARTED 0
#define RX_ALREADY_STARTED 1

// variables connected with communication buffer
byte *p_comm_buf; // pointer to the communication buffer
byte comm_buf_len; // num. of characters in the buffer
byte rx_state; // internal state of rx state machine 
uint16_t start_reception_tmout; // max tmout for starting reception
uint16_t interchar_tmout; // previous time in msec.
unsigned long prev_time; // previous time in msec.
byte comm_buf[COMM_BUF_LEN+1]; // communication buffer +1 for 0x00 termination 

enum rx_state_enum 
{
RX_NOT_FINISHED = 0, // not finished yet
RX_FINISHED, // finished, some character was received
RX_FINISHED_STR_RECV, // finished and expected string received
RX_FINISHED_STR_NOT_RECV, // finished, but expected string not received
RX_TMOUT_ERR, // finished, no character received 
// initial communication tmout occurred
RX_LAST_ITEM
};


void RxInit(uint16_t start_comm_tmout, uint16_t max_interchar_tmout)
{
rx_state = RX_NOT_STARTED;
start_reception_tmout = start_comm_tmout;
interchar_tmout = max_interchar_tmout;
prev_time = millis();
comm_buf[0] = 0x00; // end of string
p_comm_buf = &comm_buf[0];
comm_buf_len = 0;
mySerial.flush(); // erase rx circular buffer
}

byte IsRxFinished(void)
{
byte num_of_bytes;
byte ret_val = RX_NOT_FINISHED; // default not finished

if (rx_state == RX_NOT_STARTED) {
// Reception is not started yet - check tmout
if (!mySerial.available()) {
// still no character received => check timeout

if ((unsigned long)(millis() - prev_time) >= start_reception_tmout) {
// timeout elapsed => GSM module didn't start with response
// so communication is takes as finished
comm_buf[comm_buf_len] = 0x00;
ret_val = RX_TMOUT_ERR;
}
}
else {
// at least one character received => so init inter-character 
// counting process again and go to the next state
prev_time = millis(); // init tmout for inter-character space
rx_state = RX_ALREADY_STARTED;
}
}

if (rx_state == RX_ALREADY_STARTED) {
// Reception already started
// check new received bytes
// only in case we have place in the buffer
num_of_bytes = mySerial.available();
// if there are some received bytes postpone the timeout
if (num_of_bytes) prev_time = millis();

// read all received bytes 
while (num_of_bytes) {
num_of_bytes--;
if (comm_buf_len < COMM_BUF_LEN) {
// we have still place in the GSM internal comm. buffer =>
// move available bytes from circular buffer 
// to the rx buffer
*p_comm_buf = mySerial.read();

p_comm_buf++;
comm_buf_len++;
comm_buf[comm_buf_len] = 0x00; // and finish currently received characters
// so after each character we have
// valid string finished by the 0x00
}
else {
mySerial.read();
}
}
if ((unsigned long)(millis() - prev_time) >= interchar_tmout) {
comm_buf[comm_buf_len] = 0x00; // for sure finish string again
// but it is not necessary
ret_val = RX_FINISHED;
}
}


return (ret_val);
}

/**********************************************************
Method checks received bytes

compare_string - pointer to the string which should be find

return: 0 - string was NOT received
1 - string was received
**********************************************************/
byte IsStringReceived(char const *compare_string)
{
char *ch;
byte ret_val = 0;

if(comm_buf_len) {
#ifdef DEBUG_ON
Serial.println("ATT: ");
Serial.print(compare_string);
Serial.print("RIC: ");
Serial.println((char *)comm_buf);
#endif

RxInit(start_comm_tmout, max_interchar_tmout);
// wait until response is not finished
do {
status = IsRxFinished();
} while (status == RX_NOT_FINISHED);
return (status);
}

///////////////þarf eþtta kannski ekki sjá fyrir ofan
byte WaitResp(uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *expected_resp_string)
{
byte status;
byte ret_val;

RxInit(start_comm_tmout, max_interchar_tmout);
// wait until response is not finished
do {
status = IsRxFinished();
} while (status == RX_NOT_FINISHED);

if (status == RX_FINISHED) {
// something was received but what was received?
// ---------------------------------------------

if(IsStringReceived(expected_resp_string)) {
// expected string was received
// ----------------------------
ret_val = RX_FINISHED_STR_RECV;
}
else {
ret_val = RX_FINISHED_STR_NOT_RECV;
}
}
else {
// nothing was received
// --------------------
ret_val = RX_TMOUT_ERR;
}
return (ret_val);
}
///////////////þarf eþtta kannski ekki endir

/**********************************************************
Method sends AT command and waits for response

return:
-1, // no response received
0, // response_string is different from the response
1, // response_string was included in the response
**********************************************************/
char SendATCmdWaitResp(char const *AT_cmd_string,
uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *response_string,
byte no_of_attempts)
{
byte status;
char ret_val = -1;
byte i;

for (i = 0; i < no_of_attempts; i++) {
// delay 500 msec. before sending next repeated AT command
// so if we have no_of_attempts=1 tmout will not occurred
if (i > 0) delay(500);

mySerial.println(AT_cmd_string);
status = WaitResp(start_comm_tmout, max_interchar_tmout);
if (status == RX_FINISHED) {
// something was received but what was received?
// ---------------------------------------------
if(IsStringReceived(response_string)) {
ret_val = 1;
break; // response is OK => finish
}
else ret_val = 0;
}
else {
// nothing was received
// --------------------
ret_val = -1;
}
}
return (ret_val);

}//char SendATCmdWaitRes


/**********************************************************
Method sends SMS

number_str: pointer to the phone number string
message_str: pointer to the SMS text string


return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module didn't answer in timeout
-3 - GSM module has answered "ERROR" string

OK ret val:
-----------
0 - SMS was not sent
1 - SMS was sent


an example of usage:
GSM gsm;
gsm.SendSMS("00XXXYYYYYYYYY", "SMS text");
**********************************************************/
char SendSMS(char *number_str, char *message_str)
{
char end[2];
end[0]=0x1a;
end[1]='\0';/* Ctrl+Z */

mySerial.print("\r");
delay(1000); //Wait for a second while the modem sends an "OK"
mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
delay(1000);

//mySerial.print("AT+CSCA=\"+919032055002\"\r"); //Setting for the SMS Message center number,
//delay(1000); //uncomment only if required and replace with
//the message center number obtained from
//your GSM service provider.
//Note that when specifying a tring of characters
// " is entered as \"

mySerial.print("AT+CMGS=\"");mySerial.print(number_str); mySerial.print("\"\r"); //Start accepting the text for the message
//to be sent to the number specified.
//Replace this number with the target mobile number.
delay(1000);
mySerial.print(message_str); mySerial.print("\r"); //The text for the message
delay(1000);
mySerial.print(end);

}

//// E N D E N D Borrowed from GSMSHIELD Library E N D E N D //
////
//// S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P
//// S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P
//// S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P S E T U P
void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
if (-1 == SendATCmdWaitResp("AT", 500, 100, "OK", 5))
{
digitalWrite(GSM_POWER, HIGH);delay(3000);
digitalWrite(GSM_POWER, LOW);delay(2000);
}
for(int i=OPIN_FIRST;i<OPIN_LAST+1;i++)
{
pinMode(i, OUTPUT);
digitalWrite(i,OPIN_STARTSTATE);
}
Serial.println( "Starting" );
mySerial.println( "AT+CMGF=1" );
delay(200);
}
boolean bFoundMSGandNumber=false;
void loop()
{
char SerialInByte;
if(mySerial.available())
{
SerialInByte = (unsigned char)mySerial.read();
delay(5);

if( SerialInByte == 13 ){
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
}
else {
msg += String(SerialInByte);
}
}

}

void ProcessSms( String sms ){
Serial.print("ProcessSms:");Serial.println(sms);

String finna = String("xxxxx");
for(int i=OPIN_FIRST;i<OPIN_LAST+1;i++)
{ finna=String("Off");finna+=i;
if( sms.indexOf(finna) >= 0 || sms.indexOf("Offall") >= 0 ){digitalWrite(i, LOW); Serial.println(finna);}
}
for(int i=OPIN_FIRST;i<OPIN_LAST+1;i++)
{ finna=String("On");finna+=i;
if( sms.indexOf(finna) >= 0 || sms.indexOf("Onall") >= 0){digitalWrite(i, HIGH); Serial.println(finna);}
}
if( sms.indexOf("List") >= 0 )
{
finna="stada";
for(int i=OPIN_FIRST;i<OPIN_LAST+1;i++)
{ finna+=" ";finna+=i;finna+="=";finna+=digitalRead(i); }
int iSize=finna.length()+1;
char charBuf[iSize];
finna.toCharArray(charBuf, iSize);
SendSMS(PHONENUMBER, charBuf);
}


mySerial.println( "AT+CMGDA=\"DEL ALL\"" );
delay(200);

}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
mySerial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
mySerial.print( "AT+CMGR=" );
mySerial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
Serial.print( "ProcessGprsMsg();");Serial.println(msg);
if( msg.indexOf( "Call Ready" ) >= 0 ){
Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}

//todo: búa til define string úr PHONENUMBER
char findMe[strlen(PHONENUMBER)+2];strcpy(findMe,PHONENUMBER);strcat(findMe,"\"");
if( (msg.indexOf( "+CMT" ) >= 0) && (msg.indexOf( findMe )>0) ){
Serial.println( "*** SMS Received ***" );
bFoundMSGandNumber=true;
}
else
{
if(bFoundMSGandNumber==true)
{
ProcessSms( msg );
}
bFoundMSGandNumber=false;
}
ClearGprsMsg();
SmsContentFlag = 0;
}

int getValidPin(char *pNum)
{
int iNum=strlen(pNum);
if (iNum<1 || iNum>2) return false;//pin must be from 0..99
iNum = atoi (pNum);
if ((iNum < OPIN_FIRST) || (iNum > OPIN_LAST)) return false;//num must be equal or between OPIN_FIRST and OPIN_FIRST

return iNum;
}

Exibições: 840

Responder esta

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço