mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-13 17:22:54 -05:00
Add Python sender for Raspberry Pi Zero
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -6,4 +6,6 @@ obj/
|
||||
.vs/
|
||||
_ReSharper.Caches/
|
||||
|
||||
Private/
|
||||
Private/
|
||||
|
||||
__pycache__/
|
||||
|
||||
1
DeviceStatus/Sender/.gitignore
vendored
Normal file
1
DeviceStatus/Sender/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.vscode/
|
||||
6
DeviceStatus/Sender/config.py
Executable file
6
DeviceStatus/Sender/config.py
Executable file
@@ -0,0 +1,6 @@
|
||||
from device import Device
|
||||
|
||||
devices = []
|
||||
|
||||
devices.append(Device("washer", 20))
|
||||
devices.append(Device("dryer", 21))
|
||||
6
DeviceStatus/Sender/device.py
Executable file
6
DeviceStatus/Sender/device.py
Executable file
@@ -0,0 +1,6 @@
|
||||
class Device:
|
||||
last_status = 0
|
||||
|
||||
def __init__(self, name, pin):
|
||||
self.name = name
|
||||
self.pin = pin
|
||||
9
DeviceStatus/Sender/device_status_sender.service
Executable file
9
DeviceStatus/Sender/device_status_sender.service
Executable file
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=Device Status Server Service
|
||||
|
||||
[Service]
|
||||
Environment=PYTHONUNBUFFERED=1
|
||||
ExecStart=/usr/bin/python /home/ckaczor/Code/Personal/HomeMonitor/DeviceStatus/Sender/sender.py
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target
|
||||
73
DeviceStatus/Sender/sender.py
Executable file
73
DeviceStatus/Sender/sender.py
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import config
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from datetime import datetime as dt
|
||||
from paho.mqtt.client import connack_string as ack
|
||||
from paho.mqtt.properties import Properties
|
||||
from paho.mqtt.packettypes import PacketTypes
|
||||
|
||||
|
||||
def log(message=""):
|
||||
print(dt.now().strftime("%H:%M:%S.%f")[:-2] + " " + message)
|
||||
|
||||
|
||||
def on_connect(client, userdata, flags, rc, v5config=None):
|
||||
log("Connection returned result: " + ack(rc))
|
||||
|
||||
|
||||
def on_publish(client, userdata, mid, tmp=None):
|
||||
log("Published message id: " + str(mid))
|
||||
|
||||
|
||||
log("Config:")
|
||||
|
||||
for device in config.devices:
|
||||
log(" pin %s: %s" % (device.pin, device.name))
|
||||
|
||||
log()
|
||||
|
||||
client = mqtt.Client(client_id="device_status_sender",
|
||||
transport="tcp",
|
||||
protocol=mqtt.MQTTv5)
|
||||
|
||||
|
||||
properties = Properties(PacketTypes.CONNECT)
|
||||
properties.SessionExpiryInterval = 30 * 60
|
||||
|
||||
client.connect("172.23.10.3", 1883, 60,
|
||||
clean_start=mqtt.MQTT_CLEAN_START_FIRST_ONLY, properties=properties)
|
||||
|
||||
client.on_connect = on_connect
|
||||
client.on_publish = on_publish
|
||||
|
||||
client.loop_start()
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
for device in config.devices:
|
||||
log("Setting up pin %s" % (device.pin))
|
||||
GPIO.setup(device.pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
|
||||
|
||||
log()
|
||||
|
||||
while True:
|
||||
for device in config.devices:
|
||||
pin_status = GPIO.input(device.pin)
|
||||
|
||||
if pin_status != device.last_status:
|
||||
log("pin %s: %s" % (device.pin, pin_status))
|
||||
|
||||
device.last_status = pin_status
|
||||
|
||||
info = client.publish(device.name, str(pin_status), 1)
|
||||
|
||||
info.wait_for_publish()
|
||||
|
||||
time.sleep(0.25)
|
||||
|
||||
GPIO.cleanup()
|
||||
Reference in New Issue
Block a user