Compare UDP Strings

Hi,
I'm a new programmer in Arduino's world and often use it in conjunction with Crestron.

I tried to read the Reference and the other topics, but maybe I haven't the necessary experience to understand them.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Definisco i parametri Ethernet

// MAC Address (media access control) della scheda Arduino Ethernet
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x90, 0xD5};  

byte ip[] = { 192, 168, 0, 62 };
// Definizione del Gateway, DNS e SubnetMask
byte gateway[] = {192, 168, 0, 101};
byte dnServer[] = {151, 99, 125, 2};
byte subnet[] = {255, 255, 255, 0};

// Definisco i parametri per la comunicazione UDP
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
int remoteIP = (192, 168, 0, 210);
int remotePort = (8888);
unsigned int localPort = 8888; // Definisco la porta di comunicazione
int ConnectionStatus = 0;

EthernetUDP Udp; // Istanza EthernetUDP per consentirci di ricevere e inviare pacchetti in UDP


void setup() {

// Definisco l'ingresso digitale "2" come INPUT, in quanto c'è collegato il pulsante
// che invierà la stringa
   
   pinMode(2, INPUT);  
  
// Inizializzo la comunicazione sulla porta seriale
  Serial.begin (9600);
// Inizializzo la scheda Ethernet
  Ethernet.begin(mac, ip, gateway, dnServer, subnet);

// print your local IP address:
  Serial.print("My IP address: ");
    Serial.print(Ethernet.localIP()); 
    Serial.println();
// Inizializzo la porta di comunicazione    
    Udp.begin(localPort);
}
void loop () {
      
    int packetSize = Udp.parsePacket();
    if(packetSize)
   {             
     Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer    
     Serial.println(packetBuffer);

Up to here, is all fine. I read on monitor the correct string I sent by Crestron to Arduino.

Now, I need to compare the string that I received by Crestron, with a fixed string in the code.

        delay(1000);
        Serial.println("Connecting...");
        if(packetBuffer == "Crestron connect") {
        ConnectionStatus = 1;
        delay(500);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("Arduino connect successfully");
        Udp.endPacket();
        Serial.println("Connected");
   }
   else {
       ConnectionStatus = 0;
       Serial.println("Connection failed");
       delay(1000);
     
  }
 }
}

I have no errors, but the code always makes the condition with "ConnectionStatus = 0".

Where am I wrong?

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

        if(packetBuffer == "Crestron connect") {

You are comparing the address of the array and the address of the liternal string. Of course, they aren't the same.

Use strcmp() to compare the contents of the array and the literal string.

if(strcmp(packetBuffer,"Crestron connect"))

Is the syntax correct?

Because, while not having errors, the code doesn't work correctly.

Change it to:

if(strcmp(packetBuffer,"Crestron connect"==0))

Yes!! :slight_smile:

Thank you all!!

Change it to:

Or:

if(strcmp(packetBuffer,"Crestron connect")==0)

Oops. Thanks.