class PZEM004T extends HTMLElement {
constructor() {
super();
this.voltage = 0;
this.current = 0;
this.power = 0;
this.intervalId = null;
}
connectedCallback() {
this.intervalId = setInterval(() => this.updateMeasurements(), 1000);
}
disconnectedCallback() {
clearInterval(this.intervalId);
}
updateMeasurements() {
// Update measurements with random values for simulation
this.voltage = (220 + Math.random() * 5).toFixed(2);
this.current = (5 + Math.random()).toFixed(2);
this.power = (this.voltage * this.current).toFixed(2);
// Trigger an event or update UI here
console.log(`Voltage: ${this.voltage}V, Current: ${this.current}A, Power: ${this.power}W`);
}
}
customElements.define('pzem-004t', PZEM004T);
<!DOCTYPE html>
<html>
<head>
<script src="pzem-004t.js"></script>
</head>
<body>
<pzem-004t id="pzem"></pzem-004t>
</body>
</html>