Advertisement
Justinberger

Arrosage1_03.04.2020.ino

Jul 21st, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.46 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.     const char *json = (packetBuffer); //Remplacer la chaine Json par "packetBuffer"!!
  160.     // send a reply, to the IP address and port that sent us the packet we received
  161.     Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  162.     Serial.println(json);
  163.     //  Udp.write(json);
  164.     Udp.endPacket();
  165.  
  166.     //__________JsonParse______________!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> https://arduinojson.org/v6/assistant/ !!!!!!!!!!!!!
  167.  
  168.     const size_t capacity = 3 * JSON_ARRAY_SIZE(1) + 3 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(6) + 110;
  169.     DynamicJsonDocument doc(capacity);
  170.     deserializeJson(doc, json);
  171.  
  172.     const char *message = doc["message"]; // "test"
  173.  
  174.     JsonObject arrosage_0_droite_0 = doc["arrosage"][0]["droite"][0];
  175.     int arrosage_0_droite_0_zone_1 = arrosage_0_droite_0["zone_1"]; // 10
  176.     int arrosage_0_droite_0_zone_2 = arrosage_0_droite_0["zone_2"]; // 20
  177.     int arrosage_0_droite_0_zone_3 = arrosage_0_droite_0["zone_3"]; // 30
  178.     int arrosage_0_droite_0_zone_4 = arrosage_0_droite_0["zone_4"]; // 40
  179.     int arrosage_0_droite_0_zone_5 = arrosage_0_droite_0["zone_5"]; // 50
  180.     int arrosage_0_droite_0_zone_6 = arrosage_0_droite_0["zone_6"]; // 60
  181.  
  182.     int arrosage_0_gauche_0_zone_1 = doc["arrosage"][0]["gauche"][0]["zone_1"]; // 30
  183.     int arrosage_0_gauche_0_zone_2 = doc["arrosage"][0]["gauche"][0]["zone_2"]; // 200
  184.  
  185.     //__________Envoie des infos_________________________//
  186.  
  187.     Serial.print("HS_Z1 : ");
  188.     Serial.println(HS_Z1);
  189.     Serial.println((String) "droite_0_zone_1 : " + arrosage_0_droite_0_zone_1);
  190.     Serial.println((String) "droite_0_zone_2 : " + arrosage_0_droite_0_zone_2);
  191.     Serial.println((String) "droite_0_zone_3 : " + arrosage_0_droite_0_zone_3);
  192.     Serial.println((String) "droite_0_zone_4 : " + arrosage_0_droite_0_zone_4);
  193.     Serial.println((String) "droite_0_zone_5 : " + arrosage_0_droite_0_zone_5);
  194.     Serial.println((String) "droite_0_zone_6 : " + arrosage_0_droite_0_zone_6);
  195.     //  Serial.println((String) "gauche_0_zone_1 : " + arrosage_0_gauche_0_zone_1);
  196.     //  Serial.println((String) "gauche_0_zone_2 : " + arrosage_0_gauche_0_zone_2);
  197.  
  198.     Udp.print("{\"message\":\"test\",\"arrosage\":[{\"droite\":[{\"zone_1\":"); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
  199.     Udp.print(arrosage_0_droite_0_zone_1);
  200.     Udp.print(",\"zone_2\":");
  201.     Udp.print(arrosage_0_droite_0_zone_2);
  202.     Udp.print(",\"zone_3\":");
  203.     Udp.print(arrosage_0_droite_0_zone_3);
  204.     Udp.print(",\"zone_4\":");
  205.     Udp.print(arrosage_0_droite_0_zone_4);
  206.     Udp.print(",\"zone_5\":");
  207.     Udp.print(arrosage_0_droite_0_zone_5);
  208.     Udp.print(",\"zone_6\":");
  209.     Udp.print(arrosage_0_droite_0_zone_6);
  210.  
  211.     Udp.print("}],\"gauche\":[{\"zone_1\":");
  212.     Udp.print(HS_Z1);
  213.     Udp.print(",\"zone_2\":200}]}]}");
  214.  
  215.     //__________Mesures__________________________________//
  216.     if (n < 30)
  217.     {
  218.       digitalWrite(pin_mesure, LOW);
  219.     }
  220.     if (n >= 30 && n <= 40)
  221.     {
  222.       digitalWrite(pin_mesure, HIGH);
  223.       (analogRead(A2));
  224.       (analogRead(A3));
  225.       (analogRead(A4));
  226.       (analogRead(A5));
  227.       if (n >= 35)
  228.       {
  229.       HS_Z1 = (analogRead(A2));
  230.       HS_Z2 = (analogRead(A3));
  231.       HS_Z3 = (analogRead(A4));
  232.       HS_Z4 = (analogRead(A5));
  233.       }
  234.     }
  235.  
  236.     //__________Déclenchement de l'arrosage_______________//
  237. /*     Serial.println((String) "arrosage_0_droite_0_zone_1 après mesures : " + arrosage_0_droite_0_zone_1);
  238.     if (CA < 40)
  239.     {
  240.       digitalWrite(pin_zone_1, HIGH);
  241.     } */
  242.     if (CA >= 120 && CA <= 160)
  243.     {
  244.       Serial.println((String) "arrosage_0_droite_0_zone_1 dans cyle arrosage : " + arrosage_0_droite_0_zone_1);
  245.       Serial.println((String) "HS_Z1 dans cyle arrosage : " + HS_Z1);
  246.       //__________Zone 1_______________//
  247.       if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1)
  248.       {
  249.         CA1++;
  250.         Serial.println((String) "Arrosage zone_1: " + CA1);
  251.         digitalWrite(pin_zone_1, LOW);
  252.       }
  253.       //__________Zone 2_______________//
  254.       if (arrosage_0_droite_0_zone_2 > 0 && HS_Z1 < arrosage_0_droite_0_zone_2)
  255.       {
  256.         CA1++;
  257.         Serial.println((String) "Arrosage zone_2: " + CA1);
  258.         digitalWrite(pin_zone_2, LOW);
  259.       }
  260.       //__________Zone 3_______________//
  261.       if (arrosage_0_droite_0_zone_3 > 0 && HS_Z1 < arrosage_0_droite_0_zone_3)
  262.       {
  263.         CA1++;
  264.         Serial.println((String) "Arrosage zone_3: " + CA1);
  265.         digitalWrite(pin_zone_3, LOW);
  266.       }
  267.       //__________Zone 4_______________//
  268.       if (arrosage_0_droite_0_zone_4 > 0 && HS_Z1 < arrosage_0_droite_0_zone_4)
  269.       {
  270.         CA1++;
  271.         Serial.println((String) "Arrosage zone_4: " + CA1);
  272.         digitalWrite(pin_zone_4, LOW);
  273.       }
  274.       //__________Zone 5_______________//
  275.       if (arrosage_0_droite_0_zone_5 > 0 && HS_Z1 < arrosage_0_droite_0_zone_5)
  276.       {
  277.         CA1++;
  278.         Serial.println((String) "Arrosage zone_5: " + CA1);
  279.         digitalWrite(pin_zone_5, LOW);
  280.       }
  281.       //__________Zone 6_______________//
  282.       if (arrosage_0_droite_0_zone_6 > 0 && HS_Z1 < arrosage_0_droite_0_zone_6)
  283.       {
  284.         CA1++;
  285.         Serial.println((String) "Arrosage zone_6: " + CA1);
  286.         digitalWrite(pin_zone_6, LOW);
  287.       }
  288.     }
  289.  
  290.     if (CA >= 160)
  291.     {
  292.       CA = 0;
  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 (n == 161)
  302.     {
  303.       n = 0;
  304.     }
  305.   }
  306. }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328. /*
  329. unsigned long prevMillis = 0;
  330. unsigned long prevMillis1 = 0;
  331. int stop_Z1 = 0;
  332.  
  333. int temps_darrosage = 5000;
  334. int declenchement_arrosage = 10000;
  335.  
  336. //__________Arrosage zone 1__________________________________//
  337.  
  338. void arrosage_zone_1(int zone)
  339. {
  340.   if (zone == 1)
  341.   {
  342.     int pin_zone = zone + 1;
  343.     Serial.println((String) "pin_zone : " + pin_zone);
  344.     Serial.println((String) "millis() - prevMillis: " + (millis() - prevMillis));
  345.     if (millis() - prevMillis >= temps_darrosage && stop_Z1 != 1 )
  346.     {
  347.       digitalWrite(pin_zone, HIGH);
  348.       prevMillis = millis(); // RAZ
  349.       Serial.println((String) "millis() - prevMillis: " + (millis() - prevMillis));
  350.     }
  351.     if (millis() - prevMillis1 >= (declenchement_arrosage + temps_darrosage))
  352.     {
  353.       digitalWrite(pin_zone, LOW);
  354.       prevMillis1 = millis(); // RAZ
  355.       stop_Z1 = 1;
  356.     }
  357.   }
  358. }
  359.  
  360. //__________Arrosage zone 2__________________________________//
  361.  
  362. unsigned long prevMillis2 = 0;
  363. unsigned long prevMillis3 = 0;
  364.  
  365. void arrosage_zone_2(int zone)
  366. {
  367.   if (zone == 1)
  368.   {
  369.     int pin_zone = zone + 2;
  370.     Serial.println((String) "pin_zone : " + pin_zone);
  371.     if (millis() - prevMillis2 >= temps_darrosage)
  372.     {
  373.       digitalWrite(pin_zone, LOW);
  374.       prevMillis2 = millis(); // RAZ
  375.     }
  376.     if (millis() - prevMillis3 >= (declenchement_arrosage + temps_darrosage))
  377.     {
  378.       digitalWrite(pin_zone, HIGH);
  379.       prevMillis3 = millis(); // RAZ
  380.     }
  381.   }
  382. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement