Advertisement
Justinberger

Arrosage1_03.04.2020.ino

Jul 10th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 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. //Rebrancher le câble réseau
  10.  
  11. /*
  12. UDPSendReceive.pde:
  13.  This sketch receives UDP message strings, prints them to the serial port and sends an "acknowledge" string back to the sender
  14.  
  15.  A Processing sketch is included at the end of file that can be used to send and received messages for testing with a computer.
  16.  
  17.  created 21 Aug 2010
  18.  by Michael Margolis
  19.  
  20.  This code is in the public domain.
  21.  */
  22.  
  23. //#include <SPI.h>          // needed for Arduino versions later than 0018
  24. //#include <Ethernet2.h>    //!!!ATTENTTION!!! en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t _cspin" dans la bibliothéque
  25. #include <Ethernet.h> //!!!ATTENTTION!!! en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t sspin" dans la bibliothéque
  26. //#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  27. //#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  28. #include <EthernetUdp.h>
  29. #include <ArduinoJson.h>
  30. //#include <EEPROM.h>
  31.  
  32. // Enter a MAC address and IP address for your controller below.
  33. // The IP address will be dependent on your local network:
  34. byte mac[] = {
  35.     0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  36. IPAddress ip(192, 168, 1, 177);
  37.  
  38. unsigned int localPort = 8888; // local port to listen on
  39. #define MAX_SIZE 400
  40.  
  41. //int pin_vanne = 2;
  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 = 8;
  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.  
  57. boolean stop_arrosage_zone_1;
  58. boolean stop_arrosage_zone_2;
  59.  
  60. // buffers for receiving and sending data
  61. char packetBuffer[MAX_SIZE]; //buffer to hold incoming packet,
  62. //char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  63. char ReplyBuffer[] = "acknowledged"; // a string to send back
  64.  
  65. // An EthernetUDP instance to let us send and receive packets over UDP
  66. EthernetUDP Udp;
  67.  
  68. void setup()
  69. {
  70.   // start the Ethernet and UDP:
  71.   Ethernet.begin(mac, ip);
  72.   Udp.begin(localPort);
  73.   Serial.begin(9600);
  74.  
  75.   //pinMode(pin_vanne, OUTPUT);
  76.   pinMode(pin_zone_1, OUTPUT);
  77.   pinMode(pin_zone_2, OUTPUT);
  78.   pinMode(pin_zone_3, OUTPUT);
  79.   pinMode(pin_zone_4, OUTPUT);
  80.   pinMode(pin_zone_5, OUTPUT);
  81.   pinMode(pin_zone_6, OUTPUT);
  82.   pinMode(pin_mesure, OUTPUT);
  83.   pinMode(pin_zone, OUTPUT);
  84.  
  85.   //digitalWrite(pin_vanne, LOW);
  86.   digitalWrite(pin_zone_1, HIGH);
  87.   digitalWrite(pin_zone_2, HIGH);
  88.   digitalWrite(pin_zone_3, HIGH);
  89.   digitalWrite(pin_zone_4, HIGH);
  90.   digitalWrite(pin_zone_5, HIGH);
  91.   digitalWrite(pin_zone_6, HIGH);
  92.   digitalWrite(pin_mesure, HIGH);
  93.  
  94.   Serial.println("OK Test");
  95.  
  96.   /*   int valeur_1 = 42;
  97.   EEPROM.put(0, valeur_1);
  98.   float valeur_2 = 13.37;
  99.   EEPROM.put(2, valeur_2); // Un int fait deux octets, l'adresse est donc de 0 + 2 = 2
  100.  
  101.   int valeur_lue_1;
  102.   EEPROM.get(0, valeur_lue_1);
  103.   Serial.print("Valeur 1 = ");
  104.   Serial.println(valeur_lue_1);
  105.  
  106.   float valeur_lue_2;
  107.   EEPROM.get(2, valeur_lue_2);
  108.   Serial.print("Valeur 2 = ");
  109.   Serial.println(valeur_lue_2);  */
  110. }
  111.  
  112. unsigned long prevMillis4 = 0;
  113.  
  114. void loop()
  115. {
  116.   Serial.println("OK loop");
  117.   // if there's data available, read a packet
  118.   int packetSize = Udp.parsePacket();
  119.   if (packetSize)
  120.   {
  121.     Serial.print("Received packet of size ");
  122.     Serial.println(packetSize);
  123.     Serial.print("From ");
  124.     IPAddress remote = Udp.remoteIP();
  125.     for (int i = 0; i < 4; i++)
  126.     {
  127.       Serial.print(remote[i], DEC);
  128.       if (i < 3)
  129.       {
  130.         Serial.print(".");
  131.       }
  132.     }
  133.     Serial.print(", port ");
  134.     Serial.println(Udp.remotePort());
  135.  
  136.     // read the packet into packetBufffer
  137.     Udp.read(packetBuffer, MAX_SIZE);
  138.     Serial.println("Contents:");
  139.     Serial.println(packetBuffer);
  140.   }
  141.  
  142.   const char *json = (packetBuffer); //Remplacer la chaine Json par "packetBuffer"!!
  143.   // send a reply, to the IP address and port that sent us the packet we received
  144.   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  145.   Serial.println(json);
  146.   //  Udp.write(json);
  147.   Udp.endPacket();
  148.  
  149.   //__________JsonParse______________!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> https://arduinojson.org/v6/assistant/ !!!!!!!!!!!!!
  150.  
  151.   const size_t capacity = 3 * JSON_ARRAY_SIZE(1) + 4 * JSON_OBJECT_SIZE(2) + 80; //Bien penser de modifier ce paramétre
  152.   DynamicJsonDocument doc(capacity);
  153.  
  154.   deserializeJson(doc, json);
  155.  
  156.   const char *message = doc["message"]; // "test"
  157.  
  158.   const int arrosage_0_droite_0_zone_1 = doc["arrosage"][0]["droite"][0]["zone_1"]; // 100
  159.   const int arrosage_0_droite_0_zone_2 = doc["arrosage"][0]["droite"][0]["zone_2"]; // 130
  160.   const int arrosage_0_gauche_0_zone_1 = doc["arrosage"][0]["gauche"][0]["zone_1"]; // 30
  161.   const int arrosage_0_gauche_0_zone_2 = doc["arrosage"][0]["gauche"][0]["zone_2"]; // 200
  162.  
  163.   //__________Mesures__________________________________//
  164.   pinMode(A0, INPUT);
  165.   Serial.println((String) "prevMillis4 avant mesure : " + prevMillis4);
  166.   if (millis() - prevMillis4 >= 1000)
  167.   {
  168.     prevMillis4 = millis(); // RAZ
  169.     digitalWrite(pin_mesure, LOW);
  170.     Serial.println("Mesures");
  171.     Serial.println((String) "prevMillis4 : " + prevMillis4);
  172.     Serial.println((String) "millis : " + millis());
  173.   }
  174.   else
  175.   {
  176.     digitalWrite(pin_mesure, HIGH);
  177.   }
  178.   int HS_Z1 = (analogRead(A2));
  179.  
  180.   //__________Envoie des infos_________________________//
  181.   Serial.print("HS_Z1 : ");
  182.   Serial.println(HS_Z1);
  183.   Serial.println((String) "droite_0_zone_1 : " + arrosage_0_droite_0_zone_1);
  184.   Serial.println((String) "droite_0_zone_2 : " + arrosage_0_droite_0_zone_2);
  185.   Serial.println((String) "gauche_0_zone_1 : " + arrosage_0_gauche_0_zone_1);
  186.   Serial.println((String) "gauche_0_zone_2 : " + arrosage_0_gauche_0_zone_2);
  187.  
  188.   Udp.print("{\"message\":\"test\",\"arrosage\":[{\"droite\":[{\"zone_1\":"); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
  189.   Udp.print(arrosage_0_droite_0_zone_1);
  190.   Udp.print(",\"zone_2\":");
  191.   Udp.print(arrosage_0_droite_0_zone_2);
  192.   Udp.print("}],\"gauche\":[{\"zone_1\":");
  193.   Udp.print(HS_Z1);
  194.   Udp.print(",\"zone_2\":200}]}]}");
  195.  
  196.   //__________Déclenchement de l'arrosage_______________//
  197.   if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1)
  198.   {
  199.     arrosage_zone_1(1);
  200.     stop_arrosage_zone_1 = 0;
  201.   }
  202.   else
  203.   {
  204.     stop_arrosage_zone_1 = 1;
  205.   }
  206.   if (arrosage_0_droite_0_zone_2 > 0 && HS_Z1 < arrosage_0_droite_0_zone_2)
  207.   {
  208.     arrosage_zone_2(1);
  209.     stop_arrosage_zone_2 = 0;
  210.   }
  211.   else
  212.   {
  213.     stop_arrosage_zone_2 = 1;
  214.     //arret_arrosage();
  215.     /*     digitalWrite(pin_vanne, LOW);
  216.     digitalWrite(pin_zone_1, HIGH);
  217.     digitalWrite(pin_zone_2, HIGH);
  218.     digitalWrite(pin_zone_3, HIGH); */
  219.   }
  220.   if (stop_arrosage_zone_1 == 1 && stop_arrosage_zone_2 == 1)
  221.   {
  222.     Serial.print("Stop arrosage");
  223.     //    digitalWrite(pin_vanne, LOW);
  224.     digitalWrite(pin_zone_1, HIGH);
  225.     digitalWrite(pin_zone_2, HIGH);
  226.     digitalWrite(pin_zone_3, HIGH);
  227.     digitalWrite(pin_zone_4, HIGH);
  228.     digitalWrite(pin_zone_5, HIGH);
  229.     digitalWrite(pin_zone_6, HIGH);
  230.   }
  231.   delay(5000);
  232. }
  233.  
  234. unsigned long prevMillis = 0;
  235. unsigned long prevMillis1 = 0;
  236.  
  237. int temps_darrosage = 5000;
  238. int declenchement_arrosage = 10000;
  239.  
  240. void arrosage_zone_1(int zone)
  241. {
  242.   if (zone == 1)
  243.   {
  244.     int pin_zone = zone + 1;
  245.     Serial.println((String) "pin_zone : " + pin_zone);
  246.     if (millis() - prevMillis >= temps_darrosage)
  247.     {
  248.       // digitalWrite(pin_vanne, LOW);
  249.       digitalWrite(pin_zone, HIGH);
  250.       prevMillis = millis(); // RAZ
  251.     }
  252.     if (millis() - prevMillis1 >= (declenchement_arrosage + temps_darrosage))
  253.     {
  254.       //      digitalWrite(pin_vanne, HIGH);
  255.       digitalWrite(pin_zone, LOW);
  256.       prevMillis1 = millis(); // RAZ
  257.     }
  258.   }
  259. }
  260.  
  261. unsigned long prevMillis2 = 0;
  262. unsigned long prevMillis3 = 0;
  263.  
  264. void arrosage_zone_2(int zone)
  265. {
  266.   if (zone == 1)
  267.   {
  268.     int pin_zone = zone + 2;
  269.     Serial.println((String) "pin_zone : " + pin_zone);
  270.     if (millis() - prevMillis2 >= temps_darrosage)
  271.     {
  272.       // digitalWrite(pin_vanne, LOW);
  273.       digitalWrite(pin_zone, HIGH);
  274.       prevMillis2 = millis(); // RAZ
  275.     }
  276.     if (millis() - prevMillis3 >= (declenchement_arrosage + temps_darrosage))
  277.     {
  278.       //      digitalWrite(pin_vanne, HIGH);
  279.       digitalWrite(pin_zone, LOW);
  280.       prevMillis3 = millis(); // RAZ
  281.     }
  282.   }
  283. }
  284.  
  285. /*void arret_arrosage()
  286. {
  287.   digitalWrite(pin_vanne, LOW);
  288.   digitalWrite(pin_zone_1, HIGH);
  289.   digitalWrite(pin_zone_2, HIGH);
  290.   digitalWrite(pin_zone_3, HIGH);
  291. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement