Files
smart-home/klingel_sender_nano_serial/doorbell.py
T
2020-09-18 04:28:41 +00:00

29 lines
982 B
Python

# https://www.instructables.com/id/Arduino-Python-Communication-via-USB/
# in rc.local:
# su tilman -c 'python3 /media/hd/admin/doorbell/doorbell.py >> /media/hd/admin/doorbell/doorbell.log 2>&1 &'
import serial
import urllib.request
import logging
import time
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
logging.info('Initializing')
arduino = serial.Serial('/dev/ttyUSB0', 9600, timeout=.1)
while True:
data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars
if data:
data = data.decode('utf8')
if (data == 'RING'):
try:
response = urllib.request.urlopen('http://192.168.178.51/bell/on', timeout=5).read().decode('utf-8')
except Exception:
logging.error("Could not ring doorbell")
# logging.exception("Could not ring doorbell")
time.sleep(300)
else:
logging.info(data)