Using arduino as WOL switcher

Hey I'm new at using arduino so hope you all can help me.

I have found this 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 =)

1 Like

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

How?

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.

It fails in the "UdpRaw.h"

Starting to sound like a broken record here. How?

Post YOUR code, a link to the library site, and the error messages.

Hello,
I want to be able to send an HTTP request to my external IP, and have it port forwarded through my router to my Arduino. My Arduino will parse the request, and Either send a UDP Magic Packet To Wake On Lan (WOL) my Computer, or read the temperature from a temp sensor. I am still in internal testing stage, and cannot manage to get the arduino to respond to anything after the first request ... Any suggestions? I think maybe the UDP is conflicting with the Ethernet Server?
-Chris

#include <dht11.h>
#include <Versalino.h>
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
byte ARDUINOmac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ARDUINOip(192,168,1,88);
IPAddress ARDUINOgateway(192,168,1,1);
IPAddress ARDUINODNS(208,067,220,220);
IPAddress ARDUINOsubnet(255,255,255,0);
EthernetServer server(80);
EthernetClient client;
EthernetUDP Udp;
char c;
String WholeRequestString;
boolean currentLineIsBlank;
const int analogInPin = 8;
const int analogOutPin0 = A0;
const int RebootPin14 = A14;
const int RebootPin15 = A15;
dht11 DHT11;
 
void setup() 
{
  DHT11.attach(2);
  // Open serial communications and wait for port to open:
  // start the Ethernet connection and the server:
   Serial.begin(9600);
   Ethernet.begin(ARDUINOmac, ARDUINOip, ARDUINODNS, ARDUINOsubnet);
   server.begin();
 }
 
void loop() 
{
    ListenForEthernetClients();
 }
 
 void ListenForEthernetClients()
 {
    client = server.available();
     if (client) 
     {
        // an http request ends with a blank line
        currentLineIsBlank = true;
        while (client.connected()) 
        {
          if (client.available()) 
          {
            c = client.read();
            WholeRequestString+=c;
            // if you've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so you can send a reply
            if (c == '\n' && currentLineIsBlank) 
            {
              AnalyzeRequest(WholeRequestString);
              WholeRequestString = "";
              break;
            }
            if (c == '\n') 
            {// you're starting a new line
              currentLineIsBlank = true;
            } 
            else if (c != '\r') 
            {// you've gotten a character on the current line
              currentLineIsBlank = false;
            }
          }
        }
        // give the web browser time to receive the data
        delay(1);
        if (!client.connected())
        {
          WholeRequestString = "";
          currentLineIsBlank = false;
          c= 'A';
        }
     }
     WholeRequestString = "";
     c='A';
 }
//*********************************************************************************************
//*********************************************************************************************
 void AnalyzeRequest(String requestString)
 {
     analogWrite(analogOutPin0,200);
     if (requestString.indexOf('?'>0))
     {
       
         if ((WholeRequestString.substring(WholeRequestString.indexOf('?')+1, WholeRequestString.indexOf('?')+ 16) == "xxxxxxxxxxxxxxx")&&(WholeRequestString.substring(WholeRequestString.indexOf('?')+16, WholeRequestString.indexOf('?')+ 22) =="REBOOT"))
         {//192.168.1.88?xxxxxxxxxxxxxxxREBOOTChristopherUltimate
             String ComputerNameString = WholeRequestString.substring(WholeRequestString.indexOf('?')+22, WholeRequestString.indexOf('HTTP') - 3);
             WholeRequestString = "";
             SendResponseReboot(ComputerNameString);
             delay(9000);
         }
         else if ((WholeRequestString.substring(WholeRequestString.indexOf('?')+1, WholeRequestString.indexOf('?')+ 16) == "xxxxxxxxxxxxxxx")&&(WholeRequestString.substring(WholeRequestString.indexOf('?')+16, WholeRequestString.indexOf('?')+ 27) == "TEMPERATURE"))
         {//192.168.1.88?xxxxxxxxxxxxxxxTEMPERATURE
              WholeRequestString = "";
              SendResponseTemperature();
         }
         else
         {
             WholeRequestString = "";
             SendResponseNegative();
         }
         client.flush();
     }
     analogWrite(analogOutPin0,0);  
     for (int c=0; c<6; c++)
     {
       analogWrite(analogOutPin0,200);
       delay(500);
       analogWrite(analogOutPin0,0);
       delay(500);
     }
 }
//*********************************************************************************************
//*********************************************************************************************
 void SendResponseReboot(String ComputerNameString)
 {
     Udp.begin(88);
     for (int c=0; c<4; c++)
     {
       analogWrite(analogOutPin0,200);
       delay(100);
       analogWrite(analogOutPin0,0);
       delay(100);
     }
     //String MagicPacketSent = SendMagicPacketChristopherUltima(client, ComputerNameString); 
     byte wolMac[] = { 0x6C,0xF0,0x49,0x73,0xDE,0x50 };
     String stringToReturn;
     int a=0;
     for (a=0;a<6;a++)
     {
       stringToReturn+=word(wolMac[a]);
     }
     IPAddress wolIP(192,168,1,21);
     int wolPort = 88;
     //WakeOnLan::send(wolMac,8888,Udp);//ATTEMPT 1
     byte all[102];
     int b,i,c1,j=0;
     for(i = 0; i < 6; i++,j++)
     {  
       all[j] = 0xFF;
     }
     for(i = 0; i < 16; i++)
     {  
       for( c1 = 0; c1 < 6; c1++,j++)    
       all[j] = wolMac[c1];
     }
     //asssemble string to return
     for (b=0;b<102;b++)
     {
       stringToReturn+=word(all[b]) + "
";
     }
     // send a standard http response header
     client.println("HTTP/1.1 200 OK");
     client.println("Content-Type: text/html");
     client.println("Connection: close");
     client.println("<!DOCTYPE HTML>");
     client.println("<html>");
     client.println("We're sending the signal to restart" + ComputerNameString);
     client.println("MagicPacketLikeThis" + stringToReturn);
     client.println("</html>");
     for (int c=0; c<4; c++)
     {
       analogWrite(analogOutPin0,200);
       delay(100);
       analogWrite(analogOutPin0,0);
       delay(100);
     }
     
     //Udp.sendPacket(all,102,wolIP,wolPort);
     Udp.beginPacket(wolIP, wolPort);
     for (b=0;b<102;b++)
     {
       Udp.write(all[b]);
     }
     Udp.endPacket();
     Udp.stop();
     for (int c=0; c<5; c++)
     {
       analogWrite(analogOutPin0,200);
       delay(200);
       analogWrite(analogOutPin0,0);
       delay(200);
     }
 }
//*********************************************************************************************
//*********************************************************************************************
 void SendResponseTemperature()
 {
         for (int c=0; c<4; c++)
          {
            analogWrite(analogOutPin0,200);
            delay(100);
            analogWrite(analogOutPin0,0);
            delay(100);
          }
   // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("Read sensor: ");
          int chk = DHT11.read();
          switch (chk)
          {
            case 0: Serial.println("OK"); break;
            case -1: Serial.println("Checksum error"); break;
            case -2: Serial.println("Time out error"); break;
            default: Serial.println("Unknown error"); break;
          }
          client.print("Humidity (%): ");
          client.println((float)DHT11.humidity, DEC);    
          client.print("Temperature (°C): ");
          client.println((float)DHT11.temperature, DEC);    
          client.print("Temperature (°F): ");
          client.println(DHT11.fahrenheit(), DEC);  
          client.println("
");
          client.println("</html>");
 }

Any suggestions?

Dump the Strings. Learn to use strings.

I am still in internal testing stage, and cannot manage to get the arduino to respond to anything after the first request ... Any suggestions?

Start with simple code that works for various parts of your project, then start combining the code so you might see what breaks the code.