// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3 #include "EmonLib.h" // Include Emon Library EnergyMonitor emon_conso; // Create an instance EnergyMonitor emon_solar; #define NB_MESURES 50 int lectures = -10; double sum_conso = 0; double sum_solar = 0; double last_conso_value = 0; double last_solar_value = 0; const int buttonPin = 2; // the number of the pushbutton pin boolean isDark = false; #include //#include #include "LowPower.h" #define I2C_SLAVE_ADDRESS 0x0B // 12 pour l'esclave 2 et ainsi de suite #define PAYLOAD_SIZE 2 //LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display #include ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); emon_conso.current(0, 60); // Current: input pin, calibration. emon_solar.current(1, 60); Wire.begin(I2C_SLAVE_ADDRESS); Serial.begin(9600); Serial.println("Slave -------------------------------------I am Slave1"); delay(1000); Wire.onRequest(requestEvents); Wire.onReceive(receiveEvents); pinMode(buttonPin, INPUT); pinMode(6, OUTPUT); // lcd.init(); // initialize the lcd delay(500); } int bcl = 0; void loop() { if (isDark) { Serial.println("Slave Dark, go to sleep"); delay(100); LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); //LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF); } button.loop(); // MUST call the loop() function first int btnState = button.getState(); //Serial.println(btnState); double Irms_conso = emon_conso.calcIrms(1480); // Calculate Irms_conso only double Irms_solar = emon_solar.calcIrms(1480); // Calculate Irms_conso only lectures ++; delay(10); if (lectures > 0) { sum_conso += Irms_conso; sum_solar += Irms_solar; if (lectures == NB_MESURES) { last_conso_value = sum_conso / lectures; last_solar_value = sum_solar / lectures; lectures = -10; sum_conso = 0; sum_solar = 0; //delay(2000); } } // Serial.println("Slave Conso :" + String(bcl) + " " + String(sum_conso / lectures)); // Serial.println("Slave Solaire:" + String(sum_solar / lectures)); bcl ++; // // check if the pushbutton is pressed. If it is, the buttonState is HIGH: // if (btnState == 0) { // // digitalWrite(6, HIGH); // // lcd.backlight(); //// for(int i=1024; i>0; i-=25){ //// analogWrite(6, i); //// delay(500); //// lcd.clear(); //// lcd.setCursor(0,0); //// lcd.print("i " + String(i)); //// } // } // else { // lcd.noBacklight(); // // digitalWrite(6, LOW); // } // //lcd.backlight(); // if (lectures > 0) { // lcd.clear(); // lcd.setCursor(0,0); // lcd.print("Conso :" + String(bcl) + " " + String(sum_conso / lectures)); // lcd.setCursor(0,1); // lcd.print("Solaire:" + String(sum_solar / lectures)); // } } void requestEvents() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second Serial.println(F("---> received request")); Serial.print(F("sending value : ")); char TempString[10]; // Hold The Convert Data dtostrf(last_conso_value,2,2,TempString); Serial.println(TempString); Wire.write(TempString); Wire.write(";"); dtostrf(last_solar_value,2,2,TempString); Serial.println(TempString); Wire.write(TempString); Wire.write("\n"); digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); } void receiveEvents(int numBytes) { Serial.println(F("---> received events")); // int n = Wire.read(); // Serial.print(numBytes); // Serial.println(F("bytes received")); // Serial.print(F("received value : ")); // Serial.println(n); String data = ""; while(Wire.available()) // loop through all but the last { char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character data = data + c; } if (data == "jour") { isDark = false; } if (data == "nuit") { isDark = true; } }