Page 1 of 1

DHT22 readings onto a web page

Posted: Wed Nov 26, 2014 10:11 pm
by Mettauk
I want to include temperature and humidity readings on a web page.

I can get readings via python using ssh. It would be nice if it auto updated on the web page but I would be very happy for it to simply load the latest readings when the page loads.

I am not very familiar with python or php but know some html and I can't find a straight forward solution to get the "python call" on to the web page. I've managed to get the sensor working but don't know a "simple" way to put readings on a web page?

The small python script that checks the sensor and prints current temp and humidity is below (From Adafruit DHT)

Code: Select all

#!/usr/bin/python
#simpletest.py
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# edit...

import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 25
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
   print 'Temp:{0:0.1f}*C  Humidity:{1:0.1f}%'.format(temperature, humidity)
else:
   print 'Failed to obtain a reading. Try again!
I currently have two web style pages, one php with is the Pi camera page and another for gpio switching for a relay which I modified from others but cannot find "simple" way to include the temp and humidity readings?

Re: DHT22 readings onto a web page

Posted: Thu Nov 27, 2014 10:09 am
by rpdom
One of the easiest ways to get information on a web page is to make it a PHP page. Make sure you have PHP installed for your webserver. For apache that is libapache2-mod-php5.

Create the HTML page as normal, but call it something.php instead of something.html.

Stick a bit of PHP where you want the output displayed.

Code: Select all

Output from sensor:<br>
<!-- code is html up to here -->
<?php
passthru("sudo /home/pi/mycode");
?>
<!-- now back to html -->
<br>
That displays the output from the python program "as-is". You could use exec() in the PHP to put the output in a variable for processing too.

You will have to add an entry to the sudoers files to let the webserver run the code using sudo (I assume it needs sudo, as it is using gpios). The webserver usually runs under user www-data, so sudo nano /etc/sudoers/websudo and add a line like this

Code: Select all

www-data       raspberrypi=NOPASSWD:  /home/pi/mycode
Which will restrict the webserver to being able to use sudo with that program only.

Re: DHT22 readings onto a web page

Posted: Fri Nov 28, 2014 2:13 pm
by Mettauk
Thanks for this it sounds like what I'm looking for.

Once the include part is done, how do I get the python code to run so it shows up? Say using the code in my first post?

Re: DHT22 readings onto a web page

Posted: Fri Nov 28, 2014 5:27 pm
by rpdom
Save the code somewhere and change the "/home/pi/mycode" bits in what I posted to the full name of the file.

Re: DHT22 readings onto a web page

Posted: Fri Nov 28, 2014 6:51 pm
by Mettauk
Hi I've done this as follows (test.php)

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>Test humtemp</head>
</head>
<body>
<p>Output from sensor:<br>
<!-- code is html up to here -->
<?php
passthru("sudo /home/pi/humtemp.py");
?>
<!-- now back to html -->
<br>
<p>Next item...</p>
</body>
</html>
On inspecting test.php code once loaded I get

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title>Test humtemp</head>
</head>
<body>
<p>Output from sensor:<br>
<!-- code is html up to here -->
<!-- now back to html -->
<br>
<p>Next item...</p>
</body>
</html>
So no result ?
Your code (modified) is in /etc/sudoers.d as websudo as

Code: Select all

www-data       ALL=NOPASSWD:  /home/pi/humtemp.py
So i'm guessing i am missing something?
(humtemp.py is the same code as in my original post.)

Re: DHT22 readings onto a web page

Posted: Fri Nov 28, 2014 8:34 pm
by rpdom
Yes, it looks like something is missing. You should see the output from the python program in there.

Perhaps it is permissions?

Has the program got rwxr-xr-x permissions when you "ls -l humtemp.py"?

Otherwise check the webserver log files for error messages. For Apache it will be /var/log/apache2/error.log

Re: DHT22 readings onto a web page

Posted: Sat Nov 29, 2014 12:40 am
by Mettauk
Thanks for your help, it;'s now working just fine. It was a permissions issue.
..You could use exec() in the PHP to put the output in a variable for processing too.
Would this be within a form field as something like

Code: Select all

<input type="text" name="Temp1" size="20" value="<% exec ("temperature") %>"><input type="submit" value="Send" name="B1">
I come with some knowedge of ASP so please forgive my use of <%=xyz%>?

Re: DHT22 readings onto a web page

Posted: Sat Nov 29, 2014 6:59 am
by rpdom
I was thinking along the lines of

Code: Select all

<?php

$centigrade = exec("temperature");
$fahrenheit = $centigrade / 5 * 9 + 32;
printf( "%5.2f", "$fahrenheit" );
?>
as an example of conversion and formatting. Plus any other server-side processing you may wish to do.

Re: DHT22 readings onto a web page

