Arduino Leonardo ETH fails to understand commands

Hi,

I’ve been browsing for some time trying to find what I’m doing wrong but so far I haven’t been able to find the problem.

I’m trying to send UDP strings (two very short strings, ON or OFF) to an Arduino Leonardo Eth. The strings should send a serial command to a projector, raise a temperature gauge and light a LED bulb tied to a relay module. This is all to simulate a boiler working for a museum.

The servo works correctly and so does the projector and LED when I try the code that controls them. However, the leonardo does not seem to understand the command strings.

I’m trying with hercules and I have a return of the string I send via UDP. Every time I send it I get the nack and then the correct trigger string so apparently it receives the correct string but does not seem to catch it in the “if” statement.

Could someone help me see where am I making the mistake?

Thanks for your help. The code is here:

#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo 
 
int pos = 0;    // variable to store the servo position 

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0x19, 0xE4
};
// the dns server ip
IPAddress dnServer(8, 8, 8, 8);
// the ip address
IPAddress ip(192, 168, 1, 188);
// the gateway address:
IPAddress gateway(192, 168, 1, 1);
// the subnet:
IPAddress subnet(255, 255, 255, 0);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "nack:";       // a string to send back
char ONreply[] = "poweringON";
char OFFreply[] = "poweringOFF";


// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  pinMode(7, OUTPUT);
 // start the Ethernet and UDP:
  Ethernet.begin(mac, ip, dnServer, gateway, subnet);
  Udp.begin(localPort);
  myservo.attach(5);  // attaches the servo on pin 5 to the servo object
  Serial1.begin(9600);    // configura el puerto serie a 9600bps
}

void loop() {
  // put your main code here, to run repeatedly:

int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    if (packetBuffer == "ON")
    {
      digitalWrite(7, HIGH);
      Serial.println("Recibido ON");
      for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
      {                                  // in steps of 1 degree 
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      } 
      Serial1.println("V99S0001\r"); //power on
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write(ONreply);
      Udp.endPacket();
     } else if (packetBuffer == "OFF")
      {
        digitalWrite(7, LOW);
        Serial.println("Recibido OFF");
        for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
         {                                
           myservo.write(pos);              // tell servo to go to position in variable 'pos' 
         }
        Serial1.println("V99S0002\r"); //Power off 
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write(OFFreply);
        Udp.endPacket();
       } else 
        {
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write(ReplyBuffer);
        Udp.write(packetBuffer);
        Udp.endPacket(); 
        }
    }

  
}

You can't compare cstrings like this

if (packetBuffer == "ON")

When using Cstrings you must use strcmp() to compare values rather than ==

...R