Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.youtube.com/watch?v=SWirrQH-48c
- /*
- UDPSendReceive.pde:
- This sketch receives UDP message strings, prints them to the serial port
- and sends an "acknowledge" string back to the sender
- A Processing sketch is included at the end of file that can be used to send
- and received messages for testing with a computer.
- created 21 Aug 2010
- by Michael Margolis
- This code is in the public domain.
- */
- #include <SPI.h> // needed for Arduino versions later than 0018
- #include <Ethernet2.h> //ATTEBTTION en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t _cspin" dans la bibliothéque
- #include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
- #include <ArduinoJson.h>
- #include <EEPROM.h>
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- IPAddress ip(192, 168, 1, 177);
- unsigned int localPort = 8888; // local port to listen on
- #define MAX_SIZE 400
- unsigned long prevMillis = 0;
- int arrosage_0_droite_0_zone_1;
- int arrosage_0_droite_0_zone_2;
- int arrosage_0_gauche_0_zone_1;
- int arrosage_0_gauche_0_zone_2;
- int temps_darrosage = 5000;
- int declenchement_arrosage = 60000;
- int pin_vanne = 2;
- int pin_zone_1 = 3;
- int pin_zone_2 = 4;
- int pin_zone_3 = 5;
- int HS_Z1;
- // buffers for receiving and sending data
- char packetBuffer[MAX_SIZE]; //buffer to hold incoming packet,
- //char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
- char ReplyBuffer[] = "acknowledged"; // a string to send back
- // An EthernetUDP instance to let us send and receive packets over UDP
- EthernetUDP Udp;
- void setup()
- {
- // start the Ethernet and UDP:
- Ethernet.begin(mac, ip);
- Udp.begin(localPort);
- Serial.begin(9600);
- pinMode(pin_vanne, OUTPUT);
- pinMode(pin_zone_1, OUTPUT);
- pinMode(pin_zone_2, OUTPUT);
- pinMode(pin_zone_3, OUTPUT);
- /* int valeur_1 = 42;
- EEPROM.put(0, valeur_1);
- float valeur_2 = 13.37;
- EEPROM.put(2, valeur_2); // Un int fait deux octets, l'adresse est donc de 0 + 2 = 2
- int valeur_lue_1;
- EEPROM.get(0, valeur_lue_1);
- Serial.print("Valeur 1 = ");
- Serial.println(valeur_lue_1);
- float valeur_lue_2;
- EEPROM.get(2, valeur_lue_2);
- Serial.print("Valeur 2 = ");
- Serial.println(valeur_lue_2); */
- }
- void loop()
- {
- // if there's data available, read a packet
- int packetSize = Udp.parsePacket();
- if (packetSize)
- {
- Serial.print("Received packet of size ");
- Serial.println(packetSize);
- Serial.print("From ");
- IPAddress remote = Udp.remoteIP();
- for (int i = 0; i < 4; i++)
- {
- Serial.print(remote[i], DEC);
- if (i < 3)
- {
- Serial.print(".");
- }
- }
- Serial.print(", port ");
- Serial.println(Udp.remotePort());
- // read the packet into packetBufffer
- Udp.read(packetBuffer, MAX_SIZE);
- Serial.println("Contents:");
- Serial.println(packetBuffer);
- }
- const char *json = (packetBuffer); //Remplacer la chaine Json par "packetBuffer"!!
- // send a reply, to the IP address and port that sent us the packet we received
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Serial.println(json);
- // Udp.write(json);
- Udp.endPacket();
- //__________JsonParse______________!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> https://arduinojson.org/v6/assistant/ !!!!!!!!!!!!!
- const size_t capacity = 3 * JSON_ARRAY_SIZE(1) + 4 * JSON_OBJECT_SIZE(2) + 80; //Bien penser de modifier ce paramétre
- DynamicJsonDocument doc(capacity);
- deserializeJson(doc, json);
- const char *message = doc["message"]; // "test"
- const int arrosage_0_droite_0_zone_1 = doc["arrosage"][0]["droite"][0]["zone_1"]; // 100
- const int arrosage_0_droite_0_zone_2 = doc["arrosage"][0]["droite"][0]["zone_2"]; // 130
- const int arrosage_0_gauche_0_zone_1 = doc["arrosage"][0]["gauche"][0]["zone_1"]; // 30
- const int arrosage_0_gauche_0_zone_2 = doc["arrosage"][0]["gauche"][0]["zone_2"]; // 200
- //__________Mesures__________________________________//
- pinMode(A0, INPUT);
- int HS_Z1 = (analogRead(A0));
- //__________Envoie des infos_________________________//
- Serial.print("HS_Z1 : ");
- Serial.println(HS_Z1);
- Serial.println((String) "droite_0_zone_1 : " + arrosage_0_droite_0_zone_1);
- Serial.println((String) "droite_0_zone_2 : " + arrosage_0_droite_0_zone_2);
- Serial.println((String) "gauche_0_zone_1 : " + arrosage_0_gauche_0_zone_1);
- Serial.println((String) "gauche_0_zone_2 : " + arrosage_0_gauche_0_zone_2);
- Udp.print("{\"message\":\"test\",\"arrosage\":[{\"droite\":[{\"zone_1\":"); //!!!Infos ici!!!! : https://en.cppreference.com/w/cpp/language/escape!!
- Udp.print(arrosage_0_droite_0_zone_1);
- Udp.print(",\"zone_2\":");
- Udp.print(arrosage_0_droite_0_zone_2);
- Udp.print("}],\"gauche\":[{\"zone_1\":");
- Udp.print(HS_Z1);
- Udp.print(",\"zone_2\":200}]}]}");
- //__________Déclenchement de l'arrosage_______________//
- /* if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1 && (millis() - prevMillis > declenchement_arrosage)) */
- if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1)
- {
- arrosage(1);
- }
- else
- {
- arrosage(0);
- }
- delay(5000);
- }
- void arrosage(int zone)
- {
- if (zone == 1)
- {
- Serial.println((String) "arrosage zone " + zone);
- digitalWrite(pin_vanne, HIGH);
- digitalWrite(pin_zone_1, LOW);
- delay(temps_darrosage);
- Serial.println((String)"millis : " + millis());
- Serial.println((String)"millis() - prevMillis : " + (millis() - prevMillis));
- if (millis() - prevMillis > declenchement_arrosage)
- {
- prevMillis = millis(); // RAZ
- //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_._
- digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone_1, HIGH);
- }
- }
- else
- {
- digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone_1, HIGH);
- digitalWrite(pin_zone_2, HIGH);
- digitalWrite(pin_zone_3, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement