Different action depending on the magicpacket

I have now programmed a code where i can switch on/off an LED with a by using magic packet(the code is below). In this context, I would like to hear if it is possible to programme the code so it switches on the LED with one mac adress in the magic packet and switches off the LED with another?

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 5, 104);

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

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

int switchPin = 6;
int ledPin = 4;
boolean lastButton = LOW;
boolean ledOn = false;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop() {

  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From IP : ");

    IPAddress remote = Udp.remoteIP();
    //print out the remote connection's IP address
    Serial.print(remote);

    Serial.print(" on port : ");
    //print out the remote connection's port
    Serial.println(Udp.remotePort());
    
      ledOn = !ledOn;
      lastButton = HIGH;
    }
    else
    {
      lastButton = digitalRead(switchPin);
    }
    digitalWrite(ledPin, ledOn);
    }

I don't see any "magic" packets. I see the Arduino receiving UDP packets.

The MAC address being used in that sketch is the MAC address of the Ethernet shield, not the MAC address of the packet sender.

The packet itself contains data that you are not printing/using. You could use the contents of the packet to make decisions. How depends on exactly what is in the packet.

okay my mistake.. thanks for telling me that im very new at programmering and all this stuff..

but is it possible to get a different action depending on which type of udp packet i send then or?

but is it possible to get a different action depending on which type of udp packet i send then or?

Yes, look at the udp read function in the ethernet library on the main site: http://arduino.cc/en/Reference/EthernetUDPRead. Unless you already know the content of your packets, a first step might be to simply send that content, suitably formatted, to the serial port.

wildbill:

but is it possible to get a different action depending on which type of udp packet i send then or?

Yes, look at the udp read function in the ethernet library on the main site: http://arduino.cc/en/Reference/EthernetUDPRead. Unless you already know the content of your packets, a first step might be to simply send that content, suitably formatted, to the serial port.

Thank you :slight_smile:

I have now tested the content of the packages i send and found out how send different packages. but how do i get the arduino to do different actions based on the content I send it?

The code I use is:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 5, 107);

unsigned int localPort = 7;      // 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[] = "acknowledged";       // a string to send back

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
      Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

The content I can see in the Seriel monitor looks like this:
Contents:

Contents:
ÿÿÿÿÿÿÞ-­¾ïþíÞ-­¾ïþíÞ-­¾ïþíL

The content I can see in the Seriel monitor looks like this:

Which doesn't even begin to look right. What is sending the packets?

i use a program called WOL- magic packet sender on my pc to send the packets but now i found another program which looks more right.. its called UDP Test tool 3.0

in this program i can type the message that i wont to send so now the seriel monitor looks like this:

From 192.168.5.101, port 52104
Contents:
hello data to send...

I would like to thanks all who answered me in this thread and helped me.. I have solved the problem now with using UDP instead of TCP

if anybody wanna use the code it can be seen below:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

int led = 4;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 5, 107);

unsigned int localPort = 7;      // 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[] = "The power is on";  // a string to send back
char  ReplyBuffer1[] = "The power is off";

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize > 100)
  {
    digitalWrite(led, HIGH);
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 21)
      {
        Serial.print(".");
      }
      
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);

    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
   delay(10);
  }
  if(packetSize > 115)
  {
    digitalWrite(led, LOW);
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer1);
    Udp.endPacket();
    
  delay(10);
  }
}

Why is this repeated ?

   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
    delay(10);

Checking the size of the packet seems a very imprecise way of doing what you want and the flow of the code is odd too.

  if(packetSize > 100)
  {
    digitalWrite(led, HIGH);

then later

  if(packetSize > 115)
  {
    digitalWrite(led, LOW);

So, turn on the LED and respond if the packet size is greater than 100 but turn it off again and respond again if it is greater than 115

Why is this repeated ?

  Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

Udp.write(ReplyBuffer);
   Udp.endPacket();
   delay(10);

Its not repeated exactly.. I have two different replaybuffers

char  ReplyBuffer[] = "The power is on";
char  ReplyBuffer1[] = "The power is off"

Checking the size of the packet seems a very imprecise way of doing what you want and the flow of the code is odd too.

Im very new to programming arduino so i just testing out some codes, but if you have a better and smart way to do it then ill be happy if you could help me out

So, turn on the LED and respond if the packet size is greater than 100 but turn it off again and respond again if it is greater than 115

Yeah but basically i just want it to switch on and off the LED by using either magicpackets or UDP. i would prefer Magicpackets but i haven't found any code for this
And instead of the respons i would like to be able to make a request where the arduino respond if the LED is ON or OFF