Added sample code for alternative temperature measure and added method in README file.

This commit is contained in:
K 2024-10-07 00:03:57 +05:30
parent d1fb55e81a
commit b02d4d81ce
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
2 changed files with 49 additions and 0 deletions

View File

@ -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
View 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()