Last active
April 10, 2024 20:06
-
-
Save ozlb/de02e1e8c9d426375d43ff199fa1303e to your computer and use it in GitHub Desktop.
mosquitto HiveMQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <signal.h> | |
| #include <time.h> | |
| #include <ctype.h> | |
| #include "mosquitto.h" | |
| struct mosquitto *mosq; | |
| #define MQTT_HIVEMQ | |
| #ifdef MQTT_HIVEMQ | |
| #define MQTT_SERVER "isThisSomethingYouCanRemberABCDE.s2.eu.hivemq.cloud" | |
| #define MQTT_SERVER_PORT 8883 | |
| #define MQTT_USERNAME "hivemq-xxx" | |
| #define MQTT_PASSWORD "theBestPassword" | |
| #else | |
| #define MQTT_SERVER "localhost" | |
| #define MQTT_SERVER_PORT 1883 | |
| #endif | |
| #define MQTT_TOPIC_XXX "aaa/bbb/ccc/ddd/#" | |
| /* Callback called when the client receives a CONNACK message from the broker. */ | |
| void on_connect(struct mosquitto *mosq, void *obj, int reason_code) | |
| { | |
| int rc; | |
| /* Print out the connection result. mosquitto_connack_string() produces an | |
| * appropriate string for MQTT v3.x clients, the equivalent for MQTT v5.0 | |
| * clients is mosquitto_reason_string(). | |
| */ | |
| printf("%s\n", mosquitto_connack_string(reason_code)); | |
| if(reason_code != 0){ | |
| /* If the connection fails for any reason, we don't want to keep on | |
| * retrying in this example, so disconnect. Without this, the client | |
| * will attempt to reconnect. */ | |
| mosquitto_disconnect(mosq); | |
| } | |
| /* Making subscriptions in the on_connect() callback means that if the | |
| * connection drops and is automatically resumed by the client, then the | |
| * subscriptions will be recreated when the client reconnects. */ | |
| printf("Subscribing to " MQTT_TOPIC_RPI3 "\n"); | |
| rc = mosquitto_subscribe(mosq, NULL, MQTT_TOPIC_RPI3, 0); | |
| if(rc != MOSQ_ERR_SUCCESS){ | |
| fprintf(stderr, "Error subscribing: %s\n", mosquitto_strerror(rc)); | |
| /* We might as well disconnect if we were unable to subscribe */ | |
| mosquitto_disconnect(mosq); | |
| } | |
| } | |
| /* Callback called when the broker has received the DISCONNECT command and has disconnected the client.*/ | |
| void on_disconnect(struct mosquitto *mosq, void *obj, int reason_code) | |
| { | |
| printf("Disconnected.\n"); | |
| } | |
| /* Callback called when the broker sends a SUBACK in response to a SUBSCRIBE. */ | |
| void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) | |
| { | |
| int i; | |
| bool have_subscription = false; | |
| /* In this example we only subscribe to a single topic at once, but a | |
| * SUBSCRIBE can contain many topics at once, so this is one way to check | |
| * them all. */ | |
| for(i=0; i<qos_count; i++){ | |
| //printf("on_subscribe: %d:granted qos = %d\n", i, granted_qos[i]); | |
| printf("Subscription Granted.\n"); | |
| if(granted_qos[i] <= 2){ | |
| have_subscription = true; | |
| } | |
| } | |
| if(have_subscription == false){ | |
| /* The broker rejected all of our subscriptions, we know we only sent | |
| * the one SUBSCRIBE, so there is no point remaining connected. */ | |
| fprintf(stderr, "Error: All subscriptions rejected.\n"); | |
| mosquitto_disconnect(mosq); | |
| } | |
| } | |
| /* Callback called when the client receives a message. */ | |
| void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) | |
| { | |
| /* This blindly prints the payload, but the payload can be anything so take care. */ | |
| printf("[%s], QOS: %d\n%s\n", msg->topic, msg->qos, (char *)msg->payload); | |
| } | |
| int appMainStartup(int argc, const char *argv[], int consoleMode) { | |
| /* Required before calling other mosquitto functions */ | |
| mosquitto_lib_init(); | |
| /* Create a new client instance. | |
| * id = NULL -> ask the broker to generate a client id for us | |
| * clean session = true -> the broker should remove old sessions when we connect | |
| * obj = NULL -> we aren't passing any of our private data for callbacks | |
| */ | |
| mosq = mosquitto_new(NULL, true, NULL); | |
| if(mosq == NULL){ | |
| fprintf(stderr, "Error: Out of memory.\n"); | |
| return 1; | |
| } | |
| /* Configure callbacks. This should be done before connecting ideally. */ | |
| mosquitto_connect_callback_set(mosq, on_connect); | |
| mosquitto_disconnect_callback_set(mosq, on_disconnect); | |
| mosquitto_subscribe_callback_set(mosq, on_subscribe); | |
| mosquitto_message_callback_set(mosq, on_message); | |
| #ifdef MQTT_HIVEMQ | |
| mosquitto_username_pw_set(mosq, MQTT_USERNAME, MQTT_PASSWORD); | |
| mosquitto_int_option(mosq, MOSQ_OPT_TLS_USE_OS_CERTS, 1); | |
| #endif | |
| printf("Connecting to " MQTT_SERVER "\n"); | |
| mosquitto_connect_async(mosq, MQTT_SERVER, MQTT_SERVER_PORT, 5); | |
| mosquitto_loop_start(mosq); | |
| return 0; | |
| } | |
| int appMainThread(int *closeRequest, float lst, int windowWidth, int windowHeight, int consoleMode) { | |
| return 0; | |
| } | |
| int appMainShutdown() { | |
| mosquitto_loop_stop(mosq, true); | |
| mosquitto_destroy(mosq); | |
| mosquitto_lib_cleanup(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment