From 93e6e5e2116b85f7a2df9857e120053fa390a0d6 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Thu, 10 Oct 2024 22:15:46 +0530 Subject: [PATCH] Alternate method is now the main method since it's much easier to setup and it's been tested in our college IoT lab. --- README.md | 71 +++++++++++++++++++++++++----------------------- setup.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ temp-calc.py | 21 +++++++++++++++ 3 files changed, 134 insertions(+), 34 deletions(-) create mode 100755 setup.sh create mode 100755 temp-calc.py diff --git a/README.md b/README.md index 256024f..95efcad 100644 --- a/README.md +++ b/README.md @@ -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,9 +10,43 @@ 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 -y git +``` + +2. Clone the project: +```shell +git clone https://git.kska.io/notkshitij/DHT11.git +``` + +> 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 `setup.sh` script: +```shell +source setup.sh +``` + +> [!IMPORTANT] +> After running the code, and completing the execution, run `deactivate` in the terminal to exit the virtual environment. + +--- + +## Alternative method + +> [!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) + +1. Install prerequisite packages: ```shell sudo apt update &&\ sudo apt install git -y @@ -41,37 +75,6 @@ source ada-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. - -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. **Download** and **run** the code [alt-temp.py](https://git.kska.io/notkshitij/DHT11/src/branch/main/alt-temp.py) - -> [!IMPORTANT] -> After running the code, and completing the execution, run `deactivate` in the terminal to exit the virtual environment. - ---- - ### License This project is licensed under the terms of the MIT license. Read the license [here](https://git.kska.io/notkshitij/DHT11/src/branch/main/LICENSE.txt). diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..6832fec --- /dev/null +++ b/setup.sh @@ -0,0 +1,76 @@ +#!/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 + +# Create project folder +USER=$(whoami) +PROJECT_DIR="/home/$USER/Desktop/dht11-sensor" + +if [ ! -d "$PROJECT_DIR" ]; then + echo -e "$line\nCreating 'dht11-sensor' in Desktop directory for current user.\n$line\n" + mkdir -p "$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..." + exit 1 +fi + +# Create and activate Python virtual environment +cd "$PROJECT_DIR/" +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 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" diff --git a/temp-calc.py b/temp-calc.py new file mode 100755 index 0000000..7af256d --- /dev/null +++ b/temp-calc.py @@ -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()