Last active
May 25, 2022 04:52
-
-
Save tirzasrwn/0a97970bd7458180a04c326a72545390 to your computer and use it in GitHub Desktop.
mqtt-example-c
This file contains 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
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 | |
) |
This file contains 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 <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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Publish from bash