annotate doorbell_to_mqtt.py @ 1:125c794511a6

deployment config
author drewp@bigasterisk.com
date Sun, 05 Feb 2023 14:05:06 -0800
parents 7bd85b962845
children f822e7fe7120
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
drewp@bigasterisk.com
parents:
diff changeset
1 import time
drewp@bigasterisk.com
parents:
diff changeset
2 import logging
drewp@bigasterisk.com
parents:
diff changeset
3 import sys
drewp@bigasterisk.com
parents:
diff changeset
4 import paho.mqtt.client as mqtt
drewp@bigasterisk.com
parents:
diff changeset
5 from pyfirmata import Arduino, INPUT
drewp@bigasterisk.com
parents:
diff changeset
6
drewp@bigasterisk.com
parents:
diff changeset
7 PULLUP = 0x0b
drewp@bigasterisk.com
parents:
diff changeset
8
drewp@bigasterisk.com
parents:
diff changeset
9 MIN_REPEAT_SEC = .5
drewp@bigasterisk.com
parents:
diff changeset
10
drewp@bigasterisk.com
parents:
diff changeset
11 logging.basicConfig(level=logging.INFO)
drewp@bigasterisk.com
parents:
diff changeset
12 log = logging.getLogger()
drewp@bigasterisk.com
parents:
diff changeset
13
drewp@bigasterisk.com
parents:
diff changeset
14 dev, = sys.argv[1:]
drewp@bigasterisk.com
parents:
diff changeset
15
drewp@bigasterisk.com
parents:
diff changeset
16 client = mqtt.Client()
drewp@bigasterisk.com
parents:
diff changeset
17 client.will_set('doorbell/online', '0', qos=0, retain=False)
drewp@bigasterisk.com
parents:
diff changeset
18 client.connect('bang')
drewp@bigasterisk.com
parents:
diff changeset
19
drewp@bigasterisk.com
parents:
diff changeset
20 log.info(f'connecting to {dev}')
drewp@bigasterisk.com
parents:
diff changeset
21 # note that I set StandardFirmata to double baud rate
drewp@bigasterisk.com
parents:
diff changeset
22 board = Arduino(dev, baudrate=115200)
drewp@bigasterisk.com
parents:
diff changeset
23 log.info('done')
drewp@bigasterisk.com
parents:
diff changeset
24
drewp@bigasterisk.com
parents:
diff changeset
25 pin = board.get_pin('d:2:i')
drewp@bigasterisk.com
parents:
diff changeset
26 pin.mode = PULLUP # config msg to send to firmata
drewp@bigasterisk.com
parents:
diff changeset
27 pin._mode = INPUT # fake value so pyfirmata still reads this pin
drewp@bigasterisk.com
parents:
diff changeset
28
drewp@bigasterisk.com
parents:
diff changeset
29 log.info('running mqtt client')
drewp@bigasterisk.com
parents:
diff changeset
30 client.loop_start()
drewp@bigasterisk.com
parents:
diff changeset
31 log.info('done')
drewp@bigasterisk.com
parents:
diff changeset
32
drewp@bigasterisk.com
parents:
diff changeset
33 client.publish('doorbell/online', '1')
drewp@bigasterisk.com
parents:
diff changeset
34
drewp@bigasterisk.com
parents:
diff changeset
35 last_value = None
drewp@bigasterisk.com
parents:
diff changeset
36 last_edge = 0
drewp@bigasterisk.com
parents:
diff changeset
37
drewp@bigasterisk.com
parents:
diff changeset
38
drewp@bigasterisk.com
parents:
diff changeset
39 def on_press(t):
drewp@bigasterisk.com
parents:
diff changeset
40 global last_edge
drewp@bigasterisk.com
parents:
diff changeset
41 if t > last_edge + MIN_REPEAT_SEC:
drewp@bigasterisk.com
parents:
diff changeset
42 log.info(f'press {t=}')
drewp@bigasterisk.com
parents:
diff changeset
43 client.publish('doorbell/button', 'press')
drewp@bigasterisk.com
parents:
diff changeset
44 log.info('published')
drewp@bigasterisk.com
parents:
diff changeset
45 last_edge = t
drewp@bigasterisk.com
parents:
diff changeset
46
drewp@bigasterisk.com
parents:
diff changeset
47
drewp@bigasterisk.com
parents:
diff changeset
48 try:
drewp@bigasterisk.com
parents:
diff changeset
49 while True:
drewp@bigasterisk.com
parents:
diff changeset
50 now = time.time()
drewp@bigasterisk.com
parents:
diff changeset
51 board.iterate()
drewp@bigasterisk.com
parents:
diff changeset
52 value = pin.read()
drewp@bigasterisk.com
parents:
diff changeset
53 if value == 0 and last_value == 1:
drewp@bigasterisk.com
parents:
diff changeset
54 on_press(now)
drewp@bigasterisk.com
parents:
diff changeset
55 last_value = value
drewp@bigasterisk.com
parents:
diff changeset
56 client.loop()
drewp@bigasterisk.com
parents:
diff changeset
57 except Exception:
drewp@bigasterisk.com
parents:
diff changeset
58 client.publish('doorbell/online', '0')
drewp@bigasterisk.com
parents:
diff changeset
59 client.disconnect()
drewp@bigasterisk.com
parents:
diff changeset
60 raise