Paspberry Pi temperature, humidity monitoring and smoke detector system

This is my latest Raspberry Pi project I have been working on and off for a while and finally decided to finish it.

This project monitors temperature, humidity and detects smoke and other gasses and sends email alerts. There is also web dashboard with live readings and chart which is updated whenever the second script runs. The sensors I used are FC-22 MQ-2 gas sensor and AM2302 Digital Temperature and Humidity Sensor with 10k Ohm resistor. The system consists of two scripts and php page. The first one reads sensor data and writes values to www directory and if any of the values goes above the thresholds set it sends an email. The email is sent using gmail SMTP. The second script reads those values and add them to csv file which the is red by python Matplotlib module and pretty chart is created:


The green line represents humidity the blue represents temperature and the red represents smoke detection.
The php page is set to auto refresh and it reads values the first script writes to text files in the www directory and also displays the chart above.
Sensor wiring:

wiringThe sensitivity on the smoke sensor can be adjusted by turning the little phillips screw.

Dependencies for python Matplotlib:


Dependencies for AM2302:

PHP page code saved in /var/www/ directory as index.php:


Script number one:



Script number two:

Since pasting the script screwed up the indentations and Python worries a lot about them you can download the files here

Both scripts are executed by crontab and the second script is set to run every 10 minutes which means that the csv file is updated with 10 minute intervals:

During this project I learned to how important it is to put “#!/usr/bin/python” in the python scripts and that naming scripts in the same name as modules is a bad idea.

2 thoughts on “Paspberry Pi temperature, humidity monitoring and smoke detector system

  1. Hi,

    I have to admit that you have made realy good job with these scripts.


    Is there any possibility to play sound when smoke is detected?

    (somewhere in script number one in # Smoke detector section)

    Something like:

    “if newState = “Smoke detected!” thae play /home/pi/alarm.wav”


    Unfortunately, I do not know Python good enough to write it by myself hence my question and polite request 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *