control a port via sms

how can i compare characters of incoming message to give a command on my arduino. I am using tinysine GSM Shield and i would like to send sms with specific characters to arduino for controling a pump

if (theCharacterThatIJustReceived == 'X')
{
//code to be executed when X is received
}

If you want to receive whole words then the characters need to be buffered in a NULL terminated array until a terminating character is received then you can use the strcmp() function to compare the whole word with a text string.

See http://gammon.com.au/serial

Do you think that seems to work like that ?? because i dont know what exactly meens smsbuffer and n

if(started){
//Read if there are messages on SIM card and print them.
if(gsm.readSMS(smsbuffer, 160, n, 20)=='X')
{
Serial.println(n);
Serial.println(smsbuffer);
sprintf(msgToSend,"Distance is %d cm",distance); //command sprintf
if (sms.SendSMS("00306955360192",msgToSend)) //command tha sends current height of tank
{
Serial.println("\nSMS sent OK");
}
delay(1000);

i dont know what exactly meens smsbuffer and n

Nor do we because you have not posted your whole program or details of the libraries that you are using. Where did you get the program and libraries from ?

What do you see in the Serial monitor when this line executes ?

Serial.println(smsbuffer);

i uploaded whole of my programm, my library is from site of tinysine about gsm shield and my commans is something like tha t you said "Serial.println (smsbuffer);

_7_oloklirwmeno_programma.ino (2.84 KB)

OK. Before we can deal with how to compare the input with something else we need to make sure that we are actually getting an input. So, do you see anything when you print smsbuffer ? Do you see any of the messages that the program prints ?

Where did you get the program ?
Do you get any messages when you compile it ?
Why did you install the SIM900.h and sms.h library files in the same folder as the program and not in the Arduino libraries folder ?

The programm is mine, i have written it. I downloaded the library for tinysine GSM shield and begun on this project to add my sensor HC-SR04 and some commands for my pump independ from sensor. the programm sends msg "arduino is ON" in the beggining, in the next step check height by the sensor and sends msg if the height is over 13cm or under 3cm. Also in the last step, i can send a msg to arduino and will be update about the height of my tank.
Until now the programm have not any fail in compile it works nice.
I upload one word file with fotos by serial monitor in few conditions if it can help you..

what is smsbuffer n.docx (444 KB)

The programm is mine, i have written it.

but in reply #2 you say

i dont know what exactly meens smsbuffer and n

I found your screenshots very confusing. Why have you sometimes excluded the output of smsbuffer and/or n ?
To help seeing what is going on please leave both on the program but add a text label such as
Serial.print("smsbuffer is <"); andSerial.print("n is <");before printing the values using Serial.print() and Serial.println(">"); after the values, but we do need to know whereabouts in the program the values are being printed so please also post the program that you used. The output is more convenient to read if you select and copy it from the Serial monitor and add it as a quote to your post here

+CMGL: 1,"REC UNREAD","+306955360192","","14/10/04,18:32:37+12"
LEVEL

OK

ATT: OK
RIC:
OK

n is <+306955360192

smsbuffer is <LEVEL

DEBUG:SMS TEST

so i have to compare smsbuffer with charachters

_7_oloklirwmeno_programma.ino (2.84 KB)

The program that you attached is not the one that produced the output because the text "sms buffer is <" does not appear in it.

We can, however, see that sms buffer contains the word LEVEL and 2 extra characters, possibly linefeeds and that n holds a telephone number. What message was actually sent ?

i m really sorry for my confuse, the programm that contains "smsbuffer is <" compressed in this message..
in this step, i tried to compare smsbuffer but it goes something wrong and it displays the folowing message
"ISO C++ forbids comparison between pointer and integer@

if(started){
//Read if there are messages on SIM card and print them.
if(gsm.readSMS(smsbuffer, 160, n, 20))
{
Serial.println(n);
Serial.println(smsbuffer);
sprintf(msgToSend,"Distance is %d cm",distance); //command sprintf
if (smsbuffer == 'off')
{
digitalWrite (pumpPin,LOW);
}
delay(500);
if (smsbuffer == 'on')
{
digitalWrite (pumpPin,HIGH);
}
if (sms.SendSMS("00306955360192",msgToSend)) //??????? ??? ??????? ??? ????????? ?????? ??? ????? ??????
{
Serial.println("\nSMS sent OK");
}
delay(1000);
}

_8_me_entoles_gia_tin_antlia_ino_ino.ino (2.96 KB)

I found a very good way to determine which of many commands I had received from a serial buffer. First, you capture the incoming data by adding it to String one character at a time, using, of course, a standard start and stop symbol. Now to distinguish between many commands, just create an integer from the first two characters of the command (requires that all commands are distinct in this respect). Then use a switch statement based on selecting the integer index of each command.

if (smsbuffer == 'off')Even without the error when compiling this would not work.
What are you seeing when you print smsbuffer ? When you received LEVEL you could see that there was more than that in smsbuffer because of the blank lines before the > delimiter. Also a char, denoted by the use of ' can only contain a single character not several of them.

So, what to do ?
The first thing is to ignore the suggestion to read the input into a String character by character because that is not how you are receiving it and using Strings on an Arduino can be a bad thing.

Have a look at the strcmp() function which allow you to compare strings (lowercase)

i have used strcmp() function in my programm. Firstly, with key[]="ON" and one IF was OK, but in the second time added key2[]="OFF and wasn't working only when i sent message ON. Instead of message "OFF" is working as it should be.

_10_dokimes_gia_9.ino (3.42 KB)

What are you seeing when you print smsbuffer ? Put < and > in as delimiters as before so that we can see where the text starts and ends.

In the first case that i send message "ON" display in serial monitor:
n is <+306955360192

smsbuffer is <ON

In the other case message with "OFF" display in serial monitor:
n is <+306955360192

smsbuffer is <OFF

_10_dokimes_gia_9.ino (3.54 KB)

where is possible to be a fault and not working only one of two strcmp() functions?

where is possible to be a fault and not working only one of two strcmp() functions?

There is clearly something wrong in your code - the code that you haven't posted.

#include "SIM900.h"
#include <SoftwareSerial.h>    // ???? ?? ?? ????????? (7) ???? ??????? ?? ??????? ?????? ??? ?? ?????? 
#include "sms.h"               // ??? ?? ?????????? ??????? ?? ????? ?? ??????????? ???????? ?? ??? ???? ????????? (7) ????? < & > ????????
SMSGSM sms;



//pin ??? HC-SR04
int trigPin= 10;
int echoPin = 11;


//??????? ???????
int pumpPin = 6;

//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
char key1[]= "ON";
char key2[]="OFF";

void setup() 
{
   //HC-SR04
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  
  //pump as an output
  pinMode(pumpPin,OUTPUT);
  
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Shield testing.");
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  if (gsm.begin(2400)){
    Serial.println("\nstatus=READY");
    started=true;  
  }
  else Serial.println("\nstatus=IDLE");
  
  if(started){
    //send an SMS.
    if (sms.SendSMS("00306955360192", "Arduino is ON"))
      Serial.println("\nSMS sent OK");
  }

};

void loop() 
{
  //????????? ?????? ??? serial monitor
  char msgToSend[20];             //command sprintf - convert int to char
  int duration,distance;
  digitalWrite(trigPin,HIGH); //????????????? ?? ?????????? ??????? HC-RO4
  delayMicroseconds(1000); //
  digitalWrite(trigPin,LOW);  
  duration = pulseIn(echoPin,HIGH);
  distance = (duration/2)/29.1;// ???? ??? ?????????? ??? ?????????
  Serial.print(distance);
  Serial.println("cm"); //????????? ???? ????? ??? ??????
  delayMicroseconds(500);
  
  if(started){
    //Read if there are messages on SIM card and print them.
    if(gsm.readSMS(smsbuffer, 160, n, 20))
    {
      Serial.print("n is <");
      Serial.println(n);
      Serial.println(">");
      Serial.print("smsbuffer is <");
      Serial.println(smsbuffer);
      Serial.println(">");
      sprintf(msgToSend,"Distance is %d cm",distance);     //command sprintf
       if (strcmp(key1,smsbuffer)==0)
       {
       digitalWrite (pumpPin,HIGH);
       }
       delay(500);
       if (strcmp (key2,smsbuffer)==0)
       {
         digitalWrite(pumpPin,LOW);
       }
       if (sms.SendSMS("00306955360192",msgToSend))         //??????? ??? ??????? ??? ????????? ?????? ??? ????? ??????     
       {
         Serial.println("\nSMS sent OK"); 
       }
       delay(1000);
     }
    if (distance >= 13)  // ??????? ??? ?? ??????? ? ?????? ??? ?? ?????????? ???? ?????????
      {
        digitalWrite (pumpPin,LOW);
        if (sms.SendSMS("00306955360192","High level, pump is OFF"))              
       {
        Serial.println("\nSMS sent OK"); 
       }
      delay(5000); 
      }
      if (distance <= 3) // ??????? ??? ?? ??????? ? ?????? ??? ?? ?????????? ???? ?????????
      {
      digitalWrite (pumpPin,HIGH);
      if (sms.SendSMS("00306955360192","Low level, pump is ON"))              
       {
        Serial.println("\nSMS sent OK"); 
       }
      delay(5000);
      
    }    
   }
   delay (2000);
  };

I am trying to control via sms a pump with word "ON" or "OFF" and i tried as said me in other post with strcmp() function.
But arduino not understand my word as a command.

      Serial.print("smsbuffer is <");
      Serial.println(smsbuffer);
      Serial.println(">");

The middle statement is NOT supposed to be using println.

What does actually get printed between the < and the > when you use print()?