Using MQTT Binding on OpenHAB 2

Source:
http://www.eclipse.org/paho/files/javadoc/org/eclipse/paho/client/mqttv3/MqttConnectOptions.html
http://www.eclipse.org/paho/files/javadoc/index.html?overview-summary.html

Have you enabled the MQTT adapter on RabbitMQ?

Ps: It’s better to open up a new thread for this. Posting on a thread marked as solved will not get you too much attention :slight_smile:

Thanks, after RPi restart it works

I couldn’t connect to the MQTT over openHab please help

Home .items part is as follows:

//Jacksroom

Switch LEDTB “LED Tube” {mqtt=">[broker:Jacks-SmartHome/utilities/Jacksroom:command:ON:Z1ON],>[broker:Jacks-SmartHome/utilities/Jacksroom:command:OFF:Z1OFF]"}

Switch FAN “Room Fan” {mqtt=">[broker:Jacks-SmartHome/utilities/Jacksroom:command:ON:Z2ON],>[broker:Jacks-SmartHome/utilities/Jacksroom:command:OFF:Z2OFF]"}

Switch EXFAN “Exhaust Fan” {mqtt=">[broker:Jacks-SmartHome/utilities/Jacksroom:command:ON:Z3ON],>[broker:Jacks-SmartHome/utilities/Jacksroom:command:OFF:Z3OFF]"}

Switch PSRBSTR “Pressure Booster” {mqtt=">[broker:Jacks-SmartHome/utilities/Jacksroom:command:ON:Z4ON],>[broker:Jacks-SmartHome/utilities/Jacksroom:command:OFF:Z4OFF]"}

MQTT cofig file is as follows:

#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
broker.url=tcp://192.168.137.13:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
broker.clientId=openhab

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
#<broker>.user=<user>

# Optional. Password to authenticate with the broker.
#<broker>.pwd=<password>

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#<broker>.qos=<qos>

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
broker.retain=true

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
broker.async=false

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

Sitemap files is as follows:
sitemap home label=“Jacks-SmartHouse”
{
Frame label=“Jacks Room”
{
Switch item=LEDTB
Switch item=FAN
Switch item=EXFAN
Switch item=PSRBSTR
}
}

Please tell how to check the communication using MQQTFx

My ESP NodeMCU Firmware as Follows:

#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

//Only edit the settings in this section

/* WIFI Settings */
// Name of wifi network
const char* ssid = "Jacks_BSNL-3G";

// Password to wifi network
const char* password = "1m2f0m8m"; 

/* Web Updater Settings */
// Host Name of Device
const char* host = "Jacksroom";

// Path to access firmware update page (Not Neccessary to change)
const char* update_path = "/firmware";

// Username to access the web update page
const char* update_username = "admin1";

// Password to access the web update page
const char* update_password = "password1";

/* MQTT Settings */
// Topic which listens for commands
char* subscribeTopic = "Jacks-SmartHome/utilities/Jacksroom"; 

//MQTT Server IP Address
const char* server = "192.168.137.13";

//Unique device ID 
const char* mqttDeviceID = "Jacks-SmarthomeDevice1"; 

int channel1 = D0;
int channel2 = D1;
int channel3 = D2;
int channel4 = D3;

//webserver 
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

//MQTT
WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

//Connect to WiFI and MQTT
void connect();

//Setup pins, wifi, webserver and MQTT

void setup() 
{
  // set pin modes
  pinMode(channel1, OUTPUT);
  digitalWrite(channel1, LOW);

  pinMode(channel2, OUTPUT);
  digitalWrite(channel1, LOW);

  pinMode(channel3, OUTPUT);
  digitalWrite(channel3, LOW);

  pinMode(channel4, OUTPUT);
  digitalWrite(channel4, LOW);

  WiFi.mode(WIFI_STA);
  
  WiFi.begin(ssid, password);
  client.begin(server, net);

  connect();

  MDNS.begin(host);

  httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  httpServer.begin();

  MDNS.addService("http", "tcp", 80);
}

