Hardware Reference
In-Depth Information
Log incoming data in a database
In this section, we are going to use PHP to build the server-side part of the project. If you
are a complete novice in PHP, I recommend the following resource to learn the basics of
the language:
http://php.net/manual/en/tutorial.php
First, we are going to see the content of the datalogger.php file. This file will handle
the requests coming from the Arduino board, log the data in a database, and answer with a
simple message. Note that this file has to be in a folder named datalogger on your web
server. We will see the important parts of the code. To get the complete code for this sec-
tion, please refer to the GitHub repository of the chapter. Note that all the PHP code should
be between the <php … ?> tags.
The file starts by receiving the data from the GET request sent by the Arduino Ethernet
shield:
$temperature = intval($_GET["temp"]);
$humidity = intval($_GET["hum"]);
We also instantiate the connection with the SQLite database:
$db = new SQLite3('database.db');
Then, we need to give some structure to the database if the database file is brand new. If
you are not familiar with the SQL commands, I invite you to visit the following link:
http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
We are going to create four different columns inside the database: a unique ID that will be
automatically incremented by SQLite, a timestamp to know when the measurement was
made, and the temperature and humidity data. This is done using the following piece of
code:
$db->exec('CREATE TABLE IF NOT EXISTS measurements (id
INTEGER PRIMARY KEY, timestamp TIMESTAMP DEFAULT
CURRENT_TIMESTAMP NOT NULL, temperature INTEGER, humidity
INTEGER);');
Search WWH ::




Custom Search