Really stuck with MQTT running my servo

So, I’m trying to run my servo through mqtt, but I’m stuck trying to do so. Here is the code I’m using

My issues are that where do I get the command topic and state topic to put into my configuration and I have a MQTT user and password. Where would they go?

Would love some help to finally figure this out.

OK let’s see what I can do for you…

Your code subscribes to the topic “boiler” in the reconnect() function; this is your command_topic for your Home Assistant configuration:


That is an odd topic, generally MQTT topics are organized in some kind of hierarchy, like “upstairs/hallway/light” and generally the last segment gives an idea of what the topic is for (like “upstairs/hallway/light/command” for the device to subscribe to to receive commands and “upstairs/hallway/light/state” for the device to publish its state back to).

Unfortunately, the code you posted doesn’t publish anything. It receives a command over the “boiler” topic and looks like it sets the servo position based on the message received, but that’s it. So, you have no state_topic for your Home Assistant config.

Your code doesn’t provide for connecting with an MQTT username and password. But you can easily modify it to add them. Add mqtt_user and mqtt_pass variables under mqtt_server, like so:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>

const char* ssid = "weefi";
const char* password = "randompasswordhere";
const char* mqtt_server = "";
const char* mqtt_user = "[your username]";
const char* mqtt_pass = "[your password]";

Then, in the reconnect() function, add those variables to the call to client.connect(), like so:

void reconnect() {
  while (!client.connected()) 
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);

    if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass))
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
} //end reconnect()

Try those things and then let me know what else you need help with!

Really appreciate that.

Still not got it working, I take it its down to my config.yaml.

  - platform: mqtt
    name: "TH16"
    command_topic: "cmnd/sonoff/power"
    state_topic: "stat/sonoff/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: mqtt
    name: "servo"
    command_topic: "boiler"
    state_topic: "boiler"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

The First TH16 switch works fine, but putting boiler in the state topic doesn’t seem to work properly. With regards to odd topics, I’m not sure what would be better to use. I’m just trying to get a servo that can switch on my heating, hence calling it boiler.

I’m sorry for my really bad understanding!

Ok so the first issue is that your code isn’t listening for “ON” and “OFF” commands - from a quick look it seems to be expecting an integer to tell the Servo what position to go to (eg 0 degrees, 180 degrees).

Also you have no state topic, so leave that out.

I would tweak a few other settings like so:

  - platform: mqtt
    name: "servo"
    command_topic: "boiler"
    qos: 0
    payload_on: "180"
    payload_off: "0"
    retain: false

You’ll need to play with the on and off values to appropriate values for your Servo/boiler (ie what position is on? Off?). But let’s see if we can get the Servo moving first.


It makes a buzzing noise, but doesn’t move nor does the buzzing really stop. I played with the servo using some tutorials the other day and it worked fine, so I’m a bit baffled yet again

One thing I noticed in the code is that in the setup() function, myservo.attach calls D1… usually this should be an integer corresponding to an Arduino gpio#. What chip are you using? If it’s the NodeMCU, you need to use the number 5 in place of D1.

Yep, hooked up to GPIO05

I know the servos run on 5V, but I’m running from the 3.3V pin, but I had no issues the other day playing about with them on 3.3v

Yes but in the code, it should probably say 5 instead of D1 is what I’m getting at.

Right, I’ve changed the gpio to 5 in n place of D1 on the arduino sketch. Still just getting a tad bit of a buzz.