changeset 0:7bd85b962845

start
author drewp@bigasterisk.com
date Sat, 21 Jan 2023 21:59:14 -0800
parents
children 125c794511a6
files .hgignore .pdm.toml .vscode/settings.json Dockerfile deploy.yaml doorbell_to_mqtt.py pdm.lock pyproject.toml skaffold.yaml
diffstat 9 files changed, 174 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,1 @@
+.venv
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.pdm.toml	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,2 @@
+[python]
+path = "/my/serv/doorbell/.venv/bin/python"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.vscode/settings.json	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,1 @@
+{}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dockerfile	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,8 @@
+FROM bang5:5000/base_basic
+
+WORKDIR /opt
+
+COPY .pdm.toml pdm.lock pyproject.toml ./
+RUN pdm sync
+
+COPY *.py ./
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/deploy.yaml	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,33 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: doorbell
+spec:
+  replicas: 1
+  strategy: { type: Recreate }
+  selector: { matchLabels: { app: doorbell } }
+  template:
+    metadata:
+      labels: { app: doorbell }
+      annotations: { prometheus.io/scrape: "false" }
+    spec:
+      containers:
+        - name: doorbell
+          image: bang5:5000/doorbell_image
+          args:
+            - pdm
+            - run
+            - python
+            - doorbell_to_mqtt.py 
+            - /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A900cepU-if00-port0
+          securityContext: { privileged: true }
+
+          volumeMounts:
+            - { name: dev, mountPath: /dev }
+      volumes:
+        - { name: dev, hostPath: { path: /dev, type: Directory } }
+
+      affinity:
+        nodeAffinity:
+          requiredDuringSchedulingIgnoredDuringExecution:
+            { nodeSelectorTerms: [{ matchExpressions: [{ key: "kubernetes.io/hostname", operator: In, values: ["bang"] }] }] }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doorbell_to_mqtt.py	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,60 @@
+import time
+import logging
+import sys
+import paho.mqtt.client as mqtt
+from pyfirmata import Arduino, INPUT
+
+PULLUP = 0x0b
+
+MIN_REPEAT_SEC = .5
+
+logging.basicConfig(level=logging.INFO)
+log = logging.getLogger()
+
+dev, = sys.argv[1:]
+
+client = mqtt.Client()
+client.will_set('doorbell/online', '0', qos=0, retain=False)
+client.connect('bang')
+
+log.info(f'connecting to {dev}')
+# note that I set StandardFirmata to double baud rate
+board = Arduino(dev, baudrate=115200)
+log.info('done')
+
+pin = board.get_pin('d:2:i')
+pin.mode = PULLUP  # config msg to send to firmata
+pin._mode = INPUT  # fake value so pyfirmata still reads this pin
+
+log.info('running mqtt client')
+client.loop_start()
+log.info('done')
+
+client.publish('doorbell/online', '1')
+
+last_value = None
+last_edge = 0
+
+
+def on_press(t):
+    global last_edge
+    if t > last_edge + MIN_REPEAT_SEC:
+        log.info(f'press {t=}')
+        client.publish('doorbell/button', 'press')
+        log.info('published')
+    last_edge = t
+
+
+try:
+    while True:
+        now = time.time()
+        board.iterate()
+        value = pin.read()
+        if value == 0 and last_value == 1:
+            on_press(now)
+        last_value = value
+        client.loop()
+except Exception:
+    client.publish('doorbell/online', '0')
+    client.disconnect()
+    raise
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pdm.lock	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,34 @@
+[[package]]
+name = "paho-mqtt"
+version = "1.6.1"
+summary = "MQTT version 5.0/3.1.1 client class"
+
+[[package]]
+name = "pyfirmata"
+version = "1.1.0"
+summary = "A Python interface for the Firmata procotol"
+dependencies = [
+    "pyserial",
+]
+
+[[package]]
+name = "pyserial"
+version = "3.5"
+summary = "Python Serial Port Extension"
+
+[metadata]
+lock_version = "4.0"
+content_hash = "sha256:0a4f0629cf61e777ba036a8c5120073441e264d2acd07f19e4bae0816a7521ff"
+
+[metadata.files]
+"paho-mqtt 1.6.1" = [
+    {url = "https://files.pythonhosted.org/packages/f8/dd/4b75dcba025f8647bc9862ac17299e0d7d12d3beadbf026d8c8d74215c12/paho-mqtt-1.6.1.tar.gz", hash = "sha256:2a8291c81623aec00372b5a85558a372c747cbca8e9934dfe218638b8eefc26f"},
+]
+"pyfirmata 1.1.0" = [
+    {url = "https://files.pythonhosted.org/packages/14/f0/05e30c9cee38f9c0e8ef08a07ff28e4c391c7d95e2bcb1f334f58cc2bb28/pyFirmata-1.1.0-py2.py3-none-any.whl", hash = "sha256:03091ffd0b06a483d286d9738ead97a50c55cf1bf6b89a5bd0c420d0a7797420"},
+    {url = "https://files.pythonhosted.org/packages/ff/e6/512cc154c0370e22429db3139e7c906c4f8ad2646f4ca816547e9ae2c26c/pyFirmata-1.1.0.tar.gz", hash = "sha256:cc180d1b30c85a2bbca62c15fef1b871db048cdcfa80959968356d97bd3ff08e"},
+]
+"pyserial 3.5" = [
+    {url = "https://files.pythonhosted.org/packages/07/bc/587a445451b253b285629263eb51c2d8e9bcea4fc97826266d186f96f558/pyserial-3.5-py2.py3-none-any.whl", hash = "sha256:c4451db6ba391ca6ca299fb3ec7bae67a5c55dde170964c7a14ceefec02f2cf0"},
+    {url = "https://files.pythonhosted.org/packages/1e/7d/ae3f0a63f41e4d2f6cb66a5b57197850f919f59e558159a4dd3a818f5082/pyserial-3.5.tar.gz", hash = "sha256:3c77e014170dfffbd816e6ffc205e9842efb10be9f58ec16d3e8675b4925cddb"},
+]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyproject.toml	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,19 @@
+[project]
+name = ""
+version = ""
+description = ""
+authors = [
+    {name = "Drew Perttula", email = "drewp@bigasterisk.com"},
+]
+dependencies = [
+    "pyfirmata>=1.1.0",
+    "paho-mqtt>=1.6.1",
+]
+requires-python = ">=3.10"
+license = {text = "MIT"}
+
+[tool.pdm]
+
+[build-system]
+requires = ["pdm-pep517>=1.0.0"]
+build-backend = "pdm.pep517.api"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/skaffold.yaml	Sat Jan 21 21:59:14 2023 -0800
@@ -0,0 +1,16 @@
+apiVersion: skaffold/v3
+kind: Config
+metadata:
+  name: doorbell
+build:
+  artifacts:
+  - image: bang5:5000/doorbell_image
+  tagPolicy:
+    dateTime:
+      format: 2006-01-02_15-04-05
+      timezone: Local
+manifests:
+  rawYaml:
+  - deploy.yaml
+deploy:
+  kubectl: {}