Search code examples
serial-portarduinouartarduino-ide

if equals not matched but should be. Aduino IDE Serial


I'm trying to get a raspberry pi communicating with an arduino over serial, hardware all is setup and there are some comms ok.

Problem I have is the arguments passed are not matched with a if, else if expression but really should be going off what is returned.

SerialCommand library handles what commands trigger what functions. We're only interested in sayHello() for this example. RaspberryPi sends "HELLO HELLO", which should receive "YES HELLO" but I always get "NO HELLO" suggesting no match on the arg HELLO. Obviously what argument was received is echoed back regardless and I receive back what should have matched, HELLO so I'm stumped at this stage, any suggestions?

EDIT: THanks to Thanushan Balakrishnan comment in his answer below, replacing

if(arg == "HELLO")

with the below fixes the problem.

if(strcmp("HELLO", arg) == 0)

_

// Demo Code for SerialCommand Library
// Steven Cogswell
// May 2011


// temp & Humidity
#include <DHT22.h>
#define DHT22_PIN 12
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);

// tank water level
int sensorValue = 0;
int constrainedValue = 0;
int tankLevel = 0;
#define TANK_SENSOR 0
#define TANK_EMPTY 0
#define TANK_FULL 1023


/*RelayBrd */
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 4;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 3;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 2;
boolean thisState = LOW;


#include <SerialCommand.h>
#define arduinoLED 13   // Arduino LED on board

SerialCommand sCmd;     // The demo SerialCommand object

void setup() {
  pinMode(arduinoLED, OUTPUT);      // Configure the onboard LED for output
  digitalWrite(arduinoLED, LOW);    // default to LED off

  //mySerial.begin(9600);
  //mySerial.println("Ready");

  Serial.begin(115200);

  // Setup callbacks for SerialCommand commands
  sCmd.addCommand("ON",   LED_on);          // Turns LED on
  sCmd.addCommand("OFF",  LED_off);         // Turns LED off

  sCmd.addCommand("getenv",   getEnv);
  sCmd.addCommand("relaybrd",   relayBrd);
  sCmd.addCommand("gettanklevel",   getTankLevel);
  sCmd.addCommand("setlouver",   setLouver);
  sCmd.addCommand("HELLO", sayHello);        // Echos the string argument back
  sCmd.addCommand("P",     processCommand);  // Converts two arguments to integers and echos them back
  sCmd.setDefaultHandler(unrecognized);      // Handler for command that isn't matched  (says "What?")
  Serial.println("Ready");
}

void loop() {
  sCmd.readSerial();     // We don't do much, just process serial commands

 // if (mySerial.available())
 //   Serial.write(mySerial.read());
 // if (Serial.available())
 //   mySerial.write(Serial.read());

}



void relayBrd(){

  char *arg;
  char *arg1;
  arg = sCmd.next();
  arg1 = sCmd.next();


  if (arg != NULL) {
    //int num = atol(arg);
    if (arg1 != NULL) {
       int state = atol(arg1);
       if (arg == "fan") {
          //do something when var equals 1
          registerWrite(1, state);

       }else if(arg == "water"){
          //do something when var equals 2
          registerWrite(2, state);

      }else if(arg == "mister"){
          //do something when var equals 2
          registerWrite(3, state);

      }else if(arg == "heater"){
          //do something when var equals 2
          registerWrite(4, state);

      }else{ 
          // if nothing else matches, do the default
          Serial.print("ERR got:");Serial.print(arg);Serial.println(":");
      }
    }else{
        Serial.println("ERR Relay state missing 1/0");
    }  

  }else{
    Serial.println("ERR Relay argument missing fan/water/heater/mister");
  }




}

// This method sends bits to the shift register:

void registerWrite(int whichPin, int whichState) {

     Serial.print("Relay ");Serial.print(whichPin);Serial.print(" set to ");Serial.println(whichState);
// the bits you want to send
  byte bitsToSend = 0;

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

}

void getEnv(){
  Serial.println("26 degC");
   DHT22_ERROR_t errorCode;
 Serial.print("Requesting data...");
  errorCode = myDHT22.readData();
  switch(errorCode)
  {
    case DHT_ERROR_NONE:
      Serial.print("Got Data ");
      Serial.print(myDHT22.getTemperatureC());
      Serial.print("C ");
      Serial.print(myDHT22.getHumidity());
      Serial.println("%");
      break;
    default:
    Serial.print("ERR DHT22 ");
    Serial.println(errorCode);
  }

}




void getTankLevel(){
  Serial.println("78% water level");
  sensorValue = analogRead( TANK_SENSOR );
  constrainedValue = constrain( sensorValue, TANK_EMPTY, TANK_FULL );
  tankLevel = map( constrainedValue, TANK_EMPTY, TANK_FULL, 0, 100 );

}

void setLouver() {
  char *arg;


  arg = sCmd.next();
  if (arg != NULL) {
    if(arg == "OPEN"){
         Serial.println("Louver OPEN");
    }else if(arg == "CLOSE"){
          Serial.println("Louver CLOSED");
    }else{
    Serial.print(arg);Serial.println(" not known");
    }
  }else{
    Serial.println("Louver command missing OPEN/CLOSE");
  }
}  

void LED_on() {
  Serial.println("LED on");
  digitalWrite(arduinoLED, HIGH);
}

void LED_off() {
  Serial.println("LED off");
  digitalWrite(arduinoLED, LOW);
}

void sayHello() {
  char *arg;
  arg = sCmd.next();    // Get the next argument from the SerialCommand object buffer

  if (arg != NULL) {    // As long as it existed, take it
    if (arg == "HELLO") {    // As long as it existed, take it
    Serial.print("YES ");
    Serial.println(arg);
    }else{
       Serial.print("NO ");
    Serial.println(arg);
    }
  }
  else {
    Serial.println("Hello Pi");
  }
}


void processCommand() {
  int aNumber;
  char *arg;

  Serial.println("We're in processCommand");
  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atoi(arg);    // Converts a char string to an integer
    Serial.print("First argument was: ");
    Serial.println(aNumber);
  }
  else {
    Serial.println("No arguments");
  }

  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atol(arg);
    Serial.print("Second argument was: ");
    Serial.println(aNumber);
  }
  else {
    Serial.println("No second argument");
  }
}

// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(const char *command) {
  Serial.println("What?");
}

Solution

  • In the following function arg is a pointer. So arg has the address of the memory which holds "HELLO". so you should check *arg == "HELLO"

    void sayHello()
    {
       char *arg;
       arg = sCmd.next();    // Get the next argument from the SerialCommand object buffer
    
       if (arg != NULL) {    // As long as it existed, take it
          if (strcmp("HELLO\n", arg) == 0) {         <-------------------------- HERE
             Serial.print("YES ");
             Serial.println(arg);
          }else{
             Serial.print("NO ");
             Serial.println(arg);
          }
       }
       else {
          Serial.println("Hello Pi");
       }
    }