Commit ddf0d38c7dfc6ffcb0b1e27217c6d38496a3ca44
0 parents
Il s'agit du code pour écrire sur plusieurs registres modbus lors de la réceptio…
…n d'un message par MQTT.
Showing
1 changed file
with
172 additions
and
0 deletions
Show diff stats
1 | +++ a/MQTT_modbus_write_multiple_coils | ||
@@ -0,0 +1,172 @@ | @@ -0,0 +1,172 @@ | ||
1 | +#include <WiFi.h> | ||
2 | +#include <PubSubClient.h> | ||
3 | +#include <WiFiClientSecure.h> | ||
4 | +#include "identifiants.h" | ||
5 | +#include "ModbusMaster.h" | ||
6 | +ModbusMaster node; | ||
7 | + | ||
8 | +//identifiants | ||
9 | +const char* ssid = MY_SSID; | ||
10 | +const char* password = MY_PASS; | ||
11 | + | ||
12 | + | ||
13 | +//adresse du broker MQTT cloud et son port (8883 car protocole SSL) | ||
14 | +const char* mqttServer = "f63f940361134f62957ef137f9d2e29f.s1.eu.hivemq.cloud"; | ||
15 | +const int mqttPort = 8883; | ||
16 | + | ||
17 | +//identifiants du broker MQTT cloud pour s'authentifier | ||
18 | +const char* mqttUser = MQTT_USER; | ||
19 | +const char* mqttPassword = MQTT_PASSWORD; | ||
20 | + | ||
21 | +unsigned long previousMillis = 0; | ||
22 | +const long interval = 5000; | ||
23 | +uint8_t result; | ||
24 | +int messageMQTT; | ||
25 | + | ||
26 | +uint8_t coil_value; | ||
27 | + | ||
28 | +WiFiClientSecure wifiClient; | ||
29 | +PubSubClient mqttClient(wifiClient); | ||
30 | +String messageToPublish; | ||
31 | + | ||
32 | +// Certificat CA du serveur MQTT obligatoire pour authentifier l'appareil qui se connecte | ||
33 | +const char* caCert = \ | ||
34 | +"-----BEGIN CERTIFICATE-----\n" \ | ||
35 | +"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \ | ||
36 | +"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ | ||
37 | +"cmN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-----\n"; | ||
65 | + | ||
66 | +void setup() { | ||
67 | + Serial.begin(115200); //port série activé | ||
68 | + | ||
69 | + WiFi.begin(ssid, password); //connexion au WiFi | ||
70 | + while (WiFi.status() != WL_CONNECTED) { | ||
71 | + delay(1000); | ||
72 | + Serial.println("Connecting to WiFi..."); | ||
73 | + } | ||
74 | + Serial.println("Connected to WiFi"); | ||
75 | + //connexion au broker MQTT | ||
76 | + mqttClient.setServer(mqttServer, mqttPort); | ||
77 | + mqttClient.setCallback(callback); | ||
78 | + | ||
79 | + // Chargement du certificat CA du serveur MQTT | ||
80 | + wifiClient.setCACert(caCert); | ||
81 | + | ||
82 | + while (!mqttClient.connected()) { | ||
83 | + Serial.println("Connecting to MQTT server..."); | ||
84 | + if (mqttClient.connect("ESP32Client", mqttUser, mqttPassword)) { | ||
85 | + Serial.println("Connected to MQTT server"); | ||
86 | + mqttClient.subscribe("topic/hello"); | ||
87 | + } else { | ||
88 | + Serial.print("Failed to connect to MQTT server, rc="); | ||
89 | + Serial.print(mqttClient.state()); | ||
90 | + Serial.println(" Retrying in 5 seconds..."); | ||
91 | + delay(5000); | ||
92 | + } | ||
93 | + } | ||
94 | + | ||
95 | + Serial.print("\r\n*****************PRET!*******************"); | ||
96 | + Serial2.begin(9600, SERIAL_8N1); //communication modbus à 9600 bauds | ||
97 | + node.begin(1, Serial2); //ID node 1 | ||
98 | +} | ||
99 | + | ||
100 | +void loop() { | ||
101 | + mqttClient.loop(); | ||
102 | + | ||
103 | + if (!mqttClient.connected()) { | ||
104 | + reconnect(); | ||
105 | + } | ||
106 | + | ||
107 | + //Le port série permet de publier des messages sur le topic "topic/hello" | ||
108 | + if (Serial.available()){ | ||
109 | + Serial.println(); | ||
110 | + messageToPublish = Serial.readString(); | ||
111 | + mqttClient.publish("topic/hello",messageToPublish.c_str()); | ||
112 | + } | ||
113 | +} | ||
114 | + | ||
115 | +// Fonction pour se reconnecter au serveur MQTT en cas de problème | ||
116 | +void reconnect() { | ||
117 | + while (!mqttClient.connected()) { | ||
118 | + Serial.println("Connecting to MQTT server..."); | ||
119 | + if (mqttClient.connect("ESP32Client", mqttUser, mqttPassword)) { | ||
120 | + Serial.println("Connected to MQTT server"); | ||
121 | + } else { | ||
122 | + Serial.print("Failed to connect to MQTT server, rc="); | ||
123 | + Serial.print(mqttClient.state()); | ||
124 | + Serial.println(" Retrying in 5 seconds..."); | ||
125 | + delay(5000); | ||
126 | + } | ||
127 | + } | ||
128 | +} | ||
129 | + | ||
130 | +void callback(char* topic, byte* payload, unsigned int length) { | ||
131 | + // Fonction de rappel pour la réception des messages MQTT | ||
132 | + Serial.print("Message arrived on topic: "); | ||
133 | + Serial.print(topic); | ||
134 | + Serial.print(". Message: "); | ||
135 | + for (int i = 0; i < length; i++) { | ||
136 | + Serial.print((char)payload[i]); | ||
137 | + } | ||
138 | + | ||
139 | + char messageBuffer[length + 1]; | ||
140 | + memcpy(messageBuffer, payload, length); | ||
141 | + messageBuffer[length] = '\0'; // Ajouter le caractère de fin de chaîne | ||
142 | + //messageMQTT = 0; //Clear messsage | ||
143 | + messageMQTT = atoi((char*)messageBuffer); //convertir le | ||
144 | + | ||
145 | + Serial.print("message MQTT:"); | ||
146 | + Serial.println(messageMQTT); | ||
147 | + | ||
148 | + if(messageMQTT < 32767){ | ||
149 | + | ||
150 | + static uint16_t coil_values[10] = {0b0000000100100011, 0b0001001010000011, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,0x0001,0x0001}; | ||
151 | + | ||
152 | + for (int i = 0; i < 10; i++) { | ||
153 | + node.setTransmitBuffer(i, coil_values[i]); | ||
154 | + } | ||
155 | + //for(int j = 0; j < 20; j++){ | ||
156 | + result = node.writeMultipleCoils(0,40); // write coil registers to true | ||
157 | + | ||
158 | + if (result == node.ku8MBSuccess) | ||
159 | + { | ||
160 | + Serial.printf("\r\nSucces"); | ||
161 | + } | ||
162 | + else | ||
163 | + { | ||
164 | + Serial.printf("\r\nFail"); | ||
165 | + } | ||
166 | + | ||
167 | + //} | ||
168 | + } | ||
169 | + else | ||
170 | + Serial.println("number is too big"); | ||
171 | + //Serial.println(); | ||
172 | +} | ||
0 | \ No newline at end of file | 173 | \ No newline at end of file |