//Connect to wifi and MQTT
void connect() 
{
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
  }

  while (!client.connect(mqttDeviceID)) 
  {
    delay(1000);
  }

  client.subscribe(subscribeTopic);
}

void loop() 
{
  // MQTT Loop
  client.loop();
  delay(10);

  // Make sure device is connected
  if(!client.connected()) 
  {
    connect();
  }

  httpServer.handleClient();

}

// Change the state of a relay based on the MQTT Message
void messageReceived(String topic, String payload, char * bytes, unsigned int length) 
{
    String msgString = payload;

  if (msgString == "Z1ON")
  {
    digitalWrite(channel1, HIGH);
    delay(250);
  }
  else if (msgString == "Z1OFF")
  {
    digitalWrite(channel1, LOW);
    delay(250);
  }
  else if (msgString == "Z2ON")
  {
    digitalWrite(channel2, HIGH);
    delay(250);
  }
  else if (msgString == "Z2OFF")
  {
    digitalWrite(channel2, LOW);
    delay(250);
  }
  else if (msgString == "Z3ON")
  {
    digitalWrite(channel3, HIGH);
    delay(250);
  }
  else if (msgString == "Z3OFF")
  {
    digitalWrite(channel3, LOW);
    delay(250);
  }
  else if (msgString == "Z4ON")
  {
    digitalWrite(channel4, HIGH);
    delay(250);
  }
  else if (msgString == "Z4OFF")
  {
    digitalWrite(channel4, LOW);
    delay(250);
  }
}

Would you please edit your message and put the .cfg file in a code block :slight_smile:

1 Like

I’m sorry for that I’m totally new to this… :smiley:

What is the name of your broker? My mqtt.cfg looks like this: broker=mosquitto


 
mqtt.cfg

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhabian
mosquitto.qos=1
mosquitto.retain=true
mosquitto.async=false

Replace ‘broker’ with the name of your broker. Unless of course you named it “broker”.

2 Likes

broker name is same. Please check my esp firmware as its gives High Output through pins D0-D3 after uploading the firmware. I’m using nodemcu.

Hi
If you try to send the command on mosquitto_pub what happen on your nodemcu?

#MeToo,(lol) I am having some difficulties, getting MQTT up running.
I have a working broker, and MQTT bindings installed.
When I look at the payload in MQTTfx, I can see values are being published, but my Sitemap is not being populated.
Could anyone find the time to help me ?

My item(publish)

Number Netatmo_Indoor_Temperature    "Temperature [%.1f °C]"     <temperature>	{ channel = "netatmo:NAMain:d7a4c49c:70ee5019aa78:Temperature", mqtt=">[brokerOH2_2:netatmo_indoor/temp:state:*:default]"}

My item(subscribe)

Number Netatmo_Indoor_Temperature         	"Temperature [%.1f °C]"    	<temperature>	{mqtt="<[brokerOH2_2:netatmo_indoor/temp:state:*:default]"}

My sitemap:

Text		item=Netatmo_Indoor_Temperature 	icon="temperature"	label="Temperatur Alrum"		valuecolor=[>25="red",>20="green",>15="blue",<=15="purple"]

The installation is between 2 instances of OpenHAB2.

Try reading the first post of this thread and following the code examples and also the setup steps to get the MQTT binding working.

Can you please edit your response to reflect what is mentioned here?

Thank you very much - I remvoed ‘mqtt:’ and finally made MQTT binding work after hours.

plaes when i try to configure my MQTT with this command /etc/openhab2/services/mqtt.cfg it give acces denied

please help

I’m getting the error:
Connecting to repo.mosquitto.org (repo.mosquitto.org)|85.119.83.194|:80… connected.
HTTP request sent, awaiting response… 404 Not Found
2018-09-04 13:14:13 ERROR 404: Not Found.