Arduino Ethernet + Code

Not sure if this is the more appropriate section as its still in progress (reposted from Home Automation, couldn't find a delete button)

Hi Guys,

Been working for a few weeks on a project to turn on/off some power points via relays and using the EtherTen's ethernet features, so to start with I started with a basic bit of code to start a webserver on the arduino with a simple bit of text to click on and off the lights. However I didn't want to use the actual arduino hosted page due to its not so pretty looks, so I hosted a website on my XAMPP server that looks a bit nice and created a link to the arduino ie 192.168.2.102:84/?on1. The problem was that it navigated away from my pretty page and after three days I couldn't get an iframe to work so I have to use Jquery and JSON, see code below.

The XAMPP side code

<span style="float:right;padding-top:10px;padding-right:12px;font-size:18px; font-weight:bold;"><span onclick="javascript:{$.get('http://192.168.2.102:84/?on2');}">ON</span> | <span onclick="javascript:{$.get('http://192.168.2.102:84/?off2');}">OFF</span></li></span>

The Ardunio Code

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 2, 102 }; // ip in lan
byte gateway[] = { 192, 168, 2, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  pinMode(2, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("SFAP Running"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  Client 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; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          ///////////////////// control arduino pin
          if(readString.indexOf("on4") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          
          if(readString.indexOf("on2") >0)//checks for on
          {
            digitalWrite(2, HIGH);    // set pin 4 high
            Serial.println("Led 2 On");
          }
          if(readString.indexOf("off2") >0)//checks for off
          {
            digitalWrite(2, LOW);    // set pin 4 low
            Serial.println("Led 2 Off");
          }
          
          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Jotaris");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: application/json");
          client.println();

	client.print("{\"content\":\"");
	if(readString.indexOf("on2")>0)
		{
		client.print("Switch 1 = ON");
		}
	else if(readString.indexOf("off2")>0)
		{
		client.print("Switch 1 = OFF");
		}
        else
                {
                client.print("Switch 1 = UNKNOWN");
                }
	client.print("; ");
	if(readString.indexOf("on4")>0)
		{
		client.print("Switch 2 = ON");
		}
	else if(readString.indexOf("off4")>0)
		{
		client.print("Switch 2 = OFF");
		}
        else
                {
                client.print("Switch 2 = UNKNOWN");
                }
	client.print("\"}");

             }

          delay(1);
          //stopping client
          client.stop();
          
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

The questions are...

  1. Is there a way to for me to read the state of the relay (its not double pole so it would have to remember if its on or off) and request the information from my XAMPP site so when I go the page it can tell me if my lights are on?

  2. I have a PIR that I would like to activate one of the relays by motion sensing as well as by my website? Ie I have a light outside plugged into one of the powerpoints with a relay and I would like it to turn on when I walk past. As well as being able to manually turn in on?

I can run a mysql database and php off my server to hold values if there is a way for the arduino to read commands from a database. That way the DB could remember the relay state.

Idea's and thoughts would be much appreciated.

If it were me, I would just have the arduino store the state of the relay, just declare a value and set it to the initial state in your setup. Then every time you change the state flip the value. Create a new URL endpoint to get the value of the variable and return the state in the http response. You could also use Ajax to refresh the page at a set interval so the state show on the web page will automically refresh.

Hello :slight_smile:
so did you succeeded to read the state of the pin/relay and activate the pin/relay from the sensor ?