protocole
This commit is contained in:
26
tp3.md
26
tp3.md
@@ -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);
|
||||
```
|
||||
|
||||
Après 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é
|
||||
|
||||
Reference in New Issue
Block a user