kenn13542
Posts: 3
Joined: Fri Oct 30, 2015 7:53 am

Displaying Sensor Data on the Web

Fri Oct 30, 2015 8:05 am

Hi everyone, I need help with a project of mine and I can't find any straightforward solutions to do what I want. I have a webserver running on my home network and I am wanting to display just plain text from my DHT22, humidity and temperature sensor on the web page.

I have found a working python script for displaying the temperature and humidity in the terminal from Adafruit, which works great (attached). However, I now want to be able to run that script and output the data as text on my web page every time someone loads the page.

So my question is, is there any simple way to just run the script and display the output on a html website? Thanks

Code: Select all

#!/usr/bin/python

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

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_DHT

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22

# Example using a Beaglebone Black with DHT sensor
# connected to pin P8_11.
#pin = 'P8_11'

# Example using a Raspberry Pi with DHT sensor
# connected to GPIO23.
pin = 4

# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).  
# If this happens try again!
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 get reading. Try again!'

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Displaying Sensor Data on the Web

Fri Oct 30, 2015 11:19 am

Hi Kenn,

If you install this library https://github.com/dddomodossola/remi , you can use the following code I reworked for you.

Code: Select all

#!/usr/bin/python

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

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_DHT
import remi.gui as gui
from remi import start, App

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22

   
class MyApp(App):

    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

    def idle(self):
        """ Usefull function to schedule tasks. 
        Called every configuration.UPDATE_ITERVAL """
        super(MyApp, self).idle()

        global sensor
        # Example using a Beaglebone Black with DHT sensor
        # connected to pin P8_11.
        #pin = 'P8_11'
        
        # Example using a Raspberry Pi with DHT sensor
        # connected to GPIO23.
        pin = 4
        
        # Try to grab a sensor reading.  Use the read_retry method which will retry up
        # to 15 times to get a sensor reading (waiting 2 seconds between each retry).
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        
        # Note that sometimes you won't get a reading and
        # the results will be null (because Linux can't
        # guarantee the timing of calls to read the sensor).  
        # If this happens try again!
        if humidity is not None and temperature is not None:
            self.lbl.set_text( 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity) )
        else:
            self.lbl.set_text( 'Failed to get reading. Try again!' )

    def main(self, name='world'):
        self.i=0
        # the arguments are	width - height - layoutOrientationOrizontal
        wid = gui.Widget(120, 100, False, 10)
        self.lbl = gui.Label(100, 30, 'Hello %s!' % name)
        self.bt = gui.Button(100, 30, 'Press me!')

        # setting the listener for the onclick event of the button
        self.bt.set_on_click_listener(self, 'on_button_pressed')

        # appending a widget to another, the first argument is a string key
        wid.append('1', self.lbl)
        wid.append('2', self.bt)

        # returning the root widget
        return wid

    # listener function
    def on_button_pressed(self):
        self.lbl.set_text('Button pressed!')
        self.bt.set_text('Hi!')


if __name__ == "__main__":
	# setting up remi debug level 
	#       2=all debug messages   1=error messages   0=no messages
    import remi.server
    remi.server.DEBUG_MODE = 2 

	# starts the webserver
	# optional parameters   
	#       start(MyApp,address='127.0.0.1', port=8081, multiple_instance=False,enable_file_cache=True, update_interval=0.1, start_browser=True)
    start(MyApp,address='',update_interval=2.0)
I can't test it because I do not have my raspi with me now.
And furthermore, with this library you can remove your webserver. It is integrated in the library.

kenn13542
Posts: 3
Joined: Fri Oct 30, 2015 7:53 am

Re: Displaying Sensor Data on the Web

Fri Oct 30, 2015 11:38 am

Hi dddomodossola,
Thanks for the code. I already have a working web server with html, css and a couple of php files that control pins. How can I incorporate this code with my existing work, just to add the DHT22 output to the page. I appreciate the help!

Thanks, Kenn

kenn13542
Posts: 3
Joined: Fri Oct 30, 2015 7:53 am

Re: Displaying Sensor Data on the Web

Sat Oct 31, 2015 2:44 am

Hi dddomodossola,
I tried to encorporate the code you gave me with my existing web server but I couldn't get it to work.

Is there another option that I can still use all the html, css and the php scripts that control the pins on the Pi, without having to modify or change my web server much. It shouldn't be that hard, I wouldn't think at least, haha.

Thanks, Ken

Return to “Python”