Advertisement
Justinberger

DMX_Serre_2023.ino

Apr 18th, 2023 (edited)
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 42.27 KB | None | 0 0
  1. #include <LoRa.h>
  2.  
  3. //-------------------------------------------------
  4. //---------- Addresse sur le reseau LoRa ----------
  5. //#define DEBUG
  6. #define Master
  7. // #define LoRa_Interface
  8. // #define C1 //https://www.deviceplus.com/arduino/arduino-preprocessor-directives-tutorial/
  9. // #define C2
  10. // #define C3
  11. // #define C4
  12.  
  13. #ifdef Master
  14. #include <RTClib.h>
  15. #include <SparkFunDMX.h>
  16. #include <ArduinoJson.h>
  17. #include <EasyNextionLibrary.h>
  18. // Librairies pour écrire sur la mémoire :
  19. #include "SPIFFS.h"
  20. #include <esp_task_wdt.h>
  21. #endif
  22.  
  23. #ifdef LoRa_Interface
  24. String addr_LoRa = "LoRa_Interface";
  25. byte localAddress = 0xBA;
  26. byte destination = 0xAA;
  27. String incoming = "";
  28. String incoming1 = "";
  29. String incoming2 = "";
  30. String incoming3 = "";
  31. unsigned long prevMillis3;
  32. byte msgCount = 0; // count of outgoing messages
  33. //  SPI port:  LoRa (SX1278)
  34. #define LoRa_SCK 5
  35. #define LoRa_MISO 19
  36. #define LoRa_MOSI 27
  37. #define LoRa_CS 18
  38. #define DI0 26 //  LoRa_IRQ
  39. #endif
  40. #ifdef C1
  41. String addr_LoRa = "C1";
  42. byte localAddress = 0xAB;
  43. #endif
  44. #ifdef C2
  45. String addr_LoRa = "C2";
  46. byte localAddress = 0xAC;
  47. #endif
  48. #ifdef C3
  49. String addr_LoRa = "C3";
  50. byte localAddress = 0xAD;
  51. #endif
  52. #ifdef C4
  53. String addr_LoRa = "C4";
  54. byte localAddress = 0xAE;
  55. #endif
  56. #ifdef C5
  57. String addr_LoRa = "C5";
  58. byte localAddress = 0xAF;
  59. #endif
  60.  
  61. #if defined(C1) || defined(C2) || defined(C3) || defined(C4) || defined(C5)
  62. // byte destination = 0xAA;
  63. byte destination = 0xBA;
  64. #include <DFRobot_B_LUX_V30B.h>
  65. #include <LowPower.h>
  66. DFRobot_B_LUX_V30B myLux(13); // The sensor chip is set to 13 pins, SCL and SDA adopt default configuration
  67.  
  68. /* Déclaration de la sonde le lumière */
  69. long lux;
  70.  
  71. /* Paramètres des sondes NTC */
  72.  
  73. // https://www.thinksrs.com/downloads/programs/Therm%20Calc/NTCCalibrator/NTCcalculator.htm
  74. // https://www.ametherm.com/blog/thermistor/arduino-and-thermistors
  75.  
  76. /*int ThermistorPinA1 = A1;
  77. int ThermistorPinA2 = A2;
  78. int ThermistorPinA3 = A3;
  79.  
  80. int VoA1;
  81. int VoA2;
  82. int VoA3;
  83. float R1 = 10000;
  84. float logR2A1, R2A1, TA1;
  85. float logR2A2, R2A2, TA2;
  86. float logR2A3, R2A3, TA3;
  87. String Avalue = "0.8893603324e-03";
  88. String Bvalue = "2.512257790e-04";
  89. String Cvalue = "1.929834478e-07";
  90. float A = 0.8893603324e-03, B = 2.512257790e-04, C = 1.929834478e-07; // Steinhart-Hart and Hart Coefficients
  91.  
  92.  
  93. float vinA1 = 0;
  94.  
  95. //https://www.instructables.com/id/Humidity-Sensor-Arduino/
  96. float voltage = 3.3;
  97. const float resolution = 1024.0;
  98. float rhSlope = 0.0062 * voltage;
  99. float rhOffset = 0.16 * voltage;*/
  100.  
  101. String message = "message";
  102. byte msgCount = 0; // count of outgoing messages
  103. byte duree = 0;
  104. #endif
  105.  
  106. #ifdef Master
  107.  
  108. //  SPI port:  LoRa (SX1278)
  109. #define LoRa_CS 18
  110. #define DI0 26     //  LoRa_IRQ
  111. #define BAND 433E6 //  other freq: 433E6  // or ? LoRa f(MHz)
  112. #define CLK 14
  113. #define MISO 2
  114. #define MOSI 15
  115.  
  116. //!!!!!!___Il faut modifier les pins dans le fichier "SparkFunDMX.cpp" pour que ça fonctionne _____!!!!!!//
  117.  
  118. #define Select LOW    //  Low CS means that SPI device Selected
  119. #define DeSelect HIGH //  High CS means that SPI device Deselected
  120.  
  121. byte localAddress = 0xAA;
  122. byte destination = 0xEE; // address of this device
  123. int counter = 0;         //  count sent LoRa messages
  124.  
  125. RTC_DS3231 rtc;
  126. DateTime now;
  127.  
  128. SparkFunDMX dmx;
  129. // DMX Channel Definitions
  130. #define TOTAL_CHANNELS 500
  131.  
  132. EasyNex myNex(Serial);
  133. //!!! Pins du port Serial1 modifié dans le fichier "HardwareSerial.cpp" pour pouvoir faire fonctionner le port Serial1 !!!
  134.  
  135. long consigne_declenchement[12] = {20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000, 20000};
  136. unsigned int consigne_zone_neutre[12] = {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500};
  137. byte consigne_intensite[12] = {70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70};
  138. byte heure_allumage[12] = {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
  139. byte heure_extinction[12] = {22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22};
  140. int adresse_DMX[12][2] = {
  141.     {177, 208},
  142.     {161, 176},
  143.     {145, 160},
  144.     {129, 144},
  145.     {113, 128},
  146.     {97, 112},
  147.     {81, 96},
  148.     {65, 80},
  149.     {49, 64},
  150.     {1, 14},
  151.     {17, 30},
  152.     {33, 48},
  153. };
  154. bool allumage[12]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  155. bool allumage1[12]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  156. bool On_Off[12][30] = {
  157.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  158.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  159.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  160.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  161.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  162.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  163.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  164.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  165.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  166.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  167.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  168.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  169. };
  170. bool lastOn_Off[12][30];
  171.  
  172. #endif
  173. float TC_A1_C1M;
  174. float TC_A2_C1M;
  175. float TC_A3_C1M;
  176. int Humidite_C1M;
  177. int TC_DHT22_C1M;
  178. #ifdef Master
  179. long luxmetre[3]{0, 0, 0};
  180. byte heure;
  181. byte minute;
  182. byte seconde;
  183. bool sd = 0;
  184. bool demarrage = 0;
  185. //Watchdog
  186. int last = millis();
  187. //3 seconds WDT
  188. #define WDT_TIMEOUT 8
  189. // Variables Nextion
  190. unsigned long prevMillis;
  191. byte Compnextion = 0;
  192. byte lastCompnextion = 0;
  193. byte ConsigneIdNext = 0;
  194. byte lastConsigneIdNext = 0;
  195. unsigned long prevMillis1;
  196. bool actualiser = 1;
  197. unsigned int temps_admin;
  198. bool AdminNext;
  199. #endif
  200.  
  201. /***********************************************************/
  202. void setup()
  203. {
  204. #ifdef Master
  205. #ifdef DEBUG
  206.   Serial1.begin(9600);
  207.   Serial1.println("Bonjour");
  208. #endif
  209.  
  210.  
  211.   // Nextion
  212.   myNex.begin(9600); // Begin the object with a baud rate of 9600
  213.                      // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  214.   delay(500);        // Wait for Nextion to start
  215.  
  216.   pinMode(LoRa_CS, OUTPUT);
  217.  
  218.   initSPIFFS();
  219.   LoRa_Setup();
  220.   //dmx.initWrite(TOTAL_CHANNELS); // initialization for complete bus
  221.   RTC_Setup();
  222.  
  223.   ConsigneIdNext = 0;
  224.   lastConsigneIdNext = 0;
  225.   dmx.initWrite(TOTAL_CHANNELS); // initialization for complete bus
  226.   Start_eclairage();
  227.   // Watchdog
  228.   esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  229.   esp_task_wdt_add(NULL); //add current thread to WDT watch
  230. #ifdef DEBUG
  231.   Serial1.println("Setup done!");
  232. #endif
  233. #endif
  234. #if defined(C1) || defined(C2) || defined(C3) || defined(C4) || defined(C5)
  235.   Serial.begin(9600); // initialize Serial
  236.   while (!Serial)
  237.     ;
  238.   myLux.begin();
  239.   Serial.println("LoRa Duplex");
  240.   Serial.println(addr_LoRa);
  241.  
  242.   if (!LoRa.begin(433E6))
  243.   { // initialize ratio at 433 MHz
  244.     Serial.println("LoRa init failed. Check your connections.");
  245.     while (true)
  246.       ; // if failed, do nothing
  247.   }
  248.   Serial.println("LoRa init succeeded.");
  249.   delay(200);
  250. #endif
  251. #if defined(LoRa_Interface)
  252.   Serial.begin(9600); // initialize Serial
  253.   while (!Serial)
  254.     ;
  255.  
  256.   SPI.begin(LoRa_SCK, LoRa_MISO, LoRa_MOSI, LoRa_CS);
  257.  
  258.   LoRa.setPins(LoRa_CS, -1, DI0);
  259.  
  260.   Serial.println("LoRa Duplex");
  261.   Serial.println(addr_LoRa);
  262.  
  263.   if (!LoRa.begin(433E6))
  264.   { // initialize ratio at 433 MHz
  265.     Serial.println("LoRa init failed. Check your connections.");
  266.     while (true)
  267.       ; // if failed, do nothing
  268.   }
  269.   Serial.println("LoRa init succeeded.");
  270.   delay(200);
  271. #endif
  272. }
  273.  
  274. void loop()
  275. {
  276. #if defined(C1) || defined(C2) || defined(C3) || defined(C4) || defined(C5)
  277.   if (duree >= 75) // Toutes les 5 minutes
  278.   {
  279.     /* LECTURE DES SONDES NTC */
  280.  
  281.     /*VoA1 = analogRead(ThermistorPinA1);
  282.     vinA1 = ((VoA1 / 1024) * 5);
  283.     R2A1 = R1 * (1023.0 / (float)VoA1 - 1.0);
  284.     logR2A1 = log(R2A1);
  285.     TA1 = (1.0 / (A + B * logR2A1 + C * logR2A1 * logR2A1 * logR2A1)) - 273.15; // Steinhart and Hart Equation. T  = 1 / {A + B[ln(R)] + C[ln(R)]^3}
  286.  
  287.     VoA2 = analogRead(ThermistorPinA2);
  288.     R2A2 = R1 * (1023.0 / (float)VoA2 - 1.0);
  289.     logR2A2 = log(R2A2);
  290.     TA2 = (1.0 / (A + B * logR2A2 + C * logR2A2 * logR2A2 * logR2A2)) - 273.15; // Steinhart and Hart Equation. T  = 1 / {A + B[ln(R)] + C[ln(R)]^3}
  291.  
  292.     VoA3 = analogRead(ThermistorPinA3);
  293.     R2A3 = R1 * (1023.0 / (float)VoA3 - 1.0);
  294.     logR2A3 = log(R2A3);
  295.     TA3 = (1.0 / (A + B * logR2A3 + C * logR2A3 * logR2A3 * logR2A3)) - 273.15; // Steinhart and Hart Equation. T  = 1 / {A + B[ln(R)] + C[ln(R)]^3}
  296.     */
  297.  
  298.     float TA1 = 20.30;
  299.     float TempCA2 = 18.64;
  300.     float TempCA3 = 19.80;
  301.     lux = myLux.lightStrengthLux();
  302.  
  303.     /* LECTURE DES SONDES HIH3040 */
  304.  
  305.     /*int humiditySensor = analogRead(A4);
  306.       float humidityVoltage = (humiditySensor / resolution) * voltage;
  307.       float sensorRH = (humidityVoltage - rhOffset) / rhSlope;
  308.       float trueRH = sensorRH / (1.0546 - (0.00216 * TA1));*/
  309.  
  310.     float h = 62;
  311.     float t = 19;
  312.  
  313.     String message = "{\"TC_A1_" + addr_LoRa + "\":" + TA1 + ",\"TC_A2_" + addr_LoRa + "\":" + TempCA2 + ",\"TC_A3_" + addr_LoRa + "\":" + TempCA3 + ",\"Humidite_" + addr_LoRa + "\":" + h + ",\"TC_DHT22_" + addr_LoRa + "\":" + t + ",\"Lumiere_" + addr_LoRa + "\":" + lux + "}";
  314.     sendMessage(message);
  315.     Serial.println("Sending " + message);
  316.  
  317.     // parse for a packet, and call onReceive with the result:
  318.     // onReceive(LoRa.parsePacket());
  319.  
  320.     delay(400);
  321.     duree = 0;
  322.   }
  323.  
  324.   if (duree < 75) // arrêt pendant 5 minutes
  325.   {
  326.     duree++;
  327.     LoRa.sleep();
  328.     LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
  329.   }
  330. }
  331. #endif
  332. #ifdef LoRa_Interface
  333. if ((millis() - prevMillis3) > 2000)
  334. {
  335.   sendMessage(incoming1);
  336.   // Serial.println("Sending " + incoming1);
  337.   sendMessage(incoming2);
  338.   // Serial.println("Sending " + incoming2);
  339.   sendMessage(incoming3);
  340.   // Serial.println("Sending " + incoming3);
  341.   prevMillis3 = millis();
  342. }
  343.  
  344. // parse for a packet, and call onReceive with the result:
  345. onReceive(LoRa.parsePacket());
  346. }
  347.  
  348. void onReceive(int packetSize)
  349. {
  350.   if (packetSize == 0)
  351.     return; // if there's no packet, return
  352.  
  353.   // read packet header bytes:
  354.   int recipient = LoRa.read();       // recipient address
  355.   byte sender = LoRa.read();         // sender address
  356.   byte incomingMsgId = LoRa.read();  // incoming msg ID
  357.   byte incomingLength = LoRa.read(); // incoming msg length
  358.   incoming = "";
  359. #ifdef DEBUG
  360.   Serial.println("sender : " + String(sender, HEX));
  361. #endif
  362.   String adresse_LoRa = String(sender, HEX);
  363.  
  364.   while (LoRa.available())
  365.   {
  366.     incoming += (char)LoRa.read();
  367.   }
  368.  
  369.   if (incomingLength != incoming.length())
  370.   { // check length for error
  371. #ifdef DEBUG
  372.     Serial.println("error: message length does not match length");
  373. #endif
  374.     return; // skip rest of function
  375.   }
  376.  
  377.   // if the recipient isn't this device or broadcast,
  378.   if (recipient != localAddress && recipient != 0xFF)
  379.   {
  380. #ifdef DEBUG
  381.     Serial.println("This message is not for me.");
  382. #endif
  383.     return; // skip rest of function
  384.   }
  385.  
  386.   // if mdessage is for this device, or broadcast, print details:
  387.   Serial.print("{\"message\":");
  388.   Serial.print(incoming);
  389.   Serial.print(",\"Coordonees\":{\"Received_from\":\""); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
  390.   Serial.print("0x" + String(sender, HEX));
  391.   Serial.print("\",\"Message_ID\":");
  392.   Serial.print(String(incomingMsgId));
  393.   Serial.print(",\"Message_length\":");
  394.   Serial.print(String(incomingLength));
  395.   Serial.print(",\"RSSI\":");
  396.   Serial.print(String(LoRa.packetRssi()));
  397.   Serial.print(",\"Snr\":");
  398.   Serial.print(String(LoRa.packetSnr()));
  399.   Serial.println("}}");
  400.  
  401.   if (adresse_LoRa == "ab")
  402.   {
  403.     incoming1 = incoming;
  404.   }
  405.   if (adresse_LoRa == "ac")
  406.   {
  407.     incoming2 = incoming;
  408.   }
  409.   if (adresse_LoRa == "ad")
  410.   {
  411.     incoming3 = incoming;
  412.   }
  413. }
  414. #endif
  415.  
  416. #if defined(C1) || defined(C2) || defined(C3) || defined(C4) || defined(C5) || defined(LoRa_Interface)
  417. void sendMessage(String outgoing)
  418. {
  419.   LoRa.beginPacket();            // start packet
  420.   LoRa.write(destination);       // add destination address
  421.   LoRa.write(localAddress);      // add sender address
  422.   LoRa.write(msgCount);          // add message ID
  423.   LoRa.write(outgoing.length()); // add payload length
  424.   LoRa.print(outgoing);          // add payload
  425.   LoRa.endPacket();              // finish packet and send it
  426.   msgCount++;                    // increment message ID
  427. }
  428. #endif
  429.  
  430. #ifdef Master
  431. WatchDog();
  432. if (sd != 1)
  433. {
  434.   digitalWrite(LoRa_CS, Select); //  SELECT (low) LoRa SPI
  435.   onReceive(LoRa.parsePacket());
  436. }
  437. dmx.update();
  438. if (sd == 1)
  439. {
  440.   Write_SPIFFS();
  441.   sd = 0;
  442. }
  443. Nextion();
  444. Horloge();
  445. }
  446.  
  447. void WatchDog() // https://iotassistant.io/esp32/enable-hardware-watchdog-timer-esp32-arduino-ide/
  448. {
  449.   if (millis() - last >= 2000)
  450.   {
  451.     #ifdef DEBUG
  452.     Serial1.println("Resetting WDT...");
  453.     #endif
  454.     esp_task_wdt_reset();
  455.     last = millis();
  456.   }
  457. }
  458.  
  459. void onReceive(int packetSize)
  460. {
  461.   if (packetSize == 0)
  462.     return; // if there's no packet, return
  463.  
  464.   // read packet header bytes:
  465.   int recipient = LoRa.read();       // recipient address
  466.   byte sender = LoRa.read();         // sender address
  467.   byte incomingMsgId = LoRa.read();  // incoming msg ID
  468.   byte incomingLength = LoRa.read(); // incoming msg length
  469.   String incoming = "";
  470.  
  471.   while (LoRa.available())
  472.   {
  473.     incoming += (char)LoRa.read();
  474.   }
  475.  
  476.   if (incomingLength != incoming.length())
  477.   { // check length for error
  478. #ifdef DEBUG
  479.     Serial1.println("error: message length does not match length");
  480. #endif
  481.     return; // skip rest of function
  482.   }
  483.  
  484.   // if the recipient isn't this device or broadcast,
  485.   if (recipient != localAddress && recipient != 0xFF)
  486.   {
  487. #ifdef DEBUG
  488.     Serial1.println("This message is not for me.");
  489. #endif
  490.     return; // skip rest of function
  491.   }
  492.  
  493. // if mdessage is for this device, or broadcast, print details:
  494. #ifdef DEBUG
  495.   Serial1.print("{\"message\":");
  496.   Serial1.print(incoming);
  497.   Serial1.print(",\"Coordonees\":{\"Received_from\":\""); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
  498.   Serial1.print("0x" + String(sender, HEX));
  499.   Serial1.print("\",\"Message_ID\":");
  500.   Serial1.print(String(incomingMsgId));
  501.   Serial1.print(",\"Message_length\":");
  502.   Serial1.print(String(incomingLength));
  503.   Serial1.print(",\"RSSI\":");
  504.   Serial1.print(String(LoRa.packetRssi()));
  505.   Serial1.print(",\"Snr\":");
  506.   Serial1.print(String(LoRa.packetSnr()));
  507.   Serial1.println("}}");
  508. #endif
  509.   StaticJsonDocument<192> doc;
  510.  
  511.   DeserializationError error = deserializeJson(doc, incoming);
  512.  
  513.   if (error)
  514.   {
  515. #ifdef DEBUG
  516.     Serial1.print(F("deserializeJson() failed: "));
  517.     Serial1.println(error.f_str());
  518. #endif
  519.     return;
  520.   }
  521.  
  522.   TC_A1_C1M = doc["TC_A1_C1"];       // 20.3
  523.   TC_A2_C1M = doc["TC_A2_C1"];       // 18.64
  524.   TC_A3_C1M = doc["TC_A3_C1"];       // 19.8
  525.   Humidite_C1M = doc["Humidite_C1"]; // 62
  526.   TC_DHT22_C1M = doc["TC_DHT22_C1"]; // 19
  527.   if (doc["Lumiere_C1"] > 0)
  528.   {
  529.     luxmetre[0] = doc["Lumiere_C1"]; // 1669
  530.   }
  531.   if (doc["Lumiere_C2"] > 0)
  532.   {
  533.     luxmetre[1] = doc["Lumiere_C2"];
  534.   }
  535.  
  536.   if (doc["Lumiere_C3"] > 0)
  537.   {
  538.     luxmetre[2] = doc["Lumiere_C3"];
  539.   }
  540.   else
  541.   {
  542.   }
  543.  
  544.   Gestion_eclairage();
  545. }
  546.  
  547. void Gestion_eclairage()
  548. {
  549.   // Serial1.println((String) "FreeRam : " + esp_get_free_heap_size());
  550.  
  551.   byte zone[3][2] = {
  552.       {0, 4},
  553.       {4, 8},
  554.       {8, 12},
  555.   };
  556.  
  557. #ifdef DEBUG
  558.   Serial1.println((String) "luxmetre[0] : (Gestion_eclairage) " + luxmetre[0]);
  559. #endif
  560.   for (byte sonde = 0; sonde < 3; sonde++)
  561.   {
  562.     for (byte compartiment = zone[sonde][0]; compartiment < zone[sonde][1]; compartiment++)
  563.     {
  564.       if (heure >= heure_allumage[compartiment] && heure < heure_extinction[compartiment]) // partie concernant le jour
  565.  
  566.       {
  567.         if (luxmetre[sonde] < (consigne_declenchement[compartiment] - (consigne_zone_neutre[compartiment] / 2)) && luxmetre[sonde] != 0)
  568.         {
  569.           allumage[compartiment] = 1; // Cette partie permet l'extinction/allumage des lampes ce fasse unnqiuemenet si besoin et pas à chaque fois que les sondes envoient leur valeur
  570.         }
  571.         if (luxmetre[sonde] > (consigne_declenchement[compartiment] + (consigne_zone_neutre[compartiment] / 2)) && luxmetre[sonde] != 0)
  572.         {
  573.           allumage[compartiment] = 0;
  574.         }
  575.         if (allumage[compartiment] != allumage1[compartiment] || demarrage == 0)
  576.         {
  577.           for (byte adresse = adresse_DMX[compartiment][0]; adresse <= adresse_DMX[compartiment][1]; adresse++)
  578.           {
  579.             if (luxmetre[sonde] < (consigne_declenchement[compartiment] - (consigne_zone_neutre[compartiment] / 2)) && luxmetre[sonde] != 0)
  580.             {
  581.               dmx.write((adresse), (consigne_intensite[compartiment] * 2.55));
  582. #ifdef DEBUG
  583.               Serial1.println((String) "Compartiment " + (compartiment + 1) + " : ON ; Adresse : " + adresse + " intensite : " + consigne_intensite[compartiment]);
  584. #endif
  585.               allumage1[compartiment] = 1;
  586.             }
  587.             if (luxmetre[sonde] > (consigne_declenchement[compartiment] + (consigne_zone_neutre[compartiment] / 2)) && luxmetre[sonde] != 0)
  588.             {
  589.               dmx.write((adresse), 0);
  590. #ifdef DEBUG
  591.               Serial1.println((String) "Compartiment " + (compartiment + 1) + " : OFF; Adresse : " + adresse + " intensite : 0");
  592. #endif
  593.               allumage1[compartiment] = 0;
  594.             }
  595.           }
  596.           demarrage = 1; // permet de mettre en route l'eclairage unniquement au demarrage
  597.         }
  598.       }
  599.  
  600.       else // partie concernant la nuit
  601.       {
  602.         for (byte adresse = adresse_DMX[compartiment][0]; adresse < adresse_DMX[compartiment][1]; adresse++)
  603.         {
  604.           dmx.write((adresse), 0);
  605.           allumage1[compartiment] = 0;
  606.           demarrage = 1; // permet de mettre en route l'eclairage unniquement au demarrage
  607. #ifdef DEBUG
  608.           Serial1.println((String) "Adresse : " + adresse + " intensite : 0");
  609. #endif
  610.         }
  611.       }
  612.     }
  613.   }
  614. #ifdef DEBUG
  615.   Serial1.print((String) "Heure : " + heure);
  616.   Serial1.println((String) ":" + minute + ":" + seconde);
  617. #endif
  618. }
  619.  
  620. void Gestion_eclairage_Nextion()
  621. {
  622.   byte zone[3][2] = {
  623.       {0, 4},
  624.       {4, 8},
  625.       {8, 12},
  626.   };
  627.  
  628. #ifdef DEBUG
  629.   Serial1.println((String) "luxmetre[0] : (Gestion_eclairage) " + luxmetre[0]);
  630. #endif
  631.   for (byte sonde = 0; sonde < 3; sonde++)
  632.   {
  633.     // if (now.hour() >= heure_allumage[Compnextion - 1] && now.hour() < heure_extinction[Compnextion - 1]) // partie concernant le jour
  634.     if (heure >= heure_allumage[Compnextion - 1] && heure < heure_extinction[Compnextion - 1]) // partie concernant le jour
  635.     {
  636.       for (byte adresse = adresse_DMX[Compnextion - 1][0]; adresse <= adresse_DMX[Compnextion - 1][1]; adresse++)
  637.       {
  638.         if (luxmetre[sonde] < (consigne_declenchement[Compnextion - 1] - (consigne_zone_neutre[Compnextion - 1] / 2)) && luxmetre[sonde] != 0)
  639.         {
  640.           dmx.write((adresse), (consigne_intensite[Compnextion - 1] * 2.55));
  641. #ifdef DEBUG
  642.           Serial1.println((String) "Compnextion" + (Compnextion) + " : ON ; Adresse : " + adresse + " intensite : " + consigne_intensite[Compnextion - 1]);
  643. #endif
  644.           allumage1[Compnextion - 1] = 1;
  645.         }
  646.         if (luxmetre[sonde] > (consigne_declenchement[Compnextion - 1] + (consigne_zone_neutre[Compnextion - 1] / 2)) && luxmetre[sonde] != 0)
  647.         {
  648.           dmx.write((adresse), 0);
  649. #ifdef DEBUG
  650.           Serial1.println((String) "Compnextion" + (Compnextion) + " : OFF; Adresse : " + adresse + " intensite : 0");
  651. #endif
  652.           allumage1[Compnextion - 1] = 0;
  653.         }
  654.       }
  655.     }
  656.     else // partie concernant la nuit
  657.     {
  658.       for (byte adresse = adresse_DMX[Compnextion - 1][0]; adresse < adresse_DMX[Compnextion - 1][1]; adresse++)
  659.       {
  660.         dmx.write((adresse), 0);
  661.         allumage1[Compnextion - 1] = 0;
  662. #ifdef DEBUG
  663.         Serial1.println((String) "Adresse : " + adresse + " intensite : 0");
  664. #endif
  665.       }
  666.     }
  667.   }
  668. }
  669.  
  670. /*void Gestion_eclairage_Nextion_Zone_DMX()
  671. {
  672.  
  673. #ifdef DEBUG
  674.   Serial1.println((String) "luxmetre[0] : (Gestion_eclairage) " + luxmetre[0]);
  675. #endif
  676.   for (byte sonde = 0; sonde < 3; sonde++)
  677.   {
  678.     //if (now.hour() >= heure_allumage[Compnextion - 1] && now.hour() < heure_extinction[Compnextion - 1]) // partie concernant le jour
  679.     if (heure >= heure_allumage[Compnextion - 1] && heure < heure_extinction[Compnextion - 1]) // partie concernant le jour
  680.     {
  681.       for (byte adresse = adresse_DMX[Compnextion - 1][0]; adresse <= adresse_DMX[Compnextion - 1][1]; adresse++)
  682.       {
  683.         //if (luxmetre[sonde] < (consigne_declenchement[Compnextion - 1] - (consigne_zone_neutre[Compnextion - 1] / 2)) && luxmetre[sonde] != 0)
  684.         byte luminaire = adresse - adresse_DMX[Compnextion - 1][0]; // Peut-être rajouter "+1"
  685.  
  686.         if (luxmetre[sonde] < (consigne_declenchement[Compnextion - 1] - (consigne_zone_neutre[Compnextion - 1] / 2)) && luxmetre[sonde] != 0 && On_Off[Compnextion - 1][luminaire] == 1)
  687.         {
  688.           dmx.write((adresse), (consigne_intensite[Compnextion - 1] * 2.55));
  689. #ifdef DEBUG
  690.           Serial1.println((String) "Compnextion" + (Compnextion) + " : ON ; Adresse : " + adresse + " intensite : " + consigne_intensite[Compnextion - 1]);
  691. #endif
  692.           allumage1[Compnextion - 1] = 1;
  693.         }
  694.         if (luxmetre[sonde] > (consigne_declenchement[Compnextion - 1] + (consigne_zone_neutre[Compnextion - 1] / 2)) && luxmetre[sonde] != 0 || On_Off[Compnextion - 1][luminaire] == 0)
  695.         {
  696.           dmx.write((adresse), 0);
  697. #ifdef DEBUG
  698.           Serial1.println((String) "Compnextion" + (Compnextion) + " : OFF; Adresse : " + adresse + " intensite : 0");
  699. #endif
  700.           allumage1[Compnextion - 1] = 0;
  701.         }
  702.       }
  703.     }
  704.     else // partie concernant la nuit
  705.     {
  706.       for (byte adresse = adresse_DMX[Compnextion - 1][0]; adresse < adresse_DMX[Compnextion - 1][1]; adresse++)
  707.       {
  708.         dmx.write((adresse), 0);
  709.         allumage1[Compnextion - 1] = 0;
  710. #ifdef DEBUG
  711.         Serial1.println((String) "Adresse : " + adresse + " intensite : 0");
  712. #endif
  713.       }
  714.     }
  715.   }
  716. }*/
  717.  
  718. void Horloge()
  719. {
  720.   if ((millis() - prevMillis1) > 1000)
  721.   {
  722.     now = rtc.now();
  723.     heure = now.hour();
  724.     minute = now.minute();
  725.     seconde = now.second();
  726.     myNex.writeNum("Heure.val", heure);
  727.     myNex.writeNum("Minute.val", minute);
  728.     myNex.writeNum("Seconde.val", seconde);
  729.  
  730.     prevMillis1 = millis();
  731.   }
  732. }
  733.  
  734. void Nextion()
  735. {
  736.   if ((millis() - prevMillis) > 100)
  737.   {
  738.     bool HeureNextActiv;
  739.     String stringFromNextion;
  740.     bool OKNext;
  741.     bool lastOKNext;
  742.     bool lastAdminNext;
  743.  
  744.     AdminNext = myNex.readNumber("Mot_De_Passe.mdp.val");
  745.     if (AdminNext != 777777)
  746.     {
  747.       lastAdminNext = AdminNext;
  748.     }
  749.     else if (AdminNext == 777777 && AdminNext == 49)
  750.     {
  751.       AdminNext = lastAdminNext;
  752.     }
  753.  
  754.     ConsigneIdNext = myNex.readNumber("Nombre.ConsigneId.val");
  755.     if (ConsigneIdNext != 777777)
  756.     {
  757.       lastConsigneIdNext = ConsigneIdNext;
  758.     }
  759.     else if (ConsigneIdNext == 777777 && ConsigneIdNext == 49)
  760.     {
  761.       ConsigneIdNext = lastConsigneIdNext;
  762.     }
  763.  
  764.     if (ConsigneIdNext == 0 || ConsigneIdNext == 30)
  765.     {
  766.       Compnextion = myNex.readNumber("Accueil.compartiment.val"); // We read the value of n0 and store it to Compnextion variable
  767.       if (Compnextion != 777777)
  768.       {                                // 777777 is the return value if the code fails to read the new value
  769.         lastCompnextion = Compnextion; // The chances of getting a wrong value is one in a million.
  770.         // Use this if() to ensure it if you believe it is needed.
  771.         // You can either call the readCompnextion funtion again
  772.         // or set a safe value in case of failure.
  773.         // Ex: Compnextion = 2222; or use the last value method
  774.       }
  775.       else if (Compnextion == 777777)
  776.       {
  777.         Compnextion = lastCompnextion;
  778.       }
  779.     }
  780.  
  781.     if (Compnextion == 0 && actualiser == 1)
  782.     {
  783.       myNex.writeStr("page Accueil");
  784.       actualiser = 0;
  785.     }
  786.  
  787.     if (AdminNext == 1)
  788.     {
  789.       temps_admin++;
  790.       if (temps_admin >= (((2 * 60) * 100) / 25)) // evniron 2 minute
  791.       {
  792.         myNex.writeStr("Mot_De_Passe.mdp.val=0");
  793.         myNex.writeStr("page Accueil");
  794.         temps_admin = 0;
  795.       }
  796.     }
  797.     else
  798.     {
  799.       temps_admin = 0;
  800.     }
  801.     if (Compnextion > 0 && Compnextion < 50)
  802.     {
  803. #ifdef DEBUG
  804.       Serial1.println((String) "Compnextion : " + Compnextion);
  805. #endif
  806.  
  807.       if (ConsigneIdNext > 0 && ConsigneIdNext < 30)
  808.       {
  809.         OKNext = myNex.readNumber("Nombre.OK.val");
  810.         if (OKNext != 777777)
  811.         {
  812.           lastOKNext = OKNext;
  813.         }
  814.         else if (OKNext == 777777 && OKNext == 49)
  815.         {
  816.           OKNext = lastOKNext;
  817.         }
  818. #ifdef DEBUG
  819.         Serial1.println((String) "OKNext : " + OKNext);
  820. #endif
  821.       }
  822. #ifdef DEBUG
  823.       Serial1.println((String) "ConsigneIdNext : " + ConsigneIdNext);
  824. #endif
  825.  
  826.       if (ConsigneIdNext == 0)
  827.       {
  828.         NextionPageReglages();
  829.       }
  830.       if (ConsigneIdNext == 1)
  831.       {
  832.         myNex.writeStr("t2.txt", "Consigne allumage");
  833.         stringFromNextion = myNex.readStr("t1.txt");
  834.         char charBuf[50];
  835.         stringFromNextion.toCharArray(charBuf, 50);
  836.         if (atol(charBuf) > 0 && OKNext == 1)
  837.         {
  838.           consigne_declenchement[Compnextion - 1] = atol(charBuf);
  839.           NextionPageReglages();
  840.           Gestion_eclairage_Nextion();
  841.           sd = 1;
  842.           myNex.writeNum("Nombre.OK.val", 0);
  843.           OKNext = 0;
  844.         }
  845.       }
  846.       if (ConsigneIdNext == 2)
  847.       {
  848.         myNex.writeStr("t2.txt", "Consigne Zone Neutre");
  849.         stringFromNextion = myNex.readStr("t1.txt");
  850.         char charBuf[50];
  851.         stringFromNextion.toCharArray(charBuf, 50);
  852.         if (atol(charBuf) > 0 && atol(charBuf) < 10000 && OKNext == 1)
  853.         {
  854.           consigne_zone_neutre[Compnextion - 1] = atol(charBuf);
  855.           NextionPageReglages();
  856.           Gestion_eclairage_Nextion();
  857.           sd = 1;
  858.           myNex.writeNum("Nombre.OK.val", 0);
  859.           OKNext = 0;
  860.         }
  861.       }
  862.       if (ConsigneIdNext == 3)
  863.       {
  864.         myNex.writeStr("t2.txt", "Consigne intensite");
  865.         stringFromNextion = myNex.readStr("t1.txt");
  866.         char charBuf[50];
  867.         stringFromNextion.toCharArray(charBuf, 50);
  868.         if (atol(charBuf) > 0 && atol(charBuf) <= 100 && OKNext == 1)
  869.         {
  870.           consigne_intensite[Compnextion - 1] = atol(charBuf);
  871.           NextionPageReglages();
  872.           Gestion_eclairage_Nextion();
  873.           sd = 1;
  874.           myNex.writeNum("Nombre.OK.val", 0);
  875.           OKNext = 0;
  876.         }
  877.       }
  878.       if (ConsigneIdNext == 4)
  879.       {
  880.         myNex.writeStr("t2.txt", "Heure d'allumage");
  881.         stringFromNextion = myNex.readStr("t1.txt");
  882.         char charBuf[50];
  883.         stringFromNextion.toCharArray(charBuf, 50);
  884.         if (atol(charBuf) > 2 && atol(charBuf) < heure_extinction[Compnextion - 1])
  885.         {
  886.           HeureNextActiv = 1;
  887.         }
  888.         else
  889.         {
  890.           HeureNextActiv = 0;
  891.         }
  892.         if (atol(charBuf) > 0 && atol(charBuf) <= 24 && HeureNextActiv == 1 && OKNext == 1)
  893.         {
  894.           heure_allumage[Compnextion - 1] = atol(charBuf);
  895.           NextionPageReglages();
  896.           Gestion_eclairage_Nextion();
  897.           sd = 1;
  898.           myNex.writeNum("Nombre.OK.val", 0);
  899.           OKNext = 0;
  900.         }
  901.       }
  902.       if (ConsigneIdNext == 5)
  903.       {
  904.         myNex.writeStr("t2.txt", "Heure extinction");
  905.         stringFromNextion = myNex.readStr("t1.txt");
  906.         char charBuf[50];
  907.         stringFromNextion.toCharArray(charBuf, 50);
  908.         if (atol(charBuf) > heure_allumage[Compnextion - 1])
  909.         {
  910.           HeureNextActiv = 1;
  911.         }
  912.         else
  913.         {
  914.           HeureNextActiv = 0;
  915.         }
  916.         if (atol(charBuf) > 0 && atol(charBuf) <= 24 && HeureNextActiv == 1 && OKNext == 1)
  917.         {
  918.           heure_extinction[Compnextion - 1] = atol(charBuf);
  919.           NextionPageReglages();
  920.           Gestion_eclairage_Nextion();
  921.           sd = 1;
  922.           myNex.writeNum("Nombre.OK.val", 0);
  923.           OKNext = 0;
  924.         }
  925.       }
  926.       /*if (ConsigneIdNext == 6)
  927.       {
  928.         //NextionPageON_OFF();
  929.  
  930.         On_Off[Compnextion - 1][0] = myNex.readNumber("sw0.val");
  931.         On_Off[Compnextion - 1][1] = myNex.readNumber("sw1.val");
  932.         On_Off[Compnextion - 1][2] = myNex.readNumber("sw2.val");
  933.         On_Off[Compnextion - 1][3] = myNex.readNumber("sw3.val");
  934.         On_Off[Compnextion - 1][4] = myNex.readNumber("sw4.val");
  935.         On_Off[Compnextion - 1][5] = myNex.readNumber("sw5.val");
  936.         On_Off[Compnextion - 1][6] = myNex.readNumber("sw6.val");
  937.         On_Off[Compnextion - 1][7] = myNex.readNumber("sw7.val");
  938.         On_Off[Compnextion - 1][8] = myNex.readNumber("sw8.val");
  939.         On_Off[Compnextion - 1][9] = myNex.readNumber("sw9.val");
  940.         On_Off[Compnextion - 1][10] = myNex.readNumber("sw10.val");
  941.         On_Off[Compnextion - 1][11] = myNex.readNumber("sw11.val");
  942.         On_Off[Compnextion - 1][12] = myNex.readNumber("sw12.val");
  943.         On_Off[Compnextion - 1][13] = myNex.readNumber("sw13.val");
  944.         On_Off[Compnextion - 1][14] = myNex.readNumber("sw14.val");
  945.         On_Off[Compnextion - 1][15] = myNex.readNumber("sw15.val");
  946.         On_Off[Compnextion - 1][16] = myNex.readNumber("sw16.val");
  947.         On_Off[Compnextion - 1][17] = myNex.readNumber("sw17.val");
  948.         On_Off[Compnextion - 1][18] = myNex.readNumber("sw18.val");
  949.         On_Off[Compnextion - 1][19] = myNex.readNumber("sw19.val");
  950.         On_Off[Compnextion - 1][20] = myNex.readNumber("sw20.val");
  951.         On_Off[Compnextion - 1][21] = myNex.readNumber("sw21.val");
  952.         On_Off[Compnextion - 1][22] = myNex.readNumber("sw22.val");
  953.         On_Off[Compnextion - 1][23] = myNex.readNumber("sw23.val");
  954.         On_Off[Compnextion - 1][24] = myNex.readNumber("sw24.val");
  955.         On_Off[Compnextion - 1][25] = myNex.readNumber("sw25.val");
  956.         On_Off[Compnextion - 1][26] = myNex.readNumber("sw26.val");
  957.         On_Off[Compnextion - 1][27] = myNex.readNumber("sw27.val");
  958.         On_Off[Compnextion - 1][28] = myNex.readNumber("sw28.val");
  959.         On_Off[Compnextion - 1][29] = myNex.readNumber("sw29.val");
  960.  
  961.         for (byte luminaire = 0; luminaire < 30; luminaire++)
  962.         {
  963.           //String Switch1 = String("sw" + luminaire);
  964.           //String Switch = String(Switch1 + ".val");
  965.           //On_Off[Compnextion - 1][luminaire] = myNex.readNumber("sw0.val");
  966.           //On_Off[Compnextion - 1][luminaire] = myNex.readNumber(Switch);
  967.           if (On_Off[Compnextion - 1][luminaire] != 777777)
  968.           {
  969.             lastOn_Off[Compnextion - 1][luminaire] = On_Off[Compnextion - 1][luminaire];
  970.           }
  971.           else if (On_Off[Compnextion - 1][luminaire] == 777777)
  972.           {
  973.             On_Off[Compnextion - 1][luminaire] = lastOn_Off[Compnextion - 1][luminaire];
  974.           }
  975.         }
  976.  
  977.         if (OKNext == 1)
  978.         {
  979.           //NextionPageON_OFF();
  980.           NextionPageReglages();
  981.           Gestion_eclairage_Nextion_Zone_DMX();
  982.           //sd = 1;
  983.           myNex.writeNum("Nombre.OK.val", 0);
  984.           OKNext = 0;
  985.         }
  986.       }*/
  987.  
  988.       if (Compnextion >= 1 && Compnextion <= 4)
  989.       {
  990.         myNex.writeNum("nLux.val", luxmetre[0]);
  991.       }
  992.       if (Compnextion >= 5 && Compnextion <= 8)
  993.       {
  994.         myNex.writeNum("nLux.val", luxmetre[1]);
  995.       }
  996.       if (Compnextion >= 9 && Compnextion <= 12)
  997.       {
  998.         myNex.writeNum("nLux.val", luxmetre[2]);
  999.       }
  1000.     }
  1001.     prevMillis = millis();
  1002.   }
  1003. }
  1004.  
  1005. void NextionPageReglages()
  1006. {
  1007.   myNex.writeStr("page Reglages");
  1008.   myNex.writeNum("nCall.val", (consigne_declenchement[Compnextion - 1]));
  1009.   myNex.writeNum("nCZN.val", (consigne_zone_neutre[Compnextion - 1]));
  1010.   myNex.writeNum("nCInt.val", (consigne_intensite[Compnextion - 1]));
  1011.   myNex.writeNum("nCHa.val", (heure_allumage[Compnextion - 1]));
  1012.   myNex.writeNum("nCHe.val", (heure_extinction[Compnextion - 1]));
  1013.   myNex.writeNum("nAllumage.val", (consigne_declenchement[Compnextion - 1] - (consigne_zone_neutre[Compnextion - 1] / 2)));
  1014.   myNex.writeNum("nExtinction.val", (consigne_declenchement[Compnextion - 1] + (consigne_zone_neutre[Compnextion - 1] / 2)));
  1015.   myNex.writeNum("Nombre.ConsigneId.val", 30);
  1016. }
  1017.  
  1018. /*void NextionPageON_OFF()
  1019. {
  1020.   if (Compnextion == 1)
  1021.   {
  1022.     myNex.writeStr("page ON_OFF C1");
  1023.     myNex.writeNum("n0.val", Compnextion);
  1024.   }
  1025.   if (Compnextion >= 2 && Compnextion <= 9)
  1026.   {
  1027.     myNex.writeStr("page ON_OFF C2_C9");
  1028.     myNex.writeNum("n0.val", Compnextion);
  1029.   }
  1030.   if (Compnextion == 10)
  1031.   {
  1032.     myNex.writeStr("page ON_OFF C10");
  1033.     myNex.writeNum("n0.val", Compnextion);
  1034.   }
  1035.   if (Compnextion == 11)
  1036.   {
  1037.     myNex.writeStr("page ON_OFF C12");
  1038.     myNex.writeNum("n0.val", Compnextion);
  1039.   }
  1040.   if (Compnextion == 12)
  1041.   {
  1042.     myNex.writeStr("page ON_OFF C12");
  1043.     myNex.writeNum("n0.val", Compnextion);
  1044.   }
  1045.  
  1046.   myNex.writeNum("sw0.val", (On_Off[Compnextion - 1][0]));
  1047.   myNex.writeNum("sw1.val", (On_Off[Compnextion - 1][1]));
  1048.   myNex.writeNum("sw2.val", (On_Off[Compnextion - 1][2]));
  1049.   myNex.writeNum("sw3.val", (On_Off[Compnextion - 1][3]));
  1050.   myNex.writeNum("sw10.val", (On_Off[Compnextion - 1][10]));
  1051.   myNex.writeNum("sw11.val", (On_Off[Compnextion - 1][11]));
  1052.   myNex.writeNum("sw12.val", (On_Off[Compnextion - 1][12]));
  1053.   myNex.writeNum("sw13.val", (On_Off[Compnextion - 1][13]));
  1054.   myNex.writeNum("sw14.val", (On_Off[Compnextion - 1][14]));
  1055.   myNex.writeNum("sw15.val", (On_Off[Compnextion - 1][15]));
  1056.   myNex.writeNum("sw16.val", (On_Off[Compnextion - 1][16]));
  1057.   myNex.writeNum("sw17.val", (On_Off[Compnextion - 1][17]));
  1058.   myNex.writeNum("sw18.val", (On_Off[Compnextion - 1][18]));
  1059.   myNex.writeNum("sw19.val", (On_Off[Compnextion - 1][19]));
  1060.   myNex.writeNum("sw20.val", (On_Off[Compnextion - 1][20]));
  1061.   myNex.writeNum("sw21.val", (On_Off[Compnextion - 1][21]));
  1062.   myNex.writeNum("sw22.val", (On_Off[Compnextion - 1][22]));
  1063.   myNex.writeNum("sw23.val", (On_Off[Compnextion - 1][23]));
  1064.   myNex.writeNum("sw24.val", (On_Off[Compnextion - 1][24]));
  1065.   myNex.writeNum("sw25.val", (On_Off[Compnextion - 1][25]));
  1066.   myNex.writeNum("sw26.val", (On_Off[Compnextion - 1][26]));
  1067.   myNex.writeNum("sw27.val", (On_Off[Compnextion - 1][27]));
  1068.   myNex.writeNum("sw28.val", (On_Off[Compnextion - 1][28]));
  1069.   myNex.writeNum("sw29.val", (On_Off[Compnextion - 1][29]));
  1070.   //myNex.writeNum("Nombre.ConsigneId.val", 30);
  1071. }*/
  1072.  
  1073. void RTC_Setup()
  1074. {
  1075.   if (!rtc.begin())
  1076.   {
  1077. #ifdef DEBUG
  1078.     Serial1.println("Couldn't find RTC");
  1079. #endif
  1080.     Serial.flush();
  1081.     while (1)
  1082.       delay(10);
  1083.   }
  1084.  
  1085.   if (rtc.lostPower())
  1086.   {
  1087. #ifdef DEBUG
  1088.     Serial1.println("RTC lost power, let's set the time!");
  1089. #endif
  1090.     // When time needs to be set on a new device, or after a power loss, the
  1091.     // following line sets the RTC to the date & time this sketch was compiled
  1092.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  1093.     // This line sets the RTC with an explicit date & time, for example to set
  1094.     // January 21, 2014 at 3am you would call:
  1095.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  1096.   }
  1097. }
  1098.  
  1099. void LoRa_Setup()
  1100. {
  1101.   //  now test the LoRa
  1102.   SPI.begin(CLK, MISO, MOSI, LoRa_CS);
  1103.  
  1104.   LoRa.setPins(LoRa_CS, -1, DI0);
  1105.   digitalWrite(LoRa_CS, Select); //  SELECT (low) LoRa SPI
  1106. #ifdef DEBUG
  1107.   Serial1.println("LoRa Sender");
  1108. #endif
  1109.   if (!LoRa.begin(BAND))
  1110.   {
  1111. #ifdef DEBUG
  1112.     Serial1.println("Starting LoRa failed!");
  1113. #endif
  1114.     // now what?
  1115.   }
  1116.   else
  1117.   {
  1118. #ifdef DEBUG
  1119.     Serial1.println("LoRa Initial OK!");
  1120. #endif
  1121.     delay(1000);
  1122.   }
  1123. }
  1124.  
  1125. void Start_eclairage()
  1126. {
  1127.   // Serial1.println((String) "FreeRam : " + esp_get_free_heap_size());
  1128.  
  1129. #ifdef DEBUG
  1130.   Serial1.println((String) "luxmetre[0] : (Gestion_eclairage) " + luxmetre[0]);
  1131. #endif
  1132.  
  1133.   for (byte compartiment = 0; compartiment < 12; compartiment++)
  1134.   {
  1135.     if (now.hour() >= heure_allumage[compartiment] && now.hour() < heure_extinction[compartiment]) // partie concernant le jour
  1136.     {
  1137.       for (byte adresse = adresse_DMX[compartiment][0]; adresse <= adresse_DMX[compartiment][1]; adresse++)
  1138.       {
  1139.         dmx.write((adresse), (consigne_intensite[compartiment] * 2.55));
  1140. #ifdef DEBUG
  1141.         Serial1.println((String) "Compartiment " + (compartiment + 1) + " : ON ; Adresse : " + adresse + " intensite : " + consigne_intensite[compartiment]);
  1142. #endif
  1143.         allumage1[compartiment] = 1;
  1144.       }
  1145.     }
  1146.  
  1147.     else // partie concernant la nuit
  1148.     {
  1149.       for (byte adresse = adresse_DMX[compartiment][0]; adresse < adresse_DMX[compartiment][1]; adresse++)
  1150.       {
  1151.         dmx.write((adresse), 0);
  1152.         allumage1[compartiment] = 0;
  1153.         demarrage = 1; // permet de mettre en route l'eclairage unniquement au demarrage
  1154. #ifdef DEBUG
  1155.         Serial1.println((String) "Adresse : " + adresse + " intensite : 0");
  1156. #endif
  1157.       }
  1158.     }
  1159.   }
  1160. #ifdef DEBUG
  1161.   Serial1.print((String) "Heure : " + heure);
  1162.   Serial1.println((String) ":" + minute + ":" + seconde);
  1163. #endif
  1164. }
  1165.  
  1166. // Initialize SPIFFS
  1167. void initSPIFFS()
  1168. {
  1169.   if (!SPIFFS.begin(true))
  1170.   {
  1171. #ifdef DEBUG
  1172.     Serial1.println("An error has occurred while mounting SPIFFS");
  1173. #endif
  1174.   }
  1175. #ifdef DEBUG
  1176.   Serial1.println("SPIFFS mounted successfully");
  1177. #endif
  1178.  
  1179.   File root = SPIFFS.open("/");
  1180.   File file = root.openNextFile();
  1181.  
  1182.   while (file)
  1183.   {
  1184. #ifdef DEBUG
  1185.     Serial1.print("File: ");
  1186.     Serial1.println(file.name());
  1187. #endif
  1188.     file.close();
  1189.     file = root.openNextFile();
  1190.   }
  1191.  
  1192.   long number;
  1193.   String temp;
  1194.  
  1195.   for (byte i = 0; i < 12; i++)
  1196.   {
  1197.     for (byte consigne_SPIFFS = 0; consigne_SPIFFS < 5; consigne_SPIFFS++)
  1198.     {
  1199.       if (consigne_SPIFFS == 0)
  1200.       {
  1201.         temp = "/c_decl";
  1202.       }
  1203.       if (consigne_SPIFFS == 1)
  1204.       {
  1205.         temp = "/c_inte";
  1206.       }
  1207.       if (consigne_SPIFFS == 2)
  1208.       {
  1209.         temp = "/c_z_n";
  1210.       }
  1211.       if (consigne_SPIFFS == 3)
  1212.       {
  1213.         temp = "/h_all";
  1214.       }
  1215.       if (consigne_SPIFFS == 4)
  1216.       {
  1217.         temp = "/h_ext";
  1218.       }
  1219.  
  1220.       temp.concat(i);
  1221.       temp.concat(".txt");
  1222.       char FileName[temp.length() + 1];
  1223.       temp.toCharArray(FileName, sizeof(FileName));
  1224.       String fichier = readFile(SPIFFS, FileName);
  1225.       if (fichier)
  1226.       {
  1227. #ifdef DEBUG
  1228.         // Serial1.println((String) "Reading to " + FileName);
  1229. #endif
  1230.         number = fichier.toInt();
  1231. #ifdef DEBUG
  1232.         Serial1.println(number);
  1233. #endif
  1234.         if (number > 0 && consigne_SPIFFS == 0)
  1235.         {
  1236.           consigne_declenchement[i] = number;
  1237. #ifdef DEBUG
  1238.           Serial1.println((String) "long consigne_declenchement : " + consigne_declenchement[i]);
  1239. #endif
  1240.         }
  1241.         if (number > 0 && consigne_SPIFFS == 1)
  1242.         {
  1243.           consigne_intensite[i] = number;
  1244. #ifdef DEBUG
  1245.           Serial1.println((String) "long consigne_intensite : " + consigne_intensite[i]);
  1246. #endif
  1247.         }
  1248.         if (number > 0 && consigne_SPIFFS == 2)
  1249.         {
  1250.           consigne_zone_neutre[i] = number;
  1251. #ifdef DEBUG
  1252.           Serial1.println((String) "long consigne_zone_neutre : " + consigne_zone_neutre[i]);
  1253. #endif
  1254.         }
  1255.         if (number > 0 && consigne_SPIFFS == 3)
  1256.         {
  1257.           heure_allumage[i] = number;
  1258. #ifdef DEBUG
  1259.           Serial1.println((String) "long heure_allumage : " + heure_allumage[i]);
  1260. #endif
  1261.         }
  1262.         if (number > 0 && consigne_SPIFFS == 4)
  1263.         {
  1264.           heure_extinction[i] = number;
  1265. #ifdef DEBUG
  1266.           Serial1.println((String) "long heure_extinction : " + heure_extinction[i]);
  1267. #endif
  1268.         }
  1269.         // close the file:
  1270.         file.close();
  1271.       }
  1272.       else
  1273.       {
  1274. // if the file didn't open, print an error:
  1275. #ifdef DEBUG
  1276.         Serial1.println((String) "error opening : " + FileName);
  1277. #endif
  1278.       }
  1279.     }
  1280.   }
  1281. }
  1282.  
  1283. void Write_SPIFFS()
  1284. {
  1285.   char cstr[16];
  1286.   String temp = "/c_decl";
  1287.   temp.concat(Compnextion - 1);
  1288.   temp.concat(".txt");
  1289.   char FileName[temp.length() + 1];
  1290.   temp.toCharArray(FileName, sizeof(FileName));
  1291.   itoa(consigne_declenchement[Compnextion - 1], cstr, 10); // https://arduino.stackexchange.com/questions/42986/convert-int-to-char
  1292.   writeFile(SPIFFS, FileName, cstr);
  1293.  
  1294.   temp = "/c_inte";
  1295.   temp.concat(Compnextion - 1);
  1296.   temp.concat(".txt");
  1297.   FileName[temp.length() + 1];
  1298.   temp.toCharArray(FileName, sizeof(FileName));
  1299.   itoa(consigne_intensite[Compnextion - 1], cstr, 10);
  1300.   writeFile(SPIFFS, FileName, cstr);
  1301.  
  1302.   temp = "/c_z_n";
  1303.   temp.concat(Compnextion - 1);
  1304.   temp.concat(".txt");
  1305.   FileName[temp.length() + 1];
  1306.   temp.toCharArray(FileName, sizeof(FileName));
  1307.   itoa(consigne_zone_neutre[Compnextion - 1], cstr, 10);
  1308.   writeFile(SPIFFS, FileName, cstr);
  1309.  
  1310.   temp = "/h_all";
  1311.   temp.concat(Compnextion - 1);
  1312.   temp.concat(".txt");
  1313.   FileName[temp.length() + 1];
  1314.   temp.toCharArray(FileName, sizeof(FileName));
  1315.   itoa(heure_allumage[Compnextion - 1], cstr, 10);
  1316.   writeFile(SPIFFS, FileName, cstr);
  1317.  
  1318.   temp = "/h_ext";
  1319.   temp.concat(Compnextion - 1);
  1320.   temp.concat(".txt");
  1321.   FileName[temp.length() + 1];
  1322.   temp.toCharArray(FileName, sizeof(FileName));
  1323.   itoa(heure_extinction[Compnextion - 1], cstr, 10);
  1324.   writeFile(SPIFFS, FileName, cstr);
  1325. }
  1326.  
  1327. // Read File from SPIFFS
  1328. String readFile(fs::FS &fs, const char *path)
  1329. {
  1330. #ifdef DEBUG
  1331.   Serial1.printf("Reading file: %s", path);
  1332. #endif
  1333.   File file = fs.open(path);
  1334.   if (!file || file.isDirectory())
  1335.   {
  1336. #ifdef DEBUG
  1337.     Serial1.println("- failed to open file for reading");
  1338. #endif
  1339.     return String();
  1340.   }
  1341.   String fileContent;
  1342.   while (file.available())
  1343.   {
  1344.     fileContent = file.readString();
  1345.     break;
  1346.   }
  1347.   return fileContent;
  1348. }
  1349.  
  1350. // Write file to SPIFFS
  1351. void writeFile(fs::FS &fs, const char *path, const char *message)
  1352. {
  1353.   Serial1.printf("Writing file: %srn", path);
  1354.   File file = fs.open(path, FILE_WRITE);
  1355.   if (!file)
  1356.   {
  1357. #ifdef DEBUG
  1358.     Serial1.println("- failed to open file for writing");
  1359. #endif
  1360.     return;
  1361.   }
  1362.   if (file.print(message))
  1363.   {
  1364. #ifdef DEBUG
  1365.     Serial1.println("- file written");
  1366. #endif
  1367.   }
  1368.   else
  1369.   {
  1370. #ifdef DEBUG
  1371.     Serial1.println("- frite failed");
  1372. #endif
  1373.   }
  1374. }
  1375.  
  1376. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement