Go Down

Topic: Uno + Ethernet Shield to PHP (Read 17330 times) previous topic - next topic

eprilya


Enough tap dancing. Your Arduino is either a client or a server. There are some things it can do as a client that it can't do as a server. There are some things it can do as a server that it can't do as a client. Post your Arduino code so we can tell which it is, since you won't directly answer the question.

I get the impression, though, that you think that a client on one machine can "see" what a browser on another machine has rendered. That is NOT possible. For any client or any browser.


Here is my Arduino code to knows that the arduino and user room page are connected
Code: [Select]
#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// 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(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "192.168.0.2/TA/user_room.php";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,2);
byte gateway[] ={ 192, 168, 0, 1 };

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

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, gateway);
  }
  // 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)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("HOST: 192.168.0.2/TA/user_room.php");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

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 the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}


Then the serial monitor shows:

connecting...
connected

disconnecting.

How about this?

SurferTim

These are incorrect.
Code: [Select]
char server[] = "192.168.0.2/TA/user_room.php";
client.println("HOST: 192.168.0.2/TA/user_room.php");

Use these instead.
Code: [Select]
IPAddress server(192,168,0,2);
client.println("Host: 192.168.0.2");


PaulS

Code: [Select]
char server[] = "192.168.0.2/TA/user_room.php";    // name address for Google (using DNS)
Does that completely useless, incorrect comment really serve any purpose?

Code: [Select]
    client.println("GET /search?q=arduino HTTP/1.1");
After connecting to some bogus server (or trying to, anyway), does this GET request really make sense? It's fine if you are talking to google. It's useless when you try to talk to some other server.
The art of getting good answers lies in asking good questions.

eprilya


Code: [Select]
char server[] = "192.168.0.2/TA/user_room.php";    // name address for Google (using DNS)
Does that completely useless, incorrect comment really serve any purpose?

Code: [Select]
   client.println("GET /search?q=arduino HTTP/1.1");
After connecting to some bogus server (or trying to, anyway), does this GET request really make sense? It's fine if you are talking to google. It's useless when you try to talk to some other server.


I made a new code. For the example, I tried to control led using checkbox on my php. Serial monitor shows that the arduino and php are connected. But when I click the checkbox, nothing happens with the led.

Arduino code:
Code: [Select]
#if ARDUINO > 18
#include <SPI.h> // needed for Arduino versions later than 0018
#endif
#include <Ethernet.h>
#include <TextFinder.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 2 }; // my php web
byte gateway[] ={ 192, 168, 0, 1 }; // router ip
byte subnet[] ={ 255, 255, 255, 0 };
//byte server[] = {209,85,229,147 }; // google
char server[] = "192.168.0.2/TA/user_room.php"; 
EthernetClient client;
TextFinder finder( client );
float value;

void setup()
{
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, gateway);
  }
  // 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)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /192.168.0.2/TA/coba_led.php?LED2=2 HTTP/1.1"); //the ip when checkbox was clicked
    //client.println("HOST: 192.168.0.2/TA/user_room.php");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
EthernetClient client;
if (client) {
TextFinder finder(client );
while (client.connected()) {
if (client.available()) {
// counters to show the number of pin change requests
int digitalRequests = 3; // output pin
if( finder.find("GET /") ) {
// find tokens starting with "pin" and stop on the first blank line
while(finder.findUntil("2", "\n\r")){ //value of led at button
char type = client.read();
int pin = finder.getValue();
int val = finder.getValue();
if( type == '2') {
Serial.print("Digital pin ");
pinMode(3, OUTPUT);
digitalWrite(pin, HIGH);
digitalRequests++;
}
else {
Serial.print("Unexpected type ");
Serial.print(type);
}
Serial.print(pin);
Serial.print("=");
Serial.println(val);
}
}
Serial.println();
// the findUntil has detected the blank line (a lf followed by cr)
// so the http request has ended and we can send a reply
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}


eprilya


Code: [Select]
char server[] = "192.168.0.2/TA/user_room.php";    // name address for Google (using DNS)
Does that completely useless, incorrect comment really serve any purpose?

Code: [Select]
    client.println("GET /search?q=arduino HTTP/1.1");
After connecting to some bogus server (or trying to, anyway), does this GET request really make sense? It's fine if you are talking to google. It's useless when you try to talk to some other server.


The web to control arduino is not index.php
So, how is the  code?

SurferTim

Code: [Select]
char server[] = "192.168.0.2/TA/user_room.php";
This returns non-existent domain using nslookup. If you use the "char server[]", it performs a dns lookup first. That domain does not resolve.

This does not do the dns resolution. It uses the ip directly.
Code: [Select]
IPAddress server(192,168,0,2);


PaulS

Quote
Serial monitor shows that the arduino and php are connected. But when I click the checkbox, nothing happens with the led.

So, the Arduino asked the server to execute a php script and to send back the output of that script. The Arduino got some html code generated by the php script, and rendered that html code as a picture on what? You clicked the checkbox on the screen where the Arduino rendered the code?

It appears that you STILL think that the Arduino is getting some information from the client that got some information from the server. It is NOT possible for the Arduino as client to talk to another client or for another client (where you are checking the check box) to communicate with the Arduino.

Put the Arduino back in it's box, and put it away.

Go study client/server architecture, until you can see that two clients can NOT communicate directly.

Then, when you do know how client/server architecture works, get the Arduino back out IF it can do anything like you want.
The art of getting good answers lies in asking good questions.

eprilya


Go study client/server architecture, until you can see that two clients can NOT communicate directly.

Then, when you do know how client/server architecture works, get the Arduino back out IF it can do anything like you want.


After I'd go to study, I think my project doesn't works well because Arduino is not possible as client. Finally, I use arduino web server to control my servo. But I've some problems, when I click button to control one servo, it doesn't stop when it's move. Here's my sketch:

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h>
Servo altitude;  // create servo object to control a servo1
Servo azimuth;  // create servo object to control a servo2

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEC }; //physical mac address
byte ip[] = { 192, 168, 0, 3 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port


String readString;

void setup(){

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  altitude.write(180); //set initial servo1 position if desired
  azimuth.write(180); //set initial servo2 position if desired
 
  altitude.attach(9);  //the pin for the servo1 control
  azimuth.attach(8);  //the pin for the servo2 control
  //enable serial data print
  Serial.begin(9600);
  Serial.println("server servo/pin 9,8 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.print("<html><head>");
          client.print("<title>Halaman Kontrol</title>");
          client.println("</head>");
          client.print("<body bgcolor='#3399FF'>");
          client.println("<center><p><h1>Sistem Kendali Teleskop </h1></p><center><hr><br />");
         
          client.println("<form  method=get name=form>");
          client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?altitude\"\">ALTITUDE</a></button>");
          client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?alt_stop\"\">STOP</a></button>");
          client.println("<br />");
          client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?azimuth\"\">AZIMUTH</a></button>");
           client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?zm\"\">STOP</a></button>");
          client.println("</form><br />");
         
          client.println("</html></body>");
         
         delay(100);
          //stopping client
          client.stop();
         
        if(readString.indexOf("altitude") >0){
        for (int i = 0 ; i <=180; i=i+10){
            altitude.write(i);
            delay (1000);
          }
}

if(readString.indexOf("alt_stop") >0){
            altitude.write(0);
            delay (1000);
        }
       
         if(readString.indexOf("azimuth") >0){
             for (int i = 0 ; i <=180; i=i+10){
            azimuth.write(i);
            delay (1000);
          } 
         
          if(readString.indexOf("azm_stop") >0){
            azimuth.write(0);
            delay (1000);
        }
}
           readString="";
        }
      }
    }
  }
}

     


The second question is, I want to insert html code from webcam software, named "webcam xp". It is for streaming.
Can I insert this code in one table on arduino web server?

Code: [Select]
<th scope="col"><script type="text/javascript">
  var height_array = new Array();
  var width_array = new Array();
  width_array[1] = 640;
  height_array[1] = 480;
</script>
<img src="http://DESSY-PC:8080/loading.jpg" class="webcam" id="webcam1" onmousedown="PTZMouseDown1(event)" width="640" height="480" alt="Live Stream" />
<script type="text/javascript">
<!--
currentCamera1= 1;
errorimg1= 0;
document.images.webcam1.onload = DoIt1;
document.images.webcam1.onerror = ErrorImage1;
function LoadImage1()
{
        uniq1 = Math.random();
        document.images.webcam1.src = "http://DESSY-PC:8080/cam_" + currentCamera1 + ".jpg?uniq="+uniq1;
        document.images.webcam1.onload = DoIt1;
}
function PTZMouseDown1(e)
{
        var IE = document.all?true:false;
        var x,y;
        var myx,myy;
        var myifr = document.getElementById("_iframe-ptz");
        tp = getElPos1();
        myx = tp[0];
        myy = tp[1];
        if(IE){
        var scrollX = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
        var scrollY = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
        x = event.clientX - myx + scrollX;
        y = event.clientY - myy + scrollY;
        } else {
        x = e.pageX - myx;
        y = e.pageY - myy;
        }
        if ((width_array[currentCamera1] != null) && (width_array[currentCamera1] > 0)) x = Math.round((x * 400) / width_array[currentCamera1]);
        if ((height_array[currentCamera1] != null) && (height_array[currentCamera1] > 0)) y = Math.round((y * 300) / height_array[currentCamera1]);
        if (x > 400) x = 400;
        if (y > 300) y = 300;
        if (myifr != null) myifr.src = "http://DESSY-PC:8080/ptz?src=" + currentCamera1 + "&moveto_x=" + x + "&moveto_y=" + y +"";
        return true;
}
function getElPos1()
{
            el = document.images.webcam1;
            x = el.offsetLeft;
            y = el.offsetTop;
            elp = el.offsetParent;
            while(elp!=null)
              { x+=elp.offsetLeft;
                y+=elp.offsetTop;
                elp=elp.offsetParent;
              }
            return new Array(x,y);
}
function ErrorImage1()
{
        errorimg1++;
        if (errorimg1>3){
              document.images.webcam1.onload = "";
              document.images.webcam1.onerror = "";
              document.images.webcam1.src = "offline.jpg";
              }else{
                uniq1 = Math.random();
            document.images.webcam1.src = "http://DESSY-PC:8080/cam_" + currentCamera1 + ".jpg?uniq="+uniq1;
              }
}
function DoIt1()
{
        errorimg1=0;
        window.setTimeout("LoadImage1();", 40);
}
//-->
</script>
</th>

PaulS

Quote
because Arduino is not possible as client.

Yes, it is, when you understand what a client is and what a client talks to (a server, not another client).

Code: [Select]
        //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();

Regardless of what the client asked for, give it what YOU think it needs. Wrong!!!

Quote
client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?altitude\"\">ALTITUDE</a></button>");
          client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?alt_stop\"\">STOP</a></button>");

Why do you have two buttons named b? Do you have two brothers named Darryl, Darryl?

Code: [Select]
          client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?azimuth\"\">AZIMUTH</a></button>");
           client.println("<button name=b value=1 type=submit style=height:80px;width:150px><a href=\"/?zm\"\">STOP</a></button>");

Oh, wait, make that 4 buttons named b.

Code: [Select]
        if(readString.indexOf("altitude") >0){
        for (int i = 0 ; i <=180; i=i+10){
            altitude.write(i);
            delay (1000);
          }

Assuming that the client did include "altitude" in the request, this block will take 18 seconds to move the servo from 0 to 180 degrees. I fail to see how this is a relevant thing to do.

I would expect your server to have 4 buttons, named right, left, up, and down. Each request containing "right" would cause the position of one servo to be incremented by 1. Each request containing "left"would cause the position of one servo to be decremented by 1.

Each request containing "up" would cause the position of the other servo to be incremented by 1. Each request containing "down"would cause the position of that servo to be decremented by 1.

There would be no stop buttons on my page.

There might be fields where the step size could be entered. The GET request would then contain "button=right&amount=n", and you'd need to parse the statement to get the direction and the amount.

Alternatively, there might be 2 or three buttons for each direction (something like right one degree, right 5 degrees, right 20 degrees, etc.).

Quote
The second question is, I want to insert html code from webcam software, named "webcam xp". It is for streaming.
Can I insert this code in one table on arduino web server?

You can have the Arduino serve up a page that contains a placeholder for the camera image, and a source tag that says "go get the data to display here from somewhere else". You can NOT expect the Arduino to serve up the image data.
The art of getting good answers lies in asking good questions.

Go Up