Pages: 1 2 [3] 4   Go Down
Author Topic: Arduino ethernet webclient php script and mysql  (Read 58904 times)
0 Members and 2 Guests are viewing this topic.
South Coast UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

zoomkat

Well that was very handy, pasting http://192.168.1.20/update_db.php?celsius=45 updated the database. I then changed the get statement to client.println("GET /update_db.php?celsius=45 HTTP/1.0"); and that updated the database also. Thank you.  smiley

At this moment I don't understand:
Code:
int d1 = readString.indexOf('<');
int d2 = readString.indexOf('>');
readString1 = (readString.substring(d1+1,d2));
and what it's trying to do or how it works. I understand .indexOf but not sure how the String works

Tony
« Last Edit: December 21, 2010, 10:46:31 am by xsilvergs » Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The readString.indexOf('<'); finds the location of "<" in the info sent by the server. The actual useable data set from the server has < and > delimiters on each end. Based on the info sent from the arduino client to the server, the application run by the server may determine the arduino client needs to take some action. If the arduino client is monitoring temperature and decides it needs to send "celsius=45" to the server, the server could send back <heater1=off> and then the client could turn heater1 off. Just simple communication between the client and server.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

South Coast UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What sort of file do I need on the server to send such to the Arduino? Sorry if this seems a silly question but the is all new to me.

An example would be great.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What sort of file do I need on the server to send such to the Arduino? Sorry if this seems a silly question but the is all new to me.

If the requested file is in the web page directory of the server, the server will send it. If the request sent to the server requires evaluation to develop a response, then an application on the server takes the info sent to the server, processes it, then sends the info to the server for return to the client. I'm clueless to the php/mysql stuff, so you are on your own there. With my simple setups using windows/apache, I use a batch file or an exe as a cgi application to do stuff on the server pc, such as pan/tilt my web cam.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

South Coast UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So would I use the Post method from a html Form or is there a different method?

Thanks for any info
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So would I use the Post method from a html Form or is there a different method?
Post and get request are two different animals. Get request are appended to the url using the ?. Post are sent as seperate attachments with different requirements. Due to various issues on the server end, I've always use the get method because it is simple and met my needs. I think you can use either the post or get methods in an html form.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

South Coast UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm afraid this is just not working out for me  :'(
The code in the Arduino is this:
Code:
//zoomkat 12-21-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

#include <SPI.h>
#include <Ethernet.h>
String readString, readString1;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC };
byte ip[] = { 192, 168, 1, 19 };
byte server[] = { 192,168,1,20 };

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
//pinMode;
pinMode(7,OUTPUT); digitalWrite(7,LOW);  // I use this pin as GND for the LED.
pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
  
  Serial.println("starting simple arduino client test");
  Serial.println();
  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /update_db.php?celsius=45 HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    readString += c;
  }
        
  if (!client.connected()) {
     client.stop();
    
    Serial.print(readString);
    Serial.println();
    Serial.println();
    
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substring(d1+1,d2));
    Serial.println(readString1);
 
    if(readString1 == ("heater1=on")) {
      digitalWrite(7, HIGH);    // set the LED on
    
    }
    
    Serial.println("==================================");
    Serial.println("data from server:");
    Serial.println();
    Serial.println(readString1);
    Serial.println();
    Serial.println("==================================");
    Serial.println();
      
    for(;;);
    
    }
 }
 

First Question.
Can you explain "if (!client.connected()) {
     client.stop();" as used in the above code please. Won't it miss out the next part
Code:
Serial.print(readString);
    Serial.println();
    Serial.println();
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substring(d1+1,d2));
    Serial.println(readString1);
 
    if(readString1 == ("heater1=on")) {
      digitalWrite(7, HIGH);    // set the LED on

Second Question.
Shouldn't I be able to type
Code:
http://192.168.1.19/<heater1=on>
and hit Enter to turn the LED on?
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
First Question. Can you explain "if (!client.connected()) {
    client.stop();" as used in the above code please. Won't it miss out the next part

All I can deduce from the code is that the ethernet library has a function, client.connected(), which will respond true/not true if a connection is made to a server. Putting in the ! turns it into NOT connected. If there is no connection the server, then the code starts doing other things.  

Quote
Shouldn't I be able to type Code:http://192.168.1.19/<heater1=on>
and hit Enter to turn the LED on?

No for a couple of reasons. First is that the url is not made to http get standards. There is no ? to indicate an attached query_string, and the <> characters may be reserved and probably can't be used in a query_string. Second, it appears that you are trying to send a client request to an arduino client. Clients do not receive request, they send request to servers. Servers receive request from clients and return info to the client. Servers do not send request. Using google, search for things like "http tutorial", "query_string tutorial",  "http get" and "http post", and similar to get familiar with what you are working with.

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

South Coast UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

zoomkat, thanks for info, I've never understood how ethernet works, this is all very interesting but I do have a better understanding.

I'm still not sure about this bit of code:
Code:
if (!client.connected()) {
     client.stop();
    
    Serial.print(readString);
    Serial.println();
    Serial.println();
    
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substring(d1+1,d2));
    Serial.println(readString1);

    if(readString1 == ("heater1=on")) {
      digitalWrite(7, HIGH);    // set the LED on
    
    }
    
Does it not say:- If the client is not connected stop? But how does it ever manipulate the next lines of code
Code:
Serial.print(readString);
    Serial.println();
    Serial.println();
    
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substr
etc, etc....
as they are all within the same {  }.

If the client is connected will it not jump straight to here?
Code:
for(;;);
    
    [glow]}[/glow]
 }



