Log Arduino data to a web server

Log Arduino data to a web server

Published on: 02-12-2013 13:32 - Lees in het Nederlands

Please note: this article was semi-automatically translated from Dutch, translation errors may occur, links will lead to Dutch websites, you are welcome to comment in English.

Hey, you don't like advertisements but you do love quality content? That's OK, maybe you want to make a donation via PayPal or creditcard?

Or purchase your parts and tools via one of our shoppingpartner-links in the sidebar.

You have all kinds of sensors hung on your Arduino, and you can read via serial, or perhaps via an Ethernet Shield. Nice, but you might want to do something more with your data? You can! Send the data from your Arduino via the Ethernet Shield into a web server!

This article is more or less a sequel to this article on how to measure your energy consumption.

Limitations of the Arduino

Memory and processing power of an Arduino are limited. If you really want to show detailed visualizations over periods of months or years, you can not escape doing this on a remote computer or server. Because I think it's not prefered to have a computer running 24/7 at home, and I already have access to a web server where my sites run on, I log my Arduino sensor data to this Web server. In this article I will describe what steps you need to take to get your Arduino logging to a remote server.

First, you obviously need an Arduino, with some sensors to this. I use my Arduino home automation system, it registers the outdoor temperature, indoor temperature and sunlight intensity. The Arduino is via an Ethernet Loved coupled to a second Arduino in the cupboard, responsible for keeping the electricity and gas. Here you can buy an Arduino Uno and related matters. Data logging via HTTP

Servers and clients on the Internet to talk to each other via HTTP - HyperText Transfer Protocol. If you want to talk to your Arduino with a web server, he will also have to speak HTTP. When you request a Web page from a server, send a so-called GET request, this looks (simplified) like this:

GET /index.php HTTP/1.1
Host: www.engineerathome.com
Connection: close

The web server will send as a response code 200 OK plus the contents of the file index.php.

In our case, we not only want to retrieve a file, but also transfer data from our Arduino to the server. We can do this through GET variables, as you can see in the following example:

GET /index.php?stroomverbruik=2.7&gasverbruik=3.1 HTTP/1.1
Host: www.engineerathome.com
Connection: close

Sending a GET-request with an Arduino

On the Arduino website you will find the right code to build a web client However, the Web client requests a static page. By extending the web client with the code below I can do a GET request that sends the sensor values as GET variables.

if (client.connect(myServer, 80)) {
  client.print("GET /log.php?z=");
  client.print(sensorLicht);
  client.print("&tb=");
  client.print(sensorTempBuiten);
  client.print("&tk=");
  client.print(sensorTempKantoor);
  client.print("&s=");
  client.print(sensorStroom);
  client.print("&g=");
  client.print(sensorGas);
  client.println(" HTTP/1.1");
  client.println("Host: www.engineerathome.com");
  client.println("Connection: close");
  client.println();
  delay(100);
  client.stop();
}

Processing the data on the webserver

The Web server is the file log.php. This PHP file processes the incoming data, and stores the measured values ​​with a timestamp in a MySQL database on the Web server.

$zonlicht = $_GET["z"];
$tempbuiten = $_GET["tb"];
$tempkantoor = $_GET["tk"];
$stroom = $_GET["s"];
$gas = $_GET["g"];
$sql = mysql_query("INSERT INTO `data` (
  datetime,
  zonlicht,
  tempbuiten,
  tempkantoor,
  stroom,
  gas
) VALUES (
  '".date("Y-m-d H:i:s")."',
  '".$zonlicht."',
  '".$tempbuiten."',
  '".$tempkantoor."',
  '".$stroom."',
  '".$gas."'
)");

You can also use the IoT Dataplatform of EngineerAtHome! Go to data.engineerathome.com and learn how to add a sensor and log your data to our servers.

Drawing graphs from the database

Once the data is in the database, you can view them in various ways. The easiest way is to log on to the server PHPMyAdmin to view the raw data in a table. Ofcourse it's more fun to build graphs showing the data.

Arduino logged values in PHPMyAdmin

With the code below I draw a bar chart of the sunlight measurements of the last 24 hours. First, I calculate the maximum value, so that I can scale the graph. Nicely Then I get the actual data, and I make sure that the bars are drawn with the correct height.

$sqlmax = mysql_query("SELECT MAX(zonlicht) as maxi FROM `data` LIMIT 24");
$max = mysql_fetch_array($sqlmax);
$sqldata = mysql_query("SELECT s.uur, s.min, s.zonlicht FROM (SELECT HOUR(datetime) as uur, MINUTE(datetime) as min, zonlicht, datetime FROM `data` ORDER BY datetime DESC LIMIT 24) s ORDER BY datetime ASC");
while ($data = mysql_fetch_array($sqldata)) {
?>
<div class="bargraph">
<div style="height:<?=($data['zonlicht']/$max['maxi'])*100?>%;"><?=$data['zonlicht']?></div>
<span><?=$data['uur']?></span>
</div>
} ?>

The rather complex SQL query is because I want to retrieve the last 24 measurements so descending by date, but then it wants to show. Ascending In the same way I process the temperature measurements resulting in a page full of bar charts.

Bargraph of Arduino logged data on a webserver

Of course you can make it as crazy as you want, for example, I also sign daggemiddeldes in a weekly, and monthly averages in an annual review. But you could also use a plugin like Google Charts to draw line graphs or pie charts or the consumption of electricity and gas directly calculate the cost by using the high-and low rate.

Do you have more ideas? Let us know in the comments below!

25 comments on this article »

More articles like this one