Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
ateliers:consomations_electriques [2021/07/11 14:44] – jeff | ateliers:consomations_electriques [2024/02/08 17:20] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 12: | Ligne 12: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * https:// | ||
+ | {{ : | ||
**Matériel** | **Matériel** | ||
* base esp8266 | * base esp8266 | ||
Ligne 25: | Ligne 26: | ||
* code de l' | * code de l' | ||
<code c> | <code c> | ||
+ | |||
+ | /* | ||
+ | // 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' | ||
+ | poste en mqtt | ||
+ | deep sleep | ||
+ | de https:// | ||
+ | |||
+ | 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 < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | #include < | ||
+ | |||
+ | ESP8266WiFiMulti wifiMulti; | ||
+ | |||
+ | Ticker coucou; | ||
+ | char host_c[20] = " | ||
+ | char host[20] = " | ||
+ | uint64_t minute_dodo | ||
+ | |||
+ | String prog_version = " | ||
+ | String version_maj = " | ||
+ | unsigned long temps_milli = 0; | ||
+ | bool faire_maj = false; | ||
+ | bool demande_maj = false; | ||
+ | |||
+ | const char* ssid1 = " | ||
+ | const char* password1 = " | ||
+ | const char* ssid2 = " | ||
+ | const char* password2 = " | ||
+ | const char* ssid3 = " | ||
+ | const char* password3 = " | ||
+ | const char* ssid4 = " | ||
+ | const char* password4 = " | ||
+ | |||
+ | const char* mqtt_server = " | ||
+ | |||
+ | const char* mqtt_user = " | ||
+ | const char* mqtt_pass = " | ||
+ | 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; | ||
+ | |||
+ | /////////////////////////////////// | ||
+ | int retour_ok = 0; // retour du serveur | ||
+ | |||
+ | |||
+ | auto lastRead = millis(); | ||
+ | EspMQTTClient client(NULL, | ||
+ | |||
+ | void setup_wifi() { | ||
+ | delay(10); | ||
+ | // We start by connecting to a WiFi network | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.print(ssid1); | ||
+ | Serial.print(" | ||
+ | Serial.print(ssid2); | ||
+ | Serial.print(" | ||
+ | Serial.print(ssid3); | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid4); | ||
+ | coucou.attach(1, | ||
+ | |||
+ | WiFi.mode(WIFI_STA); | ||
+ | wifiMulti.addAP(ssid1, | ||
+ | wifiMulti.addAP(ssid2, | ||
+ | wifiMulti.addAP(ssid3, | ||
+ | wifiMulti.addAP(ssid4, | ||
+ | int boucle = 0; | ||
+ | while (wifiMulti.run() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | if (boucle++ > 30) dodo(); | ||
+ | } | ||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | } | ||
+ | |||
+ | void onConnectionEstablished() { | ||
+ | connexion_prette = true ; | ||
+ | Serial.println(" | ||
+ | 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 = " | ||
+ | client.subscribe(String(topic).c_str(), | ||
+ | Serial.println(payload); | ||
+ | version_maj = payload; | ||
+ | }); | ||
+ | coucou.attach(0.5, | ||
+ | |||
+ | } | ||
+ | void dodo() { | ||
+ | digitalWrite(LED_BUILTIN, | ||
+ | coucou.detach(); | ||
+ | Serial.print(" | ||
+ | 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); | ||
+ | digitalWrite(LED_BUILTIN, | ||
+ | |||
+ | ++count; | ||
+ | // when the counter reaches a certain value, start blinking like crazy | ||
+ | if (count == 20) { | ||
+ | coucou.attach(0.1, | ||
+ | } | ||
+ | // when the counter reaches yet another value, stop blinking | ||
+ | else if (count == 120) { | ||
+ | coucou.detach(); | ||
+ | } | ||
+ | } | ||
+ | ////////////////////// | ||
#include " | #include " | ||
- | // Include Emon Library | ||
EnergyMonitor emon1; | EnergyMonitor emon1; | ||
- | // Create an instance | + | double courant = 0; |
- | void setup() | + | |
- | { | + | |
- | Serial.begin(9600); | + | |
+ | void setup_SCT013() | ||
+ | { | ||
emon1.current(1, | emon1.current(1, | ||
} | } | ||
- | + | void loop_SCT013() | |
- | void loop() | + | |
{ | { | ||
- | double Irms = emon1.calcIrms(1480); | + | |
- | Serial.print(Irms*230.0); | + | Serial.print(Irms * 230.0); |
Serial.print(" | Serial.print(" | ||
- | Serial.println(Irms); | + | Serial.println(Irms); |
+ | courant = Irms * 230.0; | ||
+ | envoie_mqtt = true; | ||
} | } | ||
+ | /////////////////////////////////////////////////// | ||
+ | void setup() { | ||
+ | temps_milli = millis(); | ||
+ | pinMode(LED_BUILTIN, | ||
+ | digitalWrite(LED_BUILTIN, | ||
+ | setup_SCT013(); | ||
+ | |||
+ | // Connect D0 to RST to wake up | ||
+ | pinMode(D0, WAKEUP_PULLUP); | ||
+ | Serial.begin(115200); | ||
+ | coucou.attach(2, | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.print(" | ||
+ | Serial.println(prog_version); | ||
+ | Serial.print(" | ||
+ | Serial.println(ESP.getChipId(), | ||
+ | sprintf(host, | ||
+ | setup_wifi(); | ||
+ | Serial.println(host); | ||
+ | strlcpy(host_c, | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | unsigned long now = millis(); | ||
+ | Serial.print(" | ||
+ | Serial.println((now - temps_milli) / 1000); | ||
+ | |||
+ | if (version_maj != prog_version) { | ||
+ | Serial.println(" | ||
+ | Serial.println(version_maj); | ||
+ | faire_maj = true; | ||
+ | if (demande_maj != true ) client.enableHTTPWebUpdater(); | ||
+ | // User and password default to values of MQTTUsername and MQTTPassword. | ||
+ | //These can be overrited with enableHTTPWebUpdater(" | ||
+ | demande_maj = true; | ||
+ | } | ||
+ | envoie_mqtt = false; | ||
+ | |||
+ | /// | ||
+ | loop_SCT013(); | ||
+ | |||
+ | ///////// MQTT config | ||
+ | if (envoie_mqtt == true ) | ||
+ | { | ||
+ | // | ||
+ | client.enableDebuggingMessages(); | ||
+ | client.loop(); | ||
+ | if (now - temps_milli > 60000) { | ||
+ | Serial.println(" | ||
+ | dodo(); | ||
+ | } | ||
+ | |||
+ | if (now - temps_milli > 20000) { // attend 20s avant de faire | ||
+ | Serial.print(" | ||
+ | if ( faire_maj == true) Serial.println(WiFi.localIP()); | ||
+ | if (connexion_prette == true ) { | ||
+ | Serial.println(" | ||
+ | coucou.attach(0.3, | ||
+ | |||
+ | String topic = " | ||
+ | if ( client.publish(String(topic).c_str(), | ||
+ | /////// | ||
+ | |||
+ | topic = " | ||
+ | if ( client.publish(String(topic).c_str(), | ||
+ | |||
+ | dodo(); | ||
+ | } | ||
+ | } | ||
+ | delay(1000); | ||
+ | } | ||
+ | delay(1000); | ||
+ | } | ||
</ | </ |