Smartthings MQTT Bridge

Out of curiosity, what command are you using to start your main instance of mosquitto? The way you attempted to collect mosquitto logs in one of your previous posts was not correct (you were starting a second instance and printing out verbose logs, and it told you that 1883 was already in use). If you want to use auth (I recommend it if you are exposing your MQTT bridge to the Internet), you have to specify the credentials file in your start command (edit: or in your configuration file).

This is my default mosquitto.conf

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

And I replace PM2 with Supervisor (http://supervisord.org/) - it starts automatically.

If you’d like to use Supervisor,

sudo apt-get install supervisor

Then add the following to /etc/supervisor/conf.d/smartthings-mqtt-bridge.conf

[program:smartthings-mqtt-bridge]
command=/usr/local/bin/smartthings-mqtt-bridge
autostart=true
autorestart=true
startretries=999999999999999

Follow by

sudo systemctl restart supervisor

To learn more about it.

sudo supervisorctl
supervisor> help

@mstberto After a sudo reboot, I don’t do anything to start it. I’ve been working under the assumption that it auto starts when based from the All In One Installer. Based on your comment that mosquitto - v was starting a new instance, I assume the auto start is working because like you said, it said the port was already in use. I’d love to use auth, but if the bridge doesn’t support it, I’m fine with going unauth for a bit.

@hongtat. Thanks, it seems that Supervisor may be a better process manager for what I’m doing. If I can get the bridge working, I’ll probably switch over to that.

I have mine using auth with the ST bridge so it all definitely works.

The reason I was asking what command you use to start it is because I think you are telling your HA instance and Bridge to use auth when your broker doesn’t actually have it enabled. You can try removing the usernames and passwords from your bridge and HA configs and see if it starts working.

If you can, I’d try to figure out what command your process manager is using to see if it is pointing to a config where you can enable auth (point to a credentials file) among other things like verbose logging. You’d also have to generate credentials using mosquitto_passwd.

1 Like

That’s encouraging! I definitely want to use auth in the long run. I’ve been able to connect to the broker from HA, so I think that part was working correctly. I did set the password using mosquitto_passwd

What about the following? Do your logs look like this? mqtt://mqtt

pm2 logs smartthings-mqtt-bridge
etc etc
0|smartthi | info: Connecting to MQTT at mqtt://mqtt

You should be seeing more so the bridge is probably not connecting. You could try specifying the loopback address or your router-assigned IP directly in the ST config. Obviously that’s what localhost should resolve to but I’m just taking shots in the dark here. Just to confirm, your bridge and broker are running on the same box right?

Are your HA instance and ST bridge using the same credentials? If not, can you verify the credentials you created with mosquito match what you’re putting in ST config?

Edit: just saw mqtt://mqtt. Is your box called mqtt? That seems wrong.

Yes, the HA instance and the bridge are using the same credentials. They are both also running on the same raspberry pi.

No, the box is not called mqtt. The box is “called” raspberrypi, but I’m running it on localhost. I would assume that this is the value that controls that connection string, but I haven’t had much luck tracing it back in the code. If I changed local host to garblygoop, and rebooted, it doesnt seem to make a difference. :stuck_out_tongue:

#config.yml mqtt-bridge config

mqtt:
    # Specify your MQTT Broker's hostname or IP address here
    host: localhost
    # Preface for the topics $PREFACE/$DEVICE_NAME/$PROPERTY
    preface: smartthings

    # Suffix for the state topics $PREFACE/$DEVICE_NAME/$PROPERTY/$STATE_SUFFIX
    # state_suffix: state
    # Suffix for the command topics $PREFACE/$DEVICE_NAME/$PROPERTY/$COMMAND_SUFFIX
    # command_suffix: cmd

    # Other optional settings from https://www.npmjs.com/package/mqtt#mqttclientstreambuilder-options
    username: pi
    password: mqttpass

# Port number to listen on
port: 8080

Ah, try host: mqtt://localhost.

Looking at the sample config here since I don’t have mine in front of me:

Unfortunately, same result. Logs still show mqtt://mqtt after updating config.yml mqtt-bridge config to show the following and sudo reboot.

    host: mqtt://localhost
pm2 logs smartthings-mqtt-bridge   
0|smartthi | info: Saving current state
0|smartthi | info: Connecting to MQTT at mqtt://mqtt

Okay wow. I think i finally figured it out. In addition to the earlier port mix up, I’ve been modifying the wrong friggen config.yml!!!

In my first post, I said that I created the config like this.

sudo cp /usr/local/lib/node_modules/smartthings-mqtt-bridge/_config.yml /usr/local/lib/node_modules/smartthings-mqtt-bridge/config.yml
sudo nano /usr/local/lib/node_modules/smartthings-mqtt-bridge/config.yml

However, when smartthigns-mqtt-bridge is looking for its configuration in the process directory, NOT the node_modules directory.

server.js

var CONFIG_DIR = process.env.CONFIG_DIR || process.cwd(),
    CONFIG_FILE = path.join(CONFIG_DIR, 'config.yml'),

I finally got to this point when I started dropping debug into server.js when I was frustrated by the config changes not making a difference. I should have started in the code in the first place :confused:

So now I have a connection from smartthings-mqtt-bridge over to mqtt broker, and I can see the events in the logs when I physically press the button. If i press the button in HA, it turns off the lights. Success!

[STREAMING] Now streaming realtime logs for [smartthings-mqtt-bridge] process
0|smartthi | info: Starting SmartThings MQTT Bridge - v1.3.4
0|smartthi | info: Loading configuration
0|smartthi | info: Loading previous state
0|smartthi | info: Perfoming configuration migration
0|smartthi | info: Saving current state
0|smartthi | info: Connecting to MQTT at mqtt://localhost
0|smartthi | info: Configuring autosave
0|smartthi | info: Configuring API
0|smartthi | info: Listening at http://localhost:8080
0|smartthi | info: Incoming message from SmartThings: smartthings/Dining Room Light/switch = off
0|smartthi | info: Incoming message from SmartThings: smartthings/Dining Room Light/switch = on

ugg. So yes. You, reader in the future, if you are having problems like me, make sure you are using the default mqtt bridge port: 8080 in config.yml and make sure that you are modifying the correct smartthings-mqtt-bridge configuration. Also, mqtt authentication is now supported, so feel free to use it.

Hopefully, I’ll put toget a pull request in the near future with some debugging to help along.

Thanks everyone @hongtat @mstberto @richardholst for all the assistance and being a sounding board. I know I’m not the OP, but I appreciate it :smiley:

1 Like

Awesome, glad to hear you found it. It’s always the little things.

1 Like

I am still beating my head against the wall on this. Your thread sounds exactly like my last 8 days!!!

Can you tell me where the actual config.yml file needed to change is located exactly?

Thanks in advance!

I’m super bummed that it took me so long to see this, i was looking forward to helping someone out. :smiley:

If you did a fresh install using AOI, it should be in /home/pi. If you switched your user names, do cd ~/.

You can also try this, it will copy the default config file to the correct location and then open the editor for you to fix it.

sudo cp /usr/local/lib/node_modules/smartthings-mqtt-bridge/_config.yml ~/config.yml
sudo nano ~/config.yml
1 Like

Wow, I wish I had found this thread before I spent all day banging my head against the bridge.

I battled all the way through the setup, many of the same problems as above, and I finally got everything working correctly, except that MQTT commands from HA are not going back to SmartThings. If I turn on a light in SmartThings, it updates to HA, but not vice versa. The state changes in HA but it never makes it back to SmartThings.

Any idea what I’m doing wrong?

platform: mqtt
name: Kitchen Lights
state_topic: “smartthings/Kitchen Lights/switch”
command_topic: “smartthings/Kitchen Lights/switch”
payload_on: “on”
payload_off: “off”
retain: true

Any details in the HA log or the mqtt-bridge/MQTT log? Post anything relevant and I’d be happy to try and help troubleshoot.

Hello everyone,

I’m new to this entire platform so please excuse me if I don’t refer to things correctly.

I’m running HASS on my Raspberry Pi using the built-in mqtt broker in my configuration just like this:

mqtt:

I’m also trying to run the smartthings-mqtt-bridge with the following config:

mqtt:
    host: mqtt://<my raspberry pi IP>
    preface: smartthings
    username: homeassistant
    password: <my HA http api password>

port: 8080

When I try to run the smartthings-mqtt-bridge with pm2 and then watch the logs, this is what I get:

0|smartthi | info: Starting SmartThings MQTT Bridge - v1.3.4
0|smartthi | info: Loading configuration
0|smartthi | info: Loading previous state
0|smartthi | info: Perfoming configuration migration
0|smartthi | info: Saving current state
0|smartthi | info: Connecting to MQTT at mqtt://<my raspberry pi IP>
0|smartthi | info: Configuring autosave
0|smartthi | info: Configuring API
0|smartthi | Error: listen EADDRINUSE :::8080
0|smartthi |     at Object.exports._errnoException (util.js:1022:11)
0|smartthi |     at exports._exceptionWithHostPort (util.js:1045:20)
0|smartthi |     at Server._listen2 (net.js:1262:14)
0|smartthi |     at Server._listen2 (/usr/local/lib/node_modules/pm2/node_modules/pmx/node_modules/vxx/node_modules/continuation-local-storage/node_modules/async-listener/index.js:51:23)
0|smartthi |     at listen (net.js:1298:10)
0|smartthi |     at Server.listen (net.js:1376:9)
0|smartthi |     at Function.listen (/usr/local/lib/node_modules/smartthings-mqtt-bridge/node_modules/express/lib/application.js:617:24)
0|smartthi |     at setupApp (/usr/local/lib/node_modules/smartthings-mqtt-bridge/server.js:383:13)
0|smartthi |     at /usr/local/lib/node_modules/smartthings-mqtt-bridge/node_modules/async/lib/async.js:718:13
0|smartthi |     at iterate (/usr/local/lib/node_modules/smartthings-mqtt-bridge/node_modules/async/lib/async.js:262:13)

When I watch my HASS logs, I can see something happening whenever smartthings-mqtt-bridge is trying to run:

Jan 23 21:27:10 raspberrypi hass[522]: INFO:hbmqtt.broker:Listener 'default': 8 connections acquired
Jan 23 21:27:10 raspberrypi hass[522]: INFO:hbmqtt.broker:Connection from <my raspberry pi IP>:41344 on listener 'default'
Jan 23 21:27:10 raspberrypi hass[522]: INFO:hbmqtt.broker:Listener 'default': 7 connections acquired
Jan 23 21:27:13 raspberrypi hass[522]: INFO:hbmqtt.broker:Listener 'default': 8 connections acquired
Jan 23 21:27:13 raspberrypi hass[522]: INFO:hbmqtt.broker:Connection from <my raspberry pi IP>:41348 on listener 'default'
Jan 23 21:27:13 raspberrypi hass[522]: INFO:hbmqtt.broker:Listener 'default': 7 connections acquired
Jan 23 21:27:16 raspberrypi hass[522]: INFO:hbmqtt.broker:Listener 'default': 8 connections acquired
Jan 23 21:27:16 raspberrypi hass[522]: INFO:hbmqtt.broker:Connection from <my raspberry pi IP>:41350 on listener 'default'
Jan 23 21:27:16 raspberrypi hass[522]: INFO:hbmqtt.broker:Listener 'default': 7 connections acquired

Does anyone have any idea what I might be doing wrong? I appreciate any help!

If you’re using the AIO installer on a Pi and using the built in Mosquitto MQTT broker, your HA config should look more like this:

mqtt:
  broker: 127.0.0.1
  port: 1883
  client_id: HomeAssistant
  username: username
  password: password
  keepalive: 60

Note the indentation, as the snippet you posted has the wrong amount.
Also, make sure you’ve setup your Mosquitto config at /etc/mosquitto/mosquitto.config properly.

mine looks like this:

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883
listener 9001
protocol websockets
user mosquitto
persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/
pid_file /var/run/mosquitto.pid

Note, the above configuration assumes you’ve properly setup a mosquitto password, using this command, where you replace “username” with whatever you want there.

sudo mosquitto_passwd -c /etc/mosquitto/pwfile username

The port 8080 that you have in your HA config is not needed for the ST bridge to communicate with your broker.

Your Bridge config file located at /home/pi/config.yml
should look something like this, where username and password match what you setup in the mosquitto config. Also note this is assuming your running the MQTT broker on the same machine as the bridge, otherwise, adjust your IP accordingly:

mqtt:
    # Specify your MQTT Broker's hostname or IP address here
    host: 127.0.0.1
    # Preface for the topics $PREFACE/$DEVICE_NAME/$PROPERTY
    preface: smartthings

    # Suffix for the state topics $PREFACE/$DEVICE_NAME/$PROPERTY/$STATE_SUFFIX
    # state_suffix: state
    # Suffix for the command topics $PREFACE/$DEVICE_NAME/$PROPERTY/$COMMAND_SUFFIX
    # command_suffix: cmd

    # Other optional settings from https://www.npmjs.com/package/mqtt#mqttclientstreambuilder-options
    username: username
    password: password

# Port number to listen on
port: 8080

Could it be your indentation? I don’t know if your config is setup the same as it is in this post, but this won’t work.

I tried using mosquitto but faced weird barriers and errors so I decided to go with the embedded broker instead.

As for my indentation, I think it just got messed up during the copy/paste to this thread, as I’m using what was already templated for me in the file.

Are you sure your MQTT broker is working properly? If you used the AIO installer mosquitto is also installed by default and is not too bad to get going. I had a permission problem with my pwfile but got it sorted. Here are my notes for setting it up from a fresh install:

Edit mosquitto config
sudo nano /etc/mosquitto/mosquitto.conf

Mosquitto Config

delete all text in the file and replace with this:

allow_anonymous false password_file /etc/mosquitto/pwfile listener 1883 listener 9001 protocol websockets user mosquitto persistence true persistence_file mosquitto.db persistence_location /var/lib/mosquitto/ pid_file /var/run/mosquitto.pid

Create password file (user pi), this destroys any existing file:
sudo mosquitto_passwd -c /etc/mosquitto/pwfile pi

If you want to add users to existing password file:
sudo mosquitto_passwd /etc/mosquitto/pwfile username

Test mosquitto config for errors:
mosquitto -c /etc/mosquitto/mosquitto.conf

I had an error where I didn’t have permission to access the pwfile. I had to killall mosquitto then delete the pwfile in samba, then create a new one for this to work. Once you pass the test above, reboot and test that the systemd instance is loading and running properly:

Test mosquitto systemctl process for errors
sudo systemctl status mosquitto.service

Test mosquitto- subscribe & publish: open two terminal windows and issue these commands in different windows:
mosquitto_sub -d -u pi -P raspberry -t dev/test
mosquitto_pub -d -u pi -P raspberry -t dev/test -m "Hello world"

To view all messages coming through the broker, subscribe to all messages:
mosquitto_sub -u pi -P raspberry -v -t '#'

To see even more details, stop the systemd instance and run just a verbose instance (note, this will not load the preference file)

mosquitto -v

1 Like