diff --git a/.devcontainer/nodered/flows.json b/.devcontainer/nodered/flows.json index 4280b51..ddbbf6d 100644 --- a/.devcontainer/nodered/flows.json +++ b/.devcontainer/nodered/flows.json @@ -225,7 +225,7 @@ "type": "function", "z": "41526b8c80d5a5f7", "name": "format mqtt", - "func": "let marche = Number(flow.get(\"marche\") || 0);\nlet b0 = Number(flow.get(\"b0\") || 0);\nlet b1 = Number(flow.get(\"b1\") || 0);\nlet b2 = Number(flow.get(\"b2\") || 0);\nlet b3 = Number(flow.get(\"b3\") || 0);\n\nmsg.payload = {\n \"marche\": marche,\n \"b0\": b0,\n \"b1\": b1,\n \"b2\": b2,\n \"b3\": b3\n};\n\nreturn msg;", + "func": "let marche = Number(flow.get(\"marche\") || 0);\nlet b0 = Number(flow.get(\"b0\") || 0);\nlet b1 = Number(flow.get(\"b1\") || 0);\nlet b2 = Number(flow.get(\"b2\") || 0);\nlet b3 = Number(flow.get(\"b3\") || 0);\nlet b4 = Number(flow.get(\"b4\") || 0);\nlet b5 = Number(flow.get(\"b5\") || 0);\nlet b6 = Number(flow.get(\"b6\") || 0);\nlet b7 = Number(flow.get(\"b7\") || 0);\n\nmsg.payload = {\n \"marche\": marche,\n \"b0\": b0,\n \"b1\": b1,\n \"b2\": b2,\n \"b3\": b3,\n \"b4\": b4,\n \"b5\": b5,\n \"b6\": b6,\n \"b7\": b7\n};\n\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, @@ -248,10 +248,6 @@ "props": [ { "p": "payload" - }, - { - "p": "topic", - "vt": "str" } ], "repeat": "", @@ -265,48 +261,7 @@ "y": 40, "wires": [ [ - "d28f31d7c379f3d8" - ] - ] - }, - { - "id": "d28f31d7c379f3d8", - "type": "change", - "z": "41526b8c80d5a5f7", - "name": "", - "rules": [ - { - "t": "set", - "p": "marche", - "pt": "flow", - "to": "0", - "tot": "num" - }, - { - "t": "set", - "p": "s0", - "pt": "flow", - "to": "0", - "tot": "num" - }, - { - "t": "set", - "p": "b0", - "pt": "flow", - "to": "0", - "tot": "num" - } - ], - "action": "", - "property": "", - "from": "", - "to": "", - "reg": false, - "x": 320, - "y": 40, - "wires": [ - [ - "0ca8f4a10a6048af" + "869aedf62de05359" ] ] }, @@ -334,7 +289,7 @@ "id": "6444617c166984e0", "type": "function", "z": "41526b8c80d5a5f7", - "name": "set marche", + "name": "set b0", "func": "flow.set(\"b0\", !Number(flow.get(\"b0\")));\n\nreturn msg;", "outputs": 1, "timeout": 0, @@ -342,7 +297,7 @@ "initialize": "", "finalize": "", "libs": [], - "x": 310, + "x": 290, "y": 320, "wires": [ [ @@ -354,7 +309,7 @@ "id": "4ee747c8012c58a1", "type": "function", "z": "41526b8c80d5a5f7", - "name": "set marche", + "name": "set b1", "func": "flow.set(\"b1\", !Number(flow.get(\"b1\")));\n\nreturn msg;", "outputs": 1, "timeout": 0, @@ -362,7 +317,7 @@ "initialize": "", "finalize": "", "libs": [], - "x": 310, + "x": 290, "y": 360, "wires": [ [ @@ -374,7 +329,7 @@ "id": "d966ba0d813b7cb7", "type": "function", "z": "41526b8c80d5a5f7", - "name": "set marche", + "name": "set b2", "func": "flow.set(\"b2\", !Number(flow.get(\"b2\")));\n\nreturn msg;", "outputs": 1, "timeout": 0, @@ -382,7 +337,7 @@ "initialize": "", "finalize": "", "libs": [], - "x": 310, + "x": 290, "y": 400, "wires": [ [ @@ -394,7 +349,7 @@ "id": "501355b0c82cae08", "type": "function", "z": "41526b8c80d5a5f7", - "name": "set marche", + "name": "set b3", "func": "flow.set(\"b3\", !Number(flow.get(\"b3\")));\n\nreturn msg;", "outputs": 1, "timeout": 0, @@ -402,7 +357,7 @@ "initialize": "", "finalize": "", "libs": [], - "x": 310, + "x": 290, "y": 440, "wires": [ [ @@ -427,33 +382,16 @@ "y": 580, "wires": [ [ - "31ef90b8227cd6cb", "9ef1bb67f51b254c" ] ] }, - { - "id": "31ef90b8227cd6cb", - "type": "debug", - "z": "41526b8c80d5a5f7", - "name": "debug 1", - "active": true, - "tosidebar": true, - "console": false, - "tostatus": false, - "complete": "false", - "statusVal": "", - "statusType": "auto", - "x": 740, - "y": 460, - "wires": [] - }, { "id": "309cae1a9bcc718b", "type": "ui-text", "z": "41526b8c80d5a5f7", "group": "c3dd11d0778f9e67", - "order": 4, + "order": 1, "width": 0, "height": 0, "name": "", @@ -477,7 +415,7 @@ "type": "ui-text", "z": "41526b8c80d5a5f7", "group": "c3dd11d0778f9e67", - "order": 3, + "order": 2, "width": 0, "height": 0, "name": "", @@ -501,7 +439,7 @@ "type": "ui-text", "z": "41526b8c80d5a5f7", "group": "c3dd11d0778f9e67", - "order": 2, + "order": 3, "width": 0, "height": 0, "name": "", @@ -525,7 +463,7 @@ "type": "function", "z": "41526b8c80d5a5f7", "name": "function 1", - "func": "flow.set(\"s0\", !Number(msg.payload.s0));\nflow.set(\"s1\", !Number(msg.payload.s1));\nflow.set(\"s2\", !Number(msg.payload.s2));\n\nreturn msg;", + "func": "flow.set(\"s0\", !Number(msg.payload.s0));\nflow.set(\"s1\", !Number(msg.payload.s1));\nflow.set(\"s2\", !Number(msg.payload.s2));\nflow.set(\"s3\", !Number(msg.payload.s3));\nflow.set(\"s4\", !Number(msg.payload.s4));\nflow.set(\"s5\", !Number(msg.payload.s5));\nflow.set(\"s6\", !Number(msg.payload.s6));\nflow.set(\"s7\", !Number(msg.payload.s7));\n\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, @@ -538,7 +476,12 @@ [ "9dad44eccec1d282", "fc568b731fec8026", - "6179e102d7866a82" + "6179e102d7866a82", + "ccecf6f984daae7c", + "51bb134ec07cdcef", + "ae4e0b8f232fbdfc", + "f98062374cdfd8d1", + "9dfe2d239a268225" ] ] }, @@ -547,7 +490,7 @@ "type": "function", "z": "41526b8c80d5a5f7", "name": "s0", - "func": "msg.payload = Number(flow.get(\"s0\")) ? \"🟥\" : \"🟩\";\n\nreturn msg;", + "func": "msg.payload = Number(flow.get(\"s0\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, @@ -567,7 +510,7 @@ "type": "function", "z": "41526b8c80d5a5f7", "name": "s1", - "func": "msg.payload = Number(flow.get(\"s1\")) ? \"🟥\" : \"🟩\";\n\nreturn msg;", + "func": "msg.payload = Number(flow.get(\"s1\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, @@ -586,8 +529,8 @@ "id": "6179e102d7866a82", "type": "function", "z": "41526b8c80d5a5f7", - "name": "s0", - "func": "msg.payload = Number(flow.get(\"s2\")) ? \"🟥\" : \"🟩\";\n\nreturn msg;", + "name": "s2", + "func": "msg.payload = Number(flow.get(\"s2\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, @@ -602,6 +545,246 @@ ] ] }, + { + "id": "6172598e36ec2754", + "type": "ui-text", + "z": "41526b8c80d5a5f7", + "group": "c3dd11d0778f9e67", + "order": 4, + "width": 0, + "height": 0, + "name": "", + "label": "s3", + "format": "{{msg.payload}}", + "layout": "row-spread", + "style": false, + "font": "", + "fontSize": 16, + "color": "#717171", + "wrapText": false, + "className": "", + "value": "payload", + "valueType": "msg", + "x": 870, + "y": 640, + "wires": [] + }, + { + "id": "7b808775687a1e80", + "type": "ui-text", + "z": "41526b8c80d5a5f7", + "group": "c3dd11d0778f9e67", + "order": 5, + "width": 0, + "height": 0, + "name": "", + "label": "s4", + "format": "{{msg.payload}}", + "layout": "row-spread", + "style": false, + "font": "", + "fontSize": 16, + "color": "#717171", + "wrapText": false, + "className": "", + "value": "payload", + "valueType": "msg", + "x": 870, + "y": 680, + "wires": [] + }, + { + "id": "5e969524f2d7ac83", + "type": "ui-text", + "z": "41526b8c80d5a5f7", + "group": "c3dd11d0778f9e67", + "order": 6, + "width": 0, + "height": 0, + "name": "", + "label": "s5", + "format": "{{msg.payload}}", + "layout": "row-spread", + "style": false, + "font": "", + "fontSize": 16, + "color": "#717171", + "wrapText": false, + "className": "", + "value": "payload", + "valueType": "msg", + "x": 870, + "y": 720, + "wires": [] + }, + { + "id": "f832017038053787", + "type": "ui-text", + "z": "41526b8c80d5a5f7", + "group": "c3dd11d0778f9e67", + "order": 7, + "width": 0, + "height": 0, + "name": "", + "label": "s6", + "format": "{{msg.payload}}", + "layout": "row-spread", + "style": false, + "font": "", + "fontSize": 16, + "color": "#717171", + "wrapText": false, + "className": "", + "value": "payload", + "valueType": "msg", + "x": 870, + "y": 760, + "wires": [] + }, + { + "id": "a85963dd6d125a42", + "type": "ui-text", + "z": "41526b8c80d5a5f7", + "group": "c3dd11d0778f9e67", + "order": 8, + "width": 0, + "height": 0, + "name": "", + "label": "s7", + "format": "{{msg.payload}}", + "layout": "row-spread", + "style": false, + "font": "", + "fontSize": 16, + "color": "#717171", + "wrapText": false, + "className": "", + "value": "payload", + "valueType": "msg", + "x": 870, + "y": 800, + "wires": [] + }, + { + "id": "ccecf6f984daae7c", + "type": "function", + "z": "41526b8c80d5a5f7", + "name": "s3", + "func": "msg.payload = Number(flow.get(\"s3\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", + "outputs": 1, + "timeout": 0, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 690, + "y": 640, + "wires": [ + [ + "6172598e36ec2754" + ] + ] + }, + { + "id": "51bb134ec07cdcef", + "type": "function", + "z": "41526b8c80d5a5f7", + "name": "s4", + "func": "msg.payload = Number(flow.get(\"s4\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", + "outputs": 1, + "timeout": 0, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 690, + "y": 680, + "wires": [ + [ + "7b808775687a1e80" + ] + ] + }, + { + "id": "ae4e0b8f232fbdfc", + "type": "function", + "z": "41526b8c80d5a5f7", + "name": "s5", + "func": "msg.payload = Number(flow.get(\"s5\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", + "outputs": 1, + "timeout": 0, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 690, + "y": 720, + "wires": [ + [ + "5e969524f2d7ac83" + ] + ] + }, + { + "id": "f98062374cdfd8d1", + "type": "function", + "z": "41526b8c80d5a5f7", + "name": "s6", + "func": "msg.payload = Number(flow.get(\"s6\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", + "outputs": 1, + "timeout": 0, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 690, + "y": 760, + "wires": [ + [ + "f832017038053787" + ] + ] + }, + { + "id": "9dfe2d239a268225", + "type": "function", + "z": "41526b8c80d5a5f7", + "name": "s7", + "func": "msg.payload = Number(flow.get(\"s7\")) ? \"🟩\" : \"🟥\";\n\nreturn msg;", + "outputs": 1, + "timeout": 0, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 690, + "y": 800, + "wires": [ + [ + "a85963dd6d125a42" + ] + ] + }, + { + "id": "869aedf62de05359", + "type": "function", + "z": "41526b8c80d5a5f7", + "name": "reset", + "func": "flow.set(\"marche\", 1);\n\nflow.set(\"b0\", 0);\nflow.set(\"b1\", 0);\nflow.set(\"b2\", 0);\nflow.set(\"b3\", 0);\nflow.set(\"b4\", 0);\nflow.set(\"b5\", 0);\nflow.set(\"b6\", 0);\nflow.set(\"b7\", 0);\n\nflow.set(\"s0\", 0);\nflow.set(\"s1\", 0);\nflow.set(\"s2\", 0);\nflow.set(\"s3\", 0);\nflow.set(\"s4\", 0);\nflow.set(\"s5\", 0);\nflow.set(\"s6\", 0);\nflow.set(\"s7\", 0);\n\nreturn msg;", + "outputs": 1, + "timeout": 0, + "noerr": 0, + "initialize": "", + "finalize": "", + "libs": [], + "x": 310, + "y": 40, + "wires": [ + [ + "0ca8f4a10a6048af" + ] + ] + }, { "id": "5fe915fcd26e78ae", "type": "ui-group", @@ -609,7 +792,7 @@ "page": "bb436fe040268d40", "width": "2", "height": 1, - "order": 2, + "order": 1, "showTitle": true, "className": "", "visible": "true", @@ -654,7 +837,7 @@ "page": "bb436fe040268d40", "width": "3", "height": 1, - "order": 1, + "order": 2, "showTitle": true, "className": "", "visible": "true", @@ -737,7 +920,7 @@ } }, { - "id": "7514f74ba5dcf202", + "id": "78daa6a123a398db", "type": "global-config", "env": [], "modules": { diff --git a/AutomForArduino.cpp b/AutomForArduino.cpp index 3d957c2..e06301b 100644 --- a/AutomForArduino.cpp +++ b/AutomForArduino.cpp @@ -1,6 +1,12 @@ #include #include +#undef timeout +#include "mqtt/async_client.h" + +#include +using json = nlohmann::json; + /* From Arduino.h */ #define HIGH 0x1 @@ -74,8 +80,71 @@ void pinMode(unsigned char p, unsigned char mode) _digital[p].memory = 0; } +int b0, b1, b2, b3, b4, b5, b6, b7; +int s0, s1, s2, s3, s4, s5, s6, s7; + +/* ******************************************************** + * MQTT * + * * + ******************************************************** */ + +/* 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; + +void ProcessMQTT(mqtt::async_client* client) +{ + json obj = { + {"s0", s0 }, + {"s1", s1 }, + {"s2", s2 }, + {"s3", s3 }, + {"s4", s4 }, + {"s5", s5 }, + {"s6", s6 }, + {"s7", s7 }, + }; + + std::string payload = obj.dump(); + auto msg = mqtt::make_message("geii/out", payload); + msg->set_qos(1); + client->publish(msg); +} +// 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")) b0 = j["b0"].get() != 0; + if (j.contains("b1")) b1 = j["b1"].get() != 0; + if (j.contains("b2")) b2 = j["b2"].get() != 0; + if (j.contains("b3")) b3 = j["b3"].get() != 0; + if (j.contains("b4")) b4 = j["b4"].get() != 0; + if (j.contains("b5")) b5 = j["b5"].get() != 0; + 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; + } + catch (const json::parse_error& e) { + std::cerr << "Erreur JSON : " << e.what() << "\n"; + } + } +}; +// ************************************************************ /* KEYBOARD */ @@ -165,7 +234,7 @@ class TemporisationRetardMontee }; /******************************************************** -* TEMPORISATION RETARD A LA DESCENTE * +* TEMPORISATION RETARD A LA DESCENTE * * La sortie passe à 0 au bout de la tempo * *********************************************************/ class TemporisationRetardDescente diff --git a/main.cpp b/main.cpp index 36070f1..98ddf08 100644 --- a/main.cpp +++ b/main.cpp @@ -25,53 +25,8 @@ #include 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() != 0; - if (j.contains("b1")) pompe2 = j["b1"].get() != 0; - if (j.contains("b2")) pompe3 = j["b2"].get() != 0; - if (j.contains("b3")) pompe4 = j["b3"].get() != 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); -}