How to use remoteNumber?

hello there. gsm + arduino cant reply the sms when there is incoming sms from certain number. how to solve this?

thanks :confused:

void sendTextMessage(String remoteNumber) {
  Serial.print("sendTextMessage: ");
  Serial.print(remoteNumber);
  //Serial.println(message);
  Serial.println("AT + CMGS = \"" + remoteNumber + "\"");
  delay(100);
  Serial.print("Sent from my Arduino.");
  delay(100);
  Serial.println((char)26); // ASCII code of the ctrl+z is 26
  delay(100);
  Serial.println();
}
  Serial.print("sendTextMessage: ");
  Serial.print(remoteNumber);
  //Serial.println(message);
  Serial.println("AT + CMGS = \"" + remoteNumber + "\"");

This looks like you are using Serial to talk to both the GSM device and Serial Monitor. That won't work too well.

oh...i see...how am i suppose to change the code so that i can get the sender's number?

please :neutral_face:

how am i suppose to change the code so that i can get the sender's number?

I suggest Ctrl-A, Ctrl-X, Ctrl-S.

If you posted something other than a useless snippet that has nothing to do with your question, perhaps a different answer would be in order.

Why does
your code
jerk all over
the place?
Can't you
use Tools + Auto
Format to
tame
the beast?

void setup(){

 
    Serial.begin(9600);

The ONLY argument for putting the { on the same line as the statement that holds water is to save screen real estate. It looks pretty stupid to then use TWO blank lines after the statement with the { jammed up tight against it.

The commented out code (of which there is far too much) has nothing to do with the problem. So why is it in what you posted?

Get rid of the crap. Properly post the code. Explain what it does, and how that differs from what you want.

I am sorry.

I tried to send the sms to the number inside my gsm. I would like the gsm to reply. I managed to send the sms but the gsm did not reply it back. there will be different sender's number that i will use to send the sms to gsm. that is the reason why i used remoteNumber.

int8_t answer;
int x;

char SMS[200];
char remoteNumber[20];  // Holds the emitting number
//String number;
//char incomingNumber[20];

//=========================================================================================================
void setup()
{
Serial.begin(9600);  
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
power_on();
delay(1000);              
Serial.println("Network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
            
delay(300);
Serial.println("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
delay(300);
     
sendATcommand("AT+CMGD=1", "OK", 1000); 
Serial.println("SEND SMS... TO READ");
}

//========================================================================================================
void loop()
{
answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);    // reads the first SMS
if (answer == 1)
{x=0;
memset(SMS, '\0', 100); 
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 asnwer
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, "light ON") != NULL)    
{
digitalWrite(8, HIGH);
answer = 1;
sendTextMessage(remoteNumber);
break;
}
                
delay (200);
                
}}

while(answer == 0);    // Waits for the asnwer with time out
SMS[x] = '\0';
Serial.print(SMS);   
delay(200); 
sendATcommand("AT+CMGD=1", "OK", 1000);   
delay(100); 
sendATcommand("AT+CMGD=1,4", "OK", 1000); 
delay(200); 
}
delay(1000);
  
}

void power_on()
{
uint8_t answer=0;
    

answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// 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 asnwer
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 asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));    

return answer;
}


void Signal()
{
answer = sendATcommand("AT+CSQ", "+CSQ", 2000);    // reads the first SMS
     
if (answer == 1)
{x=0;
memset(SMS, '\0', 300); 
answer = 0;
while(Serial.available() == 0);

do{
// if there are data in the UART input buffer, reads it and checks for the asnwer

if(Serial.available() > 0){    
SMS[x] = Serial.read();
x++;
       
if (strstr(SMS, "OK") != NULL)    
{
answer = 1;

}
}
}while(answer == 0);    // Waits for the asnwer with time out
SMS[x] = '\0';
Serial.println(SMS);
         
delay(100);
}
}    

void sendTextMessage(String remoteNumber) {
  Serial.print("sendTextMessage: ");
  Serial.print(remoteNumber);
  //Serial.println(message);
  Serial.println("AT + CMGS = \"" + remoteNumber + "\"");
  delay(100);
  Serial.print("Sent from my Arduino.");
  delay(100);
  Serial.println((char)26); // ASCII code of the ctrl+z is 26
  delay(100);
  Serial.println();
}

I tried to send the sms to the number inside my gsm.

From where? What number is stored in the GSM?

I would like the gsm to reply.

Generally a good idea.

{x=0;

NOTHING follows the { on the same line!

do{

Thereisnoexcuseforjammingcodetogethermakingithardtoread. Use your space bar!

What happened to using Tools + Auto Format? Removing all indenting did not help.

}}

NEVER!

that is the reason why i used remoteNumber.

Part of the SMS IS the number that it came from. You need to parse that from the SMS. I can't see that you are doing that.