Update dht22 sensing code + sispmctl fixes
This commit is contained in:
parent
d8c9a491d1
commit
93034dd0ec
|
@ -0,0 +1,17 @@
|
|||
---
|
||||
- hosts: bedroompi, kitchenpi
|
||||
roles:
|
||||
- pi-standard-setup
|
||||
- pi-squeezelite-custom
|
||||
- pi-shairport
|
||||
- pi-lirc
|
||||
- pi-dhtsensor
|
||||
|
||||
- hosts: esszimmerradio
|
||||
roles:
|
||||
- pi-standard-setup
|
||||
- pi-squeezelite-custom
|
||||
- pi-shairport
|
||||
- pi-lirc
|
||||
- pi-sispmctl
|
||||
#- pi-dhtsensor
|
|
@ -23,6 +23,7 @@ all:
|
|||
squeezelite_name: Esszimmer
|
||||
shairport_name: EsszimmerRadio
|
||||
wifi_ssid: BauerWLAN
|
||||
alsa_card_name: 0
|
||||
newrpi:
|
||||
squeezelite_name: MyTestRaspberry
|
||||
shairport_name: MyTestRaspberry
|
||||
|
|
|
@ -9,6 +9,31 @@ import Adafruit_DHT
|
|||
config = json.load(open("/etc/dht22_sensing.json"))
|
||||
|
||||
|
||||
class Filter:
|
||||
def __init__(self, filter_length, max_mean_deviation):
|
||||
self._length = filter_length
|
||||
self.values = []
|
||||
self.max_mean_deviation = max_mean_deviation
|
||||
|
||||
def mean(self):
|
||||
return sum(self.values) / len(self.values)
|
||||
|
||||
def is_valid_value(self, val):
|
||||
if len(self.values) == 0:
|
||||
return True
|
||||
else:
|
||||
return abs(val - self.mean()) < self.max_mean_deviation
|
||||
|
||||
def add_if_valid(self, val):
|
||||
if self.is_valid_value(val):
|
||||
self.values.append(val)
|
||||
if len(self.values) > self._length:
|
||||
self.values.pop(0)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def send_to_home_assistant(temperature, humidity):
|
||||
# print(f"Sending temperature {temperature}, humidity {humidity}")
|
||||
headers = {
|
||||
|
@ -41,9 +66,32 @@ def send_to_home_assistant(temperature, humidity):
|
|||
requests.post(humidity_url, json=humidity_data, headers=headers)
|
||||
|
||||
|
||||
last_values = []
|
||||
|
||||
|
||||
def filter(value):
|
||||
last_values.append(value)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
temp_filter = Filter(10, 5)
|
||||
humidity_filter = Filter(10, 5)
|
||||
|
||||
while True:
|
||||
sensor = Adafruit_DHT.DHT22
|
||||
humidity, temperature = Adafruit_DHT.read_retry(sensor, config['dht_pin'])
|
||||
send_to_home_assistant(temperature, humidity)
|
||||
humidity, temperature = Adafruit_DHT.read_retry(
|
||||
sensor, config['dht_pin'])
|
||||
|
||||
temp_valid = temp_filter.add_if_valid(temperature)
|
||||
humidity_valid = humidity_filter.add_if_valid(humidity)
|
||||
if not (temp_valid and humidity_valid):
|
||||
print(
|
||||
f"Discarding value {temperature}C / {humidity}%, because to far from means {temp_filter.mean()}C, {humidity_filter.mean()}%")
|
||||
else:
|
||||
try:
|
||||
send_to_home_assistant(temperature, humidity)
|
||||
except Exception as e:
|
||||
print("Failed sending to home assistant")
|
||||
print(e)
|
||||
|
||||
time.sleep(float(config['polling_sleep_time_seconds']))
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
---
|
||||
|
||||
- name: Check if sispmctl already exists
|
||||
stat: path=/usr/local/bin/sispmctl
|
||||
register: sispmctl_file
|
||||
- name: Install dependencies
|
||||
apt: name=libusb-dev cache_valid_time=7200 state=present
|
||||
apt: name="libusb-dev" cache_valid_time=7200 state=present
|
||||
- name: Copy sispmctl sources
|
||||
unarchive:
|
||||
src: sispmctl-4.7.tar.gz
|
||||
dest: /tmp
|
||||
creates: /usr/local/bin/sispmctl
|
||||
unarchive: src=sispmctl-4.7.tar.gz dest=/tmp
|
||||
when: sispmctl_file.stat.exists == false
|
||||
- name: Build and install
|
||||
command: cd /tmp/sispmctl*/ && ./configure && make install
|
||||
creates: /usr/local/bin/sispmctl
|
||||
shell: cd /tmp/sispmctl*/ && ./configure --prefix=/usr && make install
|
||||
when: sispmctl_file.stat.exists == false
|
||||
- name: Install systemd service file
|
||||
copy: src=sispmctl.service dest=/lib/systemd/system/
|
||||
- name: Add script to autostart and start now
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
EsszimmerRadio Eltern
|
||||
|
||||
________
|
||||
/______ |
|
||||
| | | _
|
||||
| ===== | | | |
|
||||
| ===== | | o o
|
||||
| | | |~
|
||||
| .-. | | o o o
|
||||
| ' . ' | | |~ |_|
|
||||
..'| '._.' | | o
|
||||
.' |_______|/
|
Loading…
Reference in New Issue