Arduino Web Client

Code to Send POST to Website and Log Results!

 

Updates coming soon

 

Arduino Code – Tested and Working on Arduino IDE 1.6.5 – Standard libraries only

Most Importantly the Web Server PHP Code

 

The HTTP request

Correct format

A proper HTTP POST request looks something like this:

This will send bar as the value of foo. In a GET request you could add this to the URL (/url/?foo=bar), but POST requests don’t work like this: you have to add a message body.

Content-Length should be the amount of characters in the body (here: foo=bar, so 7 characters). The Content-Type and Content-Length fields are required to do what you want. The Host field is required in any HTTP/1.1 request.

Note that linebreaks are \r\n. Fortunately, Arduino’s println() adds both the carriage return \r and the newline feed \n. However, you should make sure that you don’t use print() unless you want to continue on the same line. So here’s a working example:

Working Arduino example

This will send 50 as the temperature.

PHP back-end

Storing the data in a file

Then you probably want to save the sent data on the server. To do this, you fetch the information from the $_POST superglobal, and save it in a file:

This opens temp.log in write-mode (see fopen()), then writes a timestamp and the current value to that file (see fwrite()), and closes the handle (see fclose()). The file then looks something like this:

You can simply go to http://tmm.site50.net/temp.log to view the file.

Logging

Every time the Arduino sends a request, the old result will be overwritten. Alternatively, you may want to log the data, and add new lines instead of overwriting old data. Then, use the a (append) mode in fopen() instead of w (write):

This will automatically append new lines to the end of the file.

Permissions

Depending on the permissions on your server, you may need to create temp.log before PHP can write to it.