Compare commits

...

26 Commits

Author SHA1 Message Date
3b537d3fac Fixed Motion to update frequently 2022-12-27 14:10:14 -05:00
dfd9635559 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-26 03:28:14 -05:00
fada461518 Changed motion variables, first test 2022-12-26 03:27:19 -05:00
4536882e81 Changed Board Status 2022-12-26 03:26:35 -05:00
5baac26ff6 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-26 02:48:55 -05:00
ed3aee79f4 First commit of Motion Sensor 2022-12-26 02:48:53 -05:00
3c3b373090 Changed restart time to 30s 2022-12-13 11:34:56 -05:00
82d8bf9982 added service file 2022-12-12 13:56:16 -05:00
e2b6537a89 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-12 07:46:28 -05:00
299d4cf8d1 troubleshooting 2022-12-12 07:46:24 -05:00
a5048bbd4f Fixed the fix that broke it in the first place 2022-12-06 14:17:58 -05:00
e53dc723c4 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-06 12:50:48 -05:00
6e3e9aa362 Moved process kill and sensor into loop 2022-12-06 12:49:51 -05:00
d28f3e93ad Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-06 09:25:06 -05:00
5738ee37c6 Changed update time to 5 minutes 2022-12-06 09:24:53 -05:00
8df0707a8a Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-06 09:03:22 -05:00
d363c6b2d1 Fixing local variable error 2022-12-06 09:03:10 -05:00
9a777acc15 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-12-06 08:31:13 -05:00
ec298423f2 debugging 2022-12-06 08:31:09 -05:00
b1de1a6d67 Fix error where sensor dies not read data & exits 2022-12-06 08:30:19 -05:00
8b630b8ddc Testing local variable error 2022-12-01 11:24:38 -05:00
6195fc4d40 Fixed timer position 2022-11-29 16:51:12 -05:00
31f1baa0c2 Changed timer to 60 seconds 2022-11-29 15:33:49 -05:00
1207ee365d Final Cleanup 2022-11-29 15:33:14 -05:00
cb411e09c9 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-11-29 15:29:09 -05:00
f1b0113c8d Version 1.0, cleaned out debugging 2022-11-29 15:28:41 -05:00
3 changed files with 131 additions and 38 deletions

90
mqtt/motionSensor.py Normal file
View 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()

View 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

View File

@@ -24,17 +24,7 @@ for proc in psutil.process_iter():
#D20 is the pin number, DHT11 is the sensor type
sensor = adafruit_dht.DHT11(board.D20)
#MQTT Connection
# def on_connect(client, rc):
# if rc == 0:
# print("Connected to MQTT Broker!")
# else:
# print("Failed to connect, return code %d\n", rc)
# #MQTT Connect
# client = mqtt_client.Client(client_id)
# client.username_pw_set(username, password)
# client.on_connect = on_connect
# client.connect(broker, port)
#Function Definitions
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
@@ -49,57 +39,60 @@ def connect_mqtt():
return client
def getValues(sensor):
try:
print(sensor)
temp = sensor.temperature
humidity = sensor.humidity
#While loop to try sensor again if it fails, seems to happen occasionally
result = None
while result is None:
try:
temp = sensor.temperature
humidity = sensor.humidity
print("Temp:", temp)
print("Humidity:", humidity)
#convert to Farenheit
temp = (temp * 1.8) + 32
#Round the decimals
temp = round(temp, 2)
#print("Temperature: {}*F Humidity: {}% ".format(temp, humidity))
result = 1
return temp, humidity
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)
#convert to Farenheit
temp = (temp * 1.8) + 32
print("Temperature: {}*F Humidity: {}% ".format(temp, humidity))
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)
except Exception as error:
sensor.exit()
raise error
return temp, humidity
def publish(client, temp, humidity):
#Publish Temp Results
tempMsg = '{{ "temperature": "{}" }}'.format(temp)
#tempMsg = temp
result = client.publish(tempTopic, tempMsg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send `{tempMsg}` to topic `{tempTopic}`")
#print(f"Send `{tempMsg}` to topic `{tempTopic}`")
pass
else:
print(f"Failed to send message to topic {tempTopic}")
#Publish Humid Results
humidMsg = '{{ "humidity": "{}" }}'.format(humidity)
result = client.publish(humidTopic, humidMsg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send `{humidMsg}` to topic `{humidTopic}`")
#print(f"Send `{humidMsg}` to topic `{humidTopic}`")
pass
else:
print(f"Failed to send message to topic {humidTopic}")
time.sleep(5.0)
print (sensor)
while True:
client = connect_mqtt()
client.loop_start()
#Run functions
client = connect_mqtt() # Connect to Broker
client.loop_start() #Start loop
temp, humidity = getValues(sensor)
getValues(sensor)
publish(client, temp, humidity)
time.sleep (5.0)
time.sleep (300.0)
client.loop_stop() #Stop loop
client.disconnect() # disconnect
client.disconnect() # Disconnect