Paho-mqtt on raspberry as client for sending data to openhab

hi @ all
I started to get in touch with mqtt. Using shellies via mqtt is no problem. But now I want to send measurement date from another raspberry PI to the build in broker.
I installed paho-mqtt on the client raspberry. The following small code is also provided with paho-mqtt.
#include “stdio.h”
#include “stdlib.h”
#include “string.h”
#include “MQTTClient.h”

#define ADDRESS “xxx.xxx.x.xxx:1883”
#define CLIENTID “ExampleClientPub”
#define TOPIC “MQTTExamples”
#define PAYLOAD “Hello World!”
#define QOS 1
#define TIMEOUT 10000L

int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;

MQTTClient_create(&client, ADDRESS, CLIENTID,
    MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;

if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
    printf("Failed to connect, return code %d\n", rc);
    exit(-1);
}
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s\n"
        "on topic %s for client with ClientID: %s\n",
        (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;

}

compiling is succsessfull and I got the following output:
Waiting for up to 10 seconds for publication of Hello World!
on topic MQTTExamples for client with ClientID: ExampleClientPub
Message with delivery token 1 delivered

so think the host adress is ok. I then added a generic mqtt thing with “ExampleClientPub” bas ID. After that I added a channel -> text value an channel ID “MQTTExamples”
The thing is online. but after executing the test program again no text is displayed when monitoring the thing in the control section of the paper UI.

I would be very grateful for some help from experts.
By the way I’m using openhab 2.5.

regards

What broker are you using? Mosquitto?
Can you show us the exact MQTT topic that you are expecting to publish to?
Can you show us your openHAB Thing and openHAB Item configuration?

I’m using the mqtt broker provided by the mqtt binding by David Graef.

The topic is: mqtt:topic:ExampleClientPub:MQTTExamples
I configured via paper UI and I didn’t create an Item yet. I thought i just need an Item for the sitemap

Unfortunately that’s still not enough information to figure out what’s going on - can you answer the last two questions?

Can you show us the exact MQTT topic that you are expecting to publish to?
Can you show us your openHAB Thing and openHAB Item configuration?

For the topic question, forget openHAB: I’m after the MQTT topic that you are publishing to. Have you verified that you are actually publishing a message to the broker on the expected topic? You could use mqtt.fx or MQTT Explorer to verify.

A word of caution: the broker that is included with openHAB (Moquette) is un-maintained, and current advice, especially if just starting out, is to remove it and install the 3rd party MQTT broker Mosquitto (outside of openHAB).

1 Like

Many Thanks for your advise. I installed mqtt.fx on my laptop ans set up de connection profile as following:
Profile Name: local mosquitto
Profile Type: MQTT Broker
Broker Adress: IP of my raspberry where openahab is running and the build in mqtt broker
Broker Port: 1883
Client ID: ExampleClientPub (like in the c-code)

everything else is unchanged.
If I connect and Subscribe and then run the test programm of my second raspberry which should provide measuring datas later on the broker connection in mqtt.fx is lost.

What am I doing wrong :thinking: ?

If possible in any way I would like to keep the mqtt-binding because I have some shellys running with it. Thank you very much in advance

So would you now have two things/devices connecting to the broker with the same Client ID? That would explain why you’re getting disconnected as you can’t have two things connecting with the same Client ID.

Ah ok. I’m beginning to understand how it works. So i generated a random ID. I also added the topic “MQTTExamples” in the SUBSRCIBE menue … run the code and voila “Hello World” appears in the bottom rigth window. Great! :grinning:

But shouldn’t be there some informations in the BROKER STATUS menue?

Unfortunately that doesn’t help me to understand why the openhab build-in mqtt broker recieves nothing

To be honest, I’m still not certain what your setup actually is. You still haven’t shown us your configurations.

I think you have:

  • Raspberry Pi #1 with:

    • OpenHAB 2.5 (which minor version? Latest is 2.5.10)
      • MQTT Binding (presumably V2)
      • MQTT Broker (can you show us the configuration for this?)
  • Shelly devices

    • Communicate fine over MQTT to openHAB
      • So presumably the binding and the broker are correctly setup.
  • Raspberry Pi #2 with:

    • Custom code using paho-mqtt as shown in your first post.

Some notes:

CLIENTID

#define CLIENTID "ExampleClientPub"

The CLIENTID is only used between your custom code on the Raspberry Pi #2, and your MQTT broker. It allows your MQTT broker to know how many individual MQTT clients are connected to it. You do not need to use the CLIENTID information at all when configuring your openHAB Things and Channels.

TOPIC

#define TOPIC "MQTTExamples"

Your topic, whilst not wrong, is slightly unusual in how short it is. Normally, you would also add a unique device identifier, perhaps even the CLIENTID into the topic. You may have seen this with your Shellies. For example, you may want to use:

#define TOPIC "ExampleClientPub/MQTTExamples"

Again, what you have isn’t wrong, but may cause confusion later on down the line.

openHAB

The only things that openHAB cares about when it comes to MQTT are:

  • Connection to the MQTT broker - with your Shellies you’ve proven that works.
  • The MQTT topic (your TOPIC in your code)

You do not use the CLIENTID in your openHAB configuration at all.

Again, you’ve not shown us any of your configurations, so I’m going to assume you’re using PaperUI to setup your Things and Channels.

Firstly, ensure Simple Mode is off in Configuration -> System:

image

Add a Generic Thing by (for example)

  • Choosing Configuration -> Things in the sidebar, or go to your Inbox
  • Click the blue + circle
  • Click MQTT Binding
  • Click MANUALLY ADD THING
  • Click Generic MQTT Thing

Give your Thing a Name, and select your bridge to your broker in Bridge Selection. Leave everything else as default.
image

With your Thing setup, now add a Channel. Choose your Thing from the list that appears after clicking Configuration -> Things in the sidebar. Click the blue + next to Channels.

Choose:

  • Channel type -> Text Value
  • Channel id -> Unique name for your channel, used internally by openHAB. No spaces!
  • Label -> Human friendly channel name. You can use spaces!
  • MQTT State Topic -> MQTTExamples

image

Click Save, and your channel will be added:

image

In order to see anything in the PaperUI Control section, you must link your Thing Channel to an Item.

Click the blue circle next to your channel, and from the Please select the item to link drop-down, choose + Create new item.

image

For now, you can leave all the defaults as they are, and click Link. You should find that the blue circle next to your Channel has a white filled-in dot in the centre, showing that it is linked to an Item.

Now, go to Control -> Other in PaperUI, and find your Thing. It should look like below:
image

Then, start your custom code on Raspberry Pi #2, and you should eventually see:

image

If I understand your second post right, you still have no item created for the thing device? This is important for most things to work. The thing is only the device itself. All messages are send to the thing channels which will be linked to items. So the mqtt message will only be visible if you link an item to the thing.

@hafniumzinc, many many thanks for this extremly detailed explaination. It’s working! Fantastic! Sorry for my incomplet description of my setup and configuration. But your assumation hit the mark. My problem was the understanding of the chanel ID and the state topic. Again, I’m happy with it. Great! Now I can go on and test with the measured values.

@buschif4 also thanks to you for your reply. I allready knew that items are needed for the sitemap. But didn’t knew that I need it for control via paper ui. Now I see clearer.

:nerd_face: :+1:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.