Multiple MQTT Switches for single Raspberry Zero W

Hello all,

I am unfortunately am completely stumped…
I’m trying to build a Pet-Feeder for our two Micro-pigs.
I’ve connected to 12V DC motors to the GPIO pins of a raspberry pi Zero and I’m using MQTT to turn the motors of and on from my Rasperry Pi 3, running Hass.IO.
Now I am a complete Pyhon noob, however I’ve been able to write a program that turns one of the DC motors on and off using a switch, so… perfect…
Well… almost, even though the first program works, writing and running the exact same program (using a different MQTT topic and a different GPIO pin) Does not work… like at all.

The programs are started on booth, using a launcher.sh file.

These are the things I’ve tested:

  1. I’ve turned both DC motors on and of using python scripts on the raspberry pi zero, both work!
  2. I’ve checked the output of the MQTT switches using Chrome’s MQTT-Lens, all switches send the expected payloads.
  3. I’ve run the non-working python scripts, letting it print messages on connect and it shows that the program runs and connects to the MQTT broker.
  4. I’ve set all switches to completely different topics, still only the one works.

I am completely unable to figure out why 1 (and by the way, always the same!) program always works, while the others do not.

These are the python scripts:

The working one: BaconSwitch.py

import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7,GPIO.OUT)

def on_connect(client, userdata, flags, rc):
#print("Connected with result code "+str(rc))
client.subscribe(“ha/PigFeeder/Bacon/set”)
GPIO.output(7, GPIO.HIGH)
client.publish(“ha/PigFeeder/Bacon”,“OFF”)

def on_message(client, userdata, msg):
if msg.payload.decode() == “ON”:
#print(“Motor On”)
GPIO.output(7, GPIO.LOW)
client.publish(“ha/PigFeeder/Bacon”,“ON”)

               #client.disconnect()
       else:
               if msg.payload.decode() == "OFF":
                       #print("Motor Off")
                       GPIO.output(7, GPIO.HIGH)
                       client.publish("ha/PigFeeder/Bacon","OFF")

client = mqtt.Client()
client.connect("[MyInternalIPAddress]", 1883,60)

client.on_connect = on_connect
client.on_message = on_message

client.loop_forever()

One of the Non-working ones: CrispySwitch.py

import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(8,GPIO.OUT)

def on_connect(client, userdata, flags, rc):
#print("Connected with result code "+str(rc))
client.subscribe(“ha/PigFeeder/Crispy/set”)
GPIO.output(8, GPIO.HIGH)
client.publish(“ha/PigFeeder/Crispy”,“OFF”)

def on_message(client, userdata, msg):
if msg.payload.decode() == “ON”:
#print(“Motor On”)
GPIO.output(8, GPIO.LOW)
client.publish(“ha/PigFeeder/Crispy”,“ON”)

               #client.disconnect()
       else:
               if msg.payload.decode() == "OFF":
                       #print("Motor Off")
                       GPIO.output(8, GPIO.HIGH)
                       client.publish("ha/PigFeeder/Crispy","OFF")

client = mqtt.Client()
client.connect("[MyInternalIPAddress]", 1883,60)

#print(“Program started”)

client.on_connect = on_connect
client.on_message = on_message

client.loop_forever()

And to (hopefuly) be complete, here’s my launcher.sh script:

#!bin/sh
#launcher.sh
#navigate to home directory, then to PythonScript directory.
#Execute Python Script, navigate back home

cd /
cd /home/pi/PythonScripts/PigFeeder
sleep 10
#screen -A -L -d -m -S ‘TestSwitch’ python3 TestSwitchV3.py &
#screen -A -L -d -m -S ‘BaconLamp’ python3 BaconLampV3.py &
screen -A -L -d -m -S ‘BaconSwitch’ python3 BaconSwitch.py &
screen -A -L -d -m -S ‘CrispySwitch’ python3 CrispySwitch.py &
screen -A -L -d -m -S ‘PigsSwitch’ python3 PigsSwitch.py &
exit 0
cd /

And finally the configuration.yaml part for the MQTT-switches:

switch:

  • platform: mqtt
    name: “BaconMQTTSwitch”
    state_topic: “ha/PigFeeder/Bacon”
    command_topic: “ha/PigFeeder/Bacon/set”
    payload_on: “ON”
    payload_off: “OFF”
    optimistic: false
    qos: 0
    retain: true
  • platform: mqtt
    name: “CrispyMQTTSwitch”
    state_topic: “ha/PigFeeder/Crispy”
    command_topic: “ha/PigFeeder/Crispy/set”
    payload_on: “AAN”
    payload_off: “UIT”
    optimistic: false
    qos: 0
    retain: true

Does anyone know/see what I’m doing wrong?

Well for a start UIT is not the same as OFF and AAN is not the same as ON.

Also please properly quote your code - see at the very top of the page.

wow…
I do not dare tell you the times I’ve re-read the code… never ever registering that little detail…
Thank you very much, and I’ll keep the markdown in mind next time!