Go Down

Topic: Arduino LAN controlled Phone Switch (Read 734 times) previous topic - next topic

wildview

Aug 16, 2013, 10:31 pm Last Edit: Aug 16, 2013, 10:46 pm by wildview Reason: 1
This is an interface I put together with an Uno + LAN shield and a 2 Channel Relay shield to allow switching a phone line on/off at certain times of the day. The interface monitors the LAN IP address with data instructing the relay shield when to open/close the relays to control the phone line (Ring/Tip wires). This interface is controlled via the LAN port by a Micasa Verde automation controller. No more waking up early to phones ringing =)

Micasa Verde Commands:
luup.inet.wget("http://192.168.1.??:??/?1,3") // Relay 1&2 On
luup.inet.wget("http://192.168.1.??:??/?2,4") // Relay 1&2 Off

Arduino Code:
////////////////////////////////////////////////////////////////////////////////////////////
//
// R.Short
// Arduino Web Server for a 2 channel relay shield
// IP Address for Relay Shield http://192.168.1.??:??
// For use with Wiznet W5100 based Ethernet shields
//
//
// Relay IP control within Vera luup
// luup.inet.wget("http://192.168.1.??:??/?1") // Relay 1 On
// luup.inet.wget("http://192.168.1.??:??/?2") // Relay 1 Off
// luup.inet.wget("http://192.168.1.??:??/?3") // Relay 2 On
// luup.inet.wget("http://192.168.1.??:??/?4") // Relay 2 Off
//
////////////////////////////////////////////////////////////////////////////////////////////

// Initialize Headers
#include <SPI.h>
#include <Ethernet.h>

// Debug mode
#define DEBUG true
#define VERSION "Phone Relay Control Switch V1.0"

// Relay pin assignments
#define RELAY1 4
#define RELAY2 5

// Network configuration
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // physical mac address (Any unique MAC address will work)
IPAddress ip(192,168,1,??);     // LAN IP address
EthernetServer Relayserver(??); // Relay server port
EthernetClient client;
boolean reading = false;

//////////////////////
void setup()
{

 // Open Ardunio serial communications and wait for port to open:
 Serial.begin(9600);
 while(!Serial)
 {
   ; // wait for serial port to connect. Needed for Leonardo only
 }

 // Sign on
 if(DEBUG){ Serial.println(VERSION); }

 // Start Ethernet shield and report shield IP
 Ethernet.begin(mac, ip);
 if(DEBUG){ Serial.print("Ardunio Web Server is at IP - "); }
 if(DEBUG){ Serial.println(Ethernet.localIP()); }
 
 // Initialize relay pins
 pinMode(RELAY1, OUTPUT); //output relay pin
 pinMode(RELAY2, OUTPUT); //output relay pin

 // Set default to Relay On state
 digitalWrite(RELAY1, HIGH);
 digitalWrite(RELAY2, HIGH);

 // Give Ethernet shield time initialize:
 delay(1000);
}

//////////////////////
void loop()
{
   RelayControl();
}

//////////////////////
void RelayControl()
{
       // Listen for incoming clients
    EthernetClient client = Relayserver.available();
   if( client )
   {
               if(DEBUG){ Serial.println(""); }
               if(DEBUG){ Serial.println("New web client request"); }

       // An http request ends with a blank line
      boolean currentLineIsBlank = true;
      boolean sentHeader = false;

      while ( client.connected() )
      {
          if(client.available())
         {      
             if(!sentHeader)
            {
               // Send a standard http response header
               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type: text/html");
               client.println();
               sentHeader = true;
            }

            // Read IP data
            char c = client.read();
            if(reading && c == ' ') reading = false;
            if(c == '?') reading = true;   // found the ?, begin reading the info

            // Handle IP Request
            if(reading)
            {
                                       if(DEBUG){ Serial.print("LAN port data = "); }
                          if(DEBUG){ Serial.println(c); }
                                    switch (c)
               {
                  case '1': // relay pin 4 on
                                                 if(DEBUG){ Serial.println("Relay 1 = On"); }
                    digitalWrite(RELAY1, HIGH);
                    break;

                  case '2': // relay pin 4 off
                                                 if(DEBUG){ Serial.println("Relay 1 = Off"); }
                    digitalWrite(RELAY1, LOW);
                    break;

                  case '3': // relay pin 5 on
                                                 if(DEBUG){ Serial.println("Relay 2 = On"); }
                    digitalWrite(RELAY2, HIGH);
                    break;

                  case '4': // relay pin 5 off
                                                 if(DEBUG){ Serial.println("Relay 2 = Off"); }
                    digitalWrite(RELAY2, LOW);
                    break;
               }
            }

            if (c == '\n' && currentLineIsBlank)  break;

            if (c == '\n')
            {
               currentLineIsBlank = true;
            }
            else if (c != '\r')
            {
               currentLineIsBlank = false;
            }
         }
      }

      delay(1);      // give the web browser time to receive the data
      client.stop();      // close the connection:
   }
}


Go Up