Compare commits

...

22 Commits

Author SHA1 Message Date
b5499a11dd
Updated method to run shell script 2024-10-07 02:12:56 +05:30
6f134a6795
removed exit at the end to keep source activated 2024-10-07 02:12:56 +05:30
64f71f4af5
removed digitalio, changed output after running blinka test file and changed final command. 2024-10-07 02:12:56 +05:30
0672904e41
Removed PIN variables. 2024-10-07 02:12:55 +05:30
73c967ed31
fixed DPIN variable 2024-10-07 02:12:55 +05:30
8be043a952
removed libgpiod from pip3 install and changed chmod perms 2024-10-07 02:12:55 +05:30
2c4991c56e
removed if statements, line 93, 96, 99 2024-10-07 02:12:55 +05:30
eb722fe6f1
Added blinka test file for testing blinka. 2024-10-07 02:12:55 +05:30
03aab911cf
Removed requirement for raspi-blinka.py 2024-10-07 02:12:55 +05:30
9d6f734097
Added blinka test file. 2024-10-07 02:12:54 +05:30
d8afb80753
Add in source 2024-10-07 02:12:54 +05:30
e8d4b9b182
Running raspi-blinka.py as sudo 2024-10-07 02:12:54 +05:30
0c2680cacc
The script now copies raspi-blinka.py and temp-calc.py file to temp-sensor folder created in desktop. 2024-10-07 02:12:54 +05:30
494ae4cfe3
Added execution permissions to .py files. 2024-10-07 02:12:54 +05:30
0ce38a43c7
Added auto yes in apt install git 2024-10-07 02:12:54 +05:30
e5ccca5640
Specified PIN 16 and added some comments. 2024-10-07 02:12:53 +05:30
e3f9059627
Added unchanged temp-calc.py 2024-10-07 02:12:53 +05:30
454656ef85
Added link for main doc in guides file 2024-10-07 02:12:53 +05:30
bc64e3600c
Fixed note in README 2024-10-07 02:12:53 +05:30
ae254fd517
Added reference for requirements and license 2024-10-07 02:12:53 +05:30
234bf7eedd
Added requirements file. 2024-10-07 02:12:53 +05:30
fd0ebff571
Added license file. 2024-10-07 02:12:49 +05:30
9 changed files with 114 additions and 165 deletions

View File

@ -4,6 +4,7 @@ This file contains guides that were used for making the script and other referen
--- ---
- [Main doc](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup)
- [Setting up Pi for CircuitPython Libraries](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi) - [Setting up Pi for CircuitPython Libraries](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)
- [AdaFruit CircuitPython Bundles](https://github.com/adafruit/Adafruit_CircuitPython_Bundle) - [AdaFruit CircuitPython Bundles](https://github.com/adafruit/Adafruit_CircuitPython_Bundle)
- [AdaFruit CircutPython](https://github.com/adafruit/circuitpython) - [AdaFruit CircutPython](https://github.com/adafruit/circuitpython)

22
LICENSE.txt Normal file
View File

@ -0,0 +1,22 @@
MIT License
Copyright (c) [2024] [Kshitij]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -4,14 +4,18 @@ This project is a uses the AdaFruit libraries for sensing temperature using DHT1
--- ---
> [!IMPORTANT]
> Check the [requirements](https://git.kska.io/notkshitij/DHT11/src/branch/main/REQUIREMENTS.md) first.
## Main method ## 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) > [!NOTE]
> This is the main method, using AdaFruit library. For an easier method, checkout [## Alternative method](https://git.kska.io/notkshitij/DHT11#alternative-method)
1. Install some prerequisite packages: 1. Install some prerequisite packages:
```shell ```shell
sudo apt update &&\ sudo apt update &&\
sudo apt install git sudo apt install git -y
``` ```
@ -29,7 +33,7 @@ cd ./DHT11
4. Run the `setup.sh` script: 4. Run the `setup.sh` script:
```shell ```shell
./setup.sh source setup.sh
``` ```
> [!IMPORTANT] > [!IMPORTANT]
@ -67,3 +71,9 @@ pip install dht11 setuptools RPi.GPIO
> 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.
--- ---
### 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).
---

8
REQUIREMENTS.md Normal file
View File

@ -0,0 +1,8 @@
# Requirements
This file specifies the requirements for this project.
---
- Raspberry Pi OS Bullseye or higher
- Python 3.7 or higher

0
alt-temp.py Normal file → Executable file
View File

19
blinka-test.py Normal file
View File

@ -0,0 +1,19 @@
import board
import digitalio
import busio
print("Hello, blinka!")
# Try to create a Digital input
pin = digitalio.DigitalInOut(board.D4)
print("Digital IO ok!")
# Try to create an I2C device
i2c = busio.I2C(board.SCL, board.SDA)
print("I2C ok!")
# Try to create an SPI device
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
print("SPI ok!")
print("done!")

View File

@ -1,155 +0,0 @@
"""
Adafruit Raspberry Pi Blinka Setup Script
(C) Adafruit Industries, Creative Commons 3.0 - Attribution Share Alike
Written by Melissa LeBlanc-Williams for Adafruit Industries
"""
import os
import sys
try:
from adafruit_shell import Shell
except ImportError:
raise RuntimeError("The library 'adafruit_shell' was not found. To install, try typing: sudo pip3 install adafruit-python-shell")
shell = Shell()
shell.group="Blinka"
default_python = 3
blinka_minimum_python_version = 3.7
def default_python_version(numeric=True):
version = shell.run_command("python -c 'import platform; print(platform.python_version())'", suppress_message=True, return_output=True)
if numeric:
try:
return float(version[0:version.rfind(".")])
except ValueError:
return None
return version
def get_python3_version(numeric=True):
version = shell.run_command("python3 -c 'import platform; print(platform.python_version())'", suppress_message=True, return_output=True)
if numeric:
return float(version[0:version.rfind(".")])
return version
def check_blinka_python_version():
"""
Check the Python 3 version for Blinka (which may be a later version than we're running this script with)
"""
print("Making sure the required version of Python is installed")
current = get_python3_version(False)
current_major, current_minor = current.split(".")[0:2]
required_major, required_minor = str(blinka_minimum_python_version).split(".")[0:2]
if int(current_major) >= int(required_major) and int(current_minor) >= int(required_minor):
return
shell.bail("Blinka requires a minimum of Python version {} to install, current one is {}. Please update your OS!".format(blinka_minimum_python_version, current))
def sys_update():
print("Updating System Packages")
if not shell.run_command("sudo apt-get update --allow-releaseinfo-change"):
shell.bail("Apt failed to update indexes!")
print("Upgrading packages...")
if not shell.run_command("sudo apt-get -y upgrade"):
shell.bail("Apt failed to install software!")
def set_raspiconfig():
"""
Enable various Raspberry Pi interfaces
"""
print("Enabling I2C")
shell.run_command("sudo raspi-config nonint do_i2c 0")
print("Enabling SPI")
shell.run_command("sudo raspi-config nonint do_spi 0")
print("Enabling Serial")
if not shell.run_command("sudo raspi-config nonint do_serial_hw 0", suppress_message=True):
shell.run_command("sudo raspi-config nonint do_serial 0")
print("Enabling SSH")
shell.run_command("sudo raspi-config nonint do_ssh 0")
print("Enabling Camera")
shell.run_command("sudo raspi-config nonint do_camera 0")
print("Disable raspi-config at Boot")
shell.run_command("sudo raspi-config nonint disable_raspi_config_at_boot 0")
def update_python():
print("Making sure Python 3 is the default")
if default_python < 3:
shell.run_command("sudo apt-get install -y python3 git python3-pip")
shell.run_command("sudo update-alternatives --install /usr/bin/python python $(which python2) 1")
shell.run_command("sudo update-alternatives --install /usr/bin/python python $(which python3) 2")
shell.run_command("sudo update-alternatives --skip-auto --config python")
def update_pip():
print("Making sure PIP and setuptools is installed")
shell.run_command("sudo apt-get install --upgrade -y python3-pip python3-setuptools")
def install_blinka(user=False):
print("Installing latest version of Blinka locally")
shell.run_command("sudo apt-get install -y i2c-tools libgpiod-dev python3-libgpiod")
pip_command = "pip3 install --upgrade"
username = None
if user:
username = os.environ["SUDO_USER"]
shell.run_command(f"{pip_command} RPi.GPIO", run_as_user=username)
shell.run_command(f"{pip_command} adafruit-blinka", run_as_user=username)
# Custom function to run additional commands for Pi 5
def check_and_install_for_pi5(pi_model):
if pi_model.startswith("RASPBERRY_PI_5"):
print("Detected Raspberry Pi 5, applying additional fixes...")
os.system("sudo apt remove python3-rpi.gpio")
os.system("pip3 install rpi-lgpio")
else:
print(f"Detected {pi_model}, no additional fixes needed.")
def main():
global default_python
shell.clear()
# Check Raspberry Pi and Bail
pi_model = shell.get_board_model()
print("""This script configures your
Raspberry Pi and installs Blinka
""")
print("{} detected.\n".format(pi_model))
if not shell.is_raspberry_pi():
shell.bail("Non-Raspberry Pi board detected. This must be run on a Raspberry Pi")
os_identifier = shell.get_os()
if os_identifier != "Raspbian":
shell.bail("Sorry, the OS detected was {}. This script currently only runs on Raspberry Pi OS.".format(os_identifier))
if not shell.is_python3():
shell.bail("You must be running Python 3. Older versions have now been deprecated.")
shell.check_kernel_update_reboot_required()
python_version = default_python_version()
if not python_version:
shell.warn("WARNING No Default System python tied to the `python` command. It will be set to Version 3.")
default_python = 0
if not shell.prompt("Continue?"):
shell.exit()
elif int(default_python_version()) < 3:
shell.warn("WARNING Default System python version is {}. It will be updated to Version 3.".format(default_python_version(False)))
default_python = 2
if not shell.prompt("Continue?"):
shell.exit()
sys_update()
check_blinka_python_version()
set_raspiconfig()
update_python()
update_pip()
install_blinka(True)
# Check and install for Pi 5 if detected
check_and_install_for_pi5(pi_model)
# Done
print("""DONE.
Settings take effect on next boot.
""")
shell.prompt_reboot()
# Main function
if __name__ == "__main__":
shell.require_root()
main()

View File

@ -64,6 +64,7 @@ PROJECT_DIR="/home/$USER/Desktop/temp-sensor"
if [ ! -d "$PROJECT_DIR" ]; then if [ ! -d "$PROJECT_DIR" ]; then
echo -e "$line\nCreating 'temp-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" "./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..."
@ -71,18 +72,26 @@ else
fi fi
# Create and activate Python virtual environment # Create and activate Python virtual environment
cd "$PROJECT_DIR" cd "$PROJECT_DIR/"
chmod 775 ./temp-calc.py ./blinka-test.py
python3 -m venv . --system-site-packages 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 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 setuptools click adafruit-python-shell adafruit-circuitpython-dht pip3 install --upgrade setuptools click adafruit-python-shell adafruit-circuitpython-dht RPi.GPIO adafruit-blinka board
echo -e "$line\nSetting up raspi-blinka...\n$line\n" echo -e "$line\nSetting up raspiberry pi...\n$line\n"
python3 raspi-blinka.py 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
echo -e "\n\n\n$line$line$line\nSetup completed.\nExecute the command 'python3 temp-calc.py' to calculate\nthe temperature.\n# DESIGNED AND ENGINEERED BY KSHITIJ.\n# END OF SCRIPT\n$line$line$line\n\n\n" python3 blinka-test.py
echo -e "$line\nBlinka works!\n$line\n"
exit 1 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"

35
temp-calc.py Executable file
View File

@ -0,0 +1,35 @@
import time
import board
import adafruit_dht
# 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
# 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)
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
)
)
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)