Hardware Reference
In-Depth Information
Note that if you are using more sensors (for example, if you are also measuring the ambi-
ent light level), you can add more fields here. Simply add a new field to the list, for ex-
ample: pressure INTEGER .
Now, we can actually insert the data as a new row in the database. Since SQLite automat-
ically adds the ID and timestamp fields, we just need to insert the data concerning the
temperature and humidity:
$db->exec("INSERT INTO measurements (temperature, humidity)
VALUES ('$temperature', '$humidity');");
If you need to insert more data into the database, for example, if you have another meas-
urement to log, you can simply extend this command to insert more data into the SQLite
database.
Tip
This simple code illustrates how easy it is to insert data into the SQLite database.
However, it leaves our server exposed to a security issue known as the SQL injection.
Since our project is made for your own local network only, this is not really an issue here.
However, if you plan to deploy the project online, I recommend modifying the code to
solve this security problem. The following link will give you more information about this
issue http://www.tutorialspoint.com/sqlite/sqlite_injection.htm .
Finally, we reply to the Arduino board with a simple message:
echo "Data received";
Now, we haven't actually talked about the database itself yet. There is a database.db
file included as an example in the code for this chapter, but I recommend you simply de-
lete it and try to make your own database. You can simply go to the directory in a terminal
where all files of the project are located and type:
sqlite3 database.db
Tip
If you are using Windows, I recommend using Console as a terminal. You can get it at the
following link http://sourceforge.net/projects/console/ .
Search WWH ::




Custom Search