SmartHome project: Database

Hello everyone. This week, we continue our Smart home project. In the last session, we established the communication between the Arduino and the Rasperry Pi. We can now receive information of the two sensors and send a setpoint from the raspberry Pi to the Arduino Card.  See the following schematic view which explains our project.

photo article 220415

Before explaining the PHP in a next article, we will describe how we store our luminosity values into the SQL database. See the following code with the comments. With this code, each 5 seconds, the value of the luminosity is readed from the Arduino by the Rasperry PI and stored into a database.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import serial

import time

import MySQLdb

while True:

port = “/dev/ttyACM0”

serialFromArduino = serial.Serial(port,9600)    //Starting the connection with the Arduino


input = serialFromArduino.readline()     //Reading the value of the Arduino

inputAsInteger = float(input)

test = 10.0


ser = serial.Serial(‘/dev/ttyACM0’, 9600)

time.sleep(4)   //We wait after the arduino is ready.

db = MySQLdb.connect(“localhost”, “monitor”, “password”, “Luminosity”)   //We open the database with its location, name, password and datatype added.


# We are using triple quotes for formatting purposes

# you can use one set of double quotes if you put the whole string on one line


curs.execute (“””INSERT INTO lumdat

values(CURRENT_DATE() – INTERVAL 1 DAY, NOW(), ‘Outdoor’, inputAsInteger)”””)  //Reading the date and the hour from the Rasperry PI. We add also the luminosity value.


print “Data committed”


print “Error: the database is being rolled back”


db.close()       //The database is closed after each value added.



time.sleep(1)              // We wait one second

ser = serial.Serial()

Sending the luminosity value to the raspberry Pi and storing it into a database with MYSQL work fine but we also want to display this database by using PHP. The next article will describe how we made the communication between the database and the website by using PHP.

That’s all for today, see you next week.

The SmartHome team

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.