Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.youtube.com/watch?v=SWirrQH-48c
- //https://recitmst.qc.ca/arduino/construire-sa-sonde-dhumidite-du-sol/?fbclid=IwAR2Oi_5zf6TPedipApf_IFJ9490UGdbTzNldNbT3DhHfQ4ulwnL1MD60Ig4
- //http://gardenbot.org/howTo/soilMoisture/
- //_____________Utilisation de la carte W5100________________//
- //Débrancher le câble réseau
- //Apuyer sur le bouton reset
- //Rebrancher le câble réseau
- /*
- 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> //!!!ATTENTTION!!! en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t _cspin" dans la bibliothéque
- #include <Ethernet.h> //!!!ATTENTTION!!! en fonction du "shield" utilisé il peut être nécessaire de changer le "uint8_t sspin" dans la bibliothéque
- //#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
- //#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
- #include <EthernetUdp.h>
- #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
- //int pin_vanne = 2;
- int pin_zone_1 = 2;
- int pin_zone_2 = 3;
- int pin_zone_3 = 4;
- int pin_zone_4 = 5;
- int pin_zone_5 = 6;
- int pin_zone_6 = 7;
- int pin_mesure = 8;
- int pin_zone;
- 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 HS_Z1;
- boolean stop_arrosage_zone_1;
- boolean stop_arrosage_zone_2;
- // 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);
- pinMode(pin_zone_4, OUTPUT);
- pinMode(pin_zone_5, OUTPUT);
- pinMode(pin_zone_6, OUTPUT);
- pinMode(pin_mesure, OUTPUT);
- pinMode(pin_zone, OUTPUT);
- //digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone_1, HIGH);
- digitalWrite(pin_zone_2, HIGH);
- digitalWrite(pin_zone_3, HIGH);
- digitalWrite(pin_zone_4, HIGH);
- digitalWrite(pin_zone_5, HIGH);
- digitalWrite(pin_zone_6, HIGH);
- digitalWrite(pin_mesure, HIGH);
- Serial.println("OK Test");
- /* 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); */
- }
- unsigned long prevMillis4 = 0;
- void loop()
- {
- Serial.println("OK 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);
- Serial.println((String) "prevMillis4 avant mesure : " + prevMillis4);
- if (millis() - prevMillis4 >= 1000)
- {
- prevMillis4 = millis(); // RAZ
- digitalWrite(pin_mesure, LOW);
- Serial.println("Mesures");
- Serial.println((String) "prevMillis4 : " + prevMillis4);
- Serial.println((String) "millis : " + millis());
- }
- else
- {
- digitalWrite(pin_mesure, HIGH);
- }
- int HS_Z1 = (analogRead(A2));
- //__________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)
- {
- arrosage_zone_1(1);
- stop_arrosage_zone_1 = 0;
- }
- else
- {
- stop_arrosage_zone_1 = 1;
- }
- if (arrosage_0_droite_0_zone_2 > 0 && HS_Z1 < arrosage_0_droite_0_zone_2)
- {
- arrosage_zone_2(1);
- stop_arrosage_zone_2 = 0;
- }
- else
- {
- stop_arrosage_zone_2 = 1;
- //arret_arrosage();
- /* digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone_1, HIGH);
- digitalWrite(pin_zone_2, HIGH);
- digitalWrite(pin_zone_3, HIGH); */
- }
- if (stop_arrosage_zone_1 == 1 && stop_arrosage_zone_2 == 1)
- {
- Serial.print("Stop arrosage");
- // digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone_1, HIGH);
- digitalWrite(pin_zone_2, HIGH);
- digitalWrite(pin_zone_3, HIGH);
- digitalWrite(pin_zone_4, HIGH);
- digitalWrite(pin_zone_5, HIGH);
- digitalWrite(pin_zone_6, HIGH);
- }
- delay(5000);
- }
- unsigned long prevMillis = 0;
- unsigned long prevMillis1 = 0;
- int temps_darrosage = 5000;
- int declenchement_arrosage = 10000;
- void arrosage_zone_1(int zone)
- {
- if (zone == 1)
- {
- int pin_zone = zone + 1;
- Serial.println((String) "pin_zone : " + pin_zone);
- if (millis() - prevMillis >= temps_darrosage)
- {
- // digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone, HIGH);
- prevMillis = millis(); // RAZ
- }
- if (millis() - prevMillis1 >= (declenchement_arrosage + temps_darrosage))
- {
- // digitalWrite(pin_vanne, HIGH);
- digitalWrite(pin_zone, LOW);
- prevMillis1 = millis(); // RAZ
- }
- }
- }
- unsigned long prevMillis2 = 0;
- unsigned long prevMillis3 = 0;
- void arrosage_zone_2(int zone)
- {
- if (zone == 1)
- {
- int pin_zone = zone + 2;
- Serial.println((String) "pin_zone : " + pin_zone);
- if (millis() - prevMillis2 >= temps_darrosage)
- {
- // digitalWrite(pin_vanne, LOW);
- digitalWrite(pin_zone, HIGH);
- prevMillis2 = millis(); // RAZ
- }
- if (millis() - prevMillis3 >= (declenchement_arrosage + temps_darrosage))
- {
- // digitalWrite(pin_vanne, HIGH);
- digitalWrite(pin_zone, LOW);
- prevMillis3 = millis(); // RAZ
- }
- }
- }
- /*void arret_arrosage()
- {
- 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