Skip to content

Instantly share code, notes, and snippets.

@evgeny-boger
Created September 7, 2015 09:04
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save evgeny-boger/8cefa502779f98efaf24 to your computer and use it in GitHub Desktop.
Save evgeny-boger/8cefa502779f98efaf24 to your computer and use it in GitHub Desktop.
libmosquitto MQTT example
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <mosquitto.h>
#define mqtt_host "localhost"
#define mqtt_port 1883
static int run = 1;
void handle_signal(int s)
{
run = 0;
}
void connect_callback(struct mosquitto *mosq, void *obj, int result)
{
printf("connect callback, rc=%d\n", result);
}
void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
bool match = 0;
printf("got message '%.*s' for topic '%s'\n", message->payloadlen, (char*) message->payload, message->topic);
mosquitto_topic_matches_sub("/devices/wb-adc/controls/+", message->topic, &match);
if (match) {
printf("got message for ADC topic\n");
}
}
int main(int argc, char *argv[])
{
uint8_t reconnect = true;
char clientid[24];
struct mosquitto *mosq;
int rc = 0;
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
mosquitto_lib_init();
memset(clientid, 0, 24);
snprintf(clientid, 23, "mysql_log_%d", getpid());
mosq = mosquitto_new(clientid, true, 0);
if(mosq){
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60);
mosquitto_subscribe(mosq, NULL, "/devices/wb-adc/controls/+", 0);
while(run){
rc = mosquitto_loop(mosq, -1, 1);
if(run && rc){
printf("connection error!\n");
sleep(10);
mosquitto_reconnect(mosq);
}
}
mosquitto_destroy(mosq);
}
mosquitto_lib_cleanup();
return rc;
}
@trilecao90
Copy link

Thank you so much. It's very helpful for my work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment