If loop problem

Hi I'm new to Arduino, i'm using the Arduino UNO.
While programming a bidirectionnal communication with Arduino and my phone (recieving sms and responding with the right answer. I'm having trouble with my loop, which means it's always just the "else " part that respond to the sms and never is the " if" part working..
Could you help me?

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>


#define PHONE_NUMBER "+33768522026"  
 
//The content of messages sent
#define MESSAGE1  "ceci est du francais"
#define MESSAGE2 "ceci est de l'anglais"

#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;

char phone[16];
char datetime[24];

#define PIN_TX    10
#define PIN_RX    11
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);

//DFRobot_SIM808 sim808(&Serial);

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  while(!sim808.init()) {
      Serial.print("Sim808 init error\r\n");
      delay(1000);
  }
  delay(3000);  
  Serial.println("Init Success, please send SMS message to me!");
 
 

  //******** define phone number and text **********
   //Serial.print("messageIndex: ");
   //Serial.println(messageIndex);
    while(!sim808.init()) {
      delay(1000);
      Serial.print("Sim808 init error\r\n");
  }  
  Serial.println("Sim808 init success");
  Serial.println("Start to send message ...");
   if (strcmp(message ,"bonjour") )
    {
    sim808.sendSMS(PHONE_NUMBER,MESSAGE1); 
    Serial.println("message1 envoyé");
    }
   else (strcmp(message ,"hello") ) ;
   Serial.println("message2 envoyé");
    {
    sim808.sendSMS(PHONE_NUMBER,MESSAGE2); 
    }
}
  


void loop() {
   //*********** Detecting unread SMS ************************
   
    //Serial.print("messageIndex: ");
    //Serial.println(messageIndex);

   //*********** At least, there is one UNREAD SMS ***********
   if (messageIndex > 0) { 
      sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
      Serial.println("Sim808 init success");
      Serial.println("Start to send message ...");
      if (messageIndex == "bonjour") 
        {
       sim808.sendSMS(PHONE_NUMBER,MESSAGE1); 
       }
      else ( messageIndex == "hello") ;
        {
        sim808.sendSMS(PHONE_NUMBER,MESSAGE2); 
        }
      
  //***********In order not to full SIM Memory, is better to delete it**********
      sim808.deleteSMS(messageIndex);
      Serial.print("From number: ");
      Serial.println(phone);  
      Serial.print("Datetime: ");
      Serial.println(datetime);        
      Serial.print("Recieved Message: ");
      Serial.println(message); 
   }
 }

‘messageIndex’ is an int, when you compare it to == "…" it will be false. you probably want to compare ‘message’ variable which you cannot compare using == either. use strcmp

1 Like

Thank you!

if (strcmp(message ,"bonjour") )

Like this? Because I tried like you said and still the if part is not working..

But in the method
bool readSMS (int messageIndex, char * message, int length, char * phone, char * datetime);
messageIndex is an integer number and cannot be "hello" or "bonjour".
Put
Serial.println(messageIndex); right after sim808.readSMS() and you will see what it is equal to.

Yes thanks, but in the loop i've replaced it by

message

Is it your code?

Oh sorry here is the new one:

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>


#define PHONE_NUMBER "+33768522026"  
 
//The content of messages sent
#define MESSAGE1  "ceci est du francais"
#define MESSAGE2 "ceci est de l'anglais"

#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
int messageIndex = 0;

char phone[16];
char datetime[24];

#define PIN_TX    10
#define PIN_RX    11
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);

//DFRobot_SIM808 sim808(&Serial);

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  while(!sim808.init()) {
      Serial.print("Sim808 init error\r\n");
      delay(1000);
  }
  delay(3000);  
  Serial.println("Init Success, please send SMS message to me!");
 
 

  //******** define phone number and text **********
   //Serial.print("messageIndex: ");
   //Serial.println(messageIndex);
    while(!sim808.init()) {
      delay(1000);
      Serial.print("Sim808 init error\r\n");
  }  
  Serial.println("Sim808 init success");
  Serial.println("Start to send message ...");
   if (strcmp(message ,"bonjour") )
    {
    sim808.sendSMS(PHONE_NUMBER,MESSAGE1); 
    Serial.println("message1 envoyé");
    }
   else (strcmp(message ,"hello") ) ;
   Serial.println("message2 envoyé");
    {
    sim808.sendSMS(PHONE_NUMBER,MESSAGE2); 
    }
}
  


void loop() {
   //*********** Detecting unread SMS ************************
   
    //Serial.print("messageIndex: ");
    //Serial.println(messageIndex);

   //*********** At least, there is one UNREAD SMS ***********
   if (messageIndex > 0) { 
      sim808.readSMS(messageIndex, message, MESSAGE_LENGTH, phone, datetime);
      Serial.println("Sim808 init success");
      Serial.println("Start to send message ...");
      if (strcmp(message ,"bonjour") )
        {
       sim808.sendSMS(PHONE_NUMBER,MESSAGE1); 
       }
      else (strcmp( message,"hello")) ;
        {
        sim808.sendSMS(PHONE_NUMBER,MESSAGE2); 
        }
   }   
  //***********In order not to full SIM Memory, is better to delete it**********
sim808.deleteSMS(messageIndex);
    
 }

Well then, print the message to the Serial Monitor and see what it is equal to.
Check its length, see if there is '\ n', '\ r' at the end.

1 Like

read documentation on strcmp, this will return false when there is exact match, you need

if (strcmp(message ,"bonjour") == 0)

1 Like

Alright, thank you

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.