Raspberry Pi Zero DHT11

Raspberry Pi Zero DHT11

Connect DHT11 module to pin 1,6,7

To set up a DHT11 sensor on a Raspberry Pi according to the article, you’ll need to use the following commands: (use DHT11 module and pin 1,6,7)

1. Update and Upgrade:

sudo apt-get update
sudo apt-get upgrade

2. Install Python Packages:

sudo apt-get install python3-pip python3-dev

3. Install DHT11 Python Library:

mkdir dht_env
cd dht_env
python3 -m venv myenv
source myenv/bin/activate
python3 -m pip install adafruit-circuitpython-dht
sudo killall libgpiod_pulsein
sudo nmcli c mod preconfigured ipv4.addresses 192.168.xxx.xxx/24
 (static address)```

-----

**4. Run a test script:**
To test the sensor, you can create a Python script and run it. The article provides the following code to save as `dht11-test.py`:

```python
import time
import board
import adafruit_dht

# Sensor data pin is connected to GPIO 4
sensor = adafruit_dht.DHT22(board.D4)
# Uncomment for DHT11
#sensor = adafruit_dht.DHT11(board.D4)

while True:
    try:
        # Print the values to the serial port
        temperature_c = sensor.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = sensor.humidity
        print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, 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:
        sensor.exit()
        raise error

    time.sleep(3.0)

```bash
python3 dht11-test.py

While the page you referenced is for the Node.js process manager PM2, you can also use it to manage Python scripts. The process is straightforward, as PM2 is designed to be a generic process manager.

Here is a quick guide on how to set up a Python script to run with PM2, based on the principles outlined in the PM2 documentation:

1. Install PM2

First, you need to install PM2 globally on your system using npm.

npm install pm2 -g

2. Start your Python Script with PM2

The simplest way to get your Python script running is to use the pm2 start command. You need to tell PM2 to use the Python interpreter to run your script.

pm2 start your_script_name.py --interpreter python3
  • your_script_name.py is the name of your Python script.
  • --interpreter python3 explicitly tells PM2 to use the python3 interpreter to execute the file.

3. Manage your Application

Once your script is running, you can use various PM2 commands to manage it.

  • List all running applications:
    pm2 list
    
  • Check the logs:
    pm2 logs your_script_name
    
  • Restart the application:
    pm2 restart your_script_name
    
  • Stop the application:
    pm2 stop your_script_name
    
  • Remove the application from the PM2 list:
    pm2 delete your_script_name
    

4. Save your Process List

To ensure your script starts automatically when the system reboots, you can save the current process list. This will generate a startup script for your operating system.

pm2 save

This will save your current processes so they automatically restart on server boot. This is crucial for maintaining uptime.

sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u roberto –hp /home/xxx
pm2 start ~/dht11/dht11.py –interpreter ./env/bin/python3
pm2 start tempServer.js

Comments are closed.