Working sound setup for raspis
This commit is contained in:
3
roles/pi-dhtsensor-circuitpython/defaults/main.yml
Normal file
3
roles/pi-dhtsensor-circuitpython/defaults/main.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
dht_pin: "D12"
|
||||
dht_polling_sleep_time_seconds: 20
|
||||
64
roles/pi-dhtsensor-circuitpython/files/dht22_sensing.py
Normal file
64
roles/pi-dhtsensor-circuitpython/files/dht22_sensing.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import multiprocessing
|
||||
|
||||
|
||||
config = json.load(open("/etc/dht22_sensing.json"))
|
||||
|
||||
|
||||
def send_to_home_assistant(temperature, humidity):
|
||||
# print(f"Sending temperature {temperature}, humidity {humidity}")
|
||||
headers = {
|
||||
'x-ha-access': config['token'],
|
||||
'Authorization': "Bearer {}".format(config['token'])
|
||||
}
|
||||
|
||||
temperature_url = "{}/api/states/sensor.{}".format(
|
||||
config['ha_url'], config['ha_temp_sensor_name'])
|
||||
temperatur_data = {
|
||||
"state": str(temperature),
|
||||
"attributes": {
|
||||
"device_class": "temperature",
|
||||
"friendly_name": config['ha_temp_friendly_name'],
|
||||
"unit_of_measurement": "°C"
|
||||
}
|
||||
}
|
||||
requests.post(temperature_url, json=temperatur_data, headers=headers)
|
||||
|
||||
humidity_url = "{}/api/states/sensor.{}".format(
|
||||
config['ha_url'], config['ha_humidity_sensor_name'])
|
||||
humidity_data = {
|
||||
"state": str(humidity),
|
||||
"attributes": {
|
||||
"device_class": "humidity",
|
||||
"friendly_name": config['ha_humidity_friendly_name'],
|
||||
"unit_of_measurement": "%"
|
||||
}
|
||||
}
|
||||
requests.post(humidity_url, json=humidity_data, headers=headers)
|
||||
|
||||
|
||||
def sense():
|
||||
import adafruit_dht
|
||||
import board
|
||||
|
||||
dht_device = adafruit_dht.DHT22(getattr(board, config['dht_pin']))
|
||||
|
||||
try:
|
||||
send_to_home_assistant(dht_device.temperature, dht_device.humidity)
|
||||
except RuntimeError as error:
|
||||
# Errors happen fairly often, DHT's are hard to read, just keep going
|
||||
print(error.args[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
multiprocessing.set_start_method('spawn')
|
||||
while True:
|
||||
process = multiprocessing.Process(target=sense)
|
||||
process.start()
|
||||
process.join()
|
||||
time.sleep(float(config['polling_sleep_time_seconds']))
|
||||
10
roles/pi-dhtsensor-circuitpython/files/dht22_sensing.service
Normal file
10
roles/pi-dhtsensor-circuitpython/files/dht22_sensing.service
Normal file
@@ -0,0 +1,10 @@
|
||||
[Unit]
|
||||
Description=DHT22 Temperature Humidity Sensing
|
||||
After=multi-user.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/python3 /usr/bin/dht22_sensing
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
20
roles/pi-dhtsensor-circuitpython/tasks/main.yml
Normal file
20
roles/pi-dhtsensor-circuitpython/tasks/main.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
- name: apt install libgpiod2
|
||||
apt: name=libgpiod2 cache_valid_time=7200 state=present
|
||||
- name: pip install adafruit-circuitpython-dht
|
||||
pip:
|
||||
name: adafruit-circuitpython-dht
|
||||
executable: pip3
|
||||
- name: Install script config
|
||||
template: src=dht22_sensing.json dest=/etc/dht22_sensing.json
|
||||
- name: Install script
|
||||
copy: src=dht22_sensing.py dest=/usr/bin/dht22_sensing owner=root mode=u+rwx
|
||||
- name: Install systemd service file
|
||||
copy: src=dht22_sensing.service dest=/lib/systemd/system/
|
||||
- name: Add script to autostart and start now
|
||||
systemd: name=dht22_sensing state=started enabled=yes daemon_reload=yes
|
||||
- name: Add to sysdweb
|
||||
include_role:
|
||||
name: pi-sysdweb
|
||||
vars:
|
||||
sysdweb_name: dht22_sensing
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ha_url": "{{home_assistant_url}}",
|
||||
"token": "{{home_assistant_token}}",
|
||||
"dht_pin": "{{dht_pin}}",
|
||||
"polling_sleep_time_seconds": "{{dht_polling_sleep_time_seconds}}",
|
||||
|
||||
"ha_temp_sensor_name": "{{sensor_room_name_ascii|lower}}_dht22_temperatur",
|
||||
"ha_temp_friendly_name": "{{sensor_room_name}} Temperatur",
|
||||
|
||||
"ha_humidity_sensor_name": "{{sensor_room_name_ascii|lower}}_dht22_luftfeuchtigkeit",
|
||||
"ha_humidity_friendly_name": "{{sensor_room_name}} Luftfeuchtigkeit"
|
||||
}
|
||||
Reference in New Issue
Block a user