Advertisement
Justinberger

Arrosage1_03.04.2020.ino

Apr 10th, 2020
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | None | 0 0
  1.  
  2. //https://www.youtube.com/watch?v=SWirrQH-48c
  3.  
  4. /*
  5. UDPSendReceive.pde:
  6.  This sketch receives UDP message strings, prints them to the serial port
  7.  and sends an "acknowledge" string back to the sender
  8.  
  9.  A Processing sketch is included at the end of file that can be used to send
  10.  and received messages for testing with a computer.
  11.  
  12.  created 21 Aug 2010
  13.  by Michael Margolis
  14.  
  15.  This code is in the public domain.
  16.  */
  17.  
  18. #include <SPI.h>          // needed for Arduino versions later than 0018
  19. #include <Ethernet2.h>    //ATTEBTTION en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t _cspin" dans la bibliothéque
  20. #include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  21. #include <ArduinoJson.h>
  22. #include <EEPROM.h>
  23.  
  24. // Enter a MAC address and IP address for your controller below.
  25. // The IP address will be dependent on your local network:
  26. byte mac[] = {
  27.     0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  28. IPAddress ip(192, 168, 1, 177);
  29.  
  30. unsigned int localPort = 8888; // local port to listen on
  31. #define MAX_SIZE 400
  32.  
  33. unsigned long prevMillis = 0;
  34. int arrosage_0_droite_0_zone_1;
  35. int arrosage_0_droite_0_zone_2;
  36. int arrosage_0_gauche_0_zone_1;
  37. int arrosage_0_gauche_0_zone_2;
  38. int temps_darrosage = 5000;
  39. int declenchement_arrosage = 60000;
  40. int pin_vanne = 2;
  41. int pin_zone_1 = 3;
  42. int pin_zone_2 = 4;
  43. int pin_zone_3 = 5;
  44. int HS_Z1;
  45.  
  46. // buffers for receiving and sending data
  47. char packetBuffer[MAX_SIZE]; //buffer to hold incoming packet,
  48. //char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  49. char ReplyBuffer[] = "acknowledged"; // a string to send back
  50.  
  51. // An EthernetUDP instance to let us send and receive packets over UDP
  52. EthernetUDP Udp;
  53.  
  54. void setup()
  55. {
  56.   // start the Ethernet and UDP:
  57.   Ethernet.begin(mac, ip);
  58.   Udp.begin(localPort);
  59.   Serial.begin(9600);
  60.  
  61.   pinMode(pin_vanne, OUTPUT);
  62.   pinMode(pin_zone_1, OUTPUT);
  63.   pinMode(pin_zone_2, OUTPUT);
  64.   pinMode(pin_zone_3, OUTPUT);
  65.  
  66.   /*   int valeur_1 = 42;
  67.   EEPROM.put(0, valeur_1);
  68.   float valeur_2 = 13.37;
  69.   EEPROM.put(2, valeur_2); // Un int fait deux octets, l'adresse est donc de 0 + 2 = 2
  70.  
  71.   int valeur_lue_1;
  72.   EEPROM.get(0, valeur_lue_1);
  73.   Serial.print("Valeur 1 = ");
  74.   Serial.println(valeur_lue_1);
  75.  
  76.   float valeur_lue_2;
  77.   EEPROM.get(2, valeur_lue_2);
  78.   Serial.print("Valeur 2 = ");
  79.   Serial.println(valeur_lue_2);  */
  80. }
  81.  
  82. void loop()
  83. {
  84.   // if there's data available, read a packet
  85.   int packetSize = Udp.parsePacket();
  86.   if (packetSize)
  87.   {
  88.     Serial.print("Received packet of size ");
  89.     Serial.println(packetSize);
  90.     Serial.print("From ");
  91.     IPAddress remote = Udp.remoteIP();
  92.     for (int i = 0; i < 4; i++)
  93.     {
  94.       Serial.print(remote[i], DEC);
  95.       if (i < 3)
  96.       {
  97.         Serial.print(".");
  98.       }
  99.     }
  100.     Serial.print(", port ");
  101.     Serial.println(Udp.remotePort());
  102.  
  103.     // read the packet into packetBufffer
  104.     Udp.read(packetBuffer, MAX_SIZE);
  105.     Serial.println("Contents:");
  106.     Serial.println(packetBuffer);
  107.   }
  108.  
  109.   const char *json = (packetBuffer); //Remplacer la chaine Json par "packetBuffer"!!
  110.   // send a reply, to the IP address and port that sent us the packet we received
  111.   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  112.   Serial.println(json);
  113.   //  Udp.write(json);
  114.   Udp.endPacket();
  115.  
  116.   //__________JsonParse______________!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> https://arduinojson.org/v6/assistant/ !!!!!!!!!!!!!
  117.  
  118.   const size_t capacity = 3 * JSON_ARRAY_SIZE(1) + 4 * JSON_OBJECT_SIZE(2) + 80; //Bien penser de modifier ce paramétre
  119.   DynamicJsonDocument doc(capacity);
  120.  
  121.   deserializeJson(doc, json);
  122.  
  123.   const char *message = doc["message"]; // "test"
  124.  
  125.   const int arrosage_0_droite_0_zone_1 = doc["arrosage"][0]["droite"][0]["zone_1"]; // 100
  126.   const int arrosage_0_droite_0_zone_2 = doc["arrosage"][0]["droite"][0]["zone_2"]; // 130
  127.   const int arrosage_0_gauche_0_zone_1 = doc["arrosage"][0]["gauche"][0]["zone_1"]; // 30
  128.   const int arrosage_0_gauche_0_zone_2 = doc["arrosage"][0]["gauche"][0]["zone_2"]; // 200
  129.  
  130.   //__________Mesures__________________________________//
  131.   pinMode(A0, INPUT);
  132.   int HS_Z1 = (analogRead(A0));
  133.  
  134.   //__________Envoie des infos_________________________//
  135.   Serial.print("HS_Z1 : ");
  136.   Serial.println(HS_Z1);
  137.   Serial.println((String) "droite_0_zone_1 : " + arrosage_0_droite_0_zone_1);
  138.   Serial.println((String) "droite_0_zone_2 : " + arrosage_0_droite_0_zone_2);
  139.   Serial.println((String) "gauche_0_zone_1 : " + arrosage_0_gauche_0_zone_1);
  140.   Serial.println((String) "gauche_0_zone_2 : " + arrosage_0_gauche_0_zone_2);
  141.  
  142.   Udp.print("{\"message\":\"test\",\"arrosage\":[{\"droite\":[{\"zone_1\":"); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
  143.   Udp.print(arrosage_0_droite_0_zone_1);
  144.   Udp.print(",\"zone_2\":");
  145.   Udp.print(arrosage_0_droite_0_zone_2);
  146.   Udp.print("}],\"gauche\":[{\"zone_1\":");
  147.   Udp.print(HS_Z1);
  148.   Udp.print(",\"zone_2\":200}]}]}");
  149.  
  150.   //__________Déclenchement de l'arrosage_______________//
  151.   /* if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1 && (millis() - prevMillis > declenchement_arrosage)) */
  152.   if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1)
  153.   {
  154.     arrosage(1);
  155.   }
  156.   else
  157.   {
  158.     arrosage(0);
  159.   }
  160.   delay(5000);
  161. }
  162.  
  163. void arrosage(int zone)
  164. {
  165.   if (zone == 1)
  166.   {
  167.     Serial.println((String) "arrosage zone " + zone);
  168.     digitalWrite(pin_vanne, HIGH);
  169.     digitalWrite(pin_zone_1, LOW);
  170.     delay(temps_darrosage);
  171.  
  172.     Serial.println((String)"millis : " + millis());
  173.     Serial.println((String)"millis() - prevMillis : " + (millis() - prevMillis));
  174.  
  175.     if (millis() - prevMillis > declenchement_arrosage)
  176.     {
  177.       prevMillis = millis(); // RAZ
  178.       //Utiliser la la fonction millis ICI https://www.udemy.com/course/arduino-la-formation-complete-pour-les-creatifs/?fbclid=IwAR3tAE__fbY0yXQv2OkaTctkOedOFfzpP9JQXgxUiHPYOGHY5JiMmDswCQ0&moon=tethys&utm_campaign=INTL-FB-PROS-DPA-Evergreen-FR-Smartly-FR-FRA_._ci__._sl_FRA_._vi__._sd_All_._la_FR_._&utm_content=_._pd_1364900_._&utm_medium=udemyads&utm_source=facebook-intl&utm_term=_._ag_fr_broad_audience_._ad_6182597964288_._
  179.       digitalWrite(pin_vanne, LOW);
  180.       digitalWrite(pin_zone_1, HIGH);
  181.     }
  182.   }
  183.   else
  184.   {
  185.     digitalWrite(pin_vanne, LOW);
  186.     digitalWrite(pin_zone_1, HIGH);
  187.     digitalWrite(pin_zone_2, HIGH);
  188.     digitalWrite(pin_zone_3, HIGH);
  189.   }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement