MQTT, Arduino & Openhab & RGB

Okay guys I need some help can’t figure out whats wrong. New at this and just lost as to what to do.

I am using 2 raspberry pi’s

Openhab 192.168.3.2
MQTT Broker Mosquitto 192.168.3.3

Arduinno Nano w/ ENC28J60 ip 192.168.3.20

Openhab bindings
Network
NTP
MQTT / MQTT Action / MQTT presence

Arduino Code (Is loaded to Nano)

#include <UIPEthernet.h>
#include <utility/logging.h>
#include <PubSubClient.h>

// Set the MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5E, 0x69 };

// Set fallback IP address if DHCP fails
IPAddress ip(192,168,3,20);

// Set the broker server IP
byte server[] = { 192,168,3,3 };

EthernetClient ethClient;

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

PubSubClient client(server, 1883, callback, ethClient);

int SoffitR;
int SoffitG;
int SoffitB;
int BLUE = 5;
int GREEN = 7;
int RED = 3;

void setup()
{
// Open serial communications
Serial.begin(9600);

// Start with a hard-coded address:
Serial.println("Assigning Static IP address:");
Ethernet.begin(mac, ip);

 Serial.print("My address:");

Serial.println(Ethernet.localIP());

// Connect to Broker, give it arduino as the name
if (client.connect(“arduino_LED2”)) {

// Publish a message to the status topic
client.publish("status/arduino_LED2","Arduino LED2 is now online");

// Listen for messages on the control topic
client.subscribe("control/arduino_LED2");

}
}

void loop()
{
client.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
// check for messages on subscribed topics
payload[length] = ‘\0’;
Serial.print("Topic: ");
Serial.println(String(topic));

// check topic to identify type of content
if(String(topic) == “control/arduino_LED2/livingroom/color”) {
// convert payload to String
String value = String((char*)payload);
//value.trim();
Serial.print (value);
//Serial.flush();
// split string at every “,” and store in proper variable
// convert final result to integer
SoffitR = value.substring(0,value.indexOf(’,’)).toInt();
SoffitG = value.substring(value.indexOf(’,’)+1,value.lastIndexOf(’,’)).toInt();
SoffitB = value.substring(value.lastIndexOf(’,’)+1).toInt();

// print obtained values for debugging
Serial.print("RED: ");
Serial.println(SoffitR);
//client.publish("status/arduino_LED2", SoffitR);

Serial.print("GREEN: ");
Serial.println(SoffitG);
//client.publish("status/arduino_LED2", SoffitG);

Serial.print("BLUE: ");
Serial.println(SoffitB);
//client.publish("status/arduino_LED2/soffit/color/blue", int SoffitB);
//Serial.flush();

analogWrite(GREEN, SoffitG);
analogWrite(RED, SoffitR);
analogWrite(BLUE, SoffitB);

while(Serial.available())
Serial.read();

}
}

OpenHap Items

#Test
String LivSoffitLight (Lights_LivingRoom) {mqtt=">[broker:control/arduino_LED2/livingroom/color:state:*:default]"}
Color LivSoffitLightColor “Living Room Accent Color”

Openhab Sitemap

sitemap Home label=“MC Smart Home Automation” {
Frame label=“Date” {
Text item=Date
}
Frame label=“Demo” {
Colorpicker item=LivSoffitLight label=“Living Room Accent” icon=“colorwheel”

    Switch item=Lights icon="light"
    Text item=LR_Temperature label="Livingroom [%.1f °C]"
    Group item=Heating
    Text item=LR_Multimedia_Summary label="Multimedia [%s]" icon="video" {
        Selection item=LR_TV_Channel mappings=[0="off", 1="DasErste", 2="BBC One", 3="Cartoon Network"]
        Slider item=LR_TV_Volume
    }
}

}

I need help guys I have a color wheel on the sitemap however doesn’t seem that the MQTT server is receiving the commands from Openhab.

Can anyone help me out? Am i missing something?

Ok, before even reading all of that, I am very confused where the actual code is.

Could you reformat the post and put all code in code fences.

Thanks.

Okay guys I need some help can’t figure out whats wrong. New at this and just lost as to what to do.

I am using 2 raspberry pi’s

Openhab 192.168.3.2
MQTT Broker Mosquitto 192.168.3.3

Arduinno Nano w/ ENC28J60 ip 192.168.3.20

Openhab bindings
Network
NTP
MQTT / MQTT Action / MQTT presence

Arduino Code (Is loaded to Nano)

#include <UIPEthernet.h>
#include <utility/logging.h>
#include <PubSubClient.h>

// Set the MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5E, 0x69 };

// Set fallback IP address if DHCP fails
IPAddress ip(192,168,3,20);

// Set the broker server IP
byte server[] = { 192,168,3,3 };

EthernetClient ethClient;

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

PubSubClient client(server, 1883, callback, ethClient);

int SoffitR;
int SoffitG;
int SoffitB;
int BLUE = 5;
int GREEN = 7;
int RED = 3;

void setup()
{
// Open serial communications
Serial.begin(9600);

// Start with a hard-coded address:
Serial.println("Assigning Static IP address:");
Ethernet.begin(mac, ip);

 Serial.print("My address:");
Serial.println(Ethernet.localIP());

// Connect to Broker, give it arduino as the name
if (client.connect(“arduino_LED2”)) {

// Publish a message to the status topic
client.publish("status/arduino_LED2","Arduino LED2 is now online");

// Listen for messages on the control topic
client.subscribe("control/arduino_LED2");
}
}

void loop()
{
client.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
// check for messages on subscribed topics
payload[length] = ‘\0’;
Serial.print("Topic: ");
Serial.println(String(topic));

// check topic to identify type of content
if(String(topic) == “control/arduino_LED2/livingroom/color”) {
// convert payload to String
String value = String((char*)payload);
//value.trim();
Serial.print (value);
//Serial.flush();
// split string at every “,” and store in proper variable
// convert final result to integer
SoffitR = value.substring(0,value.indexOf(’,’)).toInt();
SoffitG = value.substring(value.indexOf(’,’)+1,value.lastIndexOf(’,’)).toInt();
SoffitB = value.substring(value.lastIndexOf(’,’)+1).toInt();

// print obtained values for debugging
Serial.print("RED: ");
Serial.println(SoffitR);
//client.publish("status/arduino_LED2", SoffitR);

Serial.print("GREEN: ");
Serial.println(SoffitG);
//client.publish("status/arduino_LED2", SoffitG);

Serial.print("BLUE: ");
Serial.println(SoffitB);
//client.publish("status/arduino_LED2/soffit/color/blue", int SoffitB);
//Serial.flush();
analogWrite(GREEN, SoffitG);
analogWrite(RED, SoffitR);
analogWrite(BLUE, SoffitB);

while(Serial.available())
Serial.read();

}
}

OpenHap Items

#Test
String LivSoffitLight (Lights_LivingRoom) {mqtt=">[broker:control/arduino_LED2/livingroom/color:state:*:default]"}
Color LivSoffitLightColor “Living Room Accent Color”

Openhab Sitemap

sitemap Home label=“MC Smart Home Automation” {
Frame label=“Date” {
Text item=Date
}
Frame label=“Demo” {
Colorpicker item=LivSoffitLight label=“Living Room Accent” icon=“colorwheel”

    Switch item=Lights icon="light"
    Text item=LR_Temperature label="Livingroom [%.1f °C]"
    Group item=Heating
    Text item=LR_Multimedia_Summary label="Multimedia [%s]" icon="video" {
        Selection item=LR_TV_Channel mappings=[0="off", 1="DasErste", 2="BBC One", 3="Cartoon Network"]
        Slider item=LR_TV_Volume
    }
}
}

I need help guys I have a color wheel on the sitemap however doesn’t seem that the MQTT server is receiving the commands from Openhab.

Can anyone help me out? Am i missing something?

sorry about that still new to the forum.

I presume you have installed the MQTT binding and configured your mqtt broker?
what does your mqtt.cfg look like?
In case you have not installed MQTT spy, I suggest you do so to see if any MQTT commands are actually sent

I have the MQTT binding installed on openhab and yes I have the broker configured. I can run tests via the SSH windows on the MQTT broker. A publish and subscribe. in separate windows.

I think your subscribe is not correct. add a #
client.subscribe("control/arduino_LED2/#");

the condition if(String(topic) == “control/arduino_LED2/livingroom/color”) is never met as you are not subscribed to it

Ok, it seems like you have copied the broker channel config directly from the example, since it is called broker. You need to configure mqtt.conf in order to set up your broker connection.

When using MQTT it is always helpful to have a stand-alone client to check if messages are actually being received at the broker, and updated at the client.

I use MQTT.fx but other clients are available. MQTT lens (chrome extension) was the favourite until the GUI went all supersized.

MQTT config file

#
# 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
mosquitto.url=tcp://192.168.3.3:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#mosquitto.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.
#mosquitto.user=pi

# Optional. Password to authenticate with the broker.
#mosquitto.pwd=*****

# 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=<retain>

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

# 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>

indeed, your items file has the wrong name for the broker it seems
Make yr items file like this:
{mqtt=">[mosquitto:control/arduino_LED2/livingroom/color:state:*:default]"}

AND you should add the # in your subscribe in yr ino file

You need to remove the # marks from lines you wish to use.

#s are comments

Secondly, that broker is called `mosquitto’, and not ‘broker’.

removed several of the comments on the MQTT.config and corrected the arduino code to add the # and corrected the broker name.

Still not seeing a response or a print from the connection to the broker from Arduino.

Ok, let’s walk before we run.

Can you use a client and connect to the broker?

Can you publish and subscribe to a topic?

Can OH publish and subscribe?

Solve this before you get into any arduino stuff.

Correction I get a status update from the arduino that it is online. So the arduino is connected to the broker. Still seems to be a disconnect between Openhab and the broker.

MQTT Broker Listening to all code

mosquitto_sub -h localhost -t \# -v

How can I send tests to see if Openhab can publish / subscribe to the broker?

On the SSH for the Raspberry running the broker in separate windows I can publish to topic and get response in separate window subscribed to topic.

Make a dummy switch, like this:

Switch TestMQTT "MQTT TEST" <switch> ["Switchable"] {mqtt=">[mosquitto:topic:command:ON:1],>[mosquitto:topic:command:OFF:0],<[mosquitto:topic:state:ON:1],<[mosquitto:topic:state:OFF:0]"}

And in your sitemap:

Frame label="Test MQTT" {
Switch item=TestMQTT label="On / Off" icon="switch"
}

Use a client to test if subscribes.

It appears that Openhab isn’t connected to Broker. No commands from openhab on the broker

the only binding showing in Paper UI is the network binding should I see the MQTT bindings?

If I go to add bindings I see MQTT installed?

Have you set up the openhab mqtt config file (mqtt.cfg) correctly in the services folder?

#
# 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
mosquitto.url=tcp://192.168.3.3:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
mosquitto.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.
mosquitto.allowLongerClientIds=false

# Optional. User id to authenticate with the broker.
mosquitto.user=pi

# Optional. Password to authenticate with the broker.
mosquitto.pwd=***** (Blocked for security)

# 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.
mosquitto.qos=0

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

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

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

It’s wrong (reading the actual comments in the file would have told you so).

Try this:

mosquitto.url=tcp://localhost:1883
mosquitto.user=openhab
mosquitto.pwd=*****
mosquitto.retain=true
mosquitto.async=false

OH gets installed with the user openhab so you have to use that.

My appologies, I call my broker openhab, only the user has to be openhab. *edited