MQTT Topic Variations

Heya,

I've been back to working on my HomeAssistant integration for pulling CAD data, with a focus on moving to MQTT and away from HTTP polling.

It seems there are a few potential MQTT topics where data can be published to. My CAD publishes to SMART/HILD/, but I received a report from someone with a CAD publishing to SMART/DCAD/, and it seems others have CADs publishing to SMART/IGLOO/.

The MQTT server doesn't seem to mind subscribing to multiple potential topics (so I'm currently subscribing to SMART/HILD/ and SMART/DCAD/), but I was wondering if there was a REST API endpoint or similar that could be used to discover the correct topic- it doesn't seem to be exposed through the /resource or /device endpoints.

I released v0.1.0-beta last night which has rough support for smart meter autodiscovery & MQTT streaming, but it's likely this won't work at all until I've had a chance to add better error handling.

Really appreciate any suggestions/feedback on this.

Thanks :)

Comments

  • If I subscribe to

    1. "SMART/+/CAFEDEADBEEF"

    (obviously with my valid Glow ID) it doesn't matter whether it's HILD or DCAD or IGLOO.

  • Except that it wont auth - or at least it didn't when I tried.

  • As for knowing which topic - no sadly there is no programmatic way of knowing .

    However just ask - we will tell you and it won't change.

    It is an historical accident which can't change without affecting other users.

  • @clivee

    It worked perfectly for me just using `mosquitto_sub` so I've built that into all of my API code. Here's a simple example.

    1. #include <stdio.h>
    2. #include <stdint.h>
    3. #include <string.h>
    4. #include <unistd.h>
    5. #include <mosquitto.h>
    6.  
    7. const char* usr = "a@b.com";
    8. const char* pwd = "pwd";
    9. const char* dev = "DEADBEEFCAFE";
    10.  
    11. void concb(struct mosquitto *mosq, void *obj, int rc)
    12. {
    13.   if (rc) printf("Connect rc=%d\n", rc);
    14. }
    15. void msgcb(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
    16. {
    17.   printf("Topic: %s\n Payload:", msg->topic);
    18.   printf("%.*s\n", msg->payloadlen, (char*) msg->payload);
    19. }
    20. int main(int argc, char* argv[])
    21. {
    22.   struct mosquitto *mosq;
    23.   int rc = 0;
    24.   char topic[23];
    25.   mosquitto_lib_init();
    26.   mosq = mosquitto_new(NULL, true, 0);
    27.   if(mosq)
    28.   {
    29.     mosquitto_connect_callback_set(mosq, concb);
    30.     mosquitto_message_callback_set(mosq, msgcb);
    31.  
    32.     mosquitto_username_pw_set(mosq, usr, pwd);
    33.     mosquitto_tls_set(mosq, NULL,  "/etc/ssl/certs",  NULL, NULL, NULL);
    34.     mosquitto_tls_opts_set(mosq, 1, NULL, NULL);
    35.  
    36.     rc = mosquitto_connect(mosq, "glowmqtt.energyhive.com", 8883, 30);
    37.     if (rc) printf("connect, rc=%d\n", rc);
    38.  
    39.     sprintf(topic, "SMART/+/%s", dev);
    40.     mosquitto_subscribe(mosq, NULL, topic, 0);
    41.  
    42.     for(;;)
    43.     {
    44.       rc = mosquitto_loop(mosq, -1, 1);
    45.       if (rc)
    46.       {
    47.         printf("Conn error\n");
    48.         sleep(10);
    49.         mosquitto_reconnect(mosq);
    50.       }
    51.     }
    52.     mosquitto_destroy(mosq);
    53.   }
    54.   mosquitto_lib_cleanup();
    55. }
    56.  
    57.  


Sign In or Register to comment.