Compare commits

..

2 Commits

5 changed files with 170 additions and 91 deletions

View File

@ -1,6 +1,6 @@
# DHT Humidity Sensing on Raspberry Pi
This project is a uses the AdaFruit libraries for sensing temperature using DHT11 sensor and a 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.
---
@ -10,13 +10,13 @@ This project is a uses the AdaFruit libraries for sensing temperature using DHT1
## Main method
> [!NOTE]
> This is the main method, using AdaFruit library. For an easier method, checkout [## Alternative method](https://git.kska.io/notkshitij/DHT11#alternative-method)
> Main 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 some prerequisite packages:
1. Install prerequisites:
```shell
sudo apt update &&\
sudo apt install git -y
sudo apt install -y git
```
2. Clone the project:
@ -44,28 +44,31 @@ source setup.sh
## 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.
> 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)
1. Install prerequisites:
1. Install prerequisite packages:
```shell
sudo apt update &&\
sudo apt upgrade -y &&\
sudo apt install python3 python3-pip -y
sudo apt install git -y
```
2. Setup a virtual environment & install the library:
2. Clone the project:
```shell
mkdir /home/$(whoami)/temp-sensor
cd /home/$(whoami)/temp-sensor
python3 -m venv .
source bin/activate
pip install dht11 setuptools RPi.GPIO
git clone https://git.kska.io/notkshitij/DHT11.git
```
3. **Download** and **run** the code [alt-temp.py](https://git.kska.io/notkshitij/DHT11/src/branch/main/alt-temp.py)
> Alternatively, you can also [download the zip file](https://git.kska.io/notkshitij/DHT11/archive/main.zip)
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]
> After running the code, and completing the execution, run `deactivate` in the terminal to exit the virtual environment.

97
ada-setup.sh Executable file
View File

@ -0,0 +1,97 @@
#!/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"

View File

@ -1,21 +1,35 @@
# NOTE: GPIO PIN 16 used
import time
import board
import adafruit_dht
import RPi.GPIO as GPIO
import dht11
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D16) # by default I'm using GPIO PIN 16 for data/signal transmission of DHT11
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
# read data using pin 16
instance = dht11.DHT11(pin = 16)
result = instance.read()
while True:
try:
# 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():
print("Temperature: %-3.1f C" % result.temperature)
print("Humidity: %-3.1f %%" % result.humidity)
else:
print("Error: %d" % result.error_code)
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
time.sleep(2.0)
GPIO.cleanup()

View File

@ -48,23 +48,14 @@ 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"
PROJECT_DIR="/home/$USER/Desktop/dht11-sensor"
if [ ! -d "$PROJECT_DIR" ]; then
echo -e "$line\nCreating 'temp-sensor' in Desktop directory for current user.\n$line\n"
echo -e "$line\nCreating 'dht11-sensor' in Desktop directory for current user.\n$line\n"
mkdir -p "$PROJECT_DIR"
cp "./temp-calc.py" "./blinka-test.py" "$PROJECT_DIR/"
cp "./temp-calc.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..."
@ -73,25 +64,13 @@ fi
# Create and activate Python virtual environment
cd "$PROJECT_DIR/"
chmod 775 ./temp-calc.py ./blinka-test.py
python3 -m venv . --system-site-packages
chmod 775 ./temp-calc.py
python3 -m venv .
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"
pip3 install --upgrade RPi.GPIO setuptools dht11
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"

View File

@ -1,35 +1,21 @@
import time
import board
import adafruit_dht
# NOTE: GPIO PIN 16 used
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D16) # by default I'm using GPIO PIN 16 for data/signal transmission of DHT11
import RPi.GPIO as GPIO
import dht11
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
while True:
try:
# 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
)
)
# read data using pin 16
instance = dht11.DHT11(pin = 16)
result = instance.read()
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
time.sleep(2.0)
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()