Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: Using arduino as WOL switcher on: March 09, 2013, 08:23:26 pm
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
Code:
#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]) + "<br/>";
     }
     // 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("<br/>");
          client.println("</html>");
 }
Pages: [1]