Added sample code for alternative temperature measure and added method in README file.
This commit is contained in:
parent
d1fb55e81a
commit
b02d4d81ce
28
README.md
28
README.md
@ -3,3 +3,31 @@
|
|||||||
This project is a uses the AdaFruit libraries for sensing temperature using DHT11 sensor and a Raspberry Pi.
|
This project is a uses the AdaFruit libraries for sensing temperature using DHT11 sensor and a Raspberry Pi.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
|
## Alternative method
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> This alternative method uses the `DHT11` python library for reading the sensor data.
|
||||||
|
> This is a pure Python library for reading DHT11 sensor on Raspberry Pi.
|
||||||
|
|
||||||
|
1. Install prerequisites:
|
||||||
|
```shell
|
||||||
|
sudo apt update &&\
|
||||||
|
sudo apt upgrade -y &&\
|
||||||
|
sudo apt install python3 python3-pip -y
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Setup a virtual environment & install the library:
|
||||||
|
```shell
|
||||||
|
mkdir /home/$(whoami)/temp-sensor
|
||||||
|
cd /home/$(whoami)/temp-sensor
|
||||||
|
python3 -m venv .
|
||||||
|
source bin/activate
|
||||||
|
pip install dht11 setuptools RPi.GPIO
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Run the code [alt-temp.py](https://git.kska.io/notkshitij/DHT11/src/branch/main/alt-temp.py)
|
||||||
|
|
||||||
|
---
|
||||||
|
21
alt-temp.py
Normal file
21
alt-temp.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# NOTE: GPIO PIN 16 used
|
||||||
|
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
import dht11
|
||||||
|
|
||||||
|
# initialize GPIO
|
||||||
|
GPIO.setwarnings(False)
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
GPIO.cleanup()
|
||||||
|
|
||||||
|
# read data using pin 16
|
||||||
|
instance = dht11.DHT11(pin = 16)
|
||||||
|
result = instance.read()
|
||||||
|
|
||||||
|
if result.is_valid():
|
||||||
|
print("Temperature: %-3.1f C" % result.temperature)
|
||||||
|
print("Humidity: %-3.1f %%" % result.humidity)
|
||||||
|
else:
|
||||||
|
print("Error: %d" % result.error_code)
|
||||||
|
|
||||||
|
GPIO.cleanup()
|
Loading…
Reference in New Issue
Block a user