Compare commits
58 Commits
5794987ff0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b537d3fac | |||
| dfd9635559 | |||
| fada461518 | |||
| 4536882e81 | |||
| 5baac26ff6 | |||
| ed3aee79f4 | |||
| 3c3b373090 | |||
| 82d8bf9982 | |||
| e2b6537a89 | |||
| 299d4cf8d1 | |||
| a5048bbd4f | |||
| e53dc723c4 | |||
| 6e3e9aa362 | |||
| d28f3e93ad | |||
| 5738ee37c6 | |||
| 8df0707a8a | |||
| d363c6b2d1 | |||
| 9a777acc15 | |||
| ec298423f2 | |||
| b1de1a6d67 | |||
| 8b630b8ddc | |||
| 6195fc4d40 | |||
| 31f1baa0c2 | |||
| 1207ee365d | |||
| cb411e09c9 | |||
| f1b0113c8d | |||
| 711e63fb78 | |||
| a25b50120c | |||
| 05c3f1d68e | |||
| 09c6e5c786 | |||
| 5d6c2ac555 | |||
| 2e79cdd10e | |||
| 9da08f76f1 | |||
| 1d955fc15e | |||
| 025aa82971 | |||
| b39fd35fd1 | |||
| bf8b578272 | |||
| e776ae4c7f | |||
| 5a2c771b38 | |||
| 364b9c453d | |||
| 7ba0833d1f | |||
| 15b023ed40 | |||
| 168354eb96 | |||
| f34af6dae3 | |||
| 04999a7869 | |||
| 8026e39acd | |||
| 4f8827948f | |||
| e661068c69 | |||
| 8cd3467c04 | |||
| f6e783b9b5 | |||
| 001c75916f | |||
| 8cc13e8da0 | |||
| 1772d2e1ce | |||
| cfd69b682d | |||
| a489f47737 | |||
| b0b966fc67 | |||
| faecbb7939 | |||
| 8b08e98fe7 |
@@ -1,2 +1,8 @@
|
|||||||
Service File can be found at /lib/systemd/system on Pi
|
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
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
|
||||||
98
mqtt/tempSensor.py
Normal file
98
mqtt/tempSensor.py
Normal 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
29
mqtt/tempTest.py
Normal 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)
|
||||||
Reference in New Issue
Block a user