So I had set up my rpi3 as an mqtt listener by running a script on it.
My rpi2 which has hassio installed would use commands to send mqtt messages to the rpi3 which controls a relay board via ble via python scripts. I know the python scripts still work because if I run them manually on the rpi2/hassio the relays are activated just fine.
The way the rpi3 would listen was via a script, mqttReceive.py:
import paho.mqtt.client as mqtt
from failedPythonBLE import turn1ON,turn1OFF,turn2ON,turn2OFF,turn3ON,turn3OFF
MQTT_SERVER = "hassio.local"
MQTT_PATH = "test_channel"
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(MQTT_PATH)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
# more callbacks, etc
if str(msg.payload)=='01':
print "letsGoBaby! Turn On Relay 1"
turn1ON.letsgobaby()
elif str(msg.payload)=='02':
print "Turn Off Relay 1"
turn1OFF.letsgobaby()
elif str(msg.payload)=="komp-on":
print "Turn On Relay 2"
turn2ON.letsgobaby()
elif str(msg.payload)=="komp-off":
print "Turn Off Relay 2"
turn2OFF.letsgobaby()
elif str(msg.payload)=="sole-on":
print "Turn On Relay 3"
turn3ON.letsgobaby()
elif str(msg.payload)=="sole-off":
print "Turn Off Relay 3"
turn3OFF.letsgobaby()
elif str(msg.payload)=='07':
print "Turn On Relay 4"
turn4ON.letsgobaby()
elif str(msg.payload)=='08':
print "Turn Off Relay 4"
turn4OFF.letsgobaby()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
# Blocking call that processes
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
Error Im getting in tmux running the mqttListen:
outside
Traceback (most recent call last):
File “/home/pi/Documents/python/mqttReceive.py”, line 49, in
client.connect(MQTT_SERVER, 1883, 60)
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 839, in connect
return self.reconnect()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 962, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File “/usr/lib/python2.7/socket.py”, line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
What should I look into? The paho package?