POST and GET Wifly Data Into MySQL Database Server

Hello everyone. I have been working on this project a couple weeks now, and I am still stumped.

I am trying to send a name from my wifly into a database on my website and pull a different name from the database and receive it on the wifly. More specifically, my goal is to send a word for ex. “Billy” from the wifly to the database on my website and have it show up in one of the tables under “profile_names”, then read a different name from the same table, ex. “Steven” and have it echo on my wifly. I am using the WiFly shield and arduino duemilanove. Here is what I have so far:

I can type the following address into my firefox browser:

http://www.myserver.com/wiflyinsert.php?wname=Billy

This command will call the wiflyinsert.php script on my site which will put "Billy" into the database where it needs to go. Is there a way to call that address via the wifly so that I can put names into the database, or is there another method that is more efficient for inputting data into a database? I have been going back and forth between the "WiFly Wireless Talking SpeakJet Server" tutorial and the wifly manual and still cannot figure this one out.

The most recent wifly manual (ver.2.19) has a section titled “Sending and Receiving Data via Web Server”, and I think this is may be exactly what I need, but I am not sure how it is set up. I can set the dns and can open a connection to my web-server on the wifly, but I am not sure how to execute the GET commands properly. I am pretty good with php and html, as well as programming the arduino, I just cannot figure out how it all links together so they talk to each other. Any advice on this would be extremely helpful and greatly appreciated. Thank you very much for your time.

Hi,

Have you seen the [Arduino WiFly Shield library?

It’s designed to make projects like this much easier…

Is there a way to call that address via the wifly so that I can put names into the database, or is there another method that is more efficient for inputting data into a database?

Included with the library there is an example sketch named "WiFly_WebClient" that demonstrates how to retrieve a URL. You could use this to call your wiflyinsert.php script. (Strictly speaking altering the database should really use a POST request rather than a GET request but a GET request is much easier to deal with. :) )

Hope this helps you make some progress with your project. If you have any suggestions for improving the library I’d appreciate if you could post them.

–Philip;](WiFly Shield code library alpha 0 release (also SPI UART) - SparkFun Electronics Forum)

I will check it out and let you know how it goes. Thanks a lot for the help.

Wow this shield library is VERY helpful. :smiley: Thanks!

Good news! I got the wifly to connect correctly to my server and request the html, but I am now running into another issue on the response back. When I run the connect code I get a “bad request” error.

Your browser sent a request that this server could not understand. client sent HTTP/1.1 request without hostname(see RFC2616 section 14.23): /wiflyinsert.php

I know the Wifly is running correctly because I am getting a html web page echoed back, it is an issue with my request…

client.println("GET /wiflyinsert.php?wname=STEVEN HTTP/1.1");

I saw that you stated that I may need to do a POST message, which I tried using the same method to no avail. I have been looking at the google.com example and it appears you used the search box on the webpage “search?q=arduino”.

My question is: Would the following be the best method for me to input data into my database on my server? Create a blank html page that has an empty form on it that inputs data into my database, then have the wifly connect to the form and act as though data is being submitted through it?

Hi Joe,

Joe3Harris:
Wow this shield library is VERY helpful. :smiley: Thanks!

Great! I'm pleased it's useful for you. :)

When I run the connect code I get a “bad request” error.

Your browser sent a request that this server could not understand. client sent HTTP/1.1 request without hostname(see RFC2616 section 14.23): /wiflyinsert.php
Surprisingly, (for an error message :) ) this error message is actually helpful.
client.println("GET /wiflyinsert.php?wname=STEVEN HTTP/1.1");
Try changing "HTTP/1.1" to "HTTP/1.0". Version 1.1 of the HTTP protocol requires an additional "Host: xxxx" header which you don't provide.

My question is: Would the following be the best method for me to input data into my database on my server? Create a blank html page that has an empty form on it that inputs data into my database, then have the wifly connect to the form and act as though data is being submitted through it?

You don't actually need a form--you just need to supply the information in the same way. (Which is what you're doing with your "GET" request. But it's also possible to supply form data with a "POST" request.)

–Philip;

Thank you for the tip, I tried the 1.0 and I am making progress. I still am not there yet, but I think I am really close!

After changing:

client.println("GET /wiflyinsert.php?wname=STEVEN HTTP/1.1");

to the following:

client.println("GET /wiflyinsert.php?wname=STEVEN HTTP/1.0");

I now get this 404 error:

connecting...
connected
HTTP/1.1 404 Not Found
Date: Tue, 22 Jun 2010 04:18:12 GMT
Server: Apache
Content-Length: 1996
Connection: close
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

( 404 webpage html code )

Now it seems that no matter what I put in after the GET, I always get this 404 error. I double checked the IP Address, it is correct, and I am sure I am connected to my host because the error page that gets echoed by the Wifly contains code “/spicons/notfound.jpg” and a few gifs that are graphics located on my server. I opened notfound.jpg in my browser and it is a big caution image from my host 1and1.com.

I also tried creating a text file called joeinfo.txt. The text file contains the text “Hello World.” I then renamed the .txt file to .html and uploaded it to the server. I tried:

client.println("GET /joeinfo HTTP/1.0");

and…

client.println("GET /joeinfo.html HTTP/1.0");

And still get the same 404 error.

I originally thought it was something to do with using the .php extension with the GET statement, but seeing as I cannot connect to the joeinfo.html page I think it is something else. I am wondering if it is something to do with my server. The first line states “HTTP/1.1 404 Not Found”. Perhaps I need to downgrade my server so it can communicate with the wifly using HTTP/1.0?

Once again, thank you in advance for taking the time to help me out.

If you’re using a “shared host” for your web server it will probably only work if you use HTTP1.1 and include the “Host: example.com” header in your request.

You could confirm this by using telnet to connect to port 80 of the server and make the same request.

–Philip;

I finally got it to work! The problem was the “HTTP/1.1” at the end. For some reason when I was including the “HTTP/1.1” at the end it would pull up an error page, the error looked as if it was ignoring the first set of http://… and began reading from the second HTTP/1.1 as a new website. I removed it, and now I can pull up pages correctly on the wifly! :smiley:

Hopefully not including that statement at the end will not cause errors for me down the road. So now the wifly works with html pages, but now there is a new issue. When I try to pull up a page that uses .php the page gives me an “Error 500 - Internal Server Error”

connecting...
connected
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Error 500 - Internal server error</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="cache-control" content="no-cache" />
    </head>
    <body style="font-family:arial;">
    <h1 style="color:#0a328c;font-size:1.0em;">Error 500 - Internal server error</h1>
    <p style="font-size:0.8em;">An internal server error has occured!
Please try again later.</p>
</body>
</html>

I have been testing primarily on Telnet, thanks to your tip which has made things MUCH faster, and as you can imagine when I try it on the wifly, I get the same error. It looks as though there is an error regarding “no-cache” or cache-control.

I have been trying to disable the cache in the .php code and haven’t been getting much luck. Any idea what could be causing this 500 error?

Joe3Harris:
I have been testing primarily on Telnet, thanks to your tip which has made things MUCH faster, and as you can imagine when I try it on the wifly, I get the same error. It looks as though there is an error regarding “no-cache” or cache-control.

AFAICT the cache-related text is just part of the error page--not an indication of where the problem is.

I have been trying to disable the cache in the .php code and haven’t been getting much luck. Any idea what could be causing this 500 error?

You will need to look in your server logs for an error message. Try accessing the same page from a web browser and see if it gives the same error--if you get the same error your problem is probably with PHP or server configuration and you'd probably be better to look for help with that on a PHP-specific forum. I don't use PHP so won't be able to help you with that.

–Philip;

I see… well at least I am pointed in a new direction, now that I know I don’t have to keep debugging this cache issue. I will post my issue on a more php specific forum and hopefully get the answers I need.

Thank you for all of the help with the wifly and the shield library, I was truly lost before your input.

Have a great one Philip!

Joe3Harris:
Thank you for all of the help with the wifly and the shield library, I was truly lost before your input.

Glad you've found it helpful.

Have a great one Philip!

Thanks. :)

–Philip;

I’ve also managed to do a GET request, but I had to add the additional Host: example.com bit. Here’s my code snippet that went inside WebClient.pde

.... // wifly_client code
.... // make sure you enter your password, ssid and client address correctly initially.
.... // also, i've declared a integer as a global variable.

....
    Serial.println("connected");
    client.print("GET ");
    client.print("/users/1/apost/");
    client.print(n);      
    client.println(" HTTP/1.1");    
    client.println("Host: example.herokuapp.com");

The above code takes the 1st user n, and adds the integer N as a variable to that user. We’re using Rails for our application and SQLite for our database.

I’ve also found these blogs really helpful:

http://www.josephmoskie.com/

http://arduinology.tumblr.com/

Good luck everyone!

Hello,

Has anybody had any luck hitting a Heroku App with WiFly? I seem to be having trouble resolving the IP address from the DNS… I am able to hit other sites, but Heroku seems to use an odd DNS/rDNS scheme that WiFly can’t handle (?) Any suggestions? :?:

Yes, I have connected successfully to a Heroku app with wifly.

In order to connect you must specify to Heroku the host in the header data of your request. This is the only way for the DNS to resolve your request.

ie.

client.println("GET my/page/ ");

client.println(“Host: myherokuapp.herokuapp.com”);