Small project on a raspberry pi
- to collect temperature and humidity data via DHT 11 sensor
- run webserver (ngnix) on raspberry pi
- publish a local website to show data as graph (d3.js)
# DHT-11
._________________.
|.................|__ 4 -> GND (-) -> Ground
|.................|__ 3 -> Into nowhere
|.................|__ 2 -> Resistor (4,7kOhm) -> GPIO 4 {Resistor End to 3V3 Power}
|_________________|__ 1 -> 3V3 Power (+) -> Power
Do some preperation
sudo apt-get update
sudo apt-get install build-essential python-dev
Clone the GitHub Repository
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
Install the script from repository
cd Adafruit_Python_DHT/
sudo python setup.py install
Traceback (most recent call last):
File "setup.py", line 1, in <module>
from setuptools import setup, find_packages, Extension
ImportError: No module named setuptools
Install pythonX-setuptools
# To install setuptools on Debian:
sudo apt-get install python3-setuptools
# For an older version of Python (Python 2.x):
sudo apt-get install python-setuptools
Go to example folder
cd examples
sudo ./AdafruitDHT.py 11 4
The first parameter (11) indicates which sensor was used (22 for the DHT22) and the second, to which GPIO it is connected (not the pin number, but the GPIO number).
# This produces an output like the following:
emp=24.0* Humidity=41.0%
Attention: The sensors are only ready every two seconds. Be careful not to start a query every second.
To integrate the Raspberry Pi humidity library into other (Python) projects, you only need the following:
import Adafruit_DHT
# ...
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
#...
You can use the script Write data into csv-file
💡 Executive file must be (
sudo chmod +x name-of-csv-file.xyz
)
Add this to crontab -e
# Every 5min
5 * * * * python /home/pi/PATH/TO/SCRPT
Use root's crontab
Run the following command:
sudo crontab -e
#### Syntax
The syntax is:
1 2 3 4 5 /path/to/command arg1 arg2
OR
1 2 3 4 5 /root/backup.sh
Where,
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command – Script or command name to schedule
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
How to setup a website on the raspberry. Only with open source software
💡 Nginx is running on Port 80, make sure nothing else is running there! (Apache 2, PiHole,..)
# Make sure everything is up to date
apt-get update
apt-get upgrade
- Install Nginx
sudo apt install nginx
- Check if it works
# On Raspberry PI
curl localhost
# On any other maschine
curl <IP-of-Raspberry-Pi>
We just issue the following command to make sure the nginx webserver is always started on bootup:
sudo update-rc.d -f nginx defaults;
Go to /var/www/html/
and edit everything you want. You have to use sudo!
💡 CSV-file must be in the same folder! CSV-file must be (
sudo chmod +x name-of-csv-file.csv
)
You can find the HTML-file here And the JS-files here (index.js) and here (grid.js)
Date,Disk_usage,CPU,Memory,CPU_TEMP,DHT11_Temperature,DHT11_Humidity
2021-12-02 20:05:05.336617,14.7,0.0,0,23.8,15.0,60.0
2021-12-02 20:08:18.767502,14.7,0.0,0,23.5,17.0,61.0
2021-12-02 20:09:30.924323,14.7,5.1,0,23.4,20.0,62.0