Ressources informatiques

Ressources informatiques

Ressources informatiques

S'abonner à un topic MQTT en langage C

Documentations :

Installer la librairie

pi@raspberrypi:~ $ sudo  apt-get install libmosquitto-dev

Coder un programme permettant de s'abonner à un topic sans couche SSL

Coder > fichier mosquittoSubscribe.c

#include <stdio.h>
#include <mosquitto.h>

#define PORT_SERVEUR_MQTT 1883
#define ADRESSE_IP_SERVEUR_MQTT "192.168.56.101"
#define KEEPALIVE 60

// MQTT Utilisateur : user2, mot de passe : bonjour
#define USER_MQTT "user2"
#define PASSWORD_MQTT "bonjour"

void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) {
   if(message->payloadlen){
      printf("%s %s\n", message->topic, message->payload);
   } else {
     printf("%s (null)\n", message->topic);
   }
   fflush(stdout);
}

int main(int argc, char *argv[]) {
   struct       mosquitto *clientMosquitto;

   // Initialiser la librairie
   mosquitto_lib_init();

   // Créer un client mosquitto
   clientMosquitto = mosquitto_new(NULL, true, NULL);
   if (clientMosquitto == NULL)
   {perror("mosquitto_new()");return -1;}

   // Définir le callback de réception des messages
   mosquitto_message_callback_set(clientMosquitto, my_message_callback);

   // Définir l'utilisateur et le mot de passe
   mosquitto_username_pw_set(clientMosquitto, USER_MQTT, PASSWORD_MQTT);

   // Effectuer la connexion
   if (mosquitto_connect(clientMosquitto, ADRESSE_IP_SERVEUR_MQTT, PORT_SERVEUR_MQTT, KEEPALIVE) != MOSQ_ERR_SUCCESS)
   {perror("mosquitto_new()");return -1;}

   // S'abonner au topic
   mosquitto_subscribe(clientMosquitto, NULL, "iot1/temperature", 0);

   // Boucle infinie
   mosquitto_loop_forever(clientMosquitto, -1, 1);

   mosquitto_destroy(clientMosquitto);
   mosquitto_lib_cleanup();

   return 0;
}

Compiler

pi@raspberrypi:~ $ g++ -o mosquittoSubscribe mosquittoSubscribe.c -l mosquitto

Abonnement

pi@raspberrypi:~ $./mosquittoSubscribe
iot1/temperature 22
iot1/temperature 23

Publication

doe@debian:~$ mosquitto_pub -h localhost -u 'user2' -P bonjour -t 'iot1/temperature' -q 1 -m 22
doe@debian:~$ mosquitto_pub -h localhost -u 'user2' -P bonjour -t 'iot1/temperature' -q 1 -m 23

Coder un programme permettant de s'abonner à un topic avec une couche SSL

Coder > fichier mosquittoSubscribeSSL.c

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <mosquitto.h>

#define PORT_SERVEUR_MQTT 8883
#define ADRESSE_IP_SERVEUR_MQTT "broker.btscielmichelet.lan"
#define KEEPALIVE 60

// MQTT Utilisateur : user2, mot de passe : IOTPasswd
#define USER_MQTT "user2"
#define PASSWORD_MQTT "IOTPasswd"

// certificat serveur
#define CA_CRT "ca.crt"

// Cle privee et certificat client
#define CLIENT_KEY "client2.key"
#define CLIENT_CRT "client2.crt"

void on_connect(struct mosquitto *mosq, void *obj, int reason_code) {
    printf("Connect: callback, rc=%d\n", reason_code);
}

void on_disconnect(struct mosquitto *mosq, void *obj, int rc) {
    printf("Disconnect: callback, rc=%d\n", rc);
}

void on_log(struct mosquitto *mosq, void *obj, int level, const char *str) {
    printf("Log: %s\n", str);
}

void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) {
   if(message->payloadlen){
      printf("%s %s\n", message->topic, message->payload);
   } else {
     printf("%s (null)\n", message->topic);
   }
   fflush(stdout);
}

int main(int argc, char *argv[]) {
   struct       mosquitto *clientMosquitto;

   // Initialiser la librairie
   mosquitto_lib_init();

   // Créer un client mosquitto
   clientMosquitto = mosquitto_new(NULL, true, NULL);
   if (clientMosquitto == NULL)
   {perror("mosquitto_new()");return -1;}

    mosquitto_connect_callback_set(clientMosquitto, on_connect);
    mosquitto_disconnect_callback_set(clientMosquitto, on_disconnect);
    mosquitto_log_callback_set(clientMosquitto, on_log);

   // Définir le callback de réception des messages
   mosquitto_message_callback_set(clientMosquitto, my_message_callback);

    // Définir l'utilisateur et le mot de passe
   mosquitto_username_pw_set(clientMosquitto, USER_MQTT, PASSWORD_MQTT);

   // Définir la sécurité SSL
   int retour = mosquitto_tls_set(clientMosquitto, CA_CRT, NULL, CLIENT_CRT, CLIENT_KEY, NULL);
   if (retour !=  MOSQ_ERR_SUCCESS)
   {fprintf(stderr, "Error setting TLS options: %s\n", mosquitto_strerror(retour)); return -1;}

    retour = mosquitto_tls_opts_set(clientMosquitto, 1, "tlsv1.2", NULL);
    if (retour != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "Error setting TLS options: %s\n", mosquitto_strerror(retour));
        return 1;
    }

   // Effectuer la connexion
   retour = mosquitto_connect(clientMosquitto, ADRESSE_IP_SERVEUR_MQTT, PORT_SERVEUR_MQTT, KEEPALIVE);
   if (retour != MOSQ_ERR_SUCCESS)
   {fprintf(stderr, "Unable to connect: %s\n", mosquitto_strerror(retour)); return -1;}

   // S'abonner au topic
   mosquitto_subscribe(clientMosquitto, NULL, "iot1/temperature", 0);

   // Boucle infinie
   mosquitto_loop_forever(clientMosquitto, -1, 1);

   mosquitto_destroy(clientMosquitto);
   mosquitto_lib_cleanup();

   return 0;
}

Compiler

pi@raspberrypi:~ $ g++ -o mosquittoSubscribeSSL mosquittoSubscribeSSL.c -l mosquitto

Souscription

doe@client2:~$ ./mosquittoSubscribeSSL
Log: Client (null) sending CONNECT
Log: Client (null) sending SUBSCRIBE (Mid: 1, Topic: iot1/temperature, QoS: 0, Options: 0x00)
Log: Client (null) received CONNACK (0)
Connect: callback, rc=0
Log: Client (null) received SUBACK

Publication

doe@client1:~$ mosquitto_pub  -h broker.btscielmichelet.lan -u user1 -P IOTPasswd -p 8883 --cafile ca.crt --cert client1.crt --key client1.key -d -t 'iot1/temperature' -m 20
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending PUBLISH (d0, q0, r0, m1, 'iot1/temperature', ... (2 bytes))
Client (null) sending DISCONNECT
doe@client1:~$ mosquitto_pub  -h broker.btscielmichelet.lan -u user1 -P IOTPasswd -p 8883 --cafile ca.crt --cert client1.crt --key client1.key -d -t 'iot1/temperature' -m 20.3
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending PUBLISH (d0, q0, r0, m1, 'iot1/temperature', ... (4 bytes))
Client (null) sending DISCONNECT

Résultat abonnement

doe@client2:~$ ./mosquittoSubscribeSSL
   
Log: Client (null) received PUBLISH (d0, q0, r0, m0, 'iot1/temperature', ... (2 bytes))
iot1/temperature 20
Log: Client (null) received PUBLISH (d0, q0, r0, m0, 'iot1/temperature', ... (4 bytes))
iot1/temperature 20.3