Introduction: Smart Home With Arduino Ethernet Shield and Teleduino (with Web App)
This project is a simple solution for those who want a smart-home system but do not have a big budget and for the lazy ones that do not want to get up from bed to turn the lights off or on.
It also is the project of a high school student for his final exams!
This is just a basic configuration but with a little more time and dedication you could customize by adding more sensors, LEDs, relays and scheduled jobs to start with the execution of a PHP script from the web server.
I am using a free web-hosting service called Altervista which includes the SQL database server (with phpMyAdmin) and the php server.
Step 1: Hardware & Software
To realize this project you will need:
- Arduino (Mega or similar, the Uno will probably not have enough memory)
- Ethernet Shield
- 3 DHT 11 (temperature and humidity sensor)
- 3 photoresistors
- 220v Relay
- 5V fan
- NPN 337 transistor
- 16 x 2 LCD
- Resistors (220 Ω, 1 kΩ, 10 kΩ, etc)
- Lunch box
- web server (Apache)
- database and DMBS (MySQL & phpMyAdmin)
- php server
- Arduino IDE
Step 2: Teleduino
So, to send the requests from the Internet to Arduino we will use a web service called Teleduino.
Just register and wait for the email containing the unique key, after that follow the instructions on how to install the libraries and how to turn a LED on or off.
Make sure to download and correctly link the library.
If you have any problems you can contact the developer of this web service.
Step 3: The Database
In order to save the data from the sensors we will need to set up a MySql database.
In this case there are 2 tables: sensor_log and users.
CREATE TABLE `sensor_log` ( `id` smallint(6) PRIMARY KEY AUTO_INCREMENT, `timeStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `temp_0` float DEFAULT NULL, `hum_0` float DEFAULT NULL, `temp_1` float DEFAULT NULL, `hum_1` float DEFAULT NULL, `temp_2` float DEFAULT NULL, `hum_2` float DEFAULT NULL, `light_0` float DEFAULT NULL, `light_1` float DEFAULT NULL, `light_2` float DEFAULT NULL, ); CREATE TABLE `users` ( `username` char(30) PRIMARY KEY, `password` char(50) NOT NULL UNIQUE, `is_connected` tinyint(1) NOT NULL DEFAULT '0', );
Step 4: The Arduino Sketch
The Arduino I am using for this project is the Mega 2560 so you
will have to adjust the functions / pins / libraries in the .ino file if you are using a different board.
The sketch's logic is pretty simple:
In the setup() function the Arduino gets an IP address, connects to the Teleduino server and initializes the sensors.
In the loop() function it loops until a request arrives and every 60 seconds it gets the data from the sensors and sends it with the POST method to a php page that adds it to the database.
There is a custom function called .getReset() and to make it work the files Teleduino2560.cpp and Teleduino2560.h (in the libraries/Teleduino2560 folder) must be replaced.
Step 5: The Web App
The web app is composed by:
- add.php ⇒ gets the data from the Arduino and puts it into the database
- index.php ⇒ contains the login form
- login.php ⇒ verifies the username / password
- loading.php ⇒ initializes the Arduino pins with the Teleduino links
- home.php ⇒ contains the buttons to interract with the Arduino and the graphs with the data
- logout.php ⇒ terminates the connection to the web app
- aboutproject.php ⇒ contains the description of the project (for exam purpose)
The buttons send the GET request to the Teleduino server that turns on / off the pin on the Arduino.
The charts are made with a library called CanvasJS.
You will find everything in the .zip file.
N.B. You will have to add the Teleduino key and the credentials to access the database !