Files
smart-home/doorbell/klingel_sender_nano_serial/doorbell.py
T
2021-09-14 17:22:43 +02:00

36 lines
1.3 KiB
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 logging.handlers as handlers
import time
logger = logging.getLogger('Rotating Log')
logger.setLevel(logging.INFO)
logHandler = handlers.RotatingFileHandler('/media/hd/admin/doorbell/doorbell.log', maxBytes=1000000, backupCount=2)
logHandler.setLevel(logging.INFO)
logHandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S'))
logger.addHandler(logHandler)
logger.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:
logger.error("Could not ring doorbell")
# logger.exception("Could not ring doorbell")
time.sleep(300)
else:
logger.info(data)