Posted: Tue Dec 16, 2014 8:13 pm
by rawilson3
Does anyone have a working page example I could see. I am trying to accomplish this exact thing.

Re: DHT22 readings onto a web page

Posted: Tue Dec 16, 2014 8:18 pm
by rawilson3
Is there a working example I could see? I am trying to do the same thing with my DHT22 and Pi

Re: DHT22 readings onto a web page

Posted: Wed Dec 17, 2014 12:25 am
by Mettauk
If you follow the previous posts it works perfectly.

I will post a clear code
series when I get the chance in the next day or so.

Re: DHT22 readings onto a web page

Posted: Thu Dec 18, 2014 1:27 am
by rawilson3
Thanks! That will be great.

Re: DHT22 readings onto a web page

Posted: Fri Dec 19, 2014 5:21 pm
by Mettauk
OK here goes my first attempt at writing down how I did it (or anything Pi)!

First update your Pi

Code: Select all

sudo apt-get update
sudo apt-get dist-upgrade
sudo rpi-update

sudo apt-get clean
Then install Python bits (not sure you really need this but I have it for relay switching)

Code: Select all

sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio
Then you need the DHT22 bits from Adafruit which is "Adafruit_Python_DHT-master"

Code: Select all

https://github.com/adafruit/Adafruit_Python_DHT/archive/master.zip
Unzip it on your Pi and then install it with

Code: Select all

sudo python setup.py install
Once installed you should be able to test it with the examples, just follow the gude in one of the files. I used simpletest.py in the examples folder and editied the GPIO to the one I used.
If you do not get a result here you are doing something wrong. If it works then you can get it to display on a web page.

Next steps to get output onto say index.php in /var/www/ which I assume you already have?! If not you need to do that too.

From now on follow the instructions at the start of this thread from rpdom

You need to be very careful editing sudoers or sudoers.d or you can damage your version of Raspbain! Try it without first just make sure your python script has execute permissions set.

You need to be very careful editing sudoers or sudoers.d or you can damage your version of Raspbain! Try it without first just make sure your python script has exicute permissions set.

Below is another version of the python script I used that displayed well on a web page even if the code is probably poorly set out!

Code: Select all

[#!/usr/bin/python

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# edit
print 'Getting the temperature for you, please wait...'

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22

pin = 25

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)


if humidity is not None and temperature is not None:
	print 'T {0:0.1f}*C  H {1:0.1f}%'.format(temperature, humidity)
else:
	print 'Failed to get a reading. Please try again!'
if temperature >= 22:
        words = 'Wow its hot, a blistering'
elif temperature >= 18:
        words = 'Nice and warm out there at'
elif temperature >= 15:
        words = 'Its OK out there, about'
elif temperature >= 9:
        words = 'Its over 10 so not to bad at'
elif temperature >= 5:
        words = 'Its cold, quite cold, but not that bad! Its only'
elif temperature >= 2:
        words = 'Its very cold, so very cold, but not quite freezing, it just feels like it at'
else:
        words = 'Really is freezing out there right now!!! Its'

print words,"{:2.2f}".format(temperature),'*C'
Thats it.

Oh and if you or anyone else knows how to get the degree symbol to work and show in python and a web page that would be very cool! Thats the ° Degree Symbol

Re: DHT22 readings onto a web page

Posted: Fri Dec 19, 2014 6:58 pm
by rpdom
Mettauk wrote:Oh and if you or anyone else knows how to get the degree symbol to work and show in python and a web page that would be very cool!
Well, in HTML, it's &deg; and on my keyboard it is Shift-AltGr-0 (zero) °

Re: DHT22 readings onto a web page

Posted: Fri Dec 19, 2014 7:43 pm
by DougieLawson
Mettauk wrote:
Oh and if you or anyone else knows how to get the degree symbol to work and show in python and a web page that would be very cool! Thats the ° Degree Symbol
http://www.raspberrypi.org/forums/viewt ... 38&t=74106

Re: DHT22 readings onto a web page

Posted: Sun Dec 21, 2014 4:47 pm
by rawilson3
Thank you very much. I will be giving this a go!

Re: DHT22 readings onto a web page

Posted: Sat Jan 09, 2016 3:11 pm
by DEE723
Hello
I'm trying to do the same thing as the first poster. I have a relay board I control some lights on things and that all works great, I would like to have the temperature and humidity to show on the same page. I have followed the instructions on this form but no display on webpage. Its been a long time since I have done any HTML but its slowly coming back. I can get the temperature and humidity to display in the terminal using this string,

sudo /home/pi/sources/Adafruit_Python_DHT/examples/AdafruitDHT.py 2302 6
or even, sudo /home/pi/sources/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 6
but I can not get it to show on even a simple webpage.
I think I have done every thing right here maybe an update changed something, or I just over looked something,
like I said its been a long time that I did any HTML.

Thank you