// Inclure les bibliothèques nécessaires
#include <RTClib.h>
#include <HTTPClient.h>
#include <WiFi.h>
// Définir les constantes et les broches
const int LED_PIN = 13;
const int led =18;
const int pir =19;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int proxyPort = 3000;
const char* serverAddress = "host.wokwi.internal";
// Initialiser les objets
RTC_DS1307 rtc;
// Déclarer les variables globales
bool arrosoirActif = false;
//Methodes
void afficheDateHeure() {
DateTime now = rtc.now();
Serial.print("Date: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print("Heure: ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
pinMode(led,OUTPUT);
pinMode(pir,INPUT);
}
void gestionArrosoir() {
DateTime now = rtc.now();
// Activer l'arrosoir à 08h00:00
if (now.hour() == 8 && now.minute() == 0 && now.second() == 0) {
arrosoirActif = true;
Serial.print(" Arrosoir auto : Actif");
Serial.println();
}
// Désactiver l'arrosoir après 2 minutes
if (arrosoirActif && (now.hour() * 60 + now.minute() >= 8 * 60 + 1)) {
arrosoirActif = false;
Serial.println(" Arrosoir auto : HS");
}
// Contrôler la LED en fonction de l'état de l'arrosoir
digitalWrite(LED_PIN, arrosoirActif ? HIGH : LOW);
}
void envoyerDonnees(bool arrosoirActif) {
if (WiFi.status() == WL_CONNECTED && checkServerConnection()) {
sendToServer(arrosoirActif);
}
}
bool checkServerConnection() {
WiFiClient client;
bool success = client.connect(serverAddress, 3000);
if (!success) {
Serial.println("Erreur lors de la connexion au serveur");
}
return success;
}
void sendToServer(bool arrosoirActif) {
HTTPClient http;
// Données à envoyer
String dataToSend = "type=circuit2&arrosoirActif=" + String(arrosoirActif);
// Connexion au serveur
if (http.begin(serverAddress, 80)) { // Spécifiez le port 80 pour HTTP
Serial.println("Serveur trouvé !");
// Envoi de la requête POST
int httpResponseCode = http.POST(dataToSend);
if (httpResponseCode > 0) {
Serial.print("HTTP Reponse code: ");
Serial.println(httpResponseCode);
} else {
Serial.println("Erreur lors de l'envoi");
}
http.end();
} else {
Serial.println("Serveur non trouvé !");
}
http.end();
}
//Principale
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Solution IOT Part III!");
WiFi.begin(ssid, password);
pinMode(LED_PIN, OUTPUT);
if (!rtc.begin()) {
Serial.println("Could not find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running!");
}
// Réglez l'heure à 08h00:00 en utilisant la fonction adjust
rtc.adjust(DateTime(2024, 5, 10, 8, 0, 0));
}
void loop() {
afficheDateHeure();
gestionArrosoir();
envoyerDonnees(arrosoirActif);
delay(1000); // this speeds up the simulation
const int IP=digitalRead(pir);
if(IP==1){
digitalWrite(led,HIGH);
delay(1000);
}
else{
digitalWrite(led,LOW);
delay(1000);
}
}