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);
}
}