A simple program that subscribes to a single topic and prints all updates that are received.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "templates/posix_sockets.h"
void* client_refresher(void* client);
void exit_example(int status, int sockfd, pthread_t *client_daemon);
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
const char* topic;
if (argc > 1) {
addr = argv[1];
} else {
addr = "test.mosquitto.org";
}
if (argc > 2) {
port = argv[2];
} else {
port = "1883";
}
if (argc > 3) {
topic = argv[3];
} else {
topic = "datetime";
}
int sockfd = open_nb_socket(addr, port);
if (sockfd == -1) {
perror("Failed to open socket: ");
exit_example(EXIT_FAILURE, sockfd, NULL);
}
uint8_t sendbuf[2048];
uint8_t recvbuf[1024];
mqtt_init(&client, sockfd, sendbuf,
sizeof(sendbuf), recvbuf,
sizeof(recvbuf), publish_callback);
const char* client_id = NULL;
uint8_t connect_flags = MQTT_CONNECT_CLEAN_SESSION;
mqtt_connect(&client, client_id, NULL, NULL, 0, NULL, NULL, connect_flags, 400);
if (client.error != MQTT_OK) {
exit_example(EXIT_FAILURE, sockfd, NULL);
}
pthread_t client_daemon;
if(pthread_create(&client_daemon, NULL, client_refresher, &client)) {
fprintf(stderr, "Failed to start client daemon.\n");
exit_example(EXIT_FAILURE, sockfd, NULL);
}
printf("%s listening for '%s' messages.\n", argv[0], topic);
printf("Press CTRL-D to exit.\n\n");
while(fgetc(stdin) != EOF);
printf("\n%s disconnecting from %s\n", argv[0], addr);
sleep(1);
exit_example(EXIT_SUCCESS, sockfd, &client_daemon);
}
void exit_example(int status, int sockfd, pthread_t *client_daemon)
{
if (sockfd != -1) close(sockfd);
if (client_daemon != NULL) pthread_cancel(*client_daemon);
exit(status);
}
{
printf(
"Received publish('%s'): %s\n", topic_name, (
const char*) published->
application_message);
free(topic_name);
}
void* client_refresher(void* client)
{
while(1)
{
usleep(100000U);
}
return NULL;
}
enum MQTTErrors mqtt_init(struct mqtt_client *client, mqtt_pal_socket_handle sockfd, uint8_t *sendbuf, size_t sendbufsz, uint8_t *recvbuf, size_t recvbufsz, void(*publish_response_callback)(void **state, struct mqtt_response_publish *publish))
Initializes an MQTT client.
const char * mqtt_error_str(enum MQTTErrors error)
Returns an error message for error code, error.
enum MQTTErrors mqtt_connect(struct mqtt_client *client, const char *client_id, const char *will_topic, const void *will_message, size_t will_message_size, const char *user_name, const char *password, uint8_t connect_flags, uint16_t keep_alive)
Establishes a session with the MQTT broker.
enum MQTTErrors mqtt_sync(struct mqtt_client *client)
Function that does the actual sending and receiving of traffic from the network.
enum MQTTErrors mqtt_subscribe(struct mqtt_client *client, const char *topic_name, int max_qos_level)
Subscribe to a topic.
Declares all the MQTT-C functions and datastructures.
An MQTT client.
Definition mqtt.h:1096
A publish packet received from the broker.
Definition mqtt.h:343
const void * topic_name
The topic name.
Definition mqtt.h:370
const void * application_message
The publish message's application message.
Definition mqtt.h:376
uint16_t topic_name_size
Size of the topic name (number of characters).
Definition mqtt.h:363