« Last Edit: December 22, 2010, 05:30:01 am by xsilvergs » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50505
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look back at the code in Reply #24. You extended that code, adding code in the wrong place.
Logged

South Coast UK
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS,

I took the code from reply #29 which zoomkat kindly supplied. I couldn't understand how it worked, I presume the code goes after this
Code:
for(;;);

    }
but still in the void loop().

This may explain why the Serial Monitor didn't show the results I expected.

Thanks again
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50505
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What zoomkat was doing was stopping the Arduino from doing anything (with that infinite loop) if there was no client connected. Not sure exactly why.

But, you want to remove that infinite loop, and add an else clause (that gets executed if there IS a client connected.

Code:
if(!client.connected())
{
   client.stop();
}
else
{
   // There IS a client connected. Handle the client...
}
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm still not sure about this bit of code: xxx Does it not say:- If the client is not connected stop? But how does it ever manipulate the next lines of code

That part of the code only stops the connection activity with the server if the server has closed its connection with the arduino. Web servers generally do not maintain persistant connections with clients. When a web server returns the requested info, it closes its connection with the client, and then opens a connection with the next client requesting info. readString was made one character at a time during the connection with the server. When the connection with the server is closed, then it is time for the arduino to parse readString for the desired data. As to the
Code:
for(;;);
I asked as to its function in the code (below), but there was really no answer. I'd really like to replace it with something more obvious as to its function.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290705221

Quote
What zoomkat was doing was stopping the Arduino from doing anything (with that infinite loop) if there was no client connected. Not sure exactly why. But, you want to remove that infinite loop, and add an else clause (that gets executed if there IS a client connected.

The reasion I used the code is simply that "it works" for a single client connection with a server. Once the connection with the server is closed, there is no more incomming data. No more connections will exist until the process is once again started by making another request to a server. If this was server code, the checking for a client connection would be looping because a client could make a request at any time. It would be interesting to operate client and server functions at the same time, allowing a large network of arduinos to "chat" with one another as desired.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

xsilvergs, I notice you and I are working on the exact same program!!

Here's my current set up:

light relays, temp sensor > Arduino > ethernet shield > router > internet.

php,mysql database > homeserver > router > internet.

I'm trying to use the arduino / home server as a home automation system... so basically, the server will poll the arduino every 2 minutes for the temperature, then the arduino will return with the temp. I will also have a browser with an interface with buttons, when I click certain buttons, it will turn on/off the light and return the status of the light by polling the arduino for the status of that specific pin. So far I can turn LEDs on and off, and I can get the arduino to return a 0 or a 1 which is the status of the LED.

I however dont know how to get it into a database yet, matter fact I  haven't even set up a database for it.

You and I need to pool effort together and figure this out!!! :-)

My AIM: IAMSTUCKINGA
Logged

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

So here's how I did the ethernet on the Arduino side.
It's all the same, using different if and else if statements.
Code:
void loop(){
  Client client = server.available();
  if (client)
  {
    while (client.connected())
    {
      if (client.available())
        {
        char c = client.read();
        if (readString.length() < 100)
            {
            readString.append(c);
          }
        if (c == '\n')
            {
            if(readString.contains("toggleLED2"))
                {
              toggle(LED2);
                }
              if(readString.contains("getLED2"))
              {
            client.println(digitalRead(LED2);
            }
            if(readString.contains("toggleLED1"))
                {
              toggle(LED1);
                }
              if(readString.contains("getLED1"))
              {
            client.println(digitalRead(LED1));
            }
            if(readString.contains("toggleLOCK1"))
                {
              toggle(LOCK1);
                }
              if(readString.contains("getLOCK1"))
                {
                client.println(digitalRead(LOCK1));
                }
        
            readString="";
          client.stop();
            
              }
            }
        }
      }


So basically, it reads whatever the "server" or any computer on the network sends a get or post to the Arduino. Whatever it ends with, is what the arduino respond with, either turning on/off an LED, or respond with the state of that LED.

So on any browser, if I put in "http://192.168.1.77/?l=toggle8", it will toggle the 8th pin. If I do "http://192.168.1.77/?a=get8", it'll return a page with the status of the pin, which is only a 0 or a 1.

Now on the PHP side, I'm trying to "read" in this information with the following code:
Code:
if($_GET[a] == "get8")
{
$a = file_get_contents("http://192.168.1.77/?a=get8");
echo ($a);
}

Which at the moment... is not working. I dont know why it's not returning it though...
 >smiley-sad

I'm still working on it... before I was using a different version of PHP and it work using the .include function. But ever since I switch to the newest version of XAMPP (my server is windows btw) it stopped working.
Logged

Pages: 1 2 [3] 4   Go Up
Jump to: