/*
ARDUINOMATION RELAY CONTROL DEVICE
LAST UPDATED: 06/08/2014
*/
#include <SPI.h>
#include <Ethernet.h>
// VERSION NUMBER
float VersionNumber = 2.0;
// DEVICE ID - MUST BE UNIQUE
char deviceID[] = "100001";
// NETWORK SETTINGS
int port = 84;
char ipAddress[] = "192.168.1.241";
char macaddress[] = "0xDE:0xAD:0xBE:0xEF:0xFE:0xEE";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; //physical mac address
byte ip[] = { 192, 168, 1, 241 }; // DEVICE IP ADDRESS
IPAddress controllerserver(192,168,1,204); // CONTROL SERVER IP ADDRESS
EthernetServer server(port); //CONTROL SERVER PORT
EthernetClient client;
// CONTROL SERVER PARAMETERS
char updateurl1[] = "/projects/arduino/master_controller.php?command=checkin&deviceid=";
char updateurl2[] = "&ip=";
char updateurl3[] = "&mac=";
// DIGITAL OUTPUTS
String readString;
int output1 = 2;
int output2 = 3;
int output3 = 5;
int output4 = 6;
int output5 = 7;
int output6 = 8;
int output7 = 9;
// **************************** START CODE ****************************
void setup(){
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
pinMode(output5, OUTPUT);
pinMode(output6, OUTPUT);
pinMode(output7, OUTPUT);
// ETHERNET CONNECTION
Ethernet.begin(mac, ip); // , ip, gateway, subnet
server.begin();
//PRINT TO SERIAL
Serial.begin(9600);
Serial.println(" Device Controller Loading..."); // so I can keep track of what is loaded
Serial.print("IP Address: ");
Serial.print(Ethernet.localIP());
Serial.print(":");
Serial.println(port);
// CONNECT TO CONTROL SERVER
// Control server keeps track of all devices on the network
if (client.connect(controllerserver, 8888)) {
Serial.println("Connected to device server");
Serial.println("Updating System");
client.print("GET ");
client.print(updateurl1);
client.print(deviceID);
client.print(updateurl2);
client.print(ipAddress);
client.print(updateurl3);
client.print(macaddress);
client.println(" HTTP/1.0");
client.println();
client.stop();
Serial.println("Server updated");
} else {
Serial.println("Connection to server failed.");
client.stop();
delay(1000);
}
// EVERYTHING IS LOADED - WAIT FOR REQUESTS
Serial.println("Ready for requests...");
Serial.println();
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println("String:");
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Garage Control</TITLE>");
//client.println("<meta http-equiv="refresh" content="5">");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Garage Controller</H1>");
// READ SENSOR
int sensor = digitalRead(8);
// PRINT STATUS OF SENSOR
if (sensor == HIGH) {
client.println("Garage Open");
} else {
client.println("Garage Closed");
}
client.println("<br><br><a href='/?flashoutput5'>Open/Close Garage</a>");
client.println("<br><br><a href='/'>Refresh</a>");
//client.println(sensor);
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
//******* OUTPUT 5 GARAGE *************
//This one is Special for Garage.
if(readString.indexOf("flashoutput5") >0)
{
digitalWrite(output5, HIGH);
delay(500);
digitalWrite(output5, LOW);
Serial.println("Output 5 Flashed");
}
//******* OUTPUT 1 *************
if(readString.indexOf("highoutput1") >0)//checks for on
{
digitalWrite(output1, HIGH);
Serial.println("Output1 On");
}
if(readString.indexOf("lowoutput1") >0)
{
digitalWrite(output1, LOW);
Serial.println("Output1 Off");
}
//******* OUTPUT 2 *************
if(readString.indexOf("highoutput2") >0)//checks for on
{
digitalWrite(output2, HIGH);
Serial.println("Output2 On");
}
if(readString.indexOf("lowoutput2") >0)
{
digitalWrite(output2, LOW);
Serial.println("Output2 Off");
}
//******* OUTPUT 3 *************
if(readString.indexOf("highoutput3") >0)//checks for on
{
digitalWrite(output3, HIGH);
Serial.println("Output3 On");
}
if(readString.indexOf("lowoutput3") >0)
{
digitalWrite(output3, LOW);
Serial.println("Output3 Off");
}
//******* OUTPUT 4 *************
if(readString.indexOf("highoutput4") >0)//checks for on
{
digitalWrite(output4, HIGH);
Serial.println("Output4 On");
}
if(readString.indexOf("lowoutput4") >0)
{
digitalWrite(output4, LOW);
Serial.println("Output4 Off");
}
//******* OUTPUT 5 *************
if(readString.indexOf("highoutput5") >0)//checks for on
{
digitalWrite(output5, HIGH);
Serial.println("Output5 On");
}
if(readString.indexOf("lowoutput5") >0)
{
digitalWrite(output5, LOW);
Serial.println("Output5 Off");
}
//******* OUTPUT 6 *************
if(readString.indexOf("highoutput6") >0)//checks for on
{
digitalWrite(output6, HIGH);
Serial.println("Output6 On");
}
if(readString.indexOf("lowoutput6") >0)
{
digitalWrite(output6, LOW);
Serial.println("Output6 Off");
}
//******* OUTPUT 7 *************
if(readString.indexOf("highoutput7") >0)//checks for on
{
digitalWrite(output7, HIGH);
Serial.println("Output7 On");
}
if(readString.indexOf("lowoutput7") >0)
{
digitalWrite(output7, LOW);
Serial.println("Output7 Off");
}
Serial.println("--------------------------------------------------");
//clearing string for next read
readString="";
}
}
}
}
}