Saturday, May 16, 2015

Installing Mosquitto for Use with ESP8266

Introduction

I am working on a home automation project which required I installed Mosquitto, an open source MQTT broker, on my Raspberry Pi. Installation went smoothly, so a few minutes after first hearing about Mosquitto it was ready to be used on my RPi.

Next, I tested it by subscribing to topics and publishing messages from different SSH sessions, all without a problem.

Problem

When I configured my ESP8266 to communicate with this Mosquitto broker, it could not connect to it. Interestingly, my ESP8266 could successfully communicate a test broker on the internet at test.mosquitto.org. This was really weird, as usually stuff works locally and migrating to the internet things tend to break, not the other way around.

Looking through the logs I found the following errors:
May 12 12:01:47 dev mosquitto[2316]: New connection from 192.168.1.141.
May 12 12:01:47 dev mosquitto[2316]: Invalid protocol "MQTT" in CONNECT from 192.168.1.141.

Solution

A quick research on the internet revealed that I am not the only one with this problem, and that the root of the problem is that the version of Mosquitto that's in the debian wheezy repositories is not compatible with the version the ESP8266 uses.

The following steps need to be taken to correct the problem (taken from http://mosquitto.org/2013/01/mosquitto-debian-repository ):

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key

cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo apt-get update
To display what version of Mosquitto is installed you can use the following:
sudo apt-cache search mosquitto
As soon as I restarted mosquitto the ESP8266 could connect to it with no problem.

Now the logs contain much friendlier messages:
May 12 19:05:37 dev mosquitto[3315]: New connection from 192.168.1.141 on port 1883.
May 12 19:05:37 dev mosquitto[3315]: New client connected from 192.168.1.141 as viktak (c1, k60).


5 comments :

  1. How were you able to see the logs? I am too having connection issues using ESP8266 with a local RPi broker but works if I use an external IP into my RPi.

    Using Mosquitto broker MQTT v3.1.1

    ReplyDelete
    Replies
    1. The log file is located at the location you specify in the mosquitto.conf file.

      The system logs can be viewed by subscribing to the $SYS/# topic.

      Delete
    2. I did subscribe to $SYS/# however, there was a lot of info that I did not need. I sifted through it but was unable to find anything about a client connecting. The closest I could see was number of clients connected/active. I was not able to get more granular information like you did in terms of "New connection from 192.168.1.141 on port 1883."

      Delete
    3. Looks like I figured out the logging part, I forgot to set the logging to type all. Running mosquitto -v was able to see all the connections... now to get home and debug it.

      Thanks!

      Delete