Saturday, March 2, 2013

Arduino Ethernet Relay Shield with Remote


Simple Ethernet Remote

This was a real quick project. I attached a relay shield and 315Mhz remote relay switch to the Seeeduino Ethernet to make a Ethernet remote control for lights and projector.



Future case, a dead ups

–Found two different programs, one very simple, one quite involved. Since this system only does lights (not security) I went with the simple one.
–Even so, there were problems with
           •Persistence, the gui did not read the state of the remote.
           •Did not support 8 relays (since the boards only have 4)
 –Used analog in as digital output
            •Crashed after a couple of commands.
–Just allocated more
            •Remote transmitter was not stable
–Used extra relay to power remote transmitter on and off


/*
    Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Seeedino Ethernet 
 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 modified 20 Jan 2013
 */
 
#include <SPI.h>
#include <Ethernet.h>
 
// 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,0,177);
 
// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
 #define  numPins 8
int pins[numPins+1] = { 4, 5, 6, 7,16,17,18,19,9};
int pinState[numPins+2] = {0};
char pinName[numPins][8]={
  "Reset",
  "Lite 1",
  "Lite 2",
  "Lite 3",
  "R1",
  "R2",
  "P1",
  "Power"
};
  
char line1[300];

void setup()
{
  for (int i = 0; i < numPins+1; i++)
  {
     pinMode(pins[i], OUTPUT);
  }
  digitalWrite(pins[numPins],0);
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println("initialization done.");
}

void loop()
{
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      readHeader(client);
      if (! pageNameIs("/"))
      {
        client.stop();  
        return;
      }
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();

      // send the body
      client.println("<html><body>");
      client.println("<h1>Output Pins</h1>");
      client.println("<form method='GET'>");  
      setValuesFromParams();
      setPinStates();
      for (int i = 0; i < numPins; i++)
      {
         writeHTMLforPin(client, i);
      }
      client.println("<input type='submit' value='Update'/>");
      client.println("</form>");
      client.println("</body></html>");

      client.stop();            
    }
  }
}

void writeHTMLforPin(EthernetClient client, int i)
{
  client.print("<p>");
  client.print(pinName[i]);
//  client.print(pins[i]);  
  client.print("<select name='");
  client.print(i);
  client.println("'>");
  client.print("<option value='0'");
  if (pinState[i] == 0)
  {
    client.print(" selected");
  }
  client.println(">Off</option>");
  client.print("<option value='1'");
  if (pinState[i] == 1)
  {
    client.print(" selected");
  }
  client.println(">On</option>");
  client.println("</select></p>");  
}

void setPinStates()
{
  for (int i = 0; i < numPins; i++)
     digitalWrite(pins[i], pinState[i]);
  digitalWrite(pins[numPins],1);
  delay(300);
  digitalWrite(pins[numPins],0);
  if((pinState[numPins-1])||(pinState[numPins-2])||(pinState[0]))
  {
    pinState[0]=0;
    pinState[numPins-1]=0;
    pinState[numPins-2]=0;
    delay(300);
    for (int i = 0; i < numPins; i++)
       digitalWrite(pins[i], pinState[i]);
    digitalWrite(pins[numPins],1);
    delay(300);
    digitalWrite(pins[numPins],0);
  }
}

void setValuesFromParams()
{
  for (int i = 0; i < numPins; i++)
  {
    pinState[i] = valueOfParam(i + '0');
  }
}

void readHeader(EthernetClient client)
{
  // read first line of header
  char ch;
  int i = 0;
  while (ch != '\n')
  {
    if (client.available())
    {
      ch = client.read();
      line1[i] = ch;
      i ++;
    }
  }
  line1[i] = '\0'; 
  Serial.println(line1);
}

boolean pageNameIs(char* name)
{
   // page name starts at char pos 4
   // ends with space
   int i = 4;
   char ch = line1[i];
   while (ch != ' ' && ch != '\n' && ch != '?')
   {
     if (name[i-4] != line1[i])
     {
       return false;
     }
     i++;
     ch = line1[i];
   }
   return true;
}

int valueOfParam(char param)
{
  for (int i = 0; i < strlen(line1); i++)
  {
    if (line1[i] == param && line1[i+1] == '=')
    {
      return (line1[i+2] - '0');
    }
  }
  return pinState[param-'0'];
}