Go Down

Topic: basic web on / off (Read 2529 times) previous topic - next topic

HI to all.,...

I'm new in Arduino's world and I don't have so much experience in programming and PHP, but I have a lot experience in electronic...I'm trying to create a basic web with some on / off buttons which connect or disconnect I/O in arduino...

I have the new one Arduino ethernet board (not the shield) and I was looking for in all the forums but was not easy to me to find this answer.


Kind regards.

zoomkat

Don't know if it will work with your board, but some simple web control code.

Code: [Select]

//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>

#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("<a href=\"/?on\"\">ON</a>");
          client.println("<a href=\"/?off\"\">OFF</a>");

          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="";

        }
      }
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

HI ZoomKAt


it's works for my board also....onky a question....instead to put "on / off" click....how can I put a "button"?


regards

Thanks...I'm using your code it works fine...


I tried it in my local network at home and I put on or off all the I/O remotely using mozzila......

Then I changed the server port by "80" and connected the board to my router....I opened port "80" and connected using my 3G usb Card....I can connect with the board, but when I try to get some data the computer is asking for 192.168.1.xxx (local IP) instead my public address....


can you help me?


Regards!

Arrch

So you're typing your public address into your web browser and what happens? What do you mean "the computer is asking for" your local IP?

zoomkat

Basically the same code with "button" type buttons:

Code: [Select]

//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("<br><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="";

        }
      }
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Zoomkat thanks for all the info....I'm thinking to move all to one PHP server.....

I installed Wammp server in y home and I'm trying to connect the board with the server...

Do you have a basic code for it?


Regards!

Targettio

How do you want to connect the server to the board, Serial or Ethernet?

If it's possible I want to connect it using TCP / IP...

the server will be in one room and maybe the board will be in a different place.



I installed yesterday Wammp server to create the web page but I don't know exactly how to connect it with the board.


TXS!

Targettio

Well you will need to
* connect the Arduino to the router/switch
* write an Arduino sketch which responds to HTTP GET (or POST) requests
* write a php file(s) or cgi script(s) which will interact with the Arduino (HTTP GET or POST) and lay that information out in a useful way

First you will need to decide a convection to work with eg:
http://arduino-ip/?7=1
(This turns pin 7 on.)

http://arduino-ip/?7=0
(This turns pin 7 off)

So next you need to write a script for the Arduino that will act as a server, and when it gets a request it breaks the request down to pull out the pinnumber and the status (0 or 1), the finally turns that pin on or off (as appropriate). Examples of this sort of sketch are around. You can test the Arduino sketch by manually typing the address in.

Finally you need to write the php or cgi scripts that will send these requests. Check out php/cgi guides online.

zoomkat

Quote
I installed Wammp server in y home and I'm trying to connect the board with the server...


I use the apache server, so I can't speak to your setup. Are you wanting to run a web server on a pc with the arduino directly connected to that pc via the USB connection?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Thanks,..,,,


Wammp install also apache + mysql +php.....


Regards.

Go Up