Capteur courant mqtt - Seeed studio ESP32 C3 - Home Assistant
Description :¶
Conception et fabrication d'un module de capteur de courant avec communication MQTT.
EDA¶
Réalisé sur le logiciel opensource KiCAD.
BOM¶
| Id | Référence | Empreinte | Quantité | Désignation | Fournisseur et réf |
| 1 | R2,R1,R3 | R_1206_3216Metric_Pad1.30x1.75mm_HandSolder | 3 | R_Small | Digikey |
| 2 | C1 | C_1206_3216Metric_Pad1.33x1.80mm_HandSolder | 1 | C_Small | Digikey |
| 3 | J1 | CONN_ED555&slash_2DS_OST | 1 | Conn_01x02_Pin | Digikey |
| 4 | U1 | MOUDLE14P-SMD-2.54-21X17.8MM | 1 | MOUDLE-SEEEDUINO-XIAO-ESP32C3 | Seeed Studio |
R1 = 10k
R2 = 10k
R3 burden resistor = 0Ohm
C1 = 10 uF

(adaptation pour esp32 XIAO)
Analog pin D2
SeeedStudio -Xiao ESP32C3 Documentation
pince ampèremétrique SCT013 , 30A/1V Datasheet
schématique¶
Routage¶
Fabrication¶
Fait sur la CNC 3 axes Roland SRM-20 du fablab en utilisant Mods à l'aide d'une fraise javelot.
Pour avoir plus d'information concernant l'utilisation de la fraiseuse, vous pouvez consulter la documentation d'Alexis Lepage réalisé lors de la FabAcademy pendant la semaine Electronic Production.
Les paramètres de la fraiseuse utilisé sont ceux de Dylan Heneck étudiant FabAcademy du Waag d'Amsterdam en 2025.
Code Arduino¶
Pour pouvoir utiliser ce code, vérifié bien que les librairies dont il a besoins pour fonctionner sont présente dans votre IDE Arduino.
#define currentPin A2
boolean previous;
unsigned long previous_time;
unsigned long now_time;
unsigned long counted_time;
unsigned long cumulated_counted_time;
int seuil = 180;
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
#include <WiFi.h>
#include <MQTTPubSubClient.h>
const char* ssid = "SSID";
const char* pass = "MDP";
WiFiClient client;
MQTTPubSubClient mqtt;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, pass);
emon1.current(currentPin, 111.1); // Current: input pin, calibration.
now_time = millis();
previous_time = now_time;
previous = 0;
counted_time = 0;
cumulated_counted_time = 0;
delay(2000);
Serial.println("start");
Serial.println("v5");
Serial.print("connecting to wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println(" connected!");
Serial.print("connecting to host...");
while (!client.connect("BrrokerIP", 1883)) {
Serial.print(".");
delay(1000);
}
Serial.println(" connected!");
// initialize mqtt client
mqtt.begin(client);
Serial.print("connecting to mqtt broker...");
while (!mqtt.connect("plasma_counter", "BrokerLogin", "BrokerMDP")){
Serial.print(".");
delay(1000);
}
Serial.println(" connected!");
// subscribe callback which is called when every packet has come
mqtt.subscribe([](const String& topic, const String& payload, const size_t size) {
Serial.println("mqtt received: " + topic + " - " + payload);
});
// subscribe topic and callback which is called when /hello has come
mqtt.subscribe("/plasma/temps", [](const String& payload, const size_t size) {
Serial.print("/plasma/temps ");
Serial.println(payload);
});
// to have a starting message
int intermediate = 0;
char msg_out[20];
sprintf(msg_out, "%d",intermediate);
mqtt.publish("/plasma/temps", msg_out);
}
void loop()
{
mqtt.update(); // should be called
double Irms = emon1.calcIrms(1480); // Calculate Irms only
//Serial.println(Irms); // Irms
if(Irms > seuil && previous == 0 ){
previous_time = millis(); //démarre compteur
now_time = millis();
previous = 1;
}
if(Irms < seuil && previous == 1){
cumulated_counted_time = cumulated_counted_time + counted_time;
previous = 0; //remise à zéro
}
//probablement retirable
if(Irms < seuil && previous == 0){
previous = 0; //remise à zéro
}
if(previous == 1){
counted_time = millis() - previous_time;
if(counted_time % 1000 > 900){ //transmet par secondes
Serial.print(counted_time/1000);
Serial.print(" ");
//Serial.println((cumulated_counted_time + counted_time)/1000);
int intermediate = (cumulated_counted_time + counted_time)/1000;
Serial.println(intermediate);
char msg_out[20];
sprintf(msg_out, "%d",intermediate);
mqtt.publish("/plasma/temps", msg_out);
}
}
}
/*
Pour Plasma
seuil à vide :
seuil allumage 45A : dessus
*/
Code Home assistant¶
Le compteur envoi une variable en mqtt, il suffit ensuite de créer un Sensor et de lui attribuer une unité, ici en seconde, en éditant le fichier : /homeassistant/configuration.yaml
mqtt:
sensor:
- name: "Temps CNC"
unique_id: "Temps_CNC"
state_topic: "/cnc"
unit_of_measurement: "s"