Advertisement
Justinberger

Arrosage1_03.04.2020.ino

Aug 22nd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.72 KB | None | 0 0
  1.  
  2. //https://www.youtube.com/watch?v=SWirrQH-48c
  3. //https://recitmst.qc.ca/arduino/construire-sa-sonde-dhumidite-du-sol/?fbclid=IwAR2Oi_5zf6TPedipApf_IFJ9490UGdbTzNldNbT3DhHfQ4ulwnL1MD60Ig4
  4. //http://gardenbot.org/howTo/soilMoisture/
  5.  
  6. //_____________!!!!Utilisation de la carte W5100!!!!________________//
  7. //Débrancher le câble réseau
  8. //Apuyer sur le bouton reset
  9. //Attendre 10 secondes
  10. //Rebrancher le câble réseau
  11.  
  12. //#include <SPI.h>          // needed for Arduino versions later than 0018
  13. //#include <Ethernet2.h>    //!!!ATTENTTION!!! en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t _cspin" dans la bibliothéque
  14. #include <Ethernet.h> //!!!ATTENTTION!!! en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t sspin" dans la bibliothéque
  15. //#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  16. //#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  17. #include <EthernetUdp.h>
  18. #include <ArduinoJson.h>
  19. //#include <EEPROM.h>
  20.  
  21. // Enter a MAC address and IP address for your controller below.
  22. // The IP address will be dependent on your local network:
  23. byte mac[] = {
  24.     0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  25. IPAddress ip(192, 168, 1, 177);
  26.  
  27. unsigned int localPort = 8888; // local port to listen on
  28. #define MAX_SIZE 400
  29.  
  30. int pin_zone_1 = 2;
  31. int pin_zone_2 = 3;
  32. int pin_zone_3 = 4;
  33. int pin_zone_4 = 5;
  34. int pin_zone_5 = 6;
  35. int pin_zone_6 = 7;
  36. int pin_mesure = 14;
  37. int pin_zone;
  38.  
  39. int arrosage_0_droite_0_zone_1;
  40. int arrosage_0_droite_0_zone_2;
  41. int arrosage_0_gauche_0_zone_1;
  42. int arrosage_0_gauche_0_zone_2;
  43. int HS_Z1;
  44. int HS_Z2;
  45. int HS_Z3;
  46. int HS_Z4;
  47. int n = 0;
  48. int CA = 0; // Cycle d'arrosage
  49. int CA1 = 0;
  50.  
  51. boolean stop_arrosage_zone_1;
  52. boolean stop_arrosage_zone_2;
  53.  
  54. // buffers for receiving and sending data
  55. char packetBuffer[MAX_SIZE]; //buffer to hold incoming packet,
  56. //char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  57. char ReplyBuffer[] = "acknowledged"; // a string to send back
  58.  
  59. // An EthernetUDP instance to let us send and receive packets over UDP
  60. EthernetUDP Udp;
  61.  
  62. void setup()
  63. {
  64.   // start the Ethernet and UDP:
  65.   Ethernet.begin(mac, ip);
  66.   Udp.begin(localPort);
  67.   Serial.begin(9600);
  68.  
  69.   pinMode(pin_zone_1, OUTPUT);
  70.   pinMode(pin_zone_2, OUTPUT);
  71.   pinMode(pin_zone_3, OUTPUT);
  72.   pinMode(pin_zone_4, OUTPUT);
  73.   pinMode(pin_zone_5, OUTPUT);
  74.   pinMode(pin_zone_6, OUTPUT);
  75.   pinMode(pin_mesure, OUTPUT);
  76.   pinMode(pin_zone, OUTPUT);
  77.  
  78.   digitalWrite(pin_zone_1, HIGH);
  79.   digitalWrite(pin_zone_2, HIGH);
  80.   digitalWrite(pin_zone_3, HIGH);
  81.   digitalWrite(pin_zone_4, HIGH);
  82.   digitalWrite(pin_zone_5, HIGH);
  83.   digitalWrite(pin_zone_6, HIGH);
  84.   digitalWrite(pin_mesure, HIGH);
  85.  
  86.   Serial.println("OK Test");
  87.  
  88.   /*   int valeur_1 = 42;
  89.   EEPROM.put(0, valeur_1);
  90.   float valeur_2 = 13.37;
  91.   EEPROM.put(2, valeur_2); // Un int fait deux octets, l'adresse est donc de 0 + 2 = 2
  92.  
  93.   int valeur_lue_1;
  94.   EEPROM.get(0, valeur_lue_1);
  95.   Serial.print("Valeur 1 = ");
  96.   Serial.println(valeur_lue_1);
  97.  
  98.   float valeur_lue_2;
  99.   EEPROM.get(2, valeur_lue_2);
  100.   Serial.print("Valeur 2 = ");
  101.   Serial.println(valeur_lue_2);  */
  102. }
  103.  
  104. unsigned long prevMillis4 = 0;
  105.  
  106. unsigned long prevMillis = 0;
  107. unsigned long prevMillis1 = 0;
  108.  
  109. int temps_darrosage = 5000;
  110. int declenchement_arrosage = 10000;
  111.  
  112. void loop()
  113. {
  114.   //__________Compteur_________________________//
  115.  
  116.   if ((millis() - prevMillis4) > 1000)
  117.   {
  118.     prevMillis4 = millis();
  119.     n++;
  120.     CA++;
  121.   }
  122.  
  123.   Serial.print("action ");
  124.   Serial.println(n);
  125.   Serial.println((String) "Cycle d'arrosage: " + CA);
  126.   Serial.println(HS_Z1);
  127.  
  128.   //__________Programme principal_________________________//
  129.   // if there's data available, read a packet
  130.   int packetSize = Udp.parsePacket();
  131.   if (packetSize)
  132.   {
  133.     Serial.print("Received packet of size ");
  134.     Serial.println(packetSize);
  135.     Serial.print("From ");
  136.     IPAddress remote = Udp.remoteIP();
  137.     for (int i = 0; i < 4; i++)
  138.     {
  139.       Serial.print(remote[i], DEC);
  140.       if (i < 3)
  141.       {
  142.         Serial.print(".");
  143.       }
  144.     }
  145.     Serial.print(", port ");
  146.     Serial.println(Udp.remotePort());
  147.  
  148.     // read the packet into packetBufffer
  149.     Udp.read(packetBuffer, MAX_SIZE);
  150.     Serial.println("Contents:");
  151.     Serial.println(packetBuffer);
  152.   }
  153.  
  154.   // Si "action"=5 je lance le programme principal
  155.  
  156.   /* if (n >= 5)
  157.   { */
  158.  
  159.     //__________Mesures__________________________________//
  160.     if (n < 5 && n > 15)
  161.     {
  162.       digitalWrite(pin_mesure, LOW);
  163.     }
  164.     if (n >= 5 && n <= 15)
  165.     {
  166.       digitalWrite(pin_mesure, HIGH);
  167.       HS_Z1 = (analogRead(A2));
  168.       HS_Z2 = (analogRead(A3));
  169.       HS_Z3 = (analogRead(A4));
  170.       HS_Z4 = (analogRead(A5));
  171.     }
  172.  
  173.     if (n > 15)
  174.     {
  175.  
  176.       const char *json = (packetBuffer); //Remplacer la chaine Json par "packetBuffer"!!
  177.       // send a reply, to the IP address and port that sent us the packet we received
  178.       Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  179.       Serial.println(json);
  180.       //  Udp.write(json);
  181.       Udp.endPacket();
  182.  
  183.       //__________JsonParse______________!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> https://arduinojson.org/v6/assistant/ !!!!!!!!!!!!!
  184.  
  185.       const size_t capacity = 3 * JSON_ARRAY_SIZE(1) + 3 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(6) + 110;
  186.       DynamicJsonDocument doc(capacity);
  187.       deserializeJson(doc, json);
  188.  
  189.       const char *message = doc["message"]; // "test"
  190.  
  191.       JsonObject arrosage_0_droite_0 = doc["arrosage"][0]["droite"][0];
  192.       int arrosage_0_droite_0_zone_1 = arrosage_0_droite_0["zone_1"]; // 10
  193.       int arrosage_0_droite_0_zone_2 = arrosage_0_droite_0["zone_2"]; // 20
  194.       int arrosage_0_droite_0_zone_3 = arrosage_0_droite_0["zone_3"]; // 30
  195.       int arrosage_0_droite_0_zone_4 = arrosage_0_droite_0["zone_4"]; // 40
  196.       int arrosage_0_droite_0_zone_5 = arrosage_0_droite_0["zone_5"]; // 50
  197.       int arrosage_0_droite_0_zone_6 = arrosage_0_droite_0["zone_6"]; // 60
  198.  
  199.       int arrosage_0_gauche_0_zone_1 = doc["arrosage"][0]["gauche"][0]["zone_1"]; // 30
  200.       int arrosage_0_gauche_0_zone_2 = doc["arrosage"][0]["gauche"][0]["zone_2"]; // 200
  201.  
  202.       //__________Envoie des infos_________________________//
  203.       Serial.println((String) "HS_Z1 : " + HS_Z1);
  204.       Serial.println((String) "HS_Z2 : " + HS_Z2);
  205.       Serial.println((String) "HS_Z3 : " + HS_Z3);
  206.       Serial.println((String) "HS_Z4 : " + HS_Z4);
  207.       Serial.println((String) "droite_0_zone_1 : " + arrosage_0_droite_0_zone_1);
  208.       Serial.println((String) "droite_0_zone_2 : " + arrosage_0_droite_0_zone_2);
  209.       Serial.println((String) "droite_0_zone_3 : " + arrosage_0_droite_0_zone_3);
  210.       Serial.println((String) "droite_0_zone_4 : " + arrosage_0_droite_0_zone_4);
  211.       Serial.println((String) "droite_0_zone_5 : " + arrosage_0_droite_0_zone_5);
  212.       Serial.println((String) "droite_0_zone_6 : " + arrosage_0_droite_0_zone_6);
  213.       //  Serial.println((String) "gauche_0_zone_1 : " + arrosage_0_gauche_0_zone_1);
  214.       //  Serial.println((String) "gauche_0_zone_2 : " + arrosage_0_gauche_0_zone_2);
  215.  
  216.       Udp.print("{\"message\":\"test\",\"arrosage\":[{\"droite\":[{\"zone_1\":"); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
  217.       Udp.print(arrosage_0_droite_0_zone_1);
  218.       Udp.print(",\"zone_2\":");
  219.       Udp.print(arrosage_0_droite_0_zone_2);
  220.       Udp.print(",\"zone_3\":");
  221.       Udp.print(arrosage_0_droite_0_zone_3);
  222.       Udp.print(",\"zone_4\":");
  223.       Udp.print(arrosage_0_droite_0_zone_4);
  224.       Udp.print(",\"zone_5\":");
  225.       Udp.print(arrosage_0_droite_0_zone_5);
  226.       Udp.print(",\"zone_6\":");
  227.       Udp.print(arrosage_0_droite_0_zone_6);
  228.  
  229.       Udp.print("}],\"mesure\":[{\"zone_1\":");
  230.       Udp.print(HS_Z1);
  231.       Udp.print(",\"zone_2\":");
  232.       Udp.print(HS_Z2);
  233.       Udp.print(",\"zone_3\":");
  234.       Udp.print(HS_Z3);
  235.       Udp.print(",\"zone_4\":");
  236.       Udp.print(HS_Z4);
  237.       Udp.print("}]}]}");
  238.  
  239.     //__________Déclenchement de l'arrosage_______________//
  240.     /*     Serial.println((String) "arrosage_0_droite_0_zone_1 après mesures : " + arrosage_0_droite_0_zone_1);
  241.     */
  242.     //if (CA >= 120 && CA <= 160)
  243.     if (CA >= 20 && CA <= 80)
  244.     {
  245.       Serial.println((String) "arrosage_0_droite_0_zone_1 dans cyle arrosage : " + arrosage_0_droite_0_zone_1);
  246.       Serial.println((String) "HS_Z1 dans cyle arrosage : " + HS_Z1);
  247.       //__________Zone 1_______________//
  248.       if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1)
  249.       {
  250.         CA1++;
  251.         Serial.println((String) "Arrosage zone_1: " + CA1);
  252.         digitalWrite(pin_zone_1, LOW);
  253.       }
  254.       //__________Zone 2_______________//
  255.       if (arrosage_0_droite_0_zone_2 > 0 && HS_Z2 < arrosage_0_droite_0_zone_2)
  256.       {
  257.         CA1++;
  258.         Serial.println((String) "Arrosage zone_2: " + CA1);
  259.         digitalWrite(pin_zone_2, LOW);
  260.       }
  261.       //__________Zone 3_______________//
  262.       if (arrosage_0_droite_0_zone_3 > 0 && HS_Z3 < arrosage_0_droite_0_zone_3)
  263.       {
  264.         CA1++;
  265.         Serial.println((String) "Arrosage zone_3: " + CA1);
  266.         digitalWrite(pin_zone_3, LOW);
  267.       }
  268.       //__________Zone 4_______________//
  269.       if (arrosage_0_droite_0_zone_4 > 0 && HS_Z3 < arrosage_0_droite_0_zone_4)
  270.       {
  271.         CA1++;
  272.         Serial.println((String) "Arrosage zone_4: " + CA1);
  273.         digitalWrite(pin_zone_4, LOW);
  274.       }
  275.       //__________Zone 5_______________//
  276.       if (arrosage_0_droite_0_zone_5 > 0  && HS_Z3 < arrosage_0_droite_0_zone_5)
  277.       {
  278.         CA1++;
  279.         Serial.println((String) "Arrosage zone_5: " + CA1);
  280.         digitalWrite(pin_zone_5, LOW);
  281.       }
  282.       //__________Zone 6_______________//
  283.       if (arrosage_0_droite_0_zone_6 > 0 && HS_Z3 < arrosage_0_droite_0_zone_6)
  284.       {
  285.         CA1++;
  286.         Serial.println((String) "Arrosage zone_6: " + CA1);
  287.         digitalWrite(pin_zone_6, LOW);
  288.       }
  289.     }
  290.  
  291.     if (CA >= 80)
  292.     {
  293.       digitalWrite(pin_zone_1, HIGH);
  294.       digitalWrite(pin_zone_2, HIGH);
  295.       digitalWrite(pin_zone_3, HIGH);
  296.       digitalWrite(pin_zone_4, HIGH);
  297.       digitalWrite(pin_zone_5, HIGH);
  298.       digitalWrite(pin_zone_6, HIGH);
  299.     }
  300.  
  301.     if (CA >= 200)
  302.     {
  303.       CA = 0;
  304.     }
  305.  
  306.     if (n == 201)
  307.     {
  308.       n = 0;
  309.     }
  310.   }
  311. }
  312.  
  313. /*
  314. unsigned long prevMillis = 0;
  315. unsigned long prevMillis1 = 0;
  316. int stop_Z1 = 0;
  317.  
  318. int temps_darrosage = 5000;
  319. int declenchement_arrosage = 10000;
  320.  
  321. //__________Arrosage zone 1__________________________________//
  322.  
  323. void arrosage_zone_1(int zone)
  324. {
  325.   if (zone == 1)
  326.   {
  327.     int pin_zone = zone + 1;
  328.     Serial.println((String) "pin_zone : " + pin_zone);
  329.     Serial.println((String) "millis() - prevMillis: " + (millis() - prevMillis));
  330.     if (millis() - prevMillis >= temps_darrosage && stop_Z1 != 1 )
  331.     {
  332.       digitalWrite(pin_zone, HIGH);
  333.       prevMillis = millis(); // RAZ
  334.       Serial.println((String) "millis() - prevMillis: " + (millis() - prevMillis));
  335.     }
  336.     if (millis() - prevMillis1 >= (declenchement_arrosage + temps_darrosage))
  337.     {
  338.       digitalWrite(pin_zone, LOW);
  339.       prevMillis1 = millis(); // RAZ
  340.       stop_Z1 = 1;
  341.     }
  342.   }
  343. }
  344.  
  345. //__________Arrosage zone 2__________________________________//
  346.  
  347. unsigned long prevMillis2 = 0;
  348. unsigned long prevMillis3 = 0;
  349.  
  350. void arrosage_zone_2(int zone)
  351. {
  352.   if (zone == 1)
  353.   {
  354.     int pin_zone = zone + 2;
  355.     Serial.println((String) "pin_zone : " + pin_zone);
  356.     if (millis() - prevMillis2 >= temps_darrosage)
  357.     {
  358.       digitalWrite(pin_zone, LOW);
  359.       prevMillis2 = millis(); // RAZ
  360.     }
  361.     if (millis() - prevMillis3 >= (declenchement_arrosage + temps_darrosage))
  362.     {
  363.       digitalWrite(pin_zone, HIGH);
  364.       prevMillis3 = millis(); // RAZ
  365.     }
  366.   }
  367. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement