Compare commits

...

60 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
711e63fb78 Added RPi pinout link 2022-11-29 15:48:17 +00:00
a25b50120c Fixed value formatting on variables 2022-11-28 16:23:01 -05:00
05c3f1d68e Changing Connection location 2022-11-28 16:17:30 -05:00
09c6e5c786 Fixed pin clearing 2022-11-28 16:11:53 -05:00
5d6c2ac555 removing process killer 2022-11-28 16:08:47 -05:00
2e79cdd10e wait after kill 2022-11-28 16:07:55 -05:00
9da08f76f1 removing loop 2022-11-28 16:06:49 -05:00
1d955fc15e test 2022-11-28 16:05:58 -05:00
025aa82971 testing 2022-11-28 16:04:21 -05:00
b39fd35fd1 testing variable passthrough 2022-11-28 16:02:52 -05:00
bf8b578272 more 2022-11-28 16:01:06 -05:00
e776ae4c7f more protocols 2022-11-28 16:00:19 -05:00
5a2c771b38 adding test protocols 2022-11-28 15:59:29 -05:00
364b9c453d testing 2022-11-28 15:57:21 -05:00
7ba0833d1f Pass sensor through run() 2022-11-28 15:56:02 -05:00
15b023ed40 Fixed getValues arguments 2022-11-28 15:53:54 -05:00
168354eb96 fixed argument 2022-11-28 15:53:13 -05:00
f34af6dae3 Create function 2022-11-28 15:52:37 -05:00
04999a7869 Changing Temp test 2022-11-28 15:52:03 -05:00
8026e39acd Created Temp Test file 2022-11-28 15:51:06 -05:00
4f8827948f Function organization 2022-11-28 15:47:40 -05:00
e661068c69 on_connect fix 2022-11-28 15:33:08 -05:00
8cd3467c04 getvalues() fix 2022-11-28 15:31:42 -05:00
f6e783b9b5 Reorganizing functions 2022-11-28 15:30:55 -05:00
001c75916f Changed brackets to not be substituted 2022-11-28 14:58:19 -05:00
8cc13e8da0 Not conversion 2022-11-28 14:20:19 -05:00
1772d2e1ce Checking if conversion is messing it up 2022-11-28 14:15:59 -05:00
cfd69b682d Set values to string for payload 2022-11-28 14:03:58 -05:00
a489f47737 Int/String fix 2022-11-28 13:58:10 -05:00
b0b966fc67 Added MQTT to temp sensor 2022-11-28 13:53:20 -05:00
faecbb7939 Temp sensor functionality, changed README 2022-11-28 11:18:19 -05:00
8b08e98fe7 Temp Sensor functionality, Changed README 2022-11-28 11:17:59 -05:00
5794987ff0 Merge branch 'main' of https://git.jerick.xyz/jerick/homeassistantPi 2022-11-28 11:11:06 -05:00
c0f8d55e70 Single test now sends payload properly 2022-11-28 11:10:10 -05:00
7 changed files with 335 additions and 0 deletions

View File

@@ -1,2 +1,8 @@
Service File can be found at /lib/systemd/system on Pi
DHT11 temp Sensor guide: https://www.freva.com/dht11-temperature-and-humidity-sensor-on-raspberry-pi/
Find an accurate broadcom pinout here
https://www.etechnophiles.com/raspberry-pi-3-b-pinout-with-gpio-functions-schematic-and-specs-in-detail/

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()

49
mqtt/mqttSingleTest.py Normal file
View File

@@ -0,0 +1,49 @@
# python 3.6
import random
import time
from paho.mqtt import client as mqtt_client
name = 'test'
broker = '192.168.0.196'
port = 1883
topic = "home/switch/test/config"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = 'mqtt'
password = 'falrenforbreakfast'
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 publish(client):
msg = '{"name": "test", "device_class": "switch", "state_topic": "home/switch/test/state"}'
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
if __name__ == '__main__':
run()

53
mqtt/mqttTest.py Executable file
View File

@@ -0,0 +1,53 @@
# python 3.6
import random
import time
from paho.mqtt import client as mqtt_client
broker = '192.168.0.196'
port = 1883
topic = "homeassistant/switch/test/config"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = 'mqtt'
password = 'falrenforbreakfast'
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 publish(client):
msg_count = 0
while True:
time.sleep(1)
msg = f"messages: {msg_count}"
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
msg_count += 1
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
if __name__ == '__main__':
run()

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

98
mqtt/tempSensor.py Normal file
View File

@@ -0,0 +1,98 @@
import time
import board
import adafruit_dht
import psutil
import random
from paho.mqtt import client as mqtt_client
name = 'test'
broker = '192.168.0.196'
port = 1883
tempTopic = "home/office/climate/temp"
humidTopic = "home/office/climate/humid"
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()
#D20 is the pin number, DHT11 is the sensor type
sensor = adafruit_dht.DHT11(board.D20)
#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(sensor):
#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)
def publish(client, temp, humidity):
#Publish Temp Results
tempMsg = '{{ "temperature": "{}" }}'.format(temp)
#tempMsg = temp
result = client.publish(tempTopic, tempMsg)
status = result[0]
if status == 0:
#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)
status = result[0]
if status == 0:
#print(f"Send `{humidMsg}` to topic `{humidTopic}`")
pass
else:
print(f"Failed to send message to topic {humidTopic}")
while True:
#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 (300.0)
client.loop_stop() #Stop loop
client.disconnect() # Disconnect

29
mqtt/tempTest.py Normal file
View File

@@ -0,0 +1,29 @@
import time
import board
import adafruit_dht
import psutil
import random
from paho.mqtt import client as mqtt_client
sensor = adafruit_dht.DHT11(board.D20)
def getValues(sensor):
try:
temp = sensor.temperature
humidity = sensor.humidity
#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
temp, humidity = getValues(sensor)
print (temp)
print (humidity)