Skip to content

Instantly share code, notes, and snippets.

@EvelynSubarrow
Created December 2, 2020 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvelynSubarrow/1c6298b616304d447933e41d37215ed6 to your computer and use it in GitHub Desktop.
Save EvelynSubarrow/1c6298b616304d447933e41d37215ed6 to your computer and use it in GitHub Desktop.
Doorbell client/server
#!/usr/bin/python3.5
import socket, notify2, time, requests, json, datetime, hashlib, struct
notify2.init("Doorbell")
def notify(summary, text):
n = notify2.Notification(summary, text)
n.set_timeout(60)
n.set_urgency(2)
n.show()
def receive(hmac):
data, addr = client.recvfrom(1024)
if len(data) < 45: return
magic = data[:2]
hash = data[3:35]
full_body = data[36:]
seq_id = struct.unpack(">Q", full_body[:8])[0]
body = full_body[9:]
comp_hash = hashlib.sha256(hmac.encode("utf8") + full_body).digest()
if comp_hash == hash:
return seq_id, body.decode("utf8")
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind(("", 37700))
last_triggered = datetime.datetime(2015,1,1, 1,1,1)
last_seq = 0
while True:
intensity = 0
try:
seq, body = receive("Jeger")
if last_seq < seq:
last_seq = seq
intensity = json.loads(body)["value"]
except Exception as e:
print(e)
pass # todo: not any of this
if intensity > 100 and (datetime.datetime.now()-last_triggered).seconds > 15:
print(intensity)
notify("bell", "The doorbell's ringing, Evelyn.")
last_triggered = datetime.datetime.now()
#!/usr/bin/env python3
import json, time, hashlib, struct, datetime
from socket import *
import board
import busio
import adafruit_veml7700
i2c = busio.I2C(board.SCL, board.SDA)
veml7700 = adafruit_veml7700.VEML7700(i2c)
cs = socket(AF_INET, SOCK_DGRAM)
cs.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
def send_message(message, hmac):
# sequence + message
message_body = struct.pack(">Q", int(datetime.datetime.now().timestamp())) + b"\n" + message.encode("utf8")
# hmac + full message
message_digest = hashlib.sha256(hmac.encode("utf8") + message_body).digest()
composed_message = b"DN\n" + message_digest + b"\n" + message_body
cs.sendto(composed_message, ('<broadcast>', 37700))
while True:
time.sleep(0.1)
light = veml7700.lux
j_ver = json.dumps({"value": light})
if light>1:
print(light)
if light>100:
send_message(j_ver, "Jeger")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment