BUILD · CONNECT · UNDERSTAND

From first wire
to first watt.

A practical guide to the hardware, ESP32 program, connection details and a simple index.html client. Start with the placeholders, then follow the message contract.

What you need

ItemPurposeExample configuration
ESP32 DevKit (classic ESP32)Local priority policy and networkingArduino board: ESP32 Dev Module
PZEM-004T v3.0Aggregate AC electrical measurementsUART2 RX GPIO16, TX GPIO17
Suitable two-channel relay interfaceDemonstration switchingGPIO26 and GPIO27; active-low behavior must match the actual module
Two supervised demonstration loadsHigh/low priority examplesEstimated 100 W and 40 W; change to match your loads
Power, isolation and enclosureAppropriate supply and separationVerify signal levels and module requirements; no mains wiring through a breadboard
Wi-Fi + TLS MQTT brokerDevice and app communicationWi-Fi supported by your ESP32; secure WSS endpoint for app
Low-voltage signal mapping only. Cross the UART signals: PZEM TX to the ESP32’s RX16, and PZEM RX to TX17, through a suitable interface where required. Verify logic levels for your hardware. Mains connections, isolation, protective devices and switching ratings must be designed by qualified personnel.

Which API is used?

MQTT is the messaging interface in this project. There is no REST endpoint or general API key to paste in. Both clients connect to the same broker using their own permitted credentials.

ESP32 + PZEMMQTT over TLS · example port 8883
Your MQTT brokerRoutes telemetry, status and control topics
Web / Android appSecure WebSocket · provider’s WSS endpoint

Fill in these placeholders

PlaceholderWhere to get itWhere it belongs
WIFI_SSID / WIFI_PASSWORDYour Wi-Fi networkESP32 secrets.h only
MQTT_HOST / MQTT_PORTBroker provider’s TLS endpoint, without wss://ESP32 secrets.h; usually port 8883, verify with provider
MQTT_USERNAME / MQTT_PASSWORDA restricted device broker accountESP32 secrets.h
ROOT_CABroker provider’s current root CA PEMESP32 secrets.h; keep BEGIN/END CERTIFICATE lines
BROKER_URLProvider’s secure WebSocket URLApp Settings / minimal web example
USERNAME / PASSWORDA restricted app broker accountApp Settings / minimal web example
TOPIC_PREFIXYour chosen device namespaceBoth clients; default optimizer/

Create broker rules so the app subscribes to optimizer/telemetry and optimizer/status and publishes optimizer/control/+. The device publishes telemetry/status and subscribes to control/+. Choose a unique prefix per device. Match certificate time with NTP before opening TLS; do not disable certificate verification.

MQTT message contract

Topic after prefixPayloadDirection
telemetryJSON object below, every second; not retainedESP32 to app
statusonline / offline; retained last-will statusESP32 / broker to app
control/budgetInteger string from 0 to 300App to ESP32; not retained
control/load11 requests ON; 0 cancels the requestApp to ESP32; not retained
control/load21 requests ON; 0 cancels the requestApp to ESP32; not retained
Example payload · illustrative data
{
  "budget": 200, "w": 100.0,
  "v": 230.0, "i": 0.435, "pf": 0.99,
  "hz": 50.0, "kwh": 0.125, "va": 100.05,
  "l1": true, "l2": false,
  "r1": true, "r2": false,
  "e1": 100.0, "e2": 40.0,
  "status": "Live PZEM telemetry"
}

l1/l2 are actual output states; r1/r2 are requests. Sensor values are JSON null if unavailable. e1/e2 are configured estimates, not separate meter readings. Apparent power is voltage multiplied by current.

ESP32 firmware

In Arduino IDE, install Espressif’s ESP32 board package and the libraries PubSubClient, ArduinoJson 7 and PZEM004Tv30. Put the sketch in an EnergyPrioritizer folder. Copy secrets.example.h to secrets.h in the same folder, then replace every YOUR_ placeholder. Select your actual board and serial port.

Firmware behavior: Requests start off. Allocation uses the configured estimated ratings. Outputs switch off if the PZEM is invalid, Wi-Fi/MQTT disconnects, or an over-budget measurement latches a hold. Resolve the condition and send a new request/budget to clear a measured-power hold. This is demonstration logic, not protection.
secrets.example.h · fill in your connection
Download
Loading configuration template…
EnergyPrioritizer.ino
Download
Loading ESP32 code…

A simple index.html client

Save this as index.html and open it in a modern browser with internet access. Fill in BROKER_URL, USERNAME, PASSWORD and TOPIC_PREFIX in the marked block. It shows real telemetry and sends only non-retained commands while data is fresh. This small teaching example loads MQTT.js from a CDN; the main Android app bundles its library locally.

Minimal HTML + JavaScript
Download
Loading web example…

Run and package the app

Web preview

Install Node.js, then run these commands from the project folder. Open the local address printed by the dev server.

npm install
npm run dev

Capacitor Android

The Android project uses Capacitor 8. Install a compatible Android SDK and JDK 21. Sync the bundled pages before building.

npm run android:sync
npm run android:apk

Use npm run android:open to open the native project in Android Studio. A release distributed publicly needs your own signing configuration.

The intro video, six pages and learning examples are bundled. Cloud controls still require the ESP32, broker and internet connection. This project does not include an HTTP backend or cloud history database.

Reference documentation