Consommations électriques

Il faut avoir des chiffres sur les consommations électriques pour connaître les usages et planifier les économies. Je propose de construire une série de capteur, comme ceux de températures, pour suivre les consommations dans le temps

Nous allons les placer

Pistes:

Matériel

Code

/*
  // ESP_Wemos capteur de courant SCT013 yhdc.com
 
  Fonction:
  vérification et prise de mesures
  connection sur un des reseau wifi present
  se connecte au serveur MQTT
  s'abonne a une possible mise a jour
  poste en mqtt
  deep sleep
  de https://learn.openenergymonitor.org/electricity-monitoring/ct-sensors/how-to-build-an-arduino-energy-monitor-measuring-current-only
 
  1 x CT sensor YHDC SCT-013-000
  1 x Burden resistor 18 Ohms if supply voltage is 3.3V, or 33 Ohms if supply voltage is 5V
  2 x 10k Ohm resistors (or any equal value resistor pair up to 470k Ohm)
  1 x 10uF capacitor
   3.3V
 * */
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include "EspMQTTClient.h"
#include <Ticker.h>
 
ESP8266WiFiMulti wifiMulti;
 
Ticker coucou;
char host_c[20] = "ESP-12345678"; // = ausi nom client mqtt
char host[20] = "ESP-12345678";  // maj au boot
uint64_t minute_dodo       = 15; // min  ESP.deepSleep(1000000 * 60 * (uint64_t)min);
 
String prog_version = "1";
String version_maj =  "1";
unsigned long temps_milli = 0;
bool faire_maj = false;
bool demande_maj = false;
 
const char* ssid1 = "kerminy";
const char* password1 = "paspass";
const char* ssid2 = "NORD";
const char* password2 = "paspass";
const char* ssid3 = "SUD";
const char* password3 = "paspass";
const char* ssid4 = "BIBLIOTEQUE";
const char* password4 = "paspass";
 
const char* mqtt_server = "192.168.1.13";
 
const char* mqtt_user = "kerminy";
const char* mqtt_pass = "patate";
int mqtt_port = 1883;
 
bool envoie_mqtt = false;
bool connexion_prette = false;
int donnees_prette = 0;
int publie_ok = 0;
 
/////// METTRE NE NBR DE MQTT A POSTER !!
int publie_max = 1;
 
/////////////////////////////////// valeurs du msq MQTT
int retour_ok = 0;    // retour du serveur
 
 
auto lastRead = millis();
EspMQTTClient client(NULL, NULL, mqtt_server, mqtt_user, mqtt_pass, host_c, mqtt_port);
 
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(ssid1);
  Serial.print(" ");
  Serial.print(ssid2);
  Serial.print(" ");
  Serial.print(ssid3);
  Serial.print(" ");
  Serial.println(ssid4);
  coucou.attach(1, flip);
 
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(ssid1, password1);
  wifiMulti.addAP(ssid2, password2);
  wifiMulti.addAP(ssid3, password3);
  wifiMulti.addAP(ssid4, password4);
  int boucle = 0;
  while (wifiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (boucle++ > 30) dodo();
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void onConnectionEstablished() {
  connexion_prette = true ;
  Serial.println("connexion MQTT PRETTE");
  Serial.print(mqtt_server);
  Serial.print(" ");
  Serial.print(mqtt_port);
  Serial.print(" ");
  Serial.print(host);
  Serial.print(" ");
  Serial.print(mqtt_pass);
  Serial.print(" ");
  Serial.println(mqtt_user);
  // regarde si maj firmware
  String topic = "pgvrs/"  ; topic += host;
  client.subscribe(String(topic).c_str(), [](const String & payload) {
    Serial.println(payload);
    version_maj = payload;
  });
  coucou.attach(0.5, flip);
 
}
void dodo() {
  digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  coucou.detach();
  Serial.print("dodo min :");
  Serial.println((float)minute_dodo);
  // Connect D0 to RST to wake up !
  ESP.deepSleep(minute_dodo * 60 * 1000000);
}
int count = 0;
 
void flip() {
  int state = digitalRead(LED_BUILTIN);  // get the current state of GPIO1 pin
  digitalWrite(LED_BUILTIN, !state);     // set pin to the opposite state
 
  ++count;
  // when the counter reaches a certain value, start blinking like crazy
  if (count == 20) {
    coucou.attach(0.1, flip);
  }
  // when the counter reaches yet another value, stop blinking
  else if (count == 120) {
    coucou.detach();
  }
}
////////////////////// SCT013  /////////////////
#include "EmonLib.h"
EnergyMonitor emon1;
double courant = 0;
 
void setup_SCT013()
{
  emon1.current(1, 111.1);             // Current: input pin, calibration.
}
void loop_SCT013()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  Serial.print(Irms * 230.0);         // Apparent power
  Serial.print(" ");
  Serial.println(Irms);             // Irms : Valeur efficace
  courant = Irms * 230.0;
  envoie_mqtt = true;
 
}
///////////////////////////////////////////////////
void setup() {
  temps_milli = millis();
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
 
  setup_SCT013();
 
  // Connect D0 to RST to wake up
  pinMode(D0, WAKEUP_PULLUP);
  Serial.begin(115200);
  coucou.attach(2, flip);
 
  Serial.println("");
  Serial.print("prog_version:");
  Serial.println(prog_version);
  Serial.print("ChipID : ");
  Serial.println(ESP.getChipId(), HEX);
  sprintf(host, "ESP-%08X", ESP.getChipId());
  setup_wifi();
  Serial.println(host);
  strlcpy(host_c, host, sizeof(host_c));
}
 
void loop()
{
  unsigned long now = millis();
  Serial.print("DEPUIS ");
  Serial.println((now - temps_milli) / 1000);
 
  if (version_maj != prog_version) {
    Serial.println("version_maj en attente ");
    Serial.println(version_maj);
    faire_maj = true;
    if (demande_maj != true ) client.enableHTTPWebUpdater(); // Enable the web updater.
    // User and password default to values of MQTTUsername and MQTTPassword.
    //These can be overrited with enableHTTPWebUpdater("user", "password").
    demande_maj = true;
  }
  envoie_mqtt = false;
 
  ///
  loop_SCT013();
 
  ///////// MQTT config  ////////////
  if (envoie_mqtt == true )
  {
    //EspMQTTClient client(NULL, NULL, mqtt_server, mqtt_user, mqtt_pass, host, mqtt_port); //
    client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
    client.loop();
    if (now - temps_milli > 60000) {
      Serial.println("pas d'acces au serveur MQTT , dodo min :");
      dodo();
    }
 
    if (now - temps_milli > 20000) { // attend 20s avant de faire
      Serial.print("ATTEND CLIENT MQTT...sur ");
      if ( faire_maj == true) Serial.println(WiFi.localIP());
      if (connexion_prette == true ) {
        Serial.println("PUBLIE");
        coucou.attach(0.3, flip);
 
        String topic = "pgvrs/"; topic += host;
        if ( client.publish(String(topic).c_str(), String(prog_version).c_str()  ) ) publie_ok++;
        ///////
 
        topic = "courant/SCT013/"  ; topic += host;
        if ( client.publish(String(topic).c_str(), String(courant).c_str()   ) ) publie_ok++;
 
        dodo();
      }
    }
    delay(1000);
  }
  delay(1000);
}