Update environment.py, README.md files

This commit is contained in:
Will Bradley 2020-08-07 02:21:46 +00:00
parent e55d6e4b01
commit c4352ad6f5
2 changed files with 82 additions and 0 deletions

View File

@ -1,2 +1,42 @@
# Adafruit IO Raspberry Pi Environmental Sensor
Code based on https://pimylifeup.com/raspberry-pi-humidity-sensor-dht22/
## Code
If you are using the DHT22, DHT11, or the AM2302 you can switch the constant in the code as needed:
- `Adafruit_DHT.DH11`
- `Adafruit_DHT.DH22`
- `Adafruit_DHT.AM2302`
I'm using the AM2302 because it's all pre-wired.
Make sure you also change `ADAFRUIT_IO_KEY`, `ADAFRUIT_IO_USERNAME`, and `LOOP_DELAY_MINS` as desired. You can get Adafruit IO key/username info from your account at https://io.adafruit.com
## Wiring
To connect the sensor to the RasPi:
- If you're using DHT11 or DHT22, place a 10k resistor between Pin 1 and Pin 2 of the DHT sensor (a pull-up resistor from the data pin to the).
- Wire Pin 1 of the DHT22 to Physical Pin 1 (3v3) on the Pi
- Wire Pin 2 of the DHT22 to Physical Pin 7 (GPIO4) on the Pi
- Pin 3 of the DHT11/22 is left blank.
- Wire Pin 4 of the DHT22 (or Pin 3 of the AM2302) to Physical Pin 6 (GND) on the Pi
Physical pin 1 on the Pi is usually the far bottom-left corner of the GPIO header, as seen when the screenprinted text is right side up. Physical pin 2 is the top-left corner, and continuing in zigzag fashion with odd numbers on bottom and even numbers on top. See the PiMyLifeUp link above for a diagram.
## Installation
Install dependencies:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-dev python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel
sudo pip3 install Adafruit_DHT
Copy the `environment.py` file to a folder of your choosing, make sure you edit it according to the Code section above.
## Running
Run it with `python3 environment.py`

42
environment.py Normal file
View File

@ -0,0 +1,42 @@
print("Starting...")
import time
import Adafruit_DHT
from Adafruit_IO import Client, Feed, RequestError
ADAFRUIT_IO_KEY = 'YOUR_ADAFRUIT_IO_KEY_HERE'
ADAFRUIT_IO_USERNAME = 'YOUR_ADAFRUIT_IO_USERNAME_HERE'
LOOP_DELAY_MINS = 10
DHT_SENSOR = Adafruit_DHT.AM2302
DHT_PIN = 4
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
try: # if we already have the feeds, assign them.
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
except RequestError: # if we don't, create and assign them.
temperature_feed = aio.create_feed(Feed(name='temperature'))
humidity_feed = aio.create_feed(Feed(name='humidity'))
while True:
humidity, temperature_c = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
temperature_f = temperature_c * (9 / 5) + 32
if humidity is not None and temperature_c is not None:
print("TempC={0:0.1f}*C TempF={1:0.1f}*F Humidity={2:0.1f}%".format(temperature_c, temperature_f, humidity))
print('sending data to adafruit io...')
t_out = aio.send(temperature_feed.key, temperature_f)
h_out = aio.send(humidity_feed.key, humidity)
if t_out and h_out:
print('data sent, sleeping for '+str(LOOP_DELAY_MINS)+' mins')
else:
print('problem sending data:')
print(t_out)
print(h_out)
else:
print("Failed to retrieve data from humidity sensor")
time.sleep(LOOP_DELAY_MINS * 60)