element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Bluetooth Unleashed Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Bluetooth Unleashed Design Challenge
  • More
  • Cancel
Bluetooth Unleashed Design Challenge
Blog BT_Sentry : Connecting to MQTT Broker
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mcb1
  • Date Created: 16 Jul 2018 4:05 AM Date Created
  • Views 773 views
  • Likes 7 likes
  • Comments 4 comments
  • ipv6
  • mqtt
  • openhab
  • raspberry pi 3 b+
  • esp8266
  • openhab2
  • mqtt_broker
  • configuration
  • openhab_mqtt
Related
Recommended

BT_Sentry : Connecting to MQTT Broker

mcb1
mcb1
16 Jul 2018

This is the 12th of my Blogs for the Bluetooth Unleashed Design Challenge

 

 

The other posts are here :-

BT_Sentry Introduction
BT_Sentry : Zero Emission Detection
BT_Sentry : Data Transmission
BT_Sentry : Sentry Power
BT_Sentry : Raspberry Pi
BT_Sentry : Bluetooth Options
BT_Sentry : Software
BT_Sentry : Bluetooth Sniffing 
BT_Sentry : OpenHAB
BT_Sentry : Notifications
BT_Sentry : MQTT

 

Concept

The idea is to detect the bluetooth transmitted from the vehicle and signal other Home Automation functions.

If the vehicle is known then it can open the garage door, and inform the home owner that xx is home.

 

Hardware

The detection point needs to be at the start of the driveway, and because there is no power source, this will need to be low power with solar charging.

The PSOC range seems a very good fit, but because of the timeline and my need to upskill, the inital design will be Arduino based and some form of RF transmitter/transceiver.

 

Adding a vehicle detection loop or beam is necessary to ensure those vehicles without bluetooth will also trigger the system.

 

 

 

 

In my quest to sort out the glue that joins all this together, I thought I'd concentrate on the indoor notification portion.

This is an ESP8266 with a neopixel display that runs around pretending to be some sort of clock, while it's waiting on a message.

 

You can see it here BT_Sentry : Notifications

image

 

 

 

 

Broker

The default settings for Mosquitto allow you to test without specifying the host name.

Within the Raspberry Pi I was able to prove that Mosquitto was working by Subscribing an Publishing and it played nice.

 

Once I tried to connect with the broker using an ESP8266, it all went downhill.

 

No matter what I tried, the connection was refused.

RPi IP address, default hostname, and some other weird and wonderful creations were no help.

 

I even changed the WiFi the ESP8266 was connecting to, thinking it was some routing issue.

Still no luck.

 

 

In desperation I tried one of the examples, and it connected perfectly to some external broker, so that proved it wasn't my network.

Change the broker in the example back to the Mosquitto and same issue again ... no connection to the broker.

 

 

 

IPV6

During this problem solving I discovered that for whatever reason there was an IVP6 connection listening to port 1883.

I couldn't recall setting this up, or even allowing it to occur, and worse it was still there when I stopped Mosquitto.

 

 

It seems that by default IPV6 in a Raspberry Pi is on.

While this is okay, what was a bit tricky was turning it off.

 

There were a few suggestions and eventually I found this discussion.

https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=138899&start=25

 

 

It involves changing the /etc/modprobe.d/ipv6.conf file to read :-

 

alias net-pf-10 off
alias ipv6 off
options ipv6 disable_ipv6=1
blacklist ipv6

 

 

A reboot and check of Netstat shows the TCP6 connections are gone.

image

 

There is something listening to 1883 but it still refused to connect.

I even went back and tried all the weird and wonderful mqtt_server settings I had tried before, but despite all my efforts, the connection was still being refused.

 

I tried stopping mosquitto with

sudo service mosquitto stop

 

followed by

sudo service mosquitto status

 

which showed that mosquitto was stopped.

 

During all this experimenting, the configuration file I showed on the other post was causing errors

(I've edited this to show the proper settings)

 

Restarting it from the command line gave this problem.

image

 

For whatever reason something was opening a port on 1883.

 

Typing netstat --help gave me the idea to use -e and get extended information.

image

There's my problem, OpenHAB is starting a MQTT listener and therefore I'm trying to connect to it.

 

 

Stop OpenHAB and check to see it has stopped.

sudo service openhab2 stop

 

and then check the status

sudo service openhab2 status

image

 

Note that OpenHAB does take a few minutes to stop, and in the meantime the prompt disappears into the ether. image

 

recheck netstat.

image

Hurray, now we're getting somewhere.

 

 

Now restarting mosquitto works.

sudo service mosquitto restart

 

and the ESP8266 is able to connect

image

 

and when I publish something

image

image

At last some success.

 

 

/* Sketch to run a ESP3266 driven by time.
    This displays on a 12 x neopixel ring
    The colors of the 'hands' are:
    RED = Hours
    GREEN = Minutes
    BLUE = Seconds
    
    credit to https://www.hackster.io/thearduinoguy/esp8266-neopixel-ring-clock-a9cc74   
    TimeClient.h can be obtained from https://github.com/squix78/esp8266-weather-station
    Extract the TimeClient.h and TimeClient.cpp files and add them into the sketch folder (hence the "TimeClient.h")

    Mark Beckett
 */


#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
//#include "TimeClient.h"
#include <PubSubClient.h>

#define PIN D5
int ledsInString = 12;
long lastUpdate = millis();
long lastSecond = millis();

String hours, minutes, seconds;
int currentSecond, currentMinute, currentHour;


const char ssid[] = "Your WiFi";           //  your network SSID (name)
const char pass[] = "Your WiFi password";       // your network password
const char* mqttServer = "Your RPi IP address";
const int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";
const char* mqtt_topic = "OpenHAB_mqtt";       // topic is OpenHAB_mqtt

const float UTC_OFFSET = 12;
//TimeClient timeClient(UTC_OFFSET);

Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledsInString, PIN);
WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();

  strip.begin();
  strip.setBrightness(128);
  strip.show();

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  
  //timeClient.updateTime();
 // updateTime() ;
  //lastUpdate = millis();
  //lastSecond = millis();

  delay(2000); // give system time

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  
  while (!client.connected()) 
  {
    Serial.println("Connecting to MQTT...");
 
    if (client.connect("ESP8266Client"))
    {
      Serial.println("connected");  
    } else
    {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
  }
  client.subscribe(mqtt_topic);
}

void loop()
{
//  if ((millis() - lastUpdate) > 1800000) updateTime();
//
//  if ((millis() - lastSecond) > 1000)
//  {
//
//    strip.setPixelColor((currentHour * 1), 0, 0, 0);
//    strip.setPixelColor((currentMinute / 5), 0, 0, 0);
//    strip.setPixelColor((currentSecond / 5), 0, 0, 0);
//
//    strip.show();
//    lastSecond = millis();
//    currentSecond++;
//    if (currentSecond > 59)
//    { currentSecond = 0;
//      currentMinute++;
//      if (currentMinute > 59) {
//        currentMinute = 1;
//        currentHour++;
//        if (currentHour > 12) currentHour = 0;
//      }
//    }
//    String currentTime = String(currentHour) + ':' + String(currentMinute) + ':' + String(currentSecond);
//    Serial.println(currentTime);
//
//    strip.setPixelColor((currentHour * 1), 255, 0, 0);
//    strip.setPixelColor((currentMinute / 5), 0, 255, 0);
//    strip.setPixelColor((currentSecond / 5), 0, 0, 255);
//    strip.show();
//  }

    client.loop();
}

//void updateTime() 
//{
//  hours = timeClient.getHours();
//  minutes = timeClient.getMinutes();
//  seconds = timeClient.getSeconds();
//  currentHour = hours.toInt();
//  if (currentHour >= 12) currentHour = currentHour - 12;
//  currentMinute = minutes.toInt();
//  currentSecond = seconds.toInt();
//  lastUpdate = millis();
//}


void callback(char* topic, byte* payload, unsigned int length) 
{
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
 
  Serial.println();
  Serial.println("-----------------------");
 
}

 

This is my ESP8266 sketch, and I've commented out the time bits, to test the mqtt part.

 

 

 

 

 

Time

This discovery process has taken the best part of two days.

While it was frustrating at times, it has shown that having good examples, rather than detailed explanations is a better solution.

 

e.g The setting up of a configuration file https://mosquitto.org/man/mosquitto-conf-5.html states

image

However is this xxx.xxx.xxx.xxx or "xxx.xxx.xxx.xxx" or some other variation.

is there an equals sign, colon or something in between required?

I even saw a few trying to connect to tcp://xxx.xxx.xxx.xxx:1883

 

So including the actual syntax for each command does make it easier for anyone heading down this road.

 

I suspect their documentation is a WIP as the links don't seem to work (however I was using Firefox)

 

 

Mark

  • Sign in to reply

Top Comments

  • DAB
    DAB over 7 years ago +2
    Nice update. If it was easy, I could have done it. DAB
  • jomoenginer
    jomoenginer over 7 years ago +1
    The way I have done this is to use port 1883 as a local port and something like 8883 as the MQTT Broker port and then 8083 for a websocket port if needed. So, in my mosquitto.conf, I have: listener 1883…
  • genebren
    genebren over 7 years ago +1
    Nice update on your design challenge project. Wow, there is so much going on in some of these products that it takes a really skilled and vigilant detective to track down some of these issues. Keep up…
  • aspork42
    aspork42 over 7 years ago

    Good update! I ran into similar issues setting up a complete brand new system despite having done it many times before...

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 7 years ago

    Nice update.

     

    If it was easy, I could have done it. image

     

    DAB

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 7 years ago

    Nice update on your design challenge project.  Wow, there is so much going on in some of these products that it takes a really skilled and vigilant detective to track down some of these issues.

    Keep up the good work!

    Gene

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jomoenginer
    jomoenginer over 7 years ago

    The way I have done this is to use port 1883 as a local port and something like 8883 as the MQTT Broker port and then 8083 for a websocket port if needed.

     

    So, in my mosquitto.conf, I have:

    listener 1883 localhost
    listener 8883 192.168.2.202

     

    To subscribe locally, I would run:

    mosquitto_sub -h 192.168.2.202 -p 1883 -t 'topicname/stuff'

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube