Compare commits
No commits in common. "93e6e5e2116b85f7a2df9857e120053fa390a0d6" and "13863a706d3a3dd700970849f2607f486829d7bc" have entirely different histories.
93e6e5e211
...
13863a706d
39
README.md
39
README.md
@ -1,6 +1,6 @@
|
|||||||
# DHT Humidity Sensing on Raspberry Pi
|
# DHT Humidity Sensing on Raspberry Pi
|
||||||
|
|
||||||
This project is a uses the basic DHT11 python library (main method) and AdaFruit libraries (alternative method) 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.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -10,13 +10,13 @@ This project is a uses the basic DHT11 python library (main method) and AdaFruit
|
|||||||
## Main method
|
## Main method
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> Main method uses the `DHT11` python library for reading the sensor data.
|
> This is the main method, using AdaFruit library. For an easier method, checkout [## Alternative method](https://git.kska.io/notkshitij/DHT11#alternative-method)
|
||||||
> This is a pure Python library for reading DHT11 sensor on Raspberry Pi.
|
|
||||||
|
|
||||||
1. Install prerequisites:
|
1. Install some prerequisite packages:
|
||||||
```shell
|
```shell
|
||||||
sudo apt update &&\
|
sudo apt update &&\
|
||||||
sudo apt install -y git
|
sudo apt install git -y
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Clone the project:
|
2. Clone the project:
|
||||||
@ -44,31 +44,28 @@ source setup.sh
|
|||||||
## Alternative method
|
## Alternative method
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> This is the alternative method, using **AdaFruit library**. This method doesnt' seem to work on our college Raspberry Pi 3's. For easiler setup, checkout [## Main method](https://git.kska.io/notkshitij/DHT11#main-method)
|
> 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 prerequisite packages:
|
1. Install prerequisites:
|
||||||
```shell
|
```shell
|
||||||
sudo apt update &&\
|
sudo apt update &&\
|
||||||
sudo apt install git -y
|
sudo apt upgrade -y &&\
|
||||||
|
sudo apt install python3 python3-pip -y
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Clone the project:
|
2. Setup a virtual environment & install the library:
|
||||||
```shell
|
```shell
|
||||||
git clone https://git.kska.io/notkshitij/DHT11.git
|
mkdir /home/$(whoami)/temp-sensor
|
||||||
|
cd /home/$(whoami)/temp-sensor
|
||||||
|
python3 -m venv .
|
||||||
|
source bin/activate
|
||||||
|
pip install dht11 setuptools RPi.GPIO
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> Alternatively, you can also [download the zip file](https://git.kska.io/notkshitij/DHT11/archive/main.zip)
|
3. **Download** and **run** the code [alt-temp.py](https://git.kska.io/notkshitij/DHT11/src/branch/main/alt-temp.py)
|
||||||
|
|
||||||
3. Change the current working directory to the folder in which the project was cloned:
|
|
||||||
```shell
|
|
||||||
cd ./DHT11
|
|
||||||
```
|
|
||||||
|
|
||||||
4. Run the `ada-setup.sh` script:
|
|
||||||
```shell
|
|
||||||
source ada-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> After running the code, and completing the execution, run `deactivate` in the terminal to exit the virtual environment.
|
> After running the code, and completing the execution, run `deactivate` in the terminal to exit the virtual environment.
|
||||||
|
97
ada-setup.sh
97
ada-setup.sh
@ -1,97 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
cat << "EOF"
|
|
||||||
|
|
||||||
__| |______________________________________________________________________| |__
|
|
||||||
__ ______________________________________________________________________ __
|
|
||||||
| | | |
|
|
||||||
| | | |
|
|
||||||
| | ____ _ _ _ | |
|
|
||||||
| | | _ \ ___ ___(_) __ _ _ __ ___ __| | __ _ _ __ __| | | |
|
|
||||||
| | | | | |/ _ \/ __| |/ _` | '_ \ / _ \/ _` | / _` | '_ \ / _` | | |
|
|
||||||
| | | |_| | __/\__ \ | (_| | | | | __/ (_| | | (_| | | | | (_| | | |
|
|
||||||
| | |____/ \___||___/_|\__, |_| |_|\___|\__,_| \__,_|_| |_|\__,_| | |
|
|
||||||
| | |___/ | |
|
|
||||||
| | _____ _ _ _ | |
|
|
||||||
| | | ____|_ __ __ _(_)_ __ ___ ___ _ __ ___ __| | | |__ _ _ | |
|
|
||||||
| | | _| | '_ \ / _` | | '_ \ / _ \/ _ \ '__/ _ \/ _` | | '_ \| | | | | |
|
|
||||||
| | | |___| | | | (_| | | | | | __/ __/ | | __/ (_| | | |_) | |_| | | |
|
|
||||||
| | |_____|_| |_|\__, |_|_| |_|\___|\___|_| \___|\__,_| |_.__/ \__, | | |
|
|
||||||
| | |___/ |___/ | |
|
|
||||||
| | _ __ _ _ _ _ _ | |
|
|
||||||
| | | |/ /___| |__ (_) |_(_)(_) | |
|
|
||||||
| | | ' // __| '_ \| | __| || | | |
|
|
||||||
| | | . \\__ \ | | | | |_| || | | |
|
|
||||||
| | |_|\_\___/_| |_|_|\__|_|/ | | |
|
|
||||||
| | |__/ | |
|
|
||||||
| | | |
|
|
||||||
__| |______________________________________________________________________| |__
|
|
||||||
__ ______________________________________________________________________ __
|
|
||||||
| | | |
|
|
||||||
|
|
||||||
EOF
|
|
||||||
|
|
||||||
# Declaring variables
|
|
||||||
line="=================================="
|
|
||||||
|
|
||||||
# Update and upgrade packages
|
|
||||||
echo -e "$line\nUpdating and upgrading packages.\n$line\n"
|
|
||||||
sudo apt update && sudo apt upgrade -y
|
|
||||||
echo -e "$line\nFinished updating and upgrading packages.\n$line\n"
|
|
||||||
|
|
||||||
# Check if python3-pip is installed
|
|
||||||
echo -e "$line\nChecking for python3-pip.\n$line\n"
|
|
||||||
if ! dpkg -l | grep -q python3-pip; then
|
|
||||||
echo -e "$line\npython3-pip is not installed,\nInstalling python3-pip.\n$line\n"
|
|
||||||
sudo apt install python3-pip -y
|
|
||||||
else
|
|
||||||
echo -e "$line\npython3-pip already installed, moving on...\n$line\n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if libgpiod2 is installed
|
|
||||||
echo -e "$line\nChecking for libgpiod2.\n$line\n"
|
|
||||||
if ! dpkg -l | grep -q libgpiod2; then
|
|
||||||
echo -e "$line\nlibgpiod2 is not installed,\nInstalling python3-setuptools.\n$line\n"
|
|
||||||
sudo apt install libgpiod2 -y
|
|
||||||
else
|
|
||||||
echo -e "$line\nlibgpiod2 already installed, moving on...\n$line\n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Create project folder
|
|
||||||
USER=$(whoami)
|
|
||||||
PROJECT_DIR="/home/$USER/Desktop/temp-sensor"
|
|
||||||
|
|
||||||
if [ ! -d "$PROJECT_DIR" ]; then
|
|
||||||
echo -e "$line\nCreating 'temp-sensor' in Desktop directory for current user.\n$line\n"
|
|
||||||
mkdir -p "$PROJECT_DIR"
|
|
||||||
cp "./alt-temp.py" "./blinka-test.py" "$PROJECT_DIR/"
|
|
||||||
echo -e "$line\nCreated '$PROJECT_DIR' directory.\n$line\n"
|
|
||||||
else
|
|
||||||
echo -e "$line\n$PROJECT_DIR already exists. Please delete the folder before running this script.\n$line\n\nExiting..."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Create and activate Python virtual environment
|
|
||||||
cd "$PROJECT_DIR/"
|
|
||||||
chmod 775 ./alt-temp.py ./blinka-test.py
|
|
||||||
python3 -m venv . --system-site-packages
|
|
||||||
echo -e "$line\nVirtual environment created.\n$line\n"
|
|
||||||
echo -e "$line\nActivating virtual environment...\n$line\n"
|
|
||||||
source $PROJECT_DIR/bin/activate
|
|
||||||
|
|
||||||
echo -e "$line\nInstalling dependencies...\n$line\n"
|
|
||||||
pip3 install --upgrade setuptools click adafruit-python-shell adafruit-circuitpython-dht RPi.GPIO adafruit-blinka board
|
|
||||||
|
|
||||||
echo -e "$line\nSetting up raspiberry pi...\n$line\n"
|
|
||||||
sudo raspi-config nonint do_i2c 0
|
|
||||||
sudo raspi-config nonint do_spi 0
|
|
||||||
sudo raspi-config nonint do_serial_hw 0
|
|
||||||
sudo raspi-config nonint do_ssh 0
|
|
||||||
sudo raspi-config nonint do_camera 0
|
|
||||||
sudo raspi-config nonint disable_raspi_config_at_boot 0
|
|
||||||
sudo apt install -y i2c-tools libgpiod-dev python3-libgpiod
|
|
||||||
|
|
||||||
python3 blinka-test.py
|
|
||||||
echo -e "$line\nBlinka works!\n$line\n"
|
|
||||||
|
|
||||||
echo -e "\n\n\n$line$line$line\nSetup completed.\nExecute the command 'python3 alt-temp.py' to calculate the temperature.\n# DESIGNED AND ENGINEERED BY KSHITIJ.\n# END OF SCRIPT\n$line$line$line\n\n\n"
|
|
46
alt-temp.py
46
alt-temp.py
@ -1,35 +1,21 @@
|
|||||||
import time
|
# NOTE: GPIO PIN 16 used
|
||||||
import board
|
|
||||||
import adafruit_dht
|
|
||||||
|
|
||||||
# Initial the dht device, with data pin connected to:
|
import RPi.GPIO as GPIO
|
||||||
dhtDevice = adafruit_dht.DHT11(board.D16) # by default I'm using GPIO PIN 16 for data/signal transmission of DHT11
|
import dht11
|
||||||
|
|
||||||
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
|
# initialize GPIO
|
||||||
# This may be necessary on a Linux single board computer like the Raspberry Pi,
|
GPIO.setwarnings(False)
|
||||||
# but it will not work in CircuitPython.
|
GPIO.setmode(GPIO.BCM)
|
||||||
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
|
GPIO.cleanup()
|
||||||
|
|
||||||
while True:
|
# read data using pin 16
|
||||||
try:
|
instance = dht11.DHT11(pin = 16)
|
||||||
# Print the values to the serial port
|
result = instance.read()
|
||||||
temperature_c = dhtDevice.temperature # Celsius
|
|
||||||
temperature_f = temperature_c * (9 / 5) + 32 # Fahrenheit
|
|
||||||
humidity = dhtDevice.humidity
|
|
||||||
print(
|
|
||||||
"Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(
|
|
||||||
temperature_f, temperature_c, humidity
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
except RuntimeError as error:
|
if result.is_valid():
|
||||||
# Errors happen fairly often, DHT's are hard to read, just keep going
|
print("Temperature: %-3.1f C" % result.temperature)
|
||||||
print(error.args[0])
|
print("Humidity: %-3.1f %%" % result.humidity)
|
||||||
time.sleep(2.0)
|
else:
|
||||||
continue
|
print("Error: %d" % result.error_code)
|
||||||
except Exception as error:
|
|
||||||
dhtDevice.exit()
|
|
||||||
raise error
|
|
||||||
|
|
||||||
time.sleep(2.0)
|
|
||||||
|
|
||||||
|
GPIO.cleanup()
|
||||||
|
33
setup.sh
33
setup.sh
@ -48,14 +48,23 @@ else
|
|||||||
echo -e "$line\npython3-pip already installed, moving on...\n$line\n"
|
echo -e "$line\npython3-pip already installed, moving on...\n$line\n"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Check if libgpiod2 is installed
|
||||||
|
echo -e "$line\nChecking for libgpiod2.\n$line\n"
|
||||||
|
if ! dpkg -l | grep -q libgpiod2; then
|
||||||
|
echo -e "$line\nlibgpiod2 is not installed,\nInstalling python3-setuptools.\n$line\n"
|
||||||
|
sudo apt install libgpiod2 -y
|
||||||
|
else
|
||||||
|
echo -e "$line\nlibgpiod2 already installed, moving on...\n$line\n"
|
||||||
|
fi
|
||||||
|
|
||||||
# Create project folder
|
# Create project folder
|
||||||
USER=$(whoami)
|
USER=$(whoami)
|
||||||
PROJECT_DIR="/home/$USER/Desktop/dht11-sensor"
|
PROJECT_DIR="/home/$USER/Desktop/temp-sensor"
|
||||||
|
|
||||||
if [ ! -d "$PROJECT_DIR" ]; then
|
if [ ! -d "$PROJECT_DIR" ]; then
|
||||||
echo -e "$line\nCreating 'dht11-sensor' in Desktop directory for current user.\n$line\n"
|
echo -e "$line\nCreating 'temp-sensor' in Desktop directory for current user.\n$line\n"
|
||||||
mkdir -p "$PROJECT_DIR"
|
mkdir -p "$PROJECT_DIR"
|
||||||
cp "./temp-calc.py" "$PROJECT_DIR/"
|
cp "./temp-calc.py" "./blinka-test.py" "$PROJECT_DIR/"
|
||||||
echo -e "$line\nCreated '$PROJECT_DIR' directory.\n$line\n"
|
echo -e "$line\nCreated '$PROJECT_DIR' directory.\n$line\n"
|
||||||
else
|
else
|
||||||
echo -e "$line\n$PROJECT_DIR already exists. Please delete the folder before running this script.\n$line\n\nExiting..."
|
echo -e "$line\n$PROJECT_DIR already exists. Please delete the folder before running this script.\n$line\n\nExiting..."
|
||||||
@ -64,13 +73,25 @@ fi
|
|||||||
|
|
||||||
# Create and activate Python virtual environment
|
# Create and activate Python virtual environment
|
||||||
cd "$PROJECT_DIR/"
|
cd "$PROJECT_DIR/"
|
||||||
chmod 775 ./temp-calc.py
|
chmod 775 ./temp-calc.py ./blinka-test.py
|
||||||
python3 -m venv .
|
python3 -m venv . --system-site-packages
|
||||||
echo -e "$line\nVirtual environment created.\n$line\n"
|
echo -e "$line\nVirtual environment created.\n$line\n"
|
||||||
echo -e "$line\nActivating virtual environment...\n$line\n"
|
echo -e "$line\nActivating virtual environment...\n$line\n"
|
||||||
source $PROJECT_DIR/bin/activate
|
source $PROJECT_DIR/bin/activate
|
||||||
|
|
||||||
echo -e "$line\nInstalling dependencies...\n$line\n"
|
echo -e "$line\nInstalling dependencies...\n$line\n"
|
||||||
pip3 install --upgrade RPi.GPIO setuptools dht11
|
pip3 install --upgrade setuptools click adafruit-python-shell adafruit-circuitpython-dht RPi.GPIO adafruit-blinka board
|
||||||
|
|
||||||
|
echo -e "$line\nSetting up raspiberry pi...\n$line\n"
|
||||||
|
sudo raspi-config nonint do_i2c 0
|
||||||
|
sudo raspi-config nonint do_spi 0
|
||||||
|
sudo raspi-config nonint do_serial_hw 0
|
||||||
|
sudo raspi-config nonint do_ssh 0
|
||||||
|
sudo raspi-config nonint do_camera 0
|
||||||
|
sudo raspi-config nonint disable_raspi_config_at_boot 0
|
||||||
|
sudo apt install -y i2c-tools libgpiod-dev python3-libgpiod
|
||||||
|
|
||||||
|
python3 blinka-test.py
|
||||||
|
echo -e "$line\nBlinka works!\n$line\n"
|
||||||
|
|
||||||
echo -e "\n\n\n$line$line$line\nSetup completed.\nExecute the command 'python3 temp-calc.py' to calculate the temperature.\n# DESIGNED AND ENGINEERED BY KSHITIJ.\n# END OF SCRIPT\n$line$line$line\n\n\n"
|
echo -e "\n\n\n$line$line$line\nSetup completed.\nExecute the command 'python3 temp-calc.py' to calculate the temperature.\n# DESIGNED AND ENGINEERED BY KSHITIJ.\n# END OF SCRIPT\n$line$line$line\n\n\n"
|
||||||
|
46
temp-calc.py
46
temp-calc.py
@ -1,21 +1,35 @@
|
|||||||
# NOTE: GPIO PIN 16 used
|
import time
|
||||||
|
import board
|
||||||
|
import adafruit_dht
|
||||||
|
|
||||||
import RPi.GPIO as GPIO
|
# Initial the dht device, with data pin connected to:
|
||||||
import dht11
|
dhtDevice = adafruit_dht.DHT11(board.D16) # by default I'm using GPIO PIN 16 for data/signal transmission of DHT11
|
||||||
|
|
||||||
# initialize GPIO
|
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
|
||||||
GPIO.setwarnings(False)
|
# This may be necessary on a Linux single board computer like the Raspberry Pi,
|
||||||
GPIO.setmode(GPIO.BCM)
|
# but it will not work in CircuitPython.
|
||||||
GPIO.cleanup()
|
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
|
||||||
|
|
||||||
# read data using pin 16
|
while True:
|
||||||
instance = dht11.DHT11(pin = 16)
|
try:
|
||||||
result = instance.read()
|
# Print the values to the serial port
|
||||||
|
temperature_c = dhtDevice.temperature # Celsius
|
||||||
|
temperature_f = temperature_c * (9 / 5) + 32 # Fahrenheit
|
||||||
|
humidity = dhtDevice.humidity
|
||||||
|
print(
|
||||||
|
"Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(
|
||||||
|
temperature_f, temperature_c, humidity
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if result.is_valid():
|
except RuntimeError as error:
|
||||||
print("Temperature: %-3.1f C" % result.temperature)
|
# Errors happen fairly often, DHT's are hard to read, just keep going
|
||||||
print("Humidity: %-3.1f %%" % result.humidity)
|
print(error.args[0])
|
||||||
else:
|
time.sleep(2.0)
|
||||||
print("Error: %d" % result.error_code)
|
continue
|
||||||
|
except Exception as error:
|
||||||
|
dhtDevice.exit()
|
||||||
|
raise error
|
||||||
|
|
||||||
|
time.sleep(2.0)
|
||||||
|
|
||||||
GPIO.cleanup()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user