Files
smart-home/klingel_sender_nano_serial/doorbell.py
T
2020-09-02 07:28:27 +00:00

28 lines
1.0 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
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 (HTTPError, URLError, OSError) as error:
logging.error('Data of %s not retrieved because %s\nURL: %s', name, error, url)
except timeout:
logging.error('socket timed out - URL %s', url)
else:
logging.info(data)