Reprise
This commit is contained in:
119
main.cpp
119
main.cpp
@@ -1,16 +1,127 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <math.h>
|
||||
#include <locale.h>
|
||||
#include <array>
|
||||
#include "main.hpp"
|
||||
#include "autom.cpp"
|
||||
#include "AutomForArduino.cpp"
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <string>
|
||||
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <csignal>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#undef timeout
|
||||
#include "mqtt/async_client.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
using json = nlohmann::json;
|
||||
|
||||
// Constantes de fonctionnement
|
||||
#define LEVEL_MIN 2
|
||||
#define FLOW_PER_PUMP 250
|
||||
|
||||
/* Configuration MQTT */
|
||||
const std::string ADDRESS = "tcp://rabbitmq:1883";
|
||||
const std::string CLIENTID = "CppClientTP";
|
||||
const std::string TOPIC = "geii/in/#";
|
||||
const int QOS = 1;
|
||||
const int CYCLE_MS = 100;
|
||||
|
||||
int etape = 0; // Étape du grafcet : début Automatique
|
||||
int bp_mode, bp_mode_fm;
|
||||
unsigned short pompe1, pompe2, pompe3, pompe4; // bouton des pompes 0 (arrêt) / 1 (marche)
|
||||
unsigned short pompe1_old, pompe2_old, pompe3_old, pompe4_old;
|
||||
unsigned short sensor_max, sensor_high, sensor_low, sensor_min;
|
||||
|
||||
float TankInitialValue = 4.6;
|
||||
|
||||
int s0, s1, s2, s3, s4;
|
||||
|
||||
// Réception des messages MQTT
|
||||
// ************************************************************
|
||||
class callback : public virtual mqtt::callback {
|
||||
public:
|
||||
void message_arrived(mqtt::const_message_ptr msg) override {
|
||||
std::string payload = msg->to_string();
|
||||
|
||||
try {
|
||||
json j = json::parse(payload);
|
||||
|
||||
// Ne rien faire si l'objet JSON est vide
|
||||
if (j.empty()) return;
|
||||
|
||||
if (j.contains("b0")) pompe1 = j["b0"].get<int>() != 0;
|
||||
if (j.contains("b1")) pompe2 = j["b1"].get<int>() != 0;
|
||||
if (j.contains("b2")) pompe3 = j["b2"].get<int>() != 0;
|
||||
if (j.contains("b3")) pompe4 = j["b3"].get<int>() != 0;
|
||||
|
||||
std::cout << "Pompes : " << pompe1 << " " << pompe2 << " " << pompe3 << " " << pompe4 << std::endl;
|
||||
}
|
||||
catch (const json::parse_error& e) {
|
||||
std::cerr << "Erreur JSON : " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
};
|
||||
// ************************************************************
|
||||
|
||||
int main()
|
||||
{
|
||||
open();
|
||||
mqtt::async_client client(ADDRESS, CLIENTID);
|
||||
callback cb;
|
||||
client.set_callback(cb);
|
||||
|
||||
mqtt::connect_options connOpts;
|
||||
connOpts.set_clean_session(true);
|
||||
connOpts.set_user_name("admin");
|
||||
connOpts.set_password("geii2025");
|
||||
try {
|
||||
client.connect(connOpts)->wait();
|
||||
client.start_consuming();
|
||||
client.subscribe(TOPIC, QOS)->wait();
|
||||
} catch (const mqtt::exception &exc) {
|
||||
std::cerr << "Erreur MQTT: " << exc.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
send();
|
||||
ProcessMQTT(&client);
|
||||
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
close();
|
||||
try {
|
||||
client.unsubscribe(TOPIC)->wait();
|
||||
client.stop_consuming();
|
||||
client.disconnect()->wait();
|
||||
} catch(const mqtt::exception &exc){
|
||||
std::cerr << "Erreur déconnexion MQTT: " << exc.what() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Fin du programme" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ProcessMQTT(mqtt::async_client* client)
|
||||
{
|
||||
json obj = {
|
||||
{"s0", s0 },
|
||||
{"s1", s1 },
|
||||
{"s2", s2 },
|
||||
{"s3", s3 },
|
||||
};
|
||||
|
||||
std::string payload = obj.dump();
|
||||
auto msg = mqtt::make_message("geii/out", payload);
|
||||
msg->set_qos(1);
|
||||
client->publish(msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user