diff --git a/AutomForArduino.cpp b/AutomForArduino.cpp index e06301b..e897151 100644 --- a/AutomForArduino.cpp +++ b/AutomForArduino.cpp @@ -1,5 +1,6 @@ #include #include +#include #undef timeout #include "mqtt/async_client.h" @@ -95,7 +96,9 @@ const std::string TOPIC = "geii/in/#"; const int QOS = 1; const int CYCLE_MS = 100; -void ProcessMQTT(mqtt::async_client* client) +mqtt::async_client client(ADDRESS, CLIENTID); + +void mqtt_process(mqtt::async_client* client) { json obj = { {"s0", s0 }, @@ -112,6 +115,8 @@ void ProcessMQTT(mqtt::async_client* client) auto msg = mqtt::make_message("geii/out", payload); msg->set_qos(1); client->publish(msg); + + usleep(100000); } @@ -137,13 +142,25 @@ public: if (j.contains("b6")) b6 = j["b6"].get() != 0; if (j.contains("b7")) b7 = j["b7"].get() != 0; - std::cout << "Pompes : " << b0 << " " << b1 << " " << b2 << " " << b3 << std::endl; + std::cout << "Pompes : " << b0 << " " << b1 << " " << b2 << " " << b3 << " " << b4 << " " << b5 << " " << b6 << " " << b7 << std::endl; } catch (const json::parse_error& e) { std::cerr << "Erreur JSON : " << e.what() << "\n"; } } }; + + + +void mqtt_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; + } +} // ************************************************************ /* KEYBOARD */ diff --git a/main.cpp b/main.cpp index 98ddf08..bbc710f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,36 +1,12 @@ -#include -#include - -#include -#include -#include -#include #include "main.hpp" #include "AutomForArduino.cpp" -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include #undef timeout #include "mqtt/async_client.h" -#include -using json = nlohmann::json; - - - int main() { - mqtt::async_client client(ADDRESS, CLIENTID); callback cb; client.set_callback(cb); @@ -44,7 +20,6 @@ int main() client.subscribe(TOPIC, QOS)->wait(); } catch (const mqtt::exception &exc) { std::cerr << "Erreur MQTT: " << exc.what() << "\n"; - return 1; } while (1) @@ -58,19 +33,9 @@ int main() s6 = b6; s7 = b7; - ProcessMQTT(&client); - - usleep(100000); + mqtt_process(&client); } - 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; + mqtt_close(); return 0; } diff --git a/main.hpp b/main.hpp index 7b3a45b..e20bd4a 100644 --- a/main.hpp +++ b/main.hpp @@ -1,48 +1,2 @@ #undef timeout #include "mqtt/async_client.h" - -void ProcessMQTT(mqtt::async_client* client); - -// DIGITAL INPUT - -#define IN_SENSOR_MIN 10 -#define IN_SENSOR_LOW 11 -#define IN_SENSOR_HIGH 12 -#define IN_SENSOR_MAX 13 - -// ANALOG INPUT - -#define IN_TANK_LEVEL 14 -#define IN_FLOW_OUT 15 -#define IN_FLOW_IN 16 -#define IN_FLOW_DIF 17 -#define IN_TANK_MIN 18 -#define IN_TANK_MAX 19 -#define IN_FLOW_CAP 20 - -#define IN_FLOW_1 21 -#define IN_FLOW_2 22 -#define IN_FLOW_3 23 -#define IN_FLOW_4 24 - -// DIGITAL OUTPUT - -#define OUT_PUMP_1 25 -#define OUT_PUMP_2 26 -#define OUT_PUMP_3 27 -#define OUT_PUMP_4 28 - -#define OUT_DISPLAY_MODE 29 -#define OUT_DISPLAY_GRAFCET 30 - -// ANALOG OUTPUT - -#define OUT_LEVEL_MIN 31 -#define OUT_LEVEL_LOW 32 -#define OUT_LEVEL_HIGH 33 -#define OUT_LEVEL_MAX 34 -#define OUT_FLOW_PER_PUMP 35 -#define OUT_FLOW_OUT_AMPLITUDE 36 - -#define OUT_BEEP 254 -#define OUT_END 255