Pages: [1]   Go Down
Author Topic: Arduino + ethernet shield: read information from the database  (Read 956 times)
0 Members and 1 Guest are viewing this topic.
Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

How can I read information from the Internet from a database using arduino uno r3 + ethernet shield (w5100)?

I work with mySQL + PHP.
Be happy to see a simple example how you can take the information into a variable.

Thank you.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Be happy to see a simple example how you can take the information into a variable.

Me too, but there isn't a "simple" example. Do you have the PHP/MySQL server working correctly when you try the request with a web browser?
Logged

Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi
yes, i have php + mySQL.
i have a php file return text "hello word", and i wont set this text to string variable.

Succeed to write information to a database via a link that contains querystring.
For example: http://192.168.1.1/writeDB.php?myText="hello word"

i can make a php file return a text string.
« Last Edit: May 05, 2012, 09:05:32 am by VMwareUser » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a malformed URL. Spaces are not allowed (between hello and word).
Quote

If the server is returning a text response, which it should, then you must parse the data required in the Arduino from the response and convert it to a digital value. The good thing about HTML is web browsers will ignore any HTML tags they do not understand. You could send the data back in a tag. That should make it easier to find the data among the other stuff.
<myTag data=1234>
Logged

Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the answer,
How do I get the data to a variable?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must modify your client.read() routine to scan for that string. The easiest way is to make the code returned from the server as short and simple as possible, and start the line with that tag. That way, when you receive a CR or LF, you save the next 16 characters (or whatever you need) in a char array, then compare the first 6 characters with the tag.

Code:
// If you copied the first characters into inBuf, then
if(strncmp(inBuf,"<myTag",6) == 0)
{
   // the line starts with "<myTag"
}

Google search for atoi, strncmp and strtok. These are helpful functions for this.
Logged

Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I use client.read () with a specific URL?
Thank you very much
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code on this post downloads the Google home page (by ip address) every 10 seconds.
http://arduino.cc/forum/index.php/topic,99629.msg754629.html#msg754629

The getPage() function takes two parameters. The first is the server ip, and the second is the page you want to download from that server. Replace "/" with "/mypage.php?test=1234" or whatever your page is called.

Open the serial monitor and watch. That may give you some ideas as you watch it work.
Logged

Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much
I'll try it smiley
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5986
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I forgot to mention one thing. If your server is using virtual hosting, you must add the Host to the GET send in the getPage() function. If it connects, but keeps returning 404 errors, then that is normally the challenge.

Code:
  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    // add the Host here
    sprintf(outBuf,"GET %s HTTP/1.0\r\nHost: www.mydomain.com\r\n\r\n",page);
    client.write(outBuf);
  }
Logged

Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you
Logged

Israel
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works (:
Thank you!
Logged

Pages: [1]   Go Up
Jump to: