This is an Raspberry Pi based temperature and humidity logger that uses 2 (works also with 1) Adafruit DHT22 sensors for measurements. You might have seen similar kind of instructions before, but this one has a twist. It doesn't just read temperature and humidity from sensors, but it stores data to MySql database and provides means to read that temperature data with any web enabled device (computer, phone, tablet) web browser. This logger also allows you to set limits for temperatures and is able to send email if sensor temperature drops below set limit...this neat feature can be for example used to alarm when it is time to set heating on in your house / carage / greenhouse / you name it.

To complete this instructable, you will need following:

- Raspberry PI (I used Raspberry PI 1 model B+, but should be doable with other models as well)

- Power supply for the pi. (I used old Nokia microusb telephone charger which gives about 880mA)

- SD memory card. Note that you might need adapter. (I used 8Gb and had 4.1Gb available after installing everything)

- Ethernet cable or USB wlan dongle that is supported by Raspberry Pi (google can help with this)

- DHT22 sensor/s and 4.7 kOhm resistors. Instructable supports 2 sensors, but can be done with 1. https://www.adafruit.com/products/385

- Cables and breakout for connection https://www.adafruit.com/products/914

- Computer where you set up the SD card for the raspberry PI and from where you can connect and do the configurations. (NOTE: this instruction uses PC)

- Optional: Breadboard. Test assembly is easier to be created on this, than by connecting wires directly to sensors: http://thepihut.com/products/raspberry-pi-breadbo...

Internet connection is also require in location where you are going to set the raspberry logger. Either via ethernet or wlan dongle.

Keyboard + display are needed as well while installing and configuring the Wheezy OS. But once installations are finished, network is up and SSH enabled, it is possible to use connect raspberry pi remotely from PC and keyboard + display can be put aside.

Step 1: Sensor and RPI Assembly

NOTE- Assembly image updated 19.11.2015. There was error in last edition. -NOTE

Create assembly as instructed in attached image. Image instructs how you can connect two DHT22's to the Raspberry Pi, but if you have only one available just ignore another one and add one.

Notable here is that you can connect sensors to any GPIO you like. I had mine connected in GPIO22 and GPIO23. Both sensors also need +3.3Volts and ground. Resistor is set between +3.3V line and GPIO line.

When assembly is ready, prepare SD card and go to next step.

