Offline
Newbie
Karma: 0
Posts: 29
|
 |
« on: February 15, 2013, 02:21:41 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #1 on: February 15, 2013, 03:02:19 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #2 on: February 15, 2013, 06:49:02 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 26
Posts: 2450
|
 |
« Reply #3 on: February 16, 2013, 06:44:11 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #4 on: February 16, 2013, 12:21:17 pm » |
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  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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #5 on: February 16, 2013, 12:24:47 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #6 on: February 16, 2013, 12:51:15 pm » |
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...
|
|
|
|
« Last Edit: February 16, 2013, 01:06:06 pm by Malibux »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #7 on: February 16, 2013, 02:24:01 pm » |
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); } }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #8 on: February 16, 2013, 02:38:33 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #9 on: February 16, 2013, 02:57:37 pm » |
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
|
|
|
|
« Last Edit: February 18, 2013, 03:06:04 pm by Malibux »
|
Logged
|
|
|
|
|
|