Go Down

Topic: endSMS() (Read 547 times) previous topic - next topic

Stupidav

I am at a loss on where to look to figure out what I have going on with the "endSMS()". 

I am receiving a text (no problem) comparing the remoteNumber to a known numbers and send a response back. Everything works fine in my testing file but not in my other file, and am unable to determine what I am missing. Any recommendation on where to look is much appreciated.

Here is the one that doesn't work:
Code: [Select]
// include the GSM library
#include <MKRGSM.h>

#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retreived from
char myNumber[20] = "+0123456789";  //Your Phone Number
char recNum[20];

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SMS Messages Receiver");

  // connection state
  bool connected = false;

  // Start GSM connection
  while (!connected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() {
  int c;
  char ack[] = ("I Received your Text Message");
  // If there are any SMSs available()
  if (sms.available()) {
    Serial.println("Message received from:");
   
    sms.remoteNumber(recNum, 20);   // Get remote number
    Serial.println(recNum);   
       
    while ((c = sms.read()) != -1) {
      Serial.print((char)c);
    }
    Serial.println();
    sms.flush();    //Delete message for GSM modem memory

    if (strcmp(recNum, myNumber) == 0) {       //Compare received textmessage sender to owners phone numbers
      Serial.println("Received Message from authorized phone!");
      Serial.println(recNum);         
           
      sms.beginSMS(myNumber);
      sms.println("Yes I Received your Message.");
      sms.endSMS();
      Serial.println("Sent Acknoledgement");         

    } else {
      Serial.println("Received message from an unknown number.");

    }
    delay(2500);
  }
}


Here is the working code, I must be looking at it too long, this one works:

Code: [Select]
// include the GSM library
#include <MKRGSM.h>

#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;

// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;

// Array to hold the number a SMS is retrieved from
char senderNumber[20];
char myNumber[20] = "+0123456789";    //Your Phone Number

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SMS Messages Receiver");

  // connection state
  bool connected = false;

  // Start GSM connection
  while (!connected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() {
  int c;

  // If there are any SMSs available()
  if (sms.available()) {
    Serial.println("Message received from:");

    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

    // Read message bytes and print them
    while ((c = sms.read()) != -1) {
      Serial.print((char)c);
    }

    Serial.println("\nEND OF MESSAGE");

    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");

    if (strcmp(senderNumber, myNumber) == 0) {
      Serial.println("Received Message from myself!"); 

      // send the message
      sms.beginSMS(senderNumber);
      sms.print("Yes I Received your Message.");
      sms.endSMS();
      Serial.println("\nCOMPLETE!\n");
    } else {
      Serial.println("Received message from an unknown number.");
    }
  }

  delay(1000);

}


I have put in serial prints everywhere and it always freezes at sms.endSMS(); and never sends the message, yet I can't find the difference between these two.

Stupidav

#1
Dec 13, 2020, 08:20 pm Last Edit: Dec 13, 2020, 08:21 pm by Stupidav
I did come across a couple things that might help me see what the issue is, although I am not sure how to use it as described, considering it is listed in most of the functions and not a single example of it in use (that I could find).

"In asynchronous mode, xxxxxx() returns 0 if the last command is still executing, 1 if it was successful and >1 if there is an error. In synchronous mode, it returns 1 if the previous successfully executed, and 0 if it has not."

First what is the difference and how to tell what mode it is in "Asynchronous" or "Synchronous"?

Second what is the proper way to use this, something like the following maybe?
Code: [Select]
int x;
x = (sms.beginSMS(myNumber));
if (x = 1) {
  x = (sms.println("Yes I Received your Message."));
  if (x = 1) {
    x = (sms.endSMS());
  } else {
    if (x > 1) {
      Serial.println(x);
      Serial.println("Error occured");
    } else {
      Serial.println("Still executing previous");
    }
} else {
  if (x > 1) {
    Serial.priintln(x);
    Serial.println("Error occured sending number");
  } else {
    Serial.println("Still trying to setup sender number");
  }
}

Or something like this? Obviously I haven't tried this as I don't have the unit in front of me.



lavignema

I'm no expert but it looks like your problem is likely in the definition of char myNumber[20]. In the working code you take the sender's number and load it into char senderNumber[20] and the non-working code into char recNum[20]. Of course what you call it is irrelevant but later in the working code you reply to senderNumber[] and in the non-working you reply to recNum[]. I'm willing to bet that your structure is not compatible with the SMS system. When the sms.remoteNumber() function executes to load recNum[] it can be used to reply without alterations. It seems that even though your strcmp() command tests out true there must be a difference between the two strings. Possibly the "+" sign? Also you can get an int returned from endSMS(). In synchronous mode, it returns 1 if the previous successfully executed, and 0 if it has not. Most likely though, if your target phone number is malformed it will hang and you won't get back anything.

Go Up