<p>Hello there.Nice tutorial. Working perfect with raspberry pi3. Can you please tell me if I can write the database to an external remote server instead of the raspberry pi3. I want the raspberry to read the data and to write them somewhere in a server like a cloud. Any help will be acceptable. Can I remotely write a database from raspberry? </p>
<p>see my solution in the comments below ;)</p>
<p>Hi, It should be doable. Config.json has mysql part (host,user,password etc.) change localhost to remote address of your database and check what happens.</p>
<p>Hi.Thanks for this great tutorial. I'm new to all of this and I'm stuck, so I really need your help. So my DHT22 comes with 3 pins instead of 4 as shown in the tutorial. It also comes with wires. So I made the connections as instucted, but on a breadboard. And I just cannot seem to get any reading. I tried Adafruits example code, I even tried writing some python code I found online, but to no avail. I have not connected any power source to the breadboard, because I figured that adding power source to the pi would be enough. I made connections in the following fashion:</p><p>-Sensor wire for each pin to Breadboard </p><p>-Jumper wires from same vertical row on breadboard straight to the pi pins</p><p>* And I did put a 10k ohm resistor between the 3.3v and data pin.</p><p>Any kind of help is welcome.</p><p>Thanks.</p>
<p>Hi, I'm a newbie to all this hardware stuff, too, and I had a similar problem. Turned out I didn't quite understand how breadboards work. I thought the + and - horizontal rows spanned the entire length of the board, but turns out they are separated into blocks or sections (2 on my board). Make sure you have the 3.3V and the GND lines attached to the breadboard block that contains the sensor wiring.</p><p>P.S. A Raspberry Pi T-cobbler/breakout board makes life much easier for prototyping circuits with a breadboard.</p>
<p>Hi, Based on this information, I cannot really help you out much. There is so much possibilities e.g. bad connections.</p><p> Do you happen to have multimeter available. With that you could check if PI is giving enough power to the power pins. And you could also check that you have the connection all the way from the PI to the sensors pins (e.g. with continuity test mode).</p><p>Otherwise, I think that image about the connections could be handy. Maybe it would show if there is something wrong.</p>
Hi! Great tutorial, I've made it with my Raspberry Pi 2 and I used 1k ohm resistor instead of 4,7k. It works good. Thank you for your time spent on this project! :)
<p>Hi. Good job. :)</p>
<p>Hi JJ,</p><p>Do you know the best way to save /home/pi/DHT22-TemperatureLogger/Backups every day to an flash drive?</p><p>Thx Jeroen</p>
<p>Hi,</p><p>I would mount the drive directly to RPI and change the SQL backup path from the config.json to point into this drive. And check that backup dump is enabled in config.json.</p>
<p>Hi JJ</p><p>I have a problem using mysql_connect in index.php as it is shown. I am using PHP 5.6 . I heard that this command was now obsolete. What do you recommend to solve this problem ?</p>
<p>Hi, You are right. It seems to be deprecated. <a href="http://php.net/manual/en/function.mysql-connect.php" rel="nofollow">http://php.net/manual/en/function.mysql-connect.ph...</a></p><p>I think the answer is mysqli_connect()</p><p><a href="http://php.net/manual/en/function.mysqli-connect.php" rel="nofollow">http://php.net/manual/en/function.mysqli-connect.p...</a></p>
<p>Hello ! I've got one simple question : does this can work on a raspberry pi 3 ? I saw on the github that there is a file to make it work on a RPI2, but is someone actually tried id on a RPI3 ? Thanks !</p>
<p>I have set it up without problems on raspberry pi 3 with last version of raspbian</p>
Yes its work on RPI3 i have OSMC and with thi os work perfect. Only have one problem ;) mysql write temp. to tabels but i cant get this on index.php
<p>I have not tested it with RPI3, or haven't heard about that if someone has tried. But I think however, that it should run just fine.</p>
<p>Super Tutorial! Thank you! I tweak it a bit as my Pi is now remote behind a 3G dongle and I cannot access it from the web. Using &quot;requests&quot; library I am posting the info from the Pi to a DB on the internet. I have altered the python script as follows:</p><p>at begining where all imports are following line is added: </p><p><strong>import requests<br></strong></p><p>at the end in the &quot;# insert values to db&quot; section following lines are added after databaseHelper(sqlCommand,&quot;Insert&quot;). For Sensor 1:</p><p><strong>postParameters = {'dateandtime':currentTime, 'sensor':sensor1, 'temperature':sensor1temperature, 'humidity':sensor1humidity}<br> postResponse = requests.post(url='http://your.url/posttemp.php',data=postParameters)<br></strong></p><p>For Sensor 2:</p><p><strong>postParameters = {'dateandtime':currentTime, 'sensor':sensor2, 'temperature':sensor2temperature, 'humidity':sensor2humidity}<br> postResponse = requests.post(url='http://your.url/posttemp.php',data=postParameters)<br></strong></p><p>posttemp.php file is as following:</p><p><strong>&lt;?php<br>$servername = &quot;localhost&quot;;<br>$username = &quot;remote_database_user&quot;;<br>$password = &quot;remote_database_password&quot;;<br>$dbname = &quot;remote_database&quot;;<br><br>$dateandtime=htmlspecialchars($_POST[&quot;dateandtime&quot;]);<br>$sensor=htmlspecialchars($_POST[&quot;sensor&quot;]);<br>$temperature=($_POST[&quot;temperature&quot;]);<br>$humidity=htmlspecialchars($_POST[&quot;humidity&quot;]);<br><br>// Create connection<br>$conn = mysqli_connect($servername, $username, $password, $dbname);<br>// Check connection<br>if (!$conn) {<br> die(&quot;Connection failed: &quot; . mysqli_connect_error());<br>}<br><br>$sql = &quot;INSERT INTO temperaturedata (dateandtime, sensor, temperature, humidity) VALUES ('&quot;.$dateandtime.&quot;', '&quot;.$sensor.&quot;', &quot;.$temperature.&quot;, &quot;.$humidity.&quot;)&quot;;<br><br>if (mysqli_query($conn, $sql)) {<br> echo &quot;New record created successfully&quot;;<br>} else {<br> echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . mysqli_error($conn);<br>}<br><br>mysqli_close($conn);<br>?&gt; <br></strong></p><p>than you can use the same php file to display the info. Please note that remote database is not regularly emptied as the one from the script.</p><p>Some (other) improvements that I have made:</p><p>- graph for temperature and humidity using PHPlot</p><p>- display of min/max Temperature/Humidity for last week and for all time period</p><p>- display of highest variation in between readings</p><p>- (WIP) start of a electrical heating radiator: on when temperature reaches 7&deg; and off when temperature reaches 11&deg;. The on switch seems easy as this is my min warning for inside sensor, but I will have to study the off at 11&deg; as my up warning mark is at 18&deg;</p><p></p><p></p>
<p>Hi! I made it! :D but somehow, the crontab won't work for me perfectly even the original script the the author made. i just dont know why :( so the i tried an alternate (Jobber). Credits to the author! ;)</p>
<p>HI, great tutorial. </p><p>I am having a problem in that when it takes a reading from the DHT22 it will write the data to the mysql db but at the same time it sends an email to me stating that it 'Failed to read &lt;sensor name&gt; sensor'</p><p>How can it read the sensor and write it to the table but then send a warning that it Failed to read the sensor?</p><br><p>Note that at 14:00:01 it posted a reading but at 2:00 PM I get an email:</p><p>'Failed to read LSW118 sensor'</p>
<p>At glance. It seems that you have defined to use 2 sensors, but are actually using only one. First one gets reading just fine, but when trying to read second. It fails as sensor doesn't exist?</p>
I did leave 2 sensors in config.json but put the sensor amount to be 1. Should I remove sensor2 in config.json?
<p>Yes you have. I, myself, tried to remove all the commands using the second sensor and this works fine.</p>
<p>Python script works very well. Saved me a lot of time in my own project. Instead of a LAMP server I'm trying to make a nodejs + express server (maybe switch to mongodb too, for learning purposes). I'll be adding some nice GUI touches like charts, semantic ui or material css etc :) and i'll be adding a pi camera for creating timelapses of my pepper growtent.</p>
<p>Hello. I am a newbie and I have 1 question. I would like to install 8 x temperature sensors AND 8 x humidity sensors in a room. Instead of using 4 sets of Raspberry Pis (each Pi connected to 2 x DHT22), is it possible to connect 8 x DHT22 to only 1 Raspberry Pi? Otherwise I think I've to spend more money to buy extra Raspberry Pi for every pair of DHT22. Thank you.</p>
<p>Hi, Well. Out of the box, this project doesn't support more than 2 sensors. It will only try and read 2 sensors. Code could be refactored a way that it reads more sensors. But unfortunately I don't have time or plans for that at the moment.</p>
<p>Lovely!!!</p><p>Thank you so much!</p><p>I saved a thousand dollars!</p><p>We are using it to monitor our vaccines in the fridge, someone was offering me a commercial solution for $1800. And i wondered really, does it REALLY take so much $$$ to make something like this. And thanks to you i fixed it with &lt; $100.</p><p>The other question would be how can i connect this to the AWS IoT service to enable SMS and calls?</p><p>I tried IFTTT but it doesnt seem to work very well, but anyway, email is good enough for now.</p>
<p>Hi, And sorry for the delayed response. Haven't got so much time for this project lately. However, I am very pleased to hear that this was such an useful for you.</p><p> And what comes to AWS IoT. I haven't really thought about if that could be done or what it would require. So I cannot give you any answers.</p>
<p>I'm not that familiar with AWS IoT, but like all other services they support MQTT protocol to communicate with other devices so I think it will be the best option since it's easy to implement and is the industry standard these days.</p><p>Here's a tutorial for it <a href="https://www.baldengineer.com/mqtt-tutorial.html" rel="nofollow"> https://www.baldengineer.com/mqtt-tutorial.html</a></p><p>By the way, I assume vaccines are very important for you, so I'd use one more monitor for it (possibly some really cheap arduino) with one more sensor and probably on-board gsm module plus battery pack - this way you'll have a really redundant solution.</p>
<p>Oh my, unlike this tutorial, the one recommended is way out of my reach, maybe a few more years of studying programming...</p>
<p>Have you tried this example to stream results to Plotly? It couild make some great graphs. I am going to set this up I have all the ingredients so wish me luck.</p>
<p>Hi, No, I haven't tried that. Please let us know how it goes. :)</p>
<p>thank you very much. you have provided me so much information that i have been looking for for a long time </p>
<p>Hi, I am glad to hear that. :)</p>
<p>Hi JJ,</p><p>Thanks for the great guide. It work perfectly but i got one small problem.</p><p>When i type sudo /usr/local/bin/noip2 I am getting this error message</p><p><br>Can't locate configuration file /usr/local/etc/no-ip2.conf. (Try -c). Ending!</p><p>Any idea to solve it?</p><p>Thanks, J</p>
<p>Hi, Unfortunately I haven't faced this myself.</p>
<p>can i use DHT11?</p>
<p>when i used DHT11 the python DHT22logger.py isn,t running correctly </p><p>this is the output. where is the error?</p>
<p>Hi, This project doesn't support DHT11. I don't have any DHT11 sensors available and thus cannot test it.</p>
<p>it works great with DHT11s. In the config.Janson you need to amend the &quot;sensortype&quot; to :&quot;11&quot;</p>
<p>Thanks for correction. :) What I meant was that it doesn't &quot;officially&quot; support DHT11 as I cannot test them, but glad to hear that everything works ok.</p>
<p>Did you test step 8 using </p><p><strong>sudo ./AdafruitDHT.py 11 [pin]<br></strong></p>
<p>Super project! Quick question on &quot;sensortype&quot; : &quot;11&quot; in the config.Janson, can you use more than 1 type for example a DHT11 and a DHT22 together? </p>
<p>Currently, there isn't possibility to use different sensors together. However It could be done quite easily by adding multiple sensor type configurations to config file and using them when reading data from sensors.</p>
<p>Great tutorial, worked out of the box for me!</p>
<p><a href="https://www.google.com/settings/security/lesssecureapps" rel="nofollow">https://www.google.com/settings/security/lesssecur...</a></p><p>If somebody can not send e-mail, check above link.</p><p>Thanks a lot for author of this turorial!!!!</p>
<p>Excellent tutorial, it does exactly what I need it to do.</p>
<p>Here's the output every minute from 2 DHT11s</p>
<p>This was awesome and really detailed. I especially liked the step-by-step walkthrough. First Pi project:)</p>
<p>I think i am having troubles with the index.html , &quot;Remember to change settings to match your MySql settings&quot;</p><p>Following your tutorial (using as user &quot;logger&quot;, as password &quot;password&quot;, and everything the same, I do not visualize the table though the web. Particulary, which points of the index.html must I take into account to make it work?</p><p>Please help, thanks In advance...</p>

About This Instructable




Bio: .NET developer working in healthcare business
More by jjpfin:Raspberry PI and DHT22 temperature and humidity logger with LAMP and Email warnings 
Add instructable to: