I am using the following circuit to measure the power consumption of a Raspberry Pi 4. The devices used are:
- A microcontroller such as ESP32, Arduino, etc. Here, I am using ESP8266 because it is available.
- An AC module: PZEM-004T
- Power outlet and plug
- Download the Arduino IDE (I used version 2.3.4).
- Follow the ESP8266 board installation guide: ESP8266 Arduino IDE Setup
- Install the PZEM-004T library in Arduino IDE.
- Select the correct COM port to upload code → Choose the correct board (ESP8266, NodeMCU 1.0).
- Clone my
.inocode, edit your Wi-Fi SSID and password, then compile and upload.
- Download Arduino CLI:
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh- Move
arduino-clifrom your personal directory to a system-wide directory:
sudo mv ~/bin/arduino-cli /usr/local/bin/- Add your user to the
dialoutgroup to access serial ports:
sudo usermod -a -G dialout $USER- Initialize Arduino CLI configuration:
arduino-cli config init- Add the URL so Arduino CLI can fetch the ESP8266 Board package:
arduino-cli config set board_manager.additional_urls https://arduino.esp8266.com/stable/package_esp8266com_index.json- Update the board and core index:
arduino-cli core update-index- Install ESP8266 Arduino Core (includes toolchain, libraries, compiler):
arduino-cli core install esp8266:esp8266- List all installed cores (ESP8266 should appear here):
arduino-cli core list- List all connected boards:
arduino-cli board list- Install the library for the PZEM-004T module:
arduino-cli lib install "PZEM004Tv30"- Enter your Wi-Fi SSID and password, and fill in the IP address of the server you are hosting (where
server.pyruns) in theestimate_power.inofile.
- Then, compile file .ino in your folder:
arduino-cli compile --fqbn esp8266:esp8266:nodemcuv2 <folder-containing-.ino>- Upload the compiled code
arduino-cli upload -p /dev/ttyUSB0 --fqbn esp8266:esp8266:nodemcuv2 <folder-containing-.ino>- Open the serial monitor to view data from ESP8266:
arduino-cli monitor -p /dev/ttyUSB0 -c baudrate=115200python3 server.pyAfter running, the power information of the Raspberry Pi 4 will be sent to the server continuously, with updates occurring approximately once per second.