In this very first article of the website, I will show you how to monitor the temperature of a room using simply one Arduino board, a temperature sensor and your computer. This article perfectly respects the foundations of this website as it uses only open-source components. You can see it as the foundation of a more complex system to monitor information about your home from your computer.
First of all, you need an Arduino board. I used the most commonly used Arduino board for this tutorial: the Arduino Uno R3. You can buy your own here. However, any recent Arduino board like the Arduino Mega or the Arduino Due should also work fine. You will also need a temperature sensor. In this tutorial, I used an analog temperature sensor, the TMP36 temperature sensor. This sensor delivers an analog voltage, which is proportional to the ambient temperature. You can use another temperature sensor, which would also work fine. I will show you how to use a digital temperature sensor in another tutorial. Finally, you will need a breadboard and some jumper cables to connect the different parts of the system. Here is a summary of all the components you will need for this project:
On the software side, you will need the Arduino software to develop the firmware and to upload it to the Arduino board. Because the measured temperature will be uploaded to a distant server, you will need a web server running PHP where you can put the necessary files. If you do not have your own server yet, you can emulate your own server on your computer. I recommend the following software:
You will also need the Python serial module (http://pyserial.sourceforge.net).
The hardware configuration for this project is quite simple. The only thing you need to connect is the temperature sensor to the Arduino board. Usually, an analog temperature sensor like the TMP36 has three pins: a pin for the positive power supply, one for the ground, and one in the middle for the signal itself. Connect the sensor to your Arduino board as shown on this picture:
Now that we have the hardware assembled, we can test the components of the system. First, the temperature sensor. As it is connected to the analog 0 port of the Arduino, we can read back this value to the Arduino board using:
raw_temperature = analogRead(0);
Of course, this is not the temperature, but a value from 0 to 1023 that represents the input voltage at the analog 0 pin. The company that produces the sensor gives the following formula to convert this to a temperature:
temperature = (raw_temperature*0.004882814 - 0.5) * 100;
At this point, we will just record the raw temperature to see if the sensor is working. The temperature conversion will be done later in a Python script. Finally, this is the code for this part:You can now upload the code to your Arduino board, and open the Serial monitor. This should print a value that is proportional to the ambient temperature every 100 ms in the serial monitor:
You can try to see if the sensor is working by just pinching the sensor between your fingers, thus producing a raise of the temperature.
There are two steps needed to remotely access to the temperature measurements. The first one is to get the data on your computer using a Python script, and the second step is to upload this data on a distant server. Luckily, it is easy to upload a file to a remote server using Python, so we can do all in a single Python script. First, we have to get the temperature data on your computer. This is done using the serial module for Python:
Initializing the connection to the board is then very simple:
ser = serial.Serial('/dev/tty.usbmodem1421', 9600)
Of course, you will have to replace the tty.usbmodem1421 by the name of your own serial USB port. You can find it in the Arduino software, under Tools/Serial Port. Then, to read data from the Arduino, you can just use:
The data is then written into a file:
f = open('temperatures.txt', 'w') f.write(str(temp) + '\n') f.close()
These are the basic commands for the recording part. Now, to upload the file to a server, we will need the ftplib module:
At this point we need to open the connection to your FTP server:
s = ftplib.FTP('server_url','user','password')
Of course, you will need to modify the arguments of this function with the settings of your FTP server. Finally, we can upload the temperature file with:
s.storbinary('STOR temperatures.txt', f)
The final Python script can be found in the GitHub repository of the project with all the other files developed for this tutorial. Finally, we need to display the data contained in the temperature file. This can be done in many ways, but I used a simple PHP script to display the data:
This page has to be placed in the same folder where you uploaded the temperature file. If everything runs well, you should see the temperature displayed in your browser. That’s all for this really basic article, hope you enjoyed it! Of course you can use what you’ve learned in this project to create more complex projects & command them via Python, directly from your computer.
I wrote a free step-by-step tutorial to show you how to build a simple alarm system with Arduino. It will show you everything you need to build this simple project & also teach you how to use the Arduino platform at the same time. To get this free guide, simply click on the button below: