#include #include #include #include #include #include #include ////////////////////////////////////////// // WIFI ////////////////////////////////////////// #ifndef STASSID #define STASSID "Livebox-37cc" #define STAPSK "8A6060920A8A86896F770F2C47" #endif const char* ssid = STASSID; const char* password = STAPSK; WebServer server(80); ////////////////////////////////////////// // DALLAS ///////////////////////////////////////// //#define DALLAS true #ifdef DALLAS #include #include #define onewirepin 5 // DATA pin of DS18B20 wired to pin 10 of Arduino OneWire oneWire(onewirepin); DallasTemperature sensors(&oneWire); // find the DeviceAddress of your DS18B20 with the sketch DS18B20_address_reporter // then replace the 8-byte ID below with the reported one DeviceAddress Probe = { 0x28, 0xFF, 0x61, 0x1D, 0x76, 0x04, 0x00, 0x34 }; #endif #define moistPin 36 //////////////////////////////////////// // DHT //////////////////////////////////////// #ifdef DHT #include "DHT.h" #define DHTPIN 5 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); #endif //////////////////////////////////////// // PINS //////////////////////////////////////// #define LUM_PIN 33 #define LED_PIN 3 #define RELAY_PIN 26 #define BUTTON_PIN 12 const char* htmlCode = R"( Courbe en HTML )"; void handleRoot(); // Tableau pour stocker les températures const int capaciteTableau = 2000; // Ajustez selon vos besoins float temperatures[capaciteTableau]; float illuminances[capaciteTableau];// Tableau pour stocker les illuminances int indexTableau = 0; float temperature = 0; float humidity_air = 0; void setup() { // initialize digital pin LED_BUILTIN as an output. Serial.begin(9600); // OTA Serial.println("Booting"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } // Définissez les gestionnaires pour les différentes URL server.on("/", HTTP_GET, handleRoot); // server.on("/getData", HTTP_GET, handleData); // Démarrer le serveur server.begin(); Serial.println("Serveur Web démarré"); // Port defaults to 8266 ArduinoOTA.setPort(8266); // Hostname defaults to esp8266-[ChipID] // ArduinoOTA.setHostname("myesp8266"); // No authentication by default // ArduinoOTA.setPassword("admin"); // Password can be set with it's md5 value as well // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { // U_FS type = "filesystem"; } // NOTE: if updating FS this would be the place to unmount FS using FS.end() Serial.println("Start updating " + type); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { Serial.println("Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { Serial.println("Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { Serial.println("Receive Failed"); } else if (error == OTA_END_ERROR) { Serial.println("End Failed"); } }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // END OTA pinMode(RELAY_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT); pinMode(LUM_PIN, INPUT); pinMode(BUTTON_PIN, INPUT); pinMode(moistPin, INPUT); //pinMode(10, INPUT); #ifdef DALLAS sensors.begin (); // Initialize the sensor and set resolution level sensors.setResolution(Probe, 10); #endif delay(1000); Serial.println(); #ifdef DALLAS Serial.print ("Number of Devices found on bus = "); Serial.println (sensors.getDeviceCount()); Serial.print ("Getting temperatures... "); Serial.println (); #endif #ifdef DHT /** DHT **/ dht.begin(); #endif // For sleep // CLKPR = 0x80; // CLKPR = 0x01; stop(); // Initialisation du tableau avec des valeurs harmonieuses for (int i = 0; i < capaciteTableau; i++) { temperatures[i] = 5.0 * sin(2.0 * 3.14159 * i / capaciteTableau) + 20.0; } } void loop() { ArduinoOTA.handle(); server.handleClient(); // Reads the information from the CI #ifdef DHT /** * DHT11 : Temperature */ temperature = dht.readTemperature(); humidity_air = dht.readHumidity(); #endif #ifdef DALLAS sensors.requestTemperatures(); // Command all devices on bus to read temperature // Serial.print("Temperature is: "); temperature = printTemperature(Probe); //Serial.println(); #endif // Stocker les valeurs dans les tableaux int lum = analogRead(LUM_PIN); int btn_status = digitalRead(BUTTON_PIN); // int tmp = digitalRead(10); int moistVal = analogRead(moistPin); int percent = 2.718282 * 2.718282 * (.008985 * moistVal + 0.207762); //calculate percent for probes about 1 - 1.5 inches apart //Serial.println(percent); if (btn_status == 1) { avance(); //delay(1000); //stop(); } else { stop(); } float lux = 100000 * lum / 4192; // Stockage de la température dans le tableau if (temperature < 60 && temperature > -20) { // temperatures[indexTableau] = temperature; storeValue(temperature, temperatures); storeValue(lux, illuminances); indexTableau = (indexTableau + 1) % capaciteTableau; } digitalWrite(LED_PIN, HIGH); Serial.print("Humidite "); Serial.print(humidity_air); Serial.print(" température "); Serial.print(temperature,2); Serial.print(" luminosite "); Serial.print(lum); Serial.print(" lux "); Serial.print(lux); Serial.print(" bouton "); Serial.print(btn_status); Serial.print(" hum_sol "); Serial.print(moistVal); Serial.print(" %sol "); Serial.println(percent); // // Lire et imprimer l'état de tous les ports // for (int i = 0; i < 41; i++) { // int etatPort = digitalRead(i); // Serial.print("Port "); // Serial.print(i); // Serial.print(" : "); // float fvalue = analogRead(i); // // Serial.print(etatPort); // Serial.print(" : "); // Serial.println(fvalue,2); // } delay(2500); digitalWrite(LED_PIN, LOW); delay(2500); //Serial.println("Goto sleep"); // set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep_enable(); // sleep_cpu(); //Serial.println("Wake up"); } void stop() { // Serial.println("stop"); digitalWrite(RELAY_PIN, LOW); // turn the LED on (HIGH is the voltage level) delay(200); // Serial.println("stop fin"); } void avance() { Serial.println("avance"); digitalWrite(RELAY_PIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(200); //Serial.println("avance fin"); } #ifdef DALLAS float printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); // if (tempC == -127.00) // { // Serial.print ("Error getting temperature "); // } // else // { // Serial.print ("C: "); // Serial.println (tempC); // // Serial.print (" F: "); // // Serial.print(DallasTemperature::toFahrenheit(tempC)); // } return tempC; } #endif void handleRoot() { // Lecture de la température depuis le capteur DHT //float temperature = dht.readTemperature(); // Stockage de la température dans le tableau temperatures[indexTableau] = temperature; indexTableau = (indexTableau + 1) % capaciteTableau; // Construction de la page HTML avec le graphique String html = "Graphique de Température"; html += ""; html += "
"; //String htmlContent = generateHTMLChart(temperatures, capaciteTableau); String htmlCode = "\n" "\n" "\n" " \n" " \n" " Courbe en HTML\n" " \n" "\n" "\n"; htmlCode += generateHTMLChart(temperatures, capaciteTableau, "temperatureChart", "#e74c3c", "Temps", "Températures (°C)"); htmlCode += generateHTMLChart(illuminances, capaciteTableau, "illuminanceChart", "#3498db", "Temps", "Illuminance (lux)"); htmlCode += ""; // Envoi de la réponse HTTP server.send(200, "text/html", htmlCode); } // Fonction pour générer la page HTML avec le graphique String generateHTML() { String html = ""; html += ""; // Ajouter le script JavaScript pour le graphique html += ""; html += "
"; return html; } // Fonction pour convertir un tableau en chaîne de caractères String arrayToString(float array[]) { String result = ""; for (int i = 0; i < 100; i++) { result += String(array[i], 2); if (i < 99) { result += ", "; } } return result; } // Fonction pour stocker une nouvelle valeur dans le tableau circulaire void storeValue(float newValue, float array[]) { array[indexTableau] = newValue; //index = (index + 1) % capaciteTableau; // Taille maximale du tableau } //String generateHTMLChart(float temperatures[], int n) { // String htmlCode = "\n" // "\n" // "\n" // " \n" // " \n" // " Courbe en HTML\n" // " \n" // "\n" // "\n" // " \n" // "\n" // " \n" // "\n" // ""; // // return htmlCode; //} #include String generateHTMLChart(float data[], int n, const char* chartId, const char* chartColor, const char* xLabel, const char* yLabel) { String htmlCode = "\n" "\n" "\n" "\n" "\n"; return htmlCode; }