Can you please advice me, what Is the best way to control and get data from another Raspberry Pi in my situation?
I have Home Assistant installed on Raspberry Pi 3, and I have other Raspberry Pi 1 running Raspbian with temperature and moisture sensor and relay control. I want to use Rpi 1 for plant monitoring and watering.
I wrote python scripts to check temperature and soil moisture.
Now I want to get data from sensors with Home Assistant and create an automation for plant watering with a pump connected to the relay of Rpi 1.
What is the best way to do this?
I am not familiar with MQTT, can I use in not only to get data but also control the relay? Or are there other, easier ways to do this?
MQTT sounded very strange to me too, so I didnt want to get involved with it, until I did. Trust me when I tell you it is the easiest thing ever once you get into it.
You can use MQTT to read the sensors state and also to control relays. Go for it.
Using HA embedded broker would be a good way to go. Then publish data from Rpi1 to it as you said.
Installing mosquitto on Rpi1 would be correct, but if for some reason you start expanding your mqtt devices they will all need to use this broker. So you will have to make sure this pi will never go offline. Not saying it is a bad idea. You can do it! But I am just pointing out this for future purposes.
Take a look at this post I made some months ago. I had questions about MQTT and a fellow user made everything clear to me.
Thank you again for the advice. Got it working via MQTT.
I installed this addon on my Home Assistant Raspberry Pi 3 ad MQTT Broker (server) https://github.com/hassio-addons/addon-mqtt/blob/master/README.md
I tried the official MQTT addon, but it was not stable, my HA crashed every 2 hours after I changed it to “MQTT Server & Web client” there were no crashes.
On my Raspberry Pi 1 I wrote 3 scripts, for the temperature sensor, moisture sensor, and water pump. I made them run as services at startup. For MQTT connection I installed paho-mqtt library (https://pypi.org/project/paho-mqtt/). Here is script example:
#!/usr/bin/env python
import time
import paho.mqtt.publish as publish
broker = '192.168.1.11'
port = '1883'
auth = {'username': 'user',
'password': 'pass'}
pub_topic = 'plant/temperature'
base_dir = '/sys/bus/w1/devices/'
device_id = '28-020f924580f5'
device_file = base_dir + device_id + '/w1_slave'
def read_temp():
valid = False
temp = 0
with open(device_file, 'r') as f:
for line in f:
if line.strip()[-3:] == 'YES':
valid = True
temp_pos = line.find(' t=')
if temp_pos != -1:
temp = float(line[temp_pos + 3:]) / 1000.0
if valid:
return temp
else:
return None
while True:
try:
temp = read_temp()
if temp is not None:
publish.single(pub_topic, str(round(temp,1)), hostname=broker, port=port, auth=auth)
#print(temp)
except:
print("connection faild");
time.sleep(60)