protocole

This commit is contained in:
2025-12-09 13:38:35 +01:00
parent 215995d50f
commit 22fd230777
2 changed files with 112 additions and 8 deletions

26
tp3.md
View File

@@ -71,6 +71,7 @@ Ajouter la bibliothèque MQTT
#include "mqtt/async_client.h"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
```
Déclarer les constantes
@@ -97,6 +98,8 @@ public:
};
```
Intialiser le client et se connecter
```c
mqtt::async_client client(ADDRESS, CLIENTID);
callback cb;
@@ -116,6 +119,19 @@ mqtt::async_client client(ADDRESS, CLIENTID);
}
```
Après la boucle principale déconnecter MQTT.
```c
try {
client.unsubscribe(TOPIC)->wait();
client.stop_consuming();
client.disconnect()->wait();
} catch(const mqtt::exception &exc){
std::cerr << "Erreur déconnexion MQTT: " << exc.what() << "\n";
}
```
Dans la fonction ProcessMQTT créer un objet JSON avec les valeurs des différents capteurs.
```c
@@ -134,16 +150,10 @@ msg->set_qos(1);
client->publish(msg);
```
Aps la boucle principale déconnecter MQTT.
Appeler la fonction ProcessMQTT dans la boucle principale
```c
try {
client.unsubscribe(TOPIC)->wait();
client.stop_consuming();
client.disconnect()->wait();
} catch(const mqtt::exception &exc){
std::cerr << "Erreur déconnexion MQTT: " << exc.what() << "\n";
}
ProcessMQTT(&client);
```
## Énoncé