Commit 7dc0d8fd3864f8adf9aee4a298eb6256e620eb6d

Authored by dling
1 parent ddf0d38c

Code pour écrire sur plusieurs "holding Registers" (registres de conservation).

Showing 1 changed file with 168 additions and 0 deletions   Show diff stats
MQTT_modbus_write_multiple_registers 0 → 100644
... ... @@ -0,0 +1,168 @@
  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 +"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \
  38 +"WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \
  39 +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qbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL\n" \
  54 +"ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ\n" \
  55 +"3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK\n" \
  56 +"NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5\n" \
  57 +"ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n" \
  58 +"TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n" \
  59 +"jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n" \
  60 +"oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n" \
  61 +"4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n" \
  62 +"mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n" \
  63 +"emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n" \
  64 +"-----END CERTIFICATE-----\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 + if (Serial.available()){
  108 + Serial.println();
  109 + messageToPublish = Serial.readString();
  110 + mqttClient.publish("topic/hello",messageToPublish.c_str());
  111 + }
  112 +}
  113 +
  114 +void reconnect() {
  115 + while (!mqttClient.connected()) {
  116 + Serial.println("Connecting to MQTT server...");
  117 + if (mqttClient.connect("ESP32Client", mqttUser, mqttPassword)) {
  118 + Serial.println("Connected to MQTT server");
  119 + } else {
  120 + Serial.print("Failed to connect to MQTT server, rc=");
  121 + Serial.print(mqttClient.state());
  122 + Serial.println(" Retrying in 5 seconds...");
  123 + delay(5000);
  124 + }
  125 + }
  126 +}
  127 +
  128 +void callback(char* topic, byte* payload, unsigned int length) {
  129 + // Fonction de rappel pour la réception des messages MQTT
  130 + Serial.print("Message arrived on topic: ");
  131 + Serial.print(topic);
  132 + Serial.print(". Message: ");
  133 + for (int i = 0; i < length; i++) {
  134 + Serial.print((char)payload[i]);
  135 + }
  136 +
  137 + char messageBuffer[length + 1];
  138 + memcpy(messageBuffer, payload, length);
  139 + messageBuffer[length] = '\0'; // Ajouter le caractère de fin de chaîne
  140 + //messageMQTT = 0; //Clear messsage
  141 + messageMQTT = atoi((char*)messageBuffer); //convertir le
  142 +
  143 + Serial.print("message MQTT:");
  144 + Serial.println(messageMQTT);
  145 +
  146 + if(messageMQTT < 32767){
  147 +
  148 + static uint16_t register_values[10] = {0b0000000100100011, 0b0001001010000011, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,0x0001,0x0001};
  149 +
  150 + for (int i = 0; i < 10; i++) { //
  151 + node.setTransmitBuffer(i, register_values[i]);
  152 + }
  153 + result = node.writeMultipleRegisters(0,40);
  154 +
  155 + if (result == node.ku8MBSuccess)
  156 + {
  157 + Serial.printf("\r\nSucces");
  158 + }
  159 + else
  160 + {
  161 + Serial.printf("\r\nFail");
  162 + }
  163 +
  164 +
  165 + }
  166 + else
  167 + Serial.println("number is too big");
  168 +}
0 169 \ No newline at end of file
... ...