Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: Using arduino as WOL switcher on: February 15, 2013, 12:23:09 pm
It fails in the "UdpRaw.h" I have been on google for some hours now and think that it is because the "UdpRaw" is something old that is not used anymore in the "new" arduino 1.0.

I have det Arduino R3 and the Ethernet Shield R3.
2  Using Arduino / Networking, Protocols, and Devices / Using arduino as WOL switcher on: February 15, 2013, 08:42:10 am
Hey I'm new at using arduino so hope you all can help me.

I have found this code:
Code:
// Wake On Lan ( MagicPacket ) Repeater & Switcher

// Wake On Lan Repeater & Remote Power Button Control
// Support forced release.
// http:yutakalifenet.up.seesaa.net/html/WOLRepSw.html
// Digital 2, output for relay

#include <WString.h>

#include <Ethernet.h>

#include <UdpRaw.h>

byte TargetMac[] = { 0x00,0x00,0x00,0x00,0x00,0x00 }; // set a MAC for Target PC

// ETHERNET CONFIGURATION
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address

byte ip[] = { 192, 168, 1, 200 }; // Arduino のIP address
byte gw[] = { 192, 168, 1, 1 };   // Gateway IP address

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

// set a target broadcast address
byte targetIp[] = { 192, 168, 1, 255}; // set this value like "x.x.x.255"

int targetPort = 8000; // target port

#define MAX_SIZE 192         
// maximum packet size
byte packetBuffer[MAX_SIZE]; // buffer to hold incoming packet
int packetSize;              // holds received packet size

byte remoteIp[4];            // holds recvieved packet's originating IP
unsigned int remotePort;     // holds received packet's originating port

int i;

int verification;


void setup() {
    Ethernet.begin(mac,ip,gw);
    UdpRaw.begin(localPort);
    pinMode(2, OUTPUT); // initialize D2 pin as an output
    Serial.begin(9600);
}

void loop() {

    // to forward a packet, if incoming packet is available

    if(UdpRaw.available()) {
        packetSize = UdpRaw.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);
     
        Serial.print("Received packet of size ");
        Serial.println(abs(packetSize));

        Serial.print("From IP ");

        for(i=0; i<3; i++) {
            Serial.print(remoteIp[i],DEC);
            Serial.print(".");
        }
        Serial.print(remoteIp[3],DEC);

        Serial.print(" Port ");
        Serial.println(remotePort);

        if(packetSize < 0) {
            // if return value <0 the packet was truncated to fit into our buffer

            Serial.print("ERROR: Packet was truncated from ");
            Serial.print(packetSize*-1);
            Serial.print(" to ");
            Serial.print(MAX_SIZE);
            Serial.println(" bytes.");
        }

        Serial.println("Contents:");

        for(i=0; i<min(MAX_SIZE,abs(packetSize)); i++) {
            Serial.print(packetBuffer[i],HEX);
            Serial.print(" ");
        }
        Serial.println("");

        for(i=6; i<102; i++) {
            int j = abs((102 - i) % 6 - 6);
            if ( j == 6 ) j = 0;
            if (TargetMac[j] == packetBuffer[i]) verification++;
            if (j == 0) {
                if(packetBuffer[i] == 0xFF) verification += 2;
            }
        }

        // if first byte of Target MAC is 0xFF, relay is ON for 10sec

        if(verification>=112){
            Serial.println("RELAY ON for 10000ms");
            digitalWrite(2, HIGH); // relay is ON
            delay(10000);           // wait for 10sec

            digitalWrite(2, LOW);
        }else{
            // if receive MagicPacket to Target PC, then relay is ON for 1sec
            if(verification>=96){
                Serial.println("RELAY ON for 1000ms");
                digitalWrite(2, HIGH); // relay is ON

                delay(1000);           // wait for 1sec
                digitalWrite(2, LOW);
            }
        }

        // send a magic packet
        UdpRaw.sendPacket(packetBuffer,packetSize,targetIp,targetPort);
        Serial.println("Start port forwarding to broadcast address:");
        for(i=0; i<3; i++) {
            Serial.print(targetIp[i],DEC);
            Serial.print(".");
        }
        Serial.print(targetIp[3],DEC);
        Serial.println("");
        Serial.println("Done!");
    }
    verification = 0;
    // wait a bit

    delay(10); 
}

But it fails when i want to send it to the arduino.

I also want to modify it a little. I would like it to listenfor the magic packet, when it recieves the on for making pin 2 go high it has to check the state of Pin2 if it is LOW the go HIGH, if it is HIGH then go low.

Hope that you can help me =)
Pages: [1]