Advertisement
Justinberger

Arrosage1_03.04.2020.ino

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