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
- //Attendre 10 secondes
- //Rebrancher le câble réseau
- //#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_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 = 14;
- 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;
- int HS_Z2;
- int HS_Z3;
- int HS_Z4;
- int n = 0;
- int CA = 0; // Cycle d'arrosage
- int CA1 = 0;
- 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_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_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;
- unsigned long prevMillis = 0;
- unsigned long prevMillis1 = 0;
- int temps_darrosage = 5000;
- int declenchement_arrosage = 10000;
- void loop()
- {
- //__________Compteur_________________________//
- if ((millis() - prevMillis4) > 1000)
- {
- prevMillis4 = millis();
- n++;
- CA++;
- }
- Serial.print("action ");
- Serial.println(n);
- Serial.println((String) "Cycle d'arrosage: " + CA);
- Serial.println(HS_Z1);
- //__________Programme principal_________________________//
- // 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);
- }
- // Si "action"=5 je lance le programme principal
- /* if (n >= 5)
- { */
- //__________Mesures__________________________________//
- if (n < 5 && n > 15)
- {
- digitalWrite(pin_mesure, LOW);
- }
- if (n >= 5 && n <= 15)
- {
- digitalWrite(pin_mesure, HIGH);
- HS_Z1 = (analogRead(A2));
- HS_Z2 = (analogRead(A3));
- HS_Z3 = (analogRead(A4));
- HS_Z4 = (analogRead(A5));
- }
- if (n > 15)
- {
- 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) + 3 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(6) + 110;
- DynamicJsonDocument doc(capacity);
- deserializeJson(doc, json);
- const char *message = doc["message"]; // "test"
- JsonObject arrosage_0_droite_0 = doc["arrosage"][0]["droite"][0];
- int arrosage_0_droite_0_zone_1 = arrosage_0_droite_0["zone_1"]; // 10
- int arrosage_0_droite_0_zone_2 = arrosage_0_droite_0["zone_2"]; // 20
- int arrosage_0_droite_0_zone_3 = arrosage_0_droite_0["zone_3"]; // 30
- int arrosage_0_droite_0_zone_4 = arrosage_0_droite_0["zone_4"]; // 40
- int arrosage_0_droite_0_zone_5 = arrosage_0_droite_0["zone_5"]; // 50
- int arrosage_0_droite_0_zone_6 = arrosage_0_droite_0["zone_6"]; // 60
- int arrosage_0_gauche_0_zone_1 = doc["arrosage"][0]["gauche"][0]["zone_1"]; // 30
- int arrosage_0_gauche_0_zone_2 = doc["arrosage"][0]["gauche"][0]["zone_2"]; // 200
- //__________Envoie des infos_________________________//
- Serial.println((String) "HS_Z1 : " + HS_Z1);
- Serial.println((String) "HS_Z2 : " + HS_Z2);
- Serial.println((String) "HS_Z3 : " + HS_Z3);
- Serial.println((String) "HS_Z4 : " + HS_Z4);
- 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) "droite_0_zone_3 : " + arrosage_0_droite_0_zone_3);
- Serial.println((String) "droite_0_zone_4 : " + arrosage_0_droite_0_zone_4);
- Serial.println((String) "droite_0_zone_5 : " + arrosage_0_droite_0_zone_5);
- Serial.println((String) "droite_0_zone_6 : " + arrosage_0_droite_0_zone_6);
- // 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(",\"zone_3\":");
- Udp.print(arrosage_0_droite_0_zone_3);
- Udp.print(",\"zone_4\":");
- Udp.print(arrosage_0_droite_0_zone_4);
- Udp.print(",\"zone_5\":");
- Udp.print(arrosage_0_droite_0_zone_5);
- Udp.print(",\"zone_6\":");
- Udp.print(arrosage_0_droite_0_zone_6);
- Udp.print("}],\"mesure\":[{\"zone_1\":");
- Udp.print(HS_Z1);
- Udp.print(",\"zone_2\":");
- Udp.print(HS_Z2);
- Udp.print(",\"zone_3\":");
- Udp.print(HS_Z3);
- Udp.print(",\"zone_4\":");
- Udp.print(HS_Z4);
- Udp.print("}]}]}");
- //__________Déclenchement de l'arrosage_______________//
- /* Serial.println((String) "arrosage_0_droite_0_zone_1 après mesures : " + arrosage_0_droite_0_zone_1);
- */
- //if (CA >= 120 && CA <= 160)
- if (CA >= 20 && CA <= 80)
- {
- Serial.println((String) "arrosage_0_droite_0_zone_1 dans cyle arrosage : " + arrosage_0_droite_0_zone_1);
- Serial.println((String) "HS_Z1 dans cyle arrosage : " + HS_Z1);
- //__________Zone 1_______________//
- if (arrosage_0_droite_0_zone_1 > 0 && HS_Z1 < arrosage_0_droite_0_zone_1)
- {
- CA1++;
- Serial.println((String) "Arrosage zone_1: " + CA1);
- digitalWrite(pin_zone_1, LOW);
- }
- //__________Zone 2_______________//
- if (arrosage_0_droite_0_zone_2 > 0 && HS_Z2 < arrosage_0_droite_0_zone_2)
- {
- CA1++;
- Serial.println((String) "Arrosage zone_2: " + CA1);
- digitalWrite(pin_zone_2, LOW);
- }
- //__________Zone 3_______________//
- if (arrosage_0_droite_0_zone_3 > 0 && HS_Z3 < arrosage_0_droite_0_zone_3)
- {
- CA1++;
- Serial.println((String) "Arrosage zone_3: " + CA1);
- digitalWrite(pin_zone_3, LOW);
- }
- //__________Zone 4_______________//
- if (arrosage_0_droite_0_zone_4 > 0 && HS_Z3 < arrosage_0_droite_0_zone_4)
- {
- CA1++;
- Serial.println((String) "Arrosage zone_4: " + CA1);
- digitalWrite(pin_zone_4, LOW);
- }
- //__________Zone 5_______________//
- if (arrosage_0_droite_0_zone_5 > 0 && HS_Z3 < arrosage_0_droite_0_zone_5)
- {
- CA1++;
- Serial.println((String) "Arrosage zone_5: " + CA1);
- digitalWrite(pin_zone_5, LOW);
- }
- //__________Zone 6_______________//
- if (arrosage_0_droite_0_zone_6 > 0 && HS_Z3 < arrosage_0_droite_0_zone_6)
- {
- CA1++;
- Serial.println((String) "Arrosage zone_6: " + CA1);
- digitalWrite(pin_zone_6, LOW);
- }
- }
- if (CA >= 80)
- {
- 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);
- }
- if (CA >= 200)
- {
- CA = 0;
- }
- if (n == 201)
- {
- n = 0;
- }
- }
- }
- /*
- unsigned long prevMillis = 0;
- unsigned long prevMillis1 = 0;
- int stop_Z1 = 0;
- int temps_darrosage = 5000;
- int declenchement_arrosage = 10000;
- //__________Arrosage zone 1__________________________________//
- void arrosage_zone_1(int zone)
- {
- if (zone == 1)
- {
- int pin_zone = zone + 1;
- Serial.println((String) "pin_zone : " + pin_zone);
- Serial.println((String) "millis() - prevMillis: " + (millis() - prevMillis));
- if (millis() - prevMillis >= temps_darrosage && stop_Z1 != 1 )
- {
- digitalWrite(pin_zone, HIGH);
- prevMillis = millis(); // RAZ
- Serial.println((String) "millis() - prevMillis: " + (millis() - prevMillis));
- }
- if (millis() - prevMillis1 >= (declenchement_arrosage + temps_darrosage))
- {
- digitalWrite(pin_zone, LOW);
- prevMillis1 = millis(); // RAZ
- stop_Z1 = 1;
- }
- }
- }
- //__________Arrosage zone 2__________________________________//
- 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_zone, LOW);
- prevMillis2 = millis(); // RAZ
- }
- if (millis() - prevMillis3 >= (declenchement_arrosage + temps_darrosage))
- {
- digitalWrite(pin_zone, HIGH);
- prevMillis3 = millis(); // RAZ
- }
- }
- } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement