26 lines
880 B
Python
26 lines
880 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
|
|
|
|
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.exception("Could not ring doorbell")
|
|
else:
|
|
logging.info(data)
|