Activate relays with UDP

Hi,

Sorry for my English, i will try my best.

I'm trying to activate/deactivate a set of relays using UDP commands, I saw some examples here in the forums, but my project didn't work, this is my code:

void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if(packetSize)
  {
  int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);  // read the packet into packetBufffer
    packetBuffer[read] = 0;
    char ch1 = packetBuffer[0];
    char ch2 = packetBuffer[1];
    char ch[] = { packetBuffer[0], packetBuffer[1], '\0'};
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write(ch);
      // Udp.write(ch1);
      // Udp.write(ch2);
    Udp.endPacket();

  if (ch == "10")
  {
    digitalWrite(PinA, HIGH);
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write("hello");
    Udp.endPacket();
  } 
  else if (ch == "11") {
    digitalWrite(PinA, LOW);
  }
  else if (ch == "20") {
    digitalWrite(PinB, HIGH);
  }
  else if (ch == "21") {
    digitalWrite(PinB, LOW);
  }
  else if (ch == "30") {
    digitalWrite(PinC, HIGH);
  }
  else if (ch == "31") {
    digitalWrite(PinC, LOW);
  }
  else if (ch == "40") {
    digitalWrite(PinD, HIGH);
  }
  else if (ch == "41") {
    digitalWrite(PinD, LOW);
  }
  else if (ch == "50") {
    digitalWrite(PinE, HIGH);
  }
  else if (ch == "51") {
    digitalWrite(PinE, LOW);
  }
  else if (ch == "60") {
    digitalWrite(PinF, HIGH);
  }
  else if (ch == "61") {
    digitalWrite(PinF, LOW);
  }
  else
    Serial.print("No Case Found for: ");
    Serial.print(ch);
    for (int x = 0; x < 8; x++){
      digitalWrite(x, HIGH);
    }
  }

  delay(15);                                                 // waits 15ms for udp input wait ...
}                                                            // end system run, but now loop and start system run again

Is only the loop, as you can see.

My ability programming is low.

The UDP response is only to see what is stored in the variables, the response shows the correct values. With a little trial and error I get the correct values in the UDP and nothing else, but it never do the if actions.

My board is an ARDUINO compatible and HIGH-LOW works inverse in this case.

Thanks for you help.

  if (ch == "10")

The address of the array is very unlikely to equal the string "10". You MUST use strcmp() to compare strings.

Ok,

Now my code looks like this, only a piece of code...

void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if(packetSize)
  {
  int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);  // read the packet into packetBufffer
    packetBuffer[read] = 0;
    char ch1 = packetBuffer[0];
    char ch2 = packetBuffer[1];
    char ch[] = { packetBuffer[0], packetBuffer[1], '\0'};
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write(ch);
    Udp.endPacket();

  if (strcmp( ch, "10") == 0 )
  {
    digitalWrite(PinA,HIGH);
  } 
  else if (strcmp( ch, "11") == 0 ) {
    digitalWrite(PinA,LOW);
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write("hello");
    Udp.endPacket();
  }

If I send 11 via UDP I get a hello via UDP but the relay don't turn on. Why?

Ok found the problem, my last else, there it turn off everything, and I can't see relays turn on, also for stability I need to increase the last delay from 15 to 20, the 15 make that only the first call was answered and then nothing, no port response.

This is my last code, for now...

#include <SPI.h>                                             // needed for Arduino versions later than 0018
#include <Ethernet.h>                                        // Ethernet board connected - Uses A0, A1, (D2 or D4), D10, D11, D12, D13
#include <EthernetUdp.h>                                     // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };         // MAC Address
IPAddress ip(192, 168, 1, 30);                               // IP Address
unsigned int localPort = 8888;                               // Local IP port to listen on
byte gateway[] = { 192, 168, 1, 1 };                         // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                        // Subnet Address

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];                   // buffer to hold incoming packet
EthernetUDP Udp;                                             // An EthernetUDP instance to let us send and receive packets over UDP

int PinA = 2;                                                // Switch Pin 1
int PinB = 3;                                                // Switch Pin 2
int PinC = 4;                                                // Switch Pin 3
int PinD = 5;                                                // Switch Pin 4
int PinE = 6;                                                // Switch Pin 5
int PinF = 7;                                                // Switch Pin 6
int PinG = 8;                                                // Switch Pin 7
int PinH = 9;                                                // Switch Pin 8

void setup() {                                               // run setup only once on boot

  Ethernet.begin(mac,ip);                                    // Enable Ethernet 
  Udp.begin(localPort);                                      // Start UDP Connection

  pinMode(PinA, OUTPUT);                                     // attach the pin
  pinMode(PinB, OUTPUT);                                     // attach the pin
  pinMode(PinC, OUTPUT);                                     // attach the pin
  pinMode(PinD, OUTPUT);                                     // attach the pin
  pinMode(PinE, OUTPUT);                                     // attach the pin
  pinMode(PinF, OUTPUT);                                     // attach the pin
  pinMode(PinG, OUTPUT);                                     // attach the pin
  pinMode(PinH, OUTPUT);                                     // attach the pin

  // Serial.begin(9600);                                        // Start Serial Output
  // Serial.write("SYSTEM BOOT");                               // Send Power on to serial

  for (int x = 2; x < 10; x++){
    digitalWrite(x,HIGH);
  }

}                                                            // end of setup

void loop()                                                  // Start Running System
{
  int packetSize = Udp.parsePacket();                        // if there's data available, read a packet
  if(packetSize)
  {
  int read = Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);  // read the packet into packetBufffer
    packetBuffer[read] = 0;
    // char ch1 = packetBuffer[0];
    // char ch2 = packetBuffer[1];
    char ch[] = { packetBuffer[0], packetBuffer[1], '\0'};
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write(ch);
    Udp.endPacket();

  if (strcmp( ch, "10") == 0 ) {
    digitalWrite(PinA,HIGH);
  } 
  else if (strcmp( ch, "11") == 0 ) {
    digitalWrite(PinA,LOW);
  }
  else if (strcmp( ch, "20") == 0 ) {
    digitalWrite(PinB,HIGH);
  }
  else if (strcmp( ch, "21") == 0 ) {
    digitalWrite(PinB,LOW);
  }
  else if (strcmp( ch, "30") == 0 ) {
    digitalWrite(PinC,HIGH);
  }
  else if (strcmp( ch, "31") == 0 ) {
    digitalWrite(PinC,LOW);
  }
  else if (strcmp( ch, "40") == 0 ) {
    digitalWrite(PinD,HIGH);
  }
  else if (strcmp( ch, "41") == 0 ) {
    digitalWrite(PinD,LOW);
  }
  else if (strcmp( ch, "50") == 0 ) {
    digitalWrite(PinE,HIGH);
  }
  else if (strcmp( ch, "51") == 0 ) {
    digitalWrite(PinE,LOW);
  }
  else if (strcmp( ch, "60") == 0 ) {
    digitalWrite(PinF,HIGH);
  }
  else if (strcmp( ch, "61") == 0 ) {
    digitalWrite(PinF,LOW);
  }
  else if (strcmp( ch, "70") == 0 ) {
    digitalWrite(PinG,HIGH);
  }
  else if (strcmp( ch, "71") == 0 ) {
    digitalWrite(PinG,LOW);
  }
  else if (strcmp( ch, "80") == 0 ) {
    digitalWrite(PinH,HIGH);
  }
  else if (strcmp( ch, "81") == 0 ) {
    digitalWrite(PinH,LOW);
  }
  }

  delay(20);                                                 // waits 15ms for udp input wait ...
}                                                            // end system run, but now loop and start system run again
1 Like

Now my code looks like this, only a piece of code...

Here's a piece of my answer. You need to...