In this example, we’ll use a Raspberry Pi and a DHT22 temperature and humidity sensor to print values on a web interface.



1. Hardware

Wire the DHT22 following the diagram. Note the resistor is a 10kΩ.



2. Software

OS : Linux Raspbian Jessie Lite.

2.1 Installation

Install the required software:

> apt-get update > sudo apt-get install build-essential python-dev git

Install the library from Adafruit :

> git clone https://github.com/adafruit/Adafruit_Python_DHT.git > cd Adafruit_Python_DHT/ > sudo python setup.py install

2.2 Tests

> cd ~ > vi test.py

And paste the following :

#!/usr/bin/python import Adafruit_DHT # Adafruit_DHT.DHT11, Adafruit_DHT.DHT22 or Adafruit_DHT.AM2302. sensor = Adafruit_DHT.DHT22 pin = 4 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 get reading. Try again!'

Make it executable :

> chmod +x test.py

And run it :

> sudo ./test.py Temp=30.7*C Humidity=38.9%

Working !

3. Web UI

3.1 Required software

Install the required software :

> sudo apt-get install python-pip > sudo pip install flask

3.2 Main file

Edit the file :

> vim main.py

With this content :

#!/usr/bin/python from flask import Flask, render_template import Adafruit_DHT app = Flask(__name__) sensor = Adafruit_DHT.DHT22 pin = 4 @app.route("/") def main(): humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) templateData = { 'temperature' : temperature, 'humidity': humidity } return render_template('main.html', **templateData) if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True)

Make it executable :

> chmod +x server.py

3.2 Template

Edit the template file :

> vi templates/main.html

With this content :

Dynamic Resize

3.3 Static files

I use the files from justGage.com :

> wget http://justgage.com/download/justgage-1.2.2.zip > unzip justgage-1.2.2.zip > mkdir static > cp justgage-1.2.2/*.js static/

3.4 Run

Then we can run :

> sudo ./main.py * Running on http://0.0.0.0:80/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 867-781-657 192.168.137.1 - - [20/Mar/2016 10:47:04] "GET / HTTP/1.1" 200 - 192.168.137.1 - - [20/Mar/2016 10:47:04] "GET /static/raphael-2.1.4.min.js HTTP/1.1" 200 - 192.168.137.1 - - [20/Mar/2016 10:47:04] "GET /static/justgage.js HTTP/1.1" 200 -

Then open a browser and go to X.X.X.X:80 (replace by the Pi IP) :



