8 sorties
This commit is contained in:
69
main.cpp
69
main.cpp
@@ -25,53 +25,8 @@
|
||||
#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()
|
||||
{
|
||||
@@ -94,6 +49,15 @@ int main()
|
||||
|
||||
while (1)
|
||||
{
|
||||
s0 = b0;
|
||||
s1 = b1;
|
||||
s2 = b2;
|
||||
s3 = b3;
|
||||
s4 = b4;
|
||||
s5 = b5;
|
||||
s6 = b6;
|
||||
s7 = b7;
|
||||
|
||||
ProcessMQTT(&client);
|
||||
|
||||
usleep(100000);
|
||||
@@ -110,18 +74,3 @@ int main()
|
||||
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