Connecting your Arduino to the internet is pretty simple. Just get the Ethernet shield and use the Ethernet examples to start with.
I wanted to use my Arduino to capture information and post it to my website where I could format the data as needed. (Pachube/Cosm is another option, but I wanted more freedom to do custom charts and dashboards)
To do this I need my Arduino to post data to a webpage that saved the data to a Database. The database is then used to manipulate the data. Lastly the data is then displayed on dynamic webpages that show changes to the information.
The following code is for posting the data to the website that will record the data to the Database. (Remember to change it to your websites details)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
[<span class="hiddenSpellError">sourcecode</span> language="cpp"] #include <Ethernet.h> #include <SPI.h> byte mac[] = { 0x90,<span class="hiddenSpellError">0xA2</span>,<span class="hiddenSpellError">0xDA</span>,0x00,0x55,<span class="hiddenSpellError">0x8D</span>}; //Replace with your Ethernet shield MAC <span class="hiddenSpellError">EthernetClient</span> client; void setup() { Serial.begin(9600); Ethernet.begin(mac); delay(1000); Serial.println("connecting..."); } void loop(){ String data; data+=""; data+="data=12.7%2C0%2C50"; // Use HTML encoding for comma's data+="&submit=Submit"; // Submitting data if (client.connect("www.your-website.com",80)) { Serial.println("connected"); client.println("POST /pageThatTakesPostData.<span class="hiddenSpellError">php</span> HTTP/1.1"); client.println("Host: www.your-website.com"); client.println("Content-Type: application/<span class="hiddenSpellError">x-www-form-urlencoded</span>"); client.println("Connection: close"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.print(data); client.println(); //Prints your post request out for debugging Serial.println("POST /pageThatTakesPostData.php HTTP/1.1"); Serial.println("Host: www.your-website.com"); Serial.println("Content-Type: application/<span class="hiddenSpellError">x-www-form-urlencoded</span>"); Serial.println("Connection: close"); Serial.print("Content-Length: "); Serial.println(data.length()); Serial.println(); Serial.print(data); Serial.println(); } delay(2000); if (client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); } } [/<span class="hiddenSpellError">sourcecode</span>] |
Here is an example of how the PHP would look that receives the data from the Arduino Board.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[<span class="hiddenSpellError">sourcecode</span> language="php"] <?php header("Content-type: text/html"); header("Cache-Control: no-cache, <span class="hiddenSpellError">must-revalidate</span>"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past if(<span class="hiddenSpellError">strlen</span>($_POST["data"]) > 1 ) { echo "1"; // if it displays 1 it worked } if(<span class="hiddenSpellError">strlen</span>($_POST["submit"]) > 1 ) { echo "1"; // if it displays 1 it worked } ?> [/sourcecode] |
To test if everything is working on the PHP side you will need to send a post request to the above PHP page that include the post data that the Arduino will post. (data and submit… more fields can be added or removed)
<html> <body> <FORM action="abovePage.php" method="POST"> <P> <LABEL for="firstname">Data To Send:</LABEL> <INPUT type="text" name="data"><BR> <INPUT type="submit" value="Submit"> </P> </FORM> </body> </html>
We recommend the following books:
- Arduino Cookbook By Michael Margolis. It covers a huge range of Arduino topics.
- Making Things Talk: Physical Computing with Sensors, Networks, and Arduino By Tom Igoe.Β It has all kinds of ways to make Arduino’s communicate.
Did you have any luck with this? I’m trying to figure out HTTP POST with the UNO. want it to send an http post (from a temp sensor) to a wifi thermostate on my local network.
Hi Brian,
Yes… I manged to get this working very well.
My setup:
# Arduino Duemilanove/Uno
# Wiznet ethernet brick. (Same setup as Arduino Ethernet Shield)
# A post request was sent to my webserver every 10 seconds.(Temp+Humid)
# The server page is PHP page that takes the post data and inserts it into a MySQL DB.
# JQuery Mobile + PHP + Google Charts to display the graphs on my production system.
Have you manually tested the post command before trying it on the Arduino?
Hi Bert,
Thanks for sharing this nice work. However I have a problem with my php page for taking the data. Is it possible to know a bit more on this part ?
Sorry for my bad english !
Hi Guillaume,
I have made some bug fixes on the sketch. It should now output the Post request on the serial port.
I have also added a PHP example of how it processes the data from the Arduino.
The PHP side works by looking at the Post Data. (That will be the data variable in our script). To retrieve
that in PHP you use the following command : $_POST[“data”] and $_POST[“submit”]
Does that answer your question?
Thanks for the script Bert but it doesn’t work with my server. The arduino side seems to work because I get all the POST informations on the serial terminal, but nothing is displayed at the url of the php file. In addition I even have an http 500 error by using chrome or internet explorer (with mozilla the page is empty).
Mmmmm… Are you sure PHP is working? Try :
< ?php echo 'test'; ?>
Are you running Apache or IIS?
I find that the best way to debug scripting languages is by using elimination. Remove parts of the script you think might be broken and re-test. If it does work without the stuff you took out, you know the part that is left is good(So the working bit has been eliminated). Then add bits back till you find the line with the issue. If you can enable PHP warnings it will be a huge help cause that will point you to the line that is not working.
This works for Arduino’s to. Serial.print’ing debugging info so you can get a better idea of what gets sent/received.
Yes PHP is working. I fixed the error 500 problem by writing by hand your script and not with a copied pasted like previously. Strange…
However it still doesn’t work. I don’t have the two “1” on my page (it is empty). So I think that the problem is on the Arduino side, more precisely with the POST request. The connection with my server is working but I don’t have any idea to know if the leftover of the request is working or not…
it won’t work by just going to the php page because your browser does not send the needed post data…
I have added a second PHP page that will similate what the Arduino is doing. Just make sure to point the second test page to the
correct URL on the line with : FORM action=”abovePage.php”.
Ha ha! I like your response Bert!
People should know how PHP and forms work before trying to mix language and communications between!
Nice post by the asymmetric
A soon a get my ethernet sheild ill try posting to my local webserver too!
And i’ll check by an Ajax style my database and all is sensors data!
You have an exemple on how you design your database?
Hi…. I don’t have the DB structure at the moment but Google will point you in the right direction.
OK but how can I check if it’s working when I send a data from the Arduino to the server ?
Instead of echo “1”; I usually make it write data to a MySQL database or make your PHP page send a success email.
Alternative is write code on the Arduino that prints the results from the post request on the serial port.(Make the Arduino print the 1’s π
Here are some example code of how to retrieve a webpage -> http://bildr.org/2011/06/arduino-ethernet-client/
It uses client.read();
My goal is also to make it write to a MySQL database. But if I don’t have the two echo 1 in the php page it means that something isn’t working with the POST request with Arduino. Isn’t It ?
If you tested the main PHP page with the test page and it returns 1’s then the main PHP is working. If you then look at the results the Arduino gets back form the main page and there are not 1’s in the HTML the Arduino received then there is an issue with the Arduino Code. It might be as simple as a variable name not aligning between the page and the Arduino Post request. What data are you getting back from the Arduino after adding a read loop? (It could also be that your read loop is not working correctly. That can be tested by going to a page like google and comparing the returned html to what a browser returns )
Hy Guys i want write data in Google Form ( No spreedshet):
My code is, but doesn work
client.println(“POST /forms/d/……../formResponse HTTP/1.1”);
client.println(“Host: docs.google.com”);
client.println(“Content-Type: application/x-www-form-urlencoded”);
client.println(“Connection: close”);
client.print(“Content-Length: “);
client.println(data.length());
client.println();
client.print(data);
client.println();
Hi.
I can’t answer your question but I can give you some clues to get to it. Use FireFox with FireBug. FireBug has a tab called “Net”. This tab displays the browser traffic. With Firebug open, you just click on the submit button of the form that you want to simulate and look in the “Net” tab to find the Post Request headers that was sent to google.
Other thing to keep in mind is that the request needs to end with an empty line. You example does not have that at the end. It only adds a new line char after print(data) but does not add an extra empty line)
Hope this helps.