Working sound setup for raspis

This commit is contained in:
Martin Bauer
2020-05-10 15:25:38 +02:00
parent caf6232dfb
commit d8c9a491d1
40 changed files with 815 additions and 43 deletions

Binary file not shown.

View File

@@ -0,0 +1,3 @@
---
dht_pin: 12
dht_polling_sleep_time_seconds: 20

View File

@@ -4,20 +4,20 @@
import requests
import json
import time
import board
import adafruit_dht
import Adafruit_DHT
config = json.load(open("/etc/dht22_sensing.json"))
dht_device = adafruit_dht.DHT22(getattr(board, config['dht_pin']))
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'])
temperature_url = "{}/api/states/sensor.{}".format(
config['ha_url'], config['ha_temp_sensor_name'])
temperatur_data = {
"state": str(temperature),
"attributes": {
@@ -28,7 +28,8 @@ def send_to_home_assistant(temperature, humidity):
}
requests.post(temperature_url, json=temperatur_data, headers=headers)
humidity_url = "{}/api/states/sensor.{}".format(config['ha_url'], config['ha_humidity_sensor_name'])
humidity_url = "{}/api/states/sensor.{}".format(
config['ha_url'], config['ha_humidity_sensor_name'])
humidity_data = {
"state": str(humidity),
"attributes": {
@@ -40,14 +41,9 @@ def send_to_home_assistant(temperature, humidity):
requests.post(humidity_url, json=humidity_data, headers=headers)
def sense():
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])
time.sleep(config['polling_sleep_time_seconds'])
while True:
sense()
if __name__ == "__main__":
while True:
sensor = Adafruit_DHT.DHT22
humidity, temperature = Adafruit_DHT.read_retry(sensor, config['dht_pin'])
send_to_home_assistant(temperature, humidity)
time.sleep(float(config['polling_sleep_time_seconds']))

View File

@@ -0,0 +1,29 @@
---
# Use the deprecated version here instead, not the new circuitpython adafruit-circuitpython-dht version
# the new version needs a lot of CPU time and doesn't work correctly on old raspi 1
# a copy of the deprecated repo is downloaded as zip (if it goes away)
- name: Uninstall libgpiod2 (circuitpython) if present
apt: name=libgpiod2 cache_valid_time=7200 state=absent
- name: pip uninstall adafruit-circuitpython-dht
pip:
name: adafruit-circuitpython-dht
executable: pip3
state: absent
- name: pip install adafruit-dht
pip:
name: adafruit-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=restarted enabled=yes daemon_reload=yes
- name: Add to sysdweb
include_role:
name: pi-sysdweb
vars:
sysdweb_name: dht22_sensing

View File

@@ -1,8 +1,8 @@
{
"ha_url": "https://ha.bauer.tech",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxNjkxMWIzZmQ4ZWU0NDI0OTg0MjA0ZDllMDhkNGRlMCIsImlhdCI6MTU3ODE3MDU5MSwiZXhwIjoxODkzNTMwNTkxfQ.i7CdXEZy9DV9KPHAl-msK0rOfIUlPYo4zwwJ4UGhXuc",
"dht_pin": "D12",
"polling_sleep_time_seconds": 5,
"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",