#include #include // for itoa() call #include // for printf() call #include ESP8266WiFiMulti WiFiMulti; const uint16_t port = 81; const char * host = "192.168.1.3"; // ip or dns int buttonState = 0; // variable for reading the pushbutton status int aiValue = 0; // input value const int buttonPin = 14; // the number of the pushbutton pin #include #define ONE_WIRE_BUS D4 // 2 for esp01 4 for esp12 // DS18B20 pin #define PIN_ALIM_DS D3 // 1 for esp01 5 for esp12 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); // DEMUX #define MUX_A D5 #define MUX_B D6 #define MUX_C D7 #define ANALOG_INPUT A0 // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3 #include "EmonLib.h" // Include Emon Library EnergyMonitor emon_conso; // Create an instance //EnergyMonitor emon_solar; double last_conso_value = 0; double last_solar_value = 0; void setup() { Serial.begin(9600); while (!Serial) { delay(10); } pinMode(buttonPin, INPUT); //pinMode(6, OUTPUT); //Deifne output pins for Mux pinMode(MUX_A, OUTPUT); pinMode(MUX_B, OUTPUT); pinMode(MUX_C, OUTPUT); pinMode(PIN_ALIM_DS, OUTPUT); digitalWrite(PIN_ALIM_DS, HIGH); //Wire.begin(); delay(200); Serial.print("Demarrage"); // We start by connecting to a WiFi network WiFi.mode(WIFI_STA); WiFiMulti.addAP("Livebox-37cc", "8A6060920A8A86896F770F2C47"); Serial.println(); Serial.println(); Serial.print("Wait for WiFi... "); while(WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //digitalWrite(6, HIGH); emon_conso.current(0, 30); // Current: input pin, calibration. // emon_solar.current(1, 60); } void loop() { String result = ""; double temp = readTemperature(); changeMux(LOW, LOW, LOW); double last_conso_value = emon_conso.calcIrms(1480); last_conso_value = emon_conso.calcIrms(1480); last_conso_value = emon_conso.calcIrms(1480); last_conso_value = emon_conso.calcIrms(1480); last_conso_value = emon_conso.calcIrms(1480); // Calculate Irms_conso only envoieDomoticz("1053", String(last_conso_value)); // intensite delay(200); //changeMux(LOW, LOW, HIGH); changeMux(HIGH, HIGH, HIGH); double last_solar_value = emon_conso.calcIrms(1480); last_solar_value = emon_conso.calcIrms(1480); last_solar_value = emon_conso.calcIrms(1480); last_solar_value = emon_conso.calcIrms(1480); last_solar_value = emon_conso.calcIrms(1480); last_solar_value = emon_conso.calcIrms(1480); // Calculate Irms_conso only envoieDomoticz("1086", String(last_solar_value)); // intensite delay(200); Serial.println("Conso :" + String(last_conso_value)); Serial.println("Solaire:" + String(last_solar_value)); int sleep_delay = 10; if (isDark()) { sleep_delay = 60; } if (temp > -10) { envoieDomoticz("723", String(temp)); } Serial.println("Master Mode jour"); Serial.print("Waiting "); ESP.deepSleep(sleep_delay * 1000000L); //delay(2000); } void envoieDomoticz(String IDX, String Svalue) { Serial.print("Master connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; if (!client.connect(host, port)) { Serial.println("Master connection failed"); Serial.println("Master wait 5 sec..."); delay(5000); return; } // This will send the request to the server client.print("GET /json.htm?type=command¶m=udevice&idx="+IDX+"&nvalue=0&svalue=" + Svalue); client.println(" HTTP/1.1"); client.println("Host: 192.168.1.3:81"); client.println("User-Agent: Arduino-ethernet"); client.println("Connection: close"); client.println(); //read back one line from server String line = client.readStringUntil('\r'); Serial.println("Master ----------------------------------------------"); Serial.println(line); Serial.println("Master ----------------------------------------------"); //Serial.println("Master closing connection"); client.stop(); delay(500); } boolean isDark() { String separator = ","; int nb = 0; WiFiClient client; if (client.connect(host, port)) { Serial.println("Master Connected to domoticz"); client.print("GET /json.htm?type=command¶m=getuservariable&idx=6"); client.println(" HTTP/1.1"); client.println("Host: 192.168.1.3:81"); client.println("User-Agent: Arduino-ethernet"); client.println("Connection: close"); client.println(); // Read the first line of the request boolean ret = false; while (1) { String req = client.readStringUntil('\n'); if (req.indexOf("Value") != -1) { if (req.indexOf("True") != -1) { ret = true; } break; } if (req == "" || nb > 500) { break; } nb++; } client.stop(); delay(100); return ret; } return false; } String getVariableValue(String idx) { String separator = ","; int nb = 0; WiFiClient client; if (client.connect(host, port)) { Serial.println("Master Connected to domoticz"); client.print("GET /json.htm?type=command¶m=getuservariable&idx=" + idx); client.println(" HTTP/1.1"); client.println("Host: 192.168.1.3:81"); client.println("User-Agent: Arduino-ethernet"); client.println("Connection: close"); client.println(); // Read the first line of the request String ret = ""; while (1) { String req = client.readStringUntil('\n'); if (req.indexOf("Value") != -1) { ret = req.substring(req.indexOf("Value")); Serial.println("Master Idx=" + idx + " Value " + ret); break; } if (req == "" || nb > 500) { break; } nb++; } client.stop(); delay(100); return ret; } } double readTemperature() { int bcl = 0; double temp = -127; DS18B20.requestTemperatures(); while (bcl < 10 && (temp < 0 || temp > 50)) { temp = DS18B20.getTempCByIndex(0); Serial.print("Temperature: "); Serial.println(temp); bcl++; if (temp < 0 || temp > 50) { Serial.print("Temp non lue"); Serial.println(temp); delay(100); } } return temp; } double readAnalogic(int y) { float value; if (y == 0) { changeMux(LOW, LOW, LOW); value = analogRead(ANALOG_INPUT); //Value of the sensor connected Option 0 pin of Mux delay(200); Serial.println("value 1=" + String(value)); } if (y == 1) { changeMux(LOW, LOW, HIGH); value = analogRead(ANALOG_INPUT); //Value of the sensor connected Option 1 pin of Mux delay(200); Serial.println("value 2=" + String(value)); } // S2 = 0, S1 = 1, S0 = 0, - Z = Y2 // S2 = 0, S1 = 1, S0 = 1, - Z = Y3 // S2 = 1, S1 = 0, S0 = 0, - Z = Y4 // S2 = 1, S1 = 0, S0 = 1, - Z = Y5 // S2 = 1, S1 = 1, S0 = 0, - Z = Y6 // S2 = 1, S1 = 1, S0 = 1, - Z = Y7 return value; } void changeMux(int c, int b, int a) { digitalWrite(MUX_A, a); digitalWrite(MUX_B, b); digitalWrite(MUX_C, c); delay(200); } int readValue() { int vmin = 1024; int vmax = 0; int max_bcl = 1000; int RawValue = 0; for (int i = 0; i < max_bcl; i++) { int value = analogRead(ANALOG_INPUT); if (value >= 0) { RawValue += value; vmax = max(value,vmax); vmin = min(value,vmin); } else { i--; } delay(1); } RawValue = RawValue / max_bcl; Serial.print("rawValue = " ); Serial.print(RawValue); Serial.print(" min = " ); Serial.print(vmin); Serial.print(" max = " ); Serial.println(vmax); return RawValue; }