Skip to content

Instantly share code, notes, and snippets.

@tirzasrwn
Last active May 25, 2022 04:52
Show Gist options
  • Save tirzasrwn/0a97970bd7458180a04c326a72545390 to your computer and use it in GitHub Desktop.
Save tirzasrwn/0a97970bd7458180a04c326a72545390 to your computer and use it in GitHub Desktop.
mqtt-example-c
cmake_minimum_required(VERSION 3.10)
project(run C)
add_executable(run mqtt_example.c)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
target_link_libraries(run
mosquitto
)
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <mosquitto.h>
#define mqtt_host "localhost"
#define mqtt_port 1883
static int run = 1;
pthread_t g_publish;
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 = false;
printf("'%s'(%d): '%.*s'\n", message->topic, message->payloadlen, message->payloadlen, (char *)message->payload);
mosquitto_topic_matches_sub("test-match", message->topic, &match);
if (match)
{
printf("got message for test-match topic\n");
}
}
void *run_publish_topic(void *arg)
{
char message[255];
int counter = 0;
while (true)
{
sprintf(message, "こんにちは: %d", counter);
int message_len = strlen(message);
mosquitto_publish(arg, NULL, "test1", message_len, message, 0, false);
counter++;
sleep(1);
}
}
int main(int argc, char *argv[])
{
bool reconnect = true;
struct mosquitto *mosq;
int rc = 0;
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
mosquitto_lib_init();
mosq = mosquitto_new(NULL, 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);
label_run:
mosquitto_subscribe(mosq, NULL, "test1", 0);
mosquitto_subscribe(mosq, NULL, "test2", 0);
mosquitto_subscribe(mosq, NULL, "test-match", 0);
pthread_create(&g_publish, NULL, run_publish_topic, mosq);
while (run)
{
rc = mosquitto_loop(mosq, -1, 1);
if (run && rc)
{
printf("connection error!\n");
pthread_cancel(g_publish);
sleep(4);
if (reconnect)
{
mosquitto_reconnect(mosq);
goto label_run;
}
else
{
break;
}
}
}
mosquitto_destroy(mosq);
}
mosquitto_lib_cleanup();
return rc;
}
// reference:
// https://gist.github.com/evgeny-boger/8cefa502779f98efaf24
// https://github.com/Johannes4Linux/libmosquitto_examples
@tirzasrwn
Copy link
Author

tirzasrwn commented May 25, 2022

Build

gcc mqtt_example.c  -o run -lmosquitto -lpthread

Build with CMAKE

mkdir build
cd build
cmake ..
make

@tirzasrwn
Copy link
Author

Publish from bash

 q=0; while true; do mosquitto_pub -h 127.0.0.1 -t "test2" -m "Hello, world $q" -p 1883; echo ${q}; ((q++)); sleep 0.5; done

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