Im so close but, help! ethernet control

Hi there , after hundreds of google searches a cant find a simple straight forward answer to my problem.

I am making a home automation controller , have written al the code for that and all is good, then i thought it would be great to have controll over internet so i now have a freetronics etherten board it has the wiznet chip etc so practically an arduino board with an arduino ethernet shield all in one.

I have successfully been able to do a few of the examples in the ethernet librarys.

I want the arduino to be the server etc , i dont want to have it interfacing with this that and the next thing.

I have included the code of basically the server example below but with a button , could someone please show me how to change the code so when that button is pressed in the browser, maybe changes the state of an led conected to pin 4 of the arduino.

Then i think i would be able to figure the rest out.

Any help will be greatly apreciated.
cheers , corey

#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 };
byte ip[] = { 192,168,1, 177 };

Server server(80);

void setup()
{

Ethernet.begin(mac, ip);
server.begin();
}

void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();

if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(analogChannel));
client.println("
");
}

client.print(""); // this is the button i want to change the state of led

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);
// close the connection:
client.stop();
}
}

I want the arduino to be the server etc , i dont want to have it interfacing with this that and the next thing.

Something to try.

//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

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

String readString; 

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

void setup(){

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

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

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; 
          //Serial.print(c);
        } 

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

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

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println();  
             }
             else {
          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>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

AWESOME , thanks zoomkat

zoomkat:

I want the arduino to be the server etc , i dont want to have it interfacing with this that and the next thing.

Something to try.

//zoomkat 12-8-11

//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

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

String readString;

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

void setup(){

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

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

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;
          //Serial.print(c);
        }

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

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

//now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println(); 
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

client.println("");
          client.println("");
          client.println("Arduino GET test page");
          client.println("");
          client.println("");

client.println("

Zoomkat's simple Arduino button

");
         
          client.println("<a href="/?on" target="inlineframe">ON");
          client.println("<a href="/?off" target="inlineframe">OFF");

//client.println("<IFRAME name=inlineframe src="res://D:/WINDOWS/dnserror.htm" width=1 height=1">");
          client.println("<IFRAME name=inlineframe style="display:none" >");         
          client.println("");

client.println("");
          client.println("");
             }

delay(1);
          //stopping client
          client.stop();

///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

}
      }
    }
  }
}

Thanks heaps , got that working ,
How do i change it so they are buttons?, ones that say "on" and "off" on them

Thanks, corey

For button code, a google search for "button html tutorial" will supply info. Below is some test code that uses buttons. The code makes arduino pin 4 high/low, as well as move a servo between two positions.

//zoomkat 3-1-12
//simple button GET for pin and servo control
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

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

String readString; 

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

void setup(){

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

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo co
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

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; 
          //Serial.print(c);
        } 

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

          ///////////////
          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>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("
<input type=\"button\" value=\"ON\" onmousedown=\"location.href ('/?on');\"/>"); 
          client.println("<input type=\"button\" value=\"OFF\" onmousedown=\"location.href ('/?off');\"/>");        
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Thanks again zoomkat , one more question? please

i also have a text input field when i put number in like 1233 i get something like http://1.11.2.2.2/?form=1233
How do i the asign say "int analog" the value 1233.

Much apreciated , corey

Heres my test code

//*******************************

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

int analog ;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 10,1,1,177 }; // ip in lan
byte gateway[] = { 124,197,43,38 }; // the IP of the router or acsesspoint
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask (i dont think this is neccesary
Server server(80); // server port (change this if you are having a local webserver else than the arduino)

int ledPin = 4; // LED pin

int sensorPin = A0; // analog in 0 for testing
int sensorValue = 0; // integer for the analog sensor

String readString = String(30); // string for fetching data from address

boolean LEDON = false; // LED status flag

void setup()
{
Serial.begin (9600);
Ethernet.begin(mac, ip, gateway, subnet); //start Ethernet

pinMode(ledPin, OUTPUT); //Set pin 4 to output

analog = 100;
}

void loop(){

Client client = server.available(); // Create a client connection
if (client) {
while (client.connected()) {
if (client.available()) {

char c = client.read();

if (readString.length() < 30) { //read char by char HTTP request
readString += c; } //store characters to string

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

int stringthing = readString.indexOf("="); //here is a key component of where the status is being read by the arduino
Serial.println (readString);

if (stringthing > 1){

if (readString.indexOf ("on")>0) { //led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on

LEDON = true;
}

if (readString.indexOf( "off") >0) {
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF

LEDON = false;
}

}

client.println("HTTP/1.1 200 OK"); //output HTML data starting with standart header
client.println("Content-Type: text/html");
client.println();

client.print("LED . ");

client.print("LED status: ");

if (LEDON == true) {
client.println("ON");

}
else {
client.println("OFF");

}

client.print ("

");
client.print ("<form name=input action= method= get >");

client.print ("

");

client.print ( analog );

client.println("");

readString=""; //clearing string for next read

client.stop(); //stopping client

}}}}}

Below is one way to convert a readString like 1233 to a number.

int n = readString.toInt();  //convert readString into a number

zoomkat:
Below is one way to convert a readString like 1233 to a number.

int n = readString.toInt();  //convert readString into a number

Thanks zoomkat , but i cant seem to get it to work?
I tried putting in my code as
analog = = readString.toInt(); //convert readString into a number

when i first load it it says analog is 30 but then no matter what i enter it says its value is 0

Any ideas? , thanks heaps, corey

Any ideas?

Yes, you need to do some study on your own to figure out why the below are totally different things:

int n = readString.toInt();
analog =  = readString.toInt();
analog =  = readString.toInt();

[/quote]

Sorry that was a typo here, not im my code its of course

analog = readString.toInt();

So thats not the problem , thanks for your patience

Thanks to all

This is what i came up with

if (readString.substring(6,12)== "analog") // checks to make sure analog field submitted
{
newString =(readString.substring(13,17)); // gets number from string
analog = newString.toInt(); //convert newString into an int analog
}

works great :slight_smile: