//#include #include #include "DHT.h" //Wire.setClock(100000); #define DEBUG true #define SEND_MESSAGE_DELAY 30000 // Ne pas dépasser 32000 !! Delay in ms between each value's extraction //#define SEND_433_PAUSE 160 // 16 multiple #define DEBUG true #define DHTPIN A2 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11 float temperature = 0.0; float pressure = 0.0; float pression = 0.0; float presiune = 0.0; float humidite = 0.0; // ESP8266 String NomduReseauWifi = "Livebox-37cc"; // Garder les guillements String MotDePasse = "8A6060920A8A86896F770F2C47"; // Garder les guillements boolean done = false; /****************************************************************/ /* INIT */ /****************************************************************/ void setupESP() { #ifdef DEBUG Serial.begin(9600); #endif ESP8266.begin(115200); envoieAuESP8266("AT+RST"); recoitDuESP8266(2000); envoieAuESP8266("AT+CIOBAUD=9600"); recoitDuESP8266(2000); ESP8266.begin(9600); initESP8266(); } /****************************************************************/ /* Fonction qui initialise l'ESP8266 */ /****************************************************************/ void initESP8266() { pinMode(13, OUTPUT); pinMode(3, OUTPUT); digitalWrite(3, HIGH); debugPrintln("**************** DEBUT DE L'INITIALISATION ***************"); envoieAuESP8266("AT"); recoitDuESP8266(1000); envoieAuESP8266("AT+CWMODE=3"); recoitDuESP8266(1000); envoieAuESP8266("AT+CWJAP=\"" + NomduReseauWifi + "\",\"" + MotDePasse + "\""); recoitDuESP8266(10000); envoieAuESP8266("AT+CIFSR"); recoitDuESP8266(1000); envoieAuESP8266("AT+CIPMUX=1"); recoitDuESP8266(1000); envoieAuESP8266("AT+CIPSERVER=1,80"); recoitDuESP8266(1000); debugPrintln("***************** INITIALISATION TERMINEE ****************"); debugPrintln(""); } /****************************************************************/ /* Fonction qui envoie une commande à l'ESP8266 */ /****************************************************************/ void envoieAuESP8266(String commande) { // debugPrintln(commande); ESP8266.println(commande); } /****************************************************************/ /*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/ /****************************************************************/ void recoitDuESP8266(const int timeout) { String reponse = ""; long int time = millis(); while ( (time + timeout) > millis()) { while (ESP8266.available()) { char c = ESP8266.read(); reponse += c; } } debugPrintln(reponse); } // FIN ESP8266 void recup() { // Initialize DHT sensor for normal 16mhz Arduino // DHT dht(DHTPIN, DHTTYPE); // // DHT // dht.begin(); // // Commande pour barometre humidité température // // Virtual Device // // http://192.168.0.10:8080/json.htm?type=command¶m=udevice&idx=160&nvalue=0&svalue=23.3;50;2;1024.20;1024&battery=89 // // // // Reading temperature or humidity takes about 250 milliseconds! // // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) // float h = dht.readHumidity(); // humidite = h; // // // Read temperature as Celsius // float t = dht.readTemperature(); // // Read temperature as Fahrenheit // float f = dht.readTemperature(true); // // Check if any reads failed and exit early (to try again). // if (isnan(h) || isnan(t) || isnan(f)) { // debugPrintln("Failed to read from DHT sensor!"); // return; // } else { // // Compute heat index // // Must send in temp in Fahrenheit! // float hi = dht.computeHeatIndex(f, h); // // debugPrint("Humidity: "); // debugPrint(String(h)); // debugPrint(" %\t"); // debugPrint("Temperature: "); // debugPrint(String(t)); // debugPrint(" *C "); // debugPrint(String(f)); // debugPrint(" *F\t"); // debugPrint("Heat index: "); // debugPrint(String(hi)); // debugPrintln(" *F"); // // } // // long vcc = readVcc(); // // // ################# Barometre #### // Adafruit_BMP085 bmp; // // ##################### // // BMP // if (bmp.begin()) { // temperature = bmp.readTemperature(); // pressure = bmp.readPressure() / 100.0; // pression = pressure / 101.325; // pression = pression * 0.760 * 100; // // http://en.wikipedia.org/wiki/Atmospheric_pressure#Mean_sea_level_pressure // // debugPrint("Presiure la nivelul marii (calculata) = "); // presiune = bmp.readSealevelPressure(19) / 101.325; // presiune = presiune * 0.760; // // debugPrint("Temperature="); debugPrintln(String(temperature)); // debugPrint("pressure="); debugPrintln(String(pressure)); // debugPrint("pression="); debugPrintln(String(pression)); // } // // // LUX // // R=K*L^-gamma // // R étant la résistance pour un niveau d'éclairement L. // int lum = analogRead(1); // int lux = (1000.0 * lum / 1024.0); // debugPrint("Luminosite="); // debugPrintln(String(lum)); } void setup() { pinMode(13, OUTPUT); } int boucle = 0; void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(10); // wait digitalWrite(13, LOW); // recup(); // ESP8266 // ESP8266 setupESP(); // // sendToDomoticz(122,temperature); // sendToDomoticz(121,pressure); // sendToDomoticz(123,pression); // sendToDomoticz(158,150); //lux); envoie("/json.htm?type=command¶m=udevice&idx=158&svalue=255"); //envoie("/json.htm?type=devices&rid=" + String(290)); //sendToDomoticz(288,234 + boucle * 5); boucle++; // FIN ESP8266 delay(30000); // Narcoleptic.delay(SEND_MESSAGE_DELAY); // Narcoleptic.delay(SEND_MESSAGE_DELAY); // Narcoleptic.delay(SEND_MESSAGE_DELAY); // Narcoleptic.delay(SEND_MESSAGE_DELAY); } void debugPrint(String m) { #ifdef DEBUG Serial.print(m); #endif } void debugPrintln(String m) { #ifdef DEBUG Serial.println(m); #endif } //void sendToDomoticz(int id, int val) { // envoie("/json.htm?type=command¶m=udevice&idx=" + String(id) + "&svalue=" + String(val)); //} // // ////-------------------------------------------------------------------------------------------------- //// Read current supply voltage ////-------------------------------------------------------------------------------------------------- //long readVcc() { // bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC // long result; // // Read 1.1V reference against Vcc //#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) // ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84 //#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) // ADMUX = _BV(MUX3) | _BV(MUX2); //#else // ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328 //#endif // // ADCSRB = 0; // // delay(2); // Wait for Vref to settle // ADCSRA |= _BV(ADSC); // Convert // while (bit_is_set(ADCSRA, ADSC)); // result = ADCL; // result |= ADCH << 8; // result = 1126400L / result; // Back-calculate Vcc in mV // // ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power // // // analogReference(DEFAULT); // // return result; // Vcc in millivolts //} void connection() { debugPrintln("***************** Connexion ****************"); /** * Faire un HTTP GET */ debugPrintln("***************** GET ******************************"); String cmd = "AT+CIPSTART=4,\"TCP\",\"192.168.0.10\",8080"; envoieAuESP8266(cmd); recoitDuESP8266(2000); } void envoie(String url) { connection(); int maxLoops = 10; int currentLoop = 0; //String url = "/json.htm?type=devices&rid=" + String(idRadiateurInDomoticz); String cmdGET = "GET " + url + " HTTP/1.1\r\n" + "Host: 192.168.0.10\r\nUser-Agent: ESP8266_HTTP_Client\r\nConnection: close\r\n\r\n"; debugPrintln("***************Commande GET **********************"); // demande d'envoi ESP8266.print("AT+CIPSEND=4,"); ESP8266.println(cmdGET.length()); delay(500); done = ESP8266.find(">"); currentLoop = 0; while (!done) { delay(500); done = ESP8266.find(">"); if (currentLoop >= maxLoops) { //debugPrintln(" Attente dépassée"); break; } currentLoop++; //debugPrint("."); } // requete envoieAuESP8266(cmdGET + "\r\n\r\n"); // recoitDuESP8266(8000); String ret = recoitDuESP8266WithJson(10000, "status"); debugPrintln("** ret=" + ret); if (ret.equalsIgnoreCase("On")) { digitalWrite(3, HIGH); digitalWrite(13, HIGH); } else if (ret.equalsIgnoreCase("Off")) { digitalWrite(3, LOW); digitalWrite(13, LOW); } deconnexion(); } void deconnexion() { //////////////////////////////// // sendToDomoticz("288", "15", "225"); /////////////////////////////// envoieAuESP8266("AT+CIPSTATUS"); recoitDuESP8266(2000); // Close all connections envoieAuESP8266("AT+CIPCLOSE=5"); recoitDuESP8266(2000); // restart from zero envoieAuESP8266("AT"); recoitDuESP8266(2000); // 4 secondes déjà passées delay(200); } /****************************************************************/ /*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/ /****************************************************************/ String recoitDuESP8266WithJson(const int timeout, String jsonId) { String ret = ""; String reponse = ""; boolean found = false; long int time = millis(); while ( (time + timeout) > millis()) { while (ESP8266.available()) { //reponse = ""; char c = ESP8266.read(); reponse += c; //reponse.trim(); if (reponse.equalsIgnoreCase(jsonId)) { debugPrint("Trouve" + reponse + " " + jsonId); } if (c == 10 || c == 13 || reponse.length() > 80) { reponse.trim(); // debugPrint(reponse); int p = reponse.indexOf(':'); String id = reponse.substring(0, p); String value = reponse.substring(p + 1, reponse.length() - 1); id.trim(); value.trim(); id.replace("\"", ""); value.replace("\"", ""); if (id.equalsIgnoreCase(jsonId) && !found) { debugPrintln("========> Trouve " + jsonId + " == " + value); ret = value; found = true; } else { debugPrintln(id + " " + value); } reponse = ""; } } } //debugPrintln(reponse); return ret; }