//ESP32_8266_WiFi_server_oneWire_temperature_2sondes_020319 //Alain Bertout et Ph Loutrel //Dérivé de l'exemple de Rui Santos sur http://randomnerdtutorials.com //Web server détaillé sur http://a110a.free.fr/SPIP172/article.php3?id_article=187 //Chaque sonde a 3 fils; +3.3V, Gnd et Signal.Les deux fils Signal connectés à GPIO 5 //L'ESP mesure 2 temperatures et envoie au client une page Web via WiFi //Recommandé: attribuer une adresse fixe à l'ESP sur le LAN // pour que le client s'adresse au bon serveur quelques soit l'adresse fournie par DHCP //Choix de l'ESP: commenter une des 2 lignes suivante #include //ESP32 //#include //ESP8266 #include #include // Replace with your network details const char* ssid = "****"; // Identifiant WiFi, par ex 'Numericable-574e' const char* password = "***"; // Mot de passe WiFi // Data wire is plugged into pin on the ESP - GPIO 5( Port 2 perturbe le download) #define ONE_WIRE_BUS 5 // port ESP GPIO D5 // Setup myWire, a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire myWire(ONE_WIRE_BUS); // Pass our myWire reference to Dallas Temperature. DallasTemperature DS18B20(&myWire); float tempA , tempAp; //temp actuelle et precedente float tempB , tempBp;//fournie en flottant par le capteur String temperatureAString;//String à passer au client String temperatureBString; int total_Sensors; WiFiServer server(80); // Port http void setup() { // Initializing serial port for debugging purposes Serial.begin(115200); delay(100); // Start up the library DS18B20.begin(); delay(100); // locate devices on the bus Serial.print("Found "); Serial.print(DS18B20.getDeviceCount(), DEC); Serial.println(" devices."); // search for devices on the bus and assign based on an index. // if (!DS18B20.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); // if (!DS18B20.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); pinMode (5, INPUT); delay(10); // Connecting to WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Starting the server server.begin(); Serial.println("Server running. Waiting for the ESP IP..."); delay(10000); // Printing the ESP IP address Serial.println(WiFi.localIP()); } //////////////////////// Fonction get temperature void getTemperature() { tempAp = tempA; tempBp = tempB; total_Sensors = DS18B20.getDeviceCount(); DS18B20.requestTemperatures(); // request to all devices on the bus tempA = DS18B20.getTempCByIndex(0); temperatureAString = String(tempA);// tempA mise sous forme de string tempB = DS18B20.getTempCByIndex(1); temperatureBString = String(tempB); delay(100); if (tempA = -127) { //erreur de lecture tempA = tempAp; } if (tempB = -127) { tempB = tempBp; } } ////////////////////////////////// void loop() { // Listenning for new clients WiFiClient client = server.available(); if (client) { Serial.println("New client"); if (client.connected()) { getTemperature(); Serial.println("get temperature");// Suivi sur moniteur Serial.println("A" + temperatureAString); Serial.println("B" + temperatureBString); //********Envoi de la page Web**************** client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println(""); client.println("Affichage de temperatures "); //Pour l'onglet du navigateur //Pour eviter l'emission de 'GET /favicon.ico HTTP/1.1' à chaque requete client.println(""); client.println(" "); client.println(""); client.println("

*************Affichage de temperatures*************

"); client.println(""); //Saut de lignes //Noter les \" dans la ligne ci dessous pour inclure les double guillemets " dans la chaine // client.println("Pour afficher les temperatures : IcI
"); //Clic sur IcI enverra "GET /maString HTTP/1.1" au serveur.Cette string n'est pas exploitée client.println("

"); client.print("

**Temp1 = "); client.print( temperatureAString); client.println("

"); client.println(""); client.print("

**Temp2 = "); client.print( temperatureBString); client.println("

"); client.println(""); client.println(""); //Fin de la page delay(1); Serial.println("Client disconnected"); //Aller attendre une nouvelle demande du Client Serial.println(""); } delay(1); client.stop(); Serial.println("Client disconnected."); } }