-1

So, im using the example that comes with the arduino software, but with this example, i have to manually input the phone number that i want to send to everytime. I've tried to put the phone number as an int = "mynumber" and replaced remoteNum, but i doesnt seem to work..

what else can I try?

 #include <GSM.h>

#define PINNUMBER ""

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

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 Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (notConnected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      notConnected = false;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
}

void loop() {

  Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");
}

/*
  Read input serial
 */
int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}

How can I write the phone number into the code, so that I dont have to input in manually everytime?

1 Answers1

1

Your code reads the information from Serial and stores it as a character string an a char array:

  Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

All you need to do is put the number in a character string in the code instead of getting it from serial:

Serial.print("Enter a mobile number: ");
char remoteNum[20] = "15558675309";  // telephone number to send sms
Serial.println(remoteNum);

I don't know for sure that is how your phone number should be formatted, but you can look at what it has been printing out to see if it should have any dashes or anything in it.

Delta_G
  • 2,927
  • 2
  • 9
  • 15
  • I've tried doing what you're suggesting, but it doesn't seem to work.. I'm still getting asked to input a phone number – Hans Johannessen Apr 18 '20 at 15:36
  • Well you'll have to ask someone who can see the code that's messing up. I can't diagnose it without seeing. I can only see the code you posted and it has a line asking for a number. Did you take that line out? – Delta_G Apr 18 '20 at 16:48