Compare commits
15 Commits
a5048bbd4f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b537d3fac | |||
| dfd9635559 | |||
| fada461518 | |||
| 4536882e81 | |||
| 5baac26ff6 | |||
| ed3aee79f4 | |||
| 3c3b373090 | |||
| 82d8bf9982 | |||
| e2b6537a89 | |||
| 299d4cf8d1 | |||
| e53dc723c4 | |||
| d28f3e93ad | |||
| 8df0707a8a | |||
| 9a777acc15 | |||
| ec298423f2 |
90
mqtt/motionSensor.py
Normal file
90
mqtt/motionSensor.py
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import time
|
||||||
|
import board
|
||||||
|
import adafruit_dht
|
||||||
|
import psutil
|
||||||
|
import random
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
import time
|
||||||
|
|
||||||
|
from paho.mqtt import client as mqtt_client
|
||||||
|
|
||||||
|
name = 'test'
|
||||||
|
broker = '192.168.0.196'
|
||||||
|
port = 1883
|
||||||
|
motionTopic = "home/office/motion/status"
|
||||||
|
username = 'mqtt'
|
||||||
|
password = 'falrenforbreakfast'
|
||||||
|
|
||||||
|
|
||||||
|
client_id = f'python-mqtt-office-{random.randint(0, 1000)}'
|
||||||
|
|
||||||
|
#We first check if a libgpiod process is running. If yes, we kill it!
|
||||||
|
for proc in psutil.process_iter():
|
||||||
|
if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei':
|
||||||
|
proc.kill()
|
||||||
|
|
||||||
|
#12 is the pin number
|
||||||
|
pir_sensor = 12
|
||||||
|
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
|
||||||
|
GPIO.setup(pir_sensor, GPIO.IN)
|
||||||
|
|
||||||
|
#Function Definitions
|
||||||
|
def connect_mqtt():
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
if rc == 0:
|
||||||
|
print("Connected to MQTT Broker!")
|
||||||
|
else:
|
||||||
|
print("Failed to connect, return code %d\n", rc)
|
||||||
|
|
||||||
|
client = mqtt_client.Client(client_id)
|
||||||
|
client.username_pw_set(username, password)
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.connect(broker, port)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def getValues(pir_sensor):
|
||||||
|
current_state = 0
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
current_state = GPIO.input(pir_sensor)
|
||||||
|
#Condition if motion is detected
|
||||||
|
if current_state == 1:
|
||||||
|
print("GPIO pin %s is %s" % (pir_sensor, current_state))
|
||||||
|
return current_state
|
||||||
|
else:
|
||||||
|
return current_state
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
return current_state
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def publish(client, current_state):
|
||||||
|
#Publish Temp Results
|
||||||
|
motionMsg = '{{ "state": "{}" }}'.format(current_state)
|
||||||
|
#tempMsg = temp
|
||||||
|
result = client.publish(motionTopic, motionMsg)
|
||||||
|
status = result[0]
|
||||||
|
if status == 0:
|
||||||
|
#print(f"Send `{tempMsg}` to topic `{tempTopic}`")
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print(f"Failed to send message to topic {motionTopic}")
|
||||||
|
|
||||||
|
#Run functions
|
||||||
|
client = connect_mqtt() # Connect to Broker
|
||||||
|
client.loop_start() #Start loop
|
||||||
|
while True:
|
||||||
|
current_state = getValues(pir_sensor)
|
||||||
|
publish(client, current_state)
|
||||||
|
# print(current_state)
|
||||||
|
|
||||||
|
client.loop_stop() #Stop loop
|
||||||
|
client.disconnect() # Disconnect
|
||||||
|
# GPIO.cleanup()
|
||||||
10
mqtt/tempHumidSensor.service
Normal file
10
mqtt/tempHumidSensor.service
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Script for the Temp & Humidity sensors through MQTT
|
||||||
|
After=multi-user.target
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/bin/python3 /home/pi/homeassistantPi/mqtt/tempSensor.py
|
||||||
|
User=pi
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=30s
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -46,7 +46,8 @@ def getValues(sensor):
|
|||||||
try:
|
try:
|
||||||
temp = sensor.temperature
|
temp = sensor.temperature
|
||||||
humidity = sensor.humidity
|
humidity = sensor.humidity
|
||||||
|
print("Temp:", temp)
|
||||||
|
print("Humidity:", humidity)
|
||||||
#convert to Farenheit
|
#convert to Farenheit
|
||||||
temp = (temp * 1.8) + 32
|
temp = (temp * 1.8) + 32
|
||||||
#Round the decimals
|
#Round the decimals
|
||||||
|
|||||||
Reference in New Issue
Block a user