Pages: [1]   Go Down
Author Topic: Arduino Web Control  (Read 15606 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wanted to make my home automated with ethernet control, so that i could turn on and off things in my house with either a phone or a pc.

so after spending almost three weeks of research, i found some code i could use. sadly the code didn't work in version 21, so i had to modify it slightly, to get it to work.

the code is based on the orginal ethernet shield (W/ Wiznet module)
and an Arduino duemilinove (2009). it will probably work on the UNO also =)

In the name of open source, i now share the code with you:

Code:
//*******************************

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

/*
Simple Ethernet Test
Arduino server outputs simple text to browser
and controlling LED with simple checkbox

The circuit:
* Arduino Duemilanove
* Arduino Ethernet shield
* LED connected to GND and digital pin 4 via resistor
By Minde
http://www.sciencprog.com/

**************************************************************
*  Edited by Haavard GJ to work in version 0021              *
*  Thanks to PaulS for the big help on getting this to work  *
**************************************************************
*/


 byte    mac[] =     { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
 byte    ip[] =      { 192, 168, 2, 110 };                     // ip in lan
 byte    gateway[] = { 192, 168, 2, 1 };                       // 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     heatpin = 5;               // Heating *relay* (change this if you want)
 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
 boolean HEATON = false;             // Heat status flag  (add more status flags if you need more outputs)
 

void setup()
{    
  Serial.begin(57600); //enable serial datada print
  Ethernet.begin(mac, ip, gateway, subnet); //start Ethernet
  
  pinMode(ledPin, OUTPUT); //Set pin 4 to output
  pinMode(heatpin, OUTPUT);
  Serial.print("Starting server"); //for debugging
  
  sensorValue = analogRead(sensorPin);
  Serial.println("Analog in:"); //for debugging
  Serial.println("");
  Serial.println (sensorValue);
  Serial.println("");
  Serial.println("");

}

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.concat(c); } //store characters to string
          
        Serial.print(c); //output chars to serial port for debugging
        

//indexOf("L=")

        if (c == '\n') { //if HTTP request has ended
        
          Serial.println("");
          Serial.println(readString); // print for debugging
          Serial.println("");
          int Le = readString.indexOf("L="); //here is a key component of where
          int He = readString.indexOf("H="); //the status is being read by the arduino
          Serial.print("L= position: ");
          Serial.println(Le);
          Serial.print("H= position: ");
          Serial.println(He);
          
          
          
          
          
          //lets check if LED should be lighted
          if (Le > 1){
          
          if (readString.substring(Le,(Le+3)) == "L=1") { //led has to be turned ON
              digitalWrite(ledPin, HIGH); // set the LED on
              Serial.println("ledpin paa");
              LEDON = true;
          }

          if (readString.substring(Le,(Le+3))== "L=0") {
            //led has to be turned OFF
            digitalWrite(ledPin, LOW); // set the LED OFF
            Serial.println("ledpin av");
            LEDON = false;
           }}
          
           if (He > 1){
            
          if (readString.substring(He,(He+3) == "H=1") { //heat has to be turned ON
              digitalWrite(heatpin, HIGH); // set the heat on
              Serial.println("heatpin paa");
              HEATON = true;
          }

          if (readString.substring(He,(He+3) == "H=0") {
            //heat has to be turned OFF
            digitalWrite(heatpin, LOW); // set the heat OFF
            Serial.println("heatpin av");
            HEATON = 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  ("<body style=background-color:white>"); //set background to white

  // HTML Code

  client.println("<font color='red'><h1>Arduino Remote control</font></h1>");//send first heading
  client.println("<hr />");
  client.println("<font color='blue' size='5[ch8242]>Analog input: "); //output some sample data to browser
  
  
  sensorValue = analogRead(sensorPin);


  client.print(sensorValue);//Analog input.
  Serial.print(sensorValue);

  client.println("<br />");//some space between lines
  client.println("<br />");
  client.println("<hr />");


  //controlling led via checkbox
  client.println("<h1>LED control</h1>");

  // address will look like http://192.168.xxx.xxx/?L=1 when submited
  // this line will give the radiobuttons that give the input
  client.println("<form method=get name=LED> <input type='radio' name='L' value='1'>LED ON<br><input type='radio' name='L' value='0'>LED OFF<br><input type='radio' name='H' value='1'>HEAT ON<br><input type='radio' name='H' value='0'>HEAT OFF<br><input type=submit value=submit></form>");

  client.println("<br />");
  //printing LED status
  client.print("<font size='5[ch8242]>LED status: ");
  if (LEDON == true) {
     client.println("<font color='green' size='5[ch8242]>ON");
     Serial.print("led on");
   }
   else {
    client.println("<font color='grey' size='5[ch8242]>OFF");
    Serial.println("Led off")
   }

 //printing LED status
  client.print("<font size='5[ch8242]>Heatpin status: ");
  if (HEATON == true) {
     client.println("<font color='green' size='5[ch8242]>ON");
     Serial.print("Heat on");
   }
   else {
    client.println("<font color='grey' size='5[ch8242]>OFF");
    Serial.println("Heat off");
   }



  client.println("<hr />");
  client.println("</body></html>");


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

client.stop(); //stopping client

Serial.println("stopped client"); // for debugging

}}}}}

just to have said it: the web page that is outputted looks crappy as hell, but if you can simple html you easily can change the look to something much better
 smiley-wink
« Last Edit: December 22, 2010, 02:56:12 am by haavardgj » Logged

Atlanta
Offline Offline
Sr. Member
****
Karma: 1
Posts: 254
Arduino still rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice!  When my work project is finished, ethernet and home automation is where i wanna go next.
Logged

Northern VA, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino is Cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Haavard,
  Nice job.  I have an ethershield I purchased on eBay.  I can control from "inside"my network from iPhone or wireless laptop.  I cannot find an easy "REAL DUMMIES(that's me  smiley)" guide to port forward the arduino to access from outside my network.  I can access the initial LED  page from the Arduino webserver, but when I try to change states from off to on, it says "cannot open page because the server stopped responding"  Any ideas?
TNX,
J.P.
« Last Edit: December 29, 2010, 09:34:48 am by jpmcg » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 149
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you have an android device you can go into this
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284455681
nice work!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@JPMcG: you can try to see http://portforward.com/

it could be smart to add a password box to prewent others to control your arduino =)

the next version i'm working on, im going to add a password box so that none others can axcess my arduino  smiley-wink
Logged

Northern VA, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino is Cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.  I've been to that site many times.  Downloaded their Port Forward Tool...no njoy.  I can get to it from my iPhone, or any other computer(wired or WiFi) that is on my network.  But still cannot get to it from 3G from outside my "cloud".  Thanks for the suggestions...I'll keep plinking away.
J.P.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what type of router do you have? i have not tryed to reach it outside the network but it can be that you have a dynamic ip-adress. if you have dynamic, then you can go to http://www.dyndns.com/ and get a free account so you can still acsess the network even if the ip have changed
Logged

Northern VA, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino is Cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Haavard,
  Thanks for your help.  I have a Linksys WRT54G.  Tried the DYnamic DNS website.  Have a logon just need to look at the site some more to figure how to make it do what I want. I am wondering if maybe Port 80 is blocked by Comcast.  the sketch has the IP and Port defined, with the Port in the sketch set at 80.  When I try the IP of my Cable Moden with the Arduino disconnected...it sends me tro the LOGin page for my router.  Plugging teh Arduino in lets me control from within my network.  When I try from just 3G on iPhone I receive Server not responding error.
J.P.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you shoud try to use some other port on your arduino... try to use for example 84 or something that is not used for the router instead, then it will not be an conflict with the port.

I think that may solve your problem.
Logged

Mercer, PA USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 55
Arduino & Led's Rock:-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

try port 8000 i know the local cable company here blocks 80 so we have used 8000

another thing you may have to set up is the port forwarding on the router

Here is a neat little free program i use that will check to see if you have a port open and working (i think they also have a list of all the routers and what settings you need to change)
http://portforward.com/help/portcheck.htm
Logged

Pages: [1]   Go Up
Jump to: