Website light controlling problem

Hello,
I'm trying to control a bulb using ardunio + ethernet shield connected with relay
I hooked them up and everything is working properly to turn on/off the light while the arduino is the server using this code

//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
 
#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, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
 
String readString;
 
//////////////////////
 
void setup(){
 
  pinMode(6, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //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("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("
");
         
          client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
          client.println("<a href=\"/?lightoff\"\">Turn Off Light</a>
");        
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();
 
          ///////////////////// control arduino pin
          if(readString.indexOf("?lighton") >0)//checks for on
          {
            digitalWrite(6, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          else{
          if(readString.indexOf("?lightoff") >0)//checks for off
          {
            digitalWrite(6, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          }
          //clearing string for next read
          readString="";
 
        }
      }
    }
  }
}

Now what I want to do is to make arduino work as a client connected to my website to control it through a php webpage using this code for arduino (I made it as a combination of a several codes) and I was able to make connection to my website ip

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x3C, 0x0D };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(xxx,xxx,xxx,xxx);  // numeric IP for Google (no DNS)
IPAddress server(xxx,xxx,xxx,xxx);    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
 IPAddress ip(xxx,xxx,xxx,xxx);

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
 EthernetClient client;
String readString;
void setup() {
  // Open serial communications and wait for port to open:
   Serial.begin(9600);
    while (!Serial) {
     ; // wait for serial port to connect. Needed for Leonardo only
   }

   // start the Ethernet connection:
   if (Ethernet.begin(mac) == 0) {
     Serial.println("Failed to configure Ethernet using DHCP");
     // no point in carrying on, so do nothing forevermore:
     // try to congifure using IP address instead of DHCP:
     Ethernet.begin(mac, ip);
   }
   // give the Ethernet shield a second to initialize:
   delay(1000);
   Serial.println("connecting...");

   // if you get a connection, report back via serial:
   if (client.connect(server, 80)) {

     client.println();
     Serial.println("connected");
     client.println();
   } 
   else {
     // if you didn't get a connection to the server:
     Serial.println("connection failed");
   }
    pinMode(6, OUTPUT); 
}

void loop()
{
   // if there are incoming bytes available 
   // from the server, read them and print them:
   if (client.available()) {
    char c = client.read();
     Serial.print(c);
     if (readString.length() < 100) {
 
        readString += c;
     }                  
if(readString.indexOf("?lighton") >0){
  digitalWrite(6, HIGH);
  Serial.println("Led On");
}  
  else{
          if(readString.indexOf("?lightoff") >100)
          {
            digitalWrite(6, LOW);    
            Serial.println("Led Off");
          }
  }
 readString="";
   // if the server's disconnected, stop the client:
   if (!client.connected()) {
     Serial.println();
     Serial.println("disconnecting.");
     client.stop();

    while(true);
   }
   }
}

and using this php code uploaded to my website to control the light through 2 buttons but it didn't work, nothing happen with button click

<?php
 if(isset($_POST['select']))
{
    select();
}
else
{
    insert();
}
?>

<html>
<body>
<a href="/?lighton">Turn On Light</a>
<a href="/?lightoff">Turn Off Light</a>
       
</form>

<?php
function select()
{
 $what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);

echo '<'.$what_the_arduino_reads.'>';

}
function insert()
{
  $what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);

echo '<'.$what_the_arduino_reads.'>';

}
?>

I'm sure there is a certain problem with either arduino code or the php code or both cause I made them as a combination without specific knowledge specially with php

I would be very grateful if anyone can help me to get this thing done :slight_smile:

In your design, the Arduino needs to be the server. Trying to control things interactively with the Arduino as a client is not going to work.

Arduino ethernet shield is designed to work as a server or as a client so why not?!

and using this php code uploaded to my website to control the light through 2 buttons but it didn't work, nothing happen with button click

That PHP code makes something that a browser can display. What happens when the use clicks on a button? The PHP script gets called again. Calling the same script from the Arduino isn't going to tell you anything.

If that script wrote some data somewhere that another script read, and that other script was called by the Arduino, then the Arduino could set the state of the LED to match what it read. That script, though, doesn't do that, and can't communicate with the Arduino (as client) at all.

It could be modified so that the action event was to call the Arduino as server, and ask it for information. The Arduino as server could parse the request and determine what it needs to do.

I don't want the arduino to be the server coz I couldn't make my Ip address publicly accessible to control the light from any place in the world even with port forwarding (internet shit in Iraq) I tried everything but still nothing so I thought of making the arduino as a client connected to server (mywebsite) and upload a php webpage contains buttons when you click on it sends data to the client connected to the server (arduino) depending on the index of the webpage and the data stored in the string variable in arduino to make the light goes on/off but it looks like there is no data reaching the arduino even though it's connected to the server coz serial monitor is not displaying any incoming data.... one of the problems is php code (of course it's wrong) and maybe other problems so how to get this thing done ? I think it's possible
after finishing this I'm thinking of making a mobile app and pc app instead of this webpage to control the light from any place not just from the home network ... any other suggestions are welcomed I want to get this done in anyways XD

So, your web server, that your browser accesses needs to store the data from the user somewhere.

The Arduino as client calls a different script on the same server, periodically, and gets the information stored by the first script.

It isn't rocket science. The only difficulties will be determining where the first script (that the browser accesses) will store data so that the second script can access it, and knowing how often the Arduino as client should check for new data.

I can do it readily using this http://www.nearbus.net but it won't help me finishing the project by doing the mobile and pc apps
so I have to do it in the same way I'm trying to, However still don't know how to solve the problem but I don't think it's that difficult to get it done I need your help and other guys here correcting the codes to program it in the right way

up up

UN3RW4RT3T:
up up

Below is basic arduino client test code. The code gets the contents of a file on my external web account. The file could contain lighton or lightoff or what ever is desired. The arduino client code could be made to act upon what is in the downloaded file. The rest of your project questions might be better answered in a PHP forum or group.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

It's almost the same code I'm using :
Any other suggestions guys ?

Any other suggestions guys ?

Yes. Quit saying "that doesn't work".

You've been told what you need to do. The browser (a client) needs to tell the server to store the data somewhere. Are you doing that?

Then, the Arduino-as-client needs to tell the server to send it the data that is stored. Have you done that?

PaulS:

Any other suggestions guys ?

Yes. Quit saying "that doesn't work".

You've been told what you need to do. The browser (a client) needs to tell the server to store the data somewhere. Are you doing that?

Then, the Arduino-as-client needs to tell the server to send it the data that is stored. Have you done that?

First I'm not keep saying that it's not working, second I know what I need to do before you tell me what I should do... I made this topic to get help in the codes not the thoughts (As you see it's in the Programming Questions section) ... thanks for your help anyways :slight_smile:

I made this topic to get help in the codes not the thoughts

Which code(s) do you need help with? The browser is presumably not code YOU are writing. The browser executes some script on the server. We haven't seen that script, nor do we know where that script is supposed to store the data that the Arduino is supposed to read.

The Arduino as client needs to execute a script (on the same server) to get the stored data. We haven't seen that script, either.

None of that has anything to do with the Arduino.

The Arduino as client needs to do something, but you haven't shown the code on it, or explained what that code is doing that is not what you expect, nor have you explained exactly what it does.

So, it is hard to understand what you need help with (that is Arduino related).

Now, obviously, we could help you with the non-Arduino-related stuff, too, if your attitude was a little better.

Hi,
you can use arduino as a server that is listening for incomming connection and your php script can talk with it over tcp socket. It is not difficult and it is working very good for me. Look at chatserver example.