### PZEMChangeAddress Example Source: https://github.com/mandulaj/pzem-004t-v30/blob/master/README.md This example demonstrates how to change the Modbus slave address of a PZEM-004T v3.0 device. Ensure you are using the correct address for your device. ```Arduino #include #include // RX, TX pins SoftwareSerial pzemSerial(RX_PIN, TX_PIN); PZEM004Tv30 pzem(&pzemSerial); void setup() { Serial.begin(9600); pzemSerial.begin(9600); // Wait for serial to connect - optional while (!Serial) { continue; } Serial.println("PZEM-004T V3.0 Change Address Demo"); // Change the address to 0x01 // The default address is 0xF8 if (pzem.setAddress(0x01)) { Serial.println("Address changed successfully!"); } else { Serial.println("Failed to change address."); } } void loop() { // You can now use the new address to communicate with the device // For example: // pzem.setAddress(0x01); // double voltage = pzem.voltage(); // ... delay(5000); } ``` -------------------------------- ### PZEM MultiDevice Demo Source: https://github.com/mandulaj/pzem-004t-v30/blob/master/README.md Example demonstrating how to use multiple PZEM devices on the same serial interface by assigning unique slave addresses. External transistors may be needed for driving multiple devices due to GPIO current limitations. ```Arduino #include #include // Use SoftwareSerial to communicate with PZEM // RX, TX pins SoftwareSerial pzemSerial(RX_PIN, TX_PIN); // Create PZEM object PZEM004Tv30 pzem(&pzemSerial); void setup() { Serial.begin(9600); pzemSerial.begin(9600); // Set the slave address for the first device // Default address is 0xF8 pzem.setAddress(0x01); // Wait for serial to connect - optional while (!Serial) { continue; } Serial.println("PZEM-004T V3.0 Multi Device Demo"); } void loop() { // Read voltage from the first device double voltage = pzem.voltage(); if (isnan(voltage)) { Serial.println("Error reading voltage from device 1"); } else { Serial.print("Device 1 Voltage: "); Serial.print(voltage); Serial.println("V"); } // Set the slave address for the second device pzem.setAddress(0x02); double voltage2 = pzem.voltage(); if (isnan(voltage2)) { Serial.println("Error reading voltage from device 2"); } else { Serial.print("Device 2 Voltage: "); Serial.print(voltage2); Serial.println("V"); } // Set the slave address back to the first device pzem.setAddress(0x01); delay(2000); } ``` -------------------------------- ### Read PZEM-004T v3.0 with Hardware Serial Source: https://github.com/mandulaj/pzem-004t-v30/blob/master/README.md This example demonstrates how to read voltage, current, power, energy, frequency, and power factor from the PZEM-004T v3.0 module using Hardware Serial (Serial2). It includes error handling for invalid readings and is suitable for boards like the Arduino Mega 2560. Note that Hardware Serial may conflict with debug output on some boards like the Arduino Uno and ESP8266. ```c++ #include /* Hardware Serial2 is only available on certain boards. * For example the Arduino MEGA 2560 */ #if defined(ESP32) PZEM004Tv30 pzem(Serial2, 16, 17); #else PZEM004Tv30 pzem(Serial2); #endif void setup() { Serial.begin(115200); // Uncomment in order to reset the internal energy counter // pzem.resetEnergy(); } void loop() { Serial.print("Custom Address:"); Serial.println(pzem.readAddress(), HEX); // Read the data from the sensor float voltage = pzem.voltage(); float current = pzem.current(); float power = pzem.power(); float energy = pzem.energy(); float frequency = pzem.frequency(); float pf = pzem.pf(); // Check if the data is valid if(isnan(voltage)){ Serial.println("Error reading voltage"); } else if (isnan(current)) { Serial.println("Error reading current"); } else if (isnan(power)) { Serial.println("Error reading power"); } else if (isnan(energy)) { Serial.println("Error reading energy"); } else if (isnan(frequency)) { Serial.println("Error reading frequency"); } else if (isnan(pf)) { Serial.println("Error reading power factor"); } else { // Print the values to the Serial console Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); Serial.print("Current: "); Serial.print(current); Serial.println("A"); Serial.print("Power: "); Serial.print(power); Serial.println("W"); Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh"); Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz"); Serial.print("PF: "); Serial.println(pf); } Serial.println(); delay(2000); } ``` -------------------------------- ### Scan for Connected PZEM Devices Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Use the search() function to scan all possible ModBUS addresses (0x01-0xF8) for connected devices. This is useful for debugging multi-device setups. ```cpp #include PZEM004Tv30 pzem(Serial2); void setup() { Serial.begin(115200); Serial.println("Searching for PZEM devices..."); // Scan all addresses on the bus pzem.search(); Serial.println("Search complete."); } void loop() {} // Output: // Searching for PZEM devices... // Device on addr: 16 // Device on addr: 17 // Search complete. ``` -------------------------------- ### Power Factor API Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Get the power factor, which is the ratio of real power to apparent power. The value ranges from 0.00 to 1.00, with 1.00 being the ideal power factor. ```APIDOC ## GET /pf ### Description Returns the power factor (0.00-1.00). ### Method GET ### Endpoint /pf ### Parameters None ### Request Example None ### Response #### Success Response (200) - **pf** (float) - Power factor value. #### Response Example ```json { "pf": 0.19 } ``` ``` -------------------------------- ### Get Device Address Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves the internal ModBUS address stored in the device without initiating communication. ```cpp #include PZEM004Tv30 pzem(Serial2, 0x15); void setup() { Serial.begin(115200); Serial.print("Configured address: 0x"); Serial.println(pzem.getAddress(), HEX); } void loop() {} // Output: Configured address: 0x15 ``` -------------------------------- ### Energy Measurement API Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Get the accumulated energy consumption in kilowatt-hours (kWh) since the last reset. The resolution is 1Wh, and the range is from 0 to 9999.99kWh. ```APIDOC ## GET /energy ### Description Returns the accumulated energy consumption in kilowatt-hours (kWh) since the last reset. ### Method GET ### Endpoint /energy ### Parameters None ### Request Example None ### Response #### Success Response (200) - **energy** (float) - Accumulated energy consumption in kilowatt-hours (kWh). #### Response Example ```json { "energy": 7.368 } ``` ``` -------------------------------- ### getPowerAlarm() - Get Power Alarm Status Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Returns true if the power alarm threshold has been exceeded, indicating an over-power condition. ```APIDOC ## getPowerAlarm() ### Description Returns true if the power alarm threshold has been exceeded. ### Method GET (conceptual, as it's a status read) ### Endpoint N/A (Library function) ### Parameters None ### Request Example ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { if (pzem.getPowerAlarm()) { Serial.println("ALARM: High power consumption detected!"); // Trigger relay, send notification, etc. } delay(1000); } ``` ### Response #### Success Response (200) - **alarm_triggered** (boolean) - True if the power alarm threshold is exceeded, false otherwise. #### Response Example ``` ALARM: High power consumption detected! ``` ``` -------------------------------- ### getAddress() - Get ModBUS Address Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves the currently stored internal ModBUS address of the device. This function does not communicate with the device. ```APIDOC ## getAddress() ### Description Returns the currently stored internal ModBUS address (does not communicate with device). ### Method GET (conceptual, as it's a local read) ### Endpoint N/A (Library function) ### Parameters None ### Request Example ```cpp #include PZEM004Tv30 pzem(Serial2, 0x15); void setup() { Serial.begin(115200); Serial.print("Configured address: 0x"); Serial.println(pzem.getAddress(), HEX); } void loop() {} ``` ### Response #### Success Response (Local Read) - **address** (uint16_t) - The internal ModBUS address of the device. #### Response Example ``` Configured address: 0x15 ``` ``` -------------------------------- ### Initialize Software Serial Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Use SoftwareSerial for boards with limited hardware serial ports. This is not supported on ESP32. ```cpp #include #include // Create SoftwareSerial: RX pin 11, TX pin 12 SoftwareSerial pzemSWSerial(11, 12); PZEM004Tv30 pzem; void setup() { Serial.begin(115200); // Initialize PZEM with SoftwareSerial pzem = PZEM004Tv30(pzemSWSerial); } void loop() { float voltage = pzem.voltage(); float current = pzem.current(); if (!isnan(voltage)) { Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); Serial.print("Current: "); Serial.print(current); Serial.println("A"); } else { Serial.println("Error reading sensor"); } delay(2000); } ``` -------------------------------- ### Initialize Hardware Serial (ESP32) Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt ESP32 requires explicit RX and TX pin definitions for hardware serial communication. ```cpp #include // ESP32: Hardware Serial2 with RX=16, TX=17 PZEM004Tv30 pzem(Serial2, 16, 17); // ESP32: With custom ModBUS address PZEM004Tv30 pzem(Serial2, 16, 17, 0x10); void setup() { Serial.begin(115200); } void loop() { Serial.print("Address: 0x"); Serial.println(pzem.readAddress(), HEX); float voltage = pzem.voltage(); float current = pzem.current(); float power = pzem.power(); float energy = pzem.energy(); float frequency = pzem.frequency(); float pf = pzem.pf(); if (!isnan(voltage)) { Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); Serial.print("Current: "); Serial.print(current); Serial.println("A"); Serial.print("Power: "); Serial.print(power); Serial.println("W"); Serial.print("Energy: "); Serial.print(energy, 3); Serial.println("kWh"); Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz"); Serial.print("PF: "); Serial.println(pf); } delay(2000); } // Expected output: // Address: 0xF8 // Voltage: 229.60V // Current: 0.10A // Power: 4.50W // Energy: 7.368kWh // Frequency: 50.0Hz // PF: 0.19 ``` -------------------------------- ### Read Power Factor with PZEM-004T Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves the power factor and demonstrates calculating apparent and reactive power. ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { float voltage = pzem.voltage(); float current = pzem.current(); float power = pzem.power(); float pf = pzem.pf(); if (!isnan(pf)) { Serial.print("Power Factor: "); Serial.println(pf, 2); // Calculate apparent power for comparison float apparentPower = voltage * current; Serial.print("Apparent Power: "); Serial.print(apparentPower); Serial.println(" VA"); Serial.print("Active Power: "); Serial.print(power); Serial.println(" W"); // Reactive power estimation float reactivePower = sqrt(apparentPower * apparentPower - power * power); Serial.print("Reactive Power: "); Serial.print(reactivePower); Serial.println(" VAR"); } delay(2000); } // Output: // Power Factor: 0.19 // Apparent Power: 22.96 VA // Active Power: 4.50 W // Reactive Power: 22.52 VAR ``` -------------------------------- ### Initialize Hardware Serial (Non-ESP32) Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Use this constructor for standard Arduino boards with hardware serial ports. The default ModBUS address is 0xF8. ```cpp #include // Using Hardware Serial2 (Arduino Mega, etc.) PZEM004Tv30 pzem(Serial2); // Using Hardware Serial2 with custom address PZEM004Tv30 pzem(Serial2, 0x10); void setup() { Serial.begin(115200); } void loop() { float voltage = pzem.voltage(); if (!isnan(voltage)) { Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); } delay(2000); } ``` -------------------------------- ### Using Multiple PZEM Modules on One Bus Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Connect multiple PZEM modules to a single serial interface by assigning unique addresses to each device. Ensure each device is pre-configured with setAddress(). ```cpp #include #define NUM_PZEMS 3 // Array of PZEM objects - empty constructor for array initialization PZEM004Tv30 pzems[NUM_PZEMS]; void setup() { Serial.begin(115200); // Initialize each PZEM with unique address (0x10, 0x11, 0x12) // Note: Each device must be pre-configured with setAddress() for (int i = 0; i < NUM_PZEMS; i++) { #if defined(ESP32) pzems[i] = PZEM004Tv30(Serial2, 16, 17, 0x10 + i); #else pzems[i] = PZEM004Tv30(Serial2, 0x10 + i); #endif } } void loop() { float totalPower = 0; float totalEnergy = 0; for (int i = 0; i < NUM_PZEMS; i++) { Serial.print("\n=== PZEM "); Serial.print(i); Serial.print(" (Address: 0x"); Serial.print(pzems[i].getAddress(), HEX); Serial.println(") ==="); float voltage = pzems[i].voltage(); float current = pzems[i].current(); float power = pzems[i].power(); float energy = pzems[i].energy(); float frequency = pzems[i].frequency(); float pf = pzems[i].pf(); if (!isnan(voltage)) { Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); Serial.print("Current: "); Serial.print(current); Serial.println("A"); Serial.print("Power: "); Serial.print(power); Serial.println("W"); Serial.print("Energy: "); Serial.print(energy, 3); Serial.println("kWh"); Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz"); Serial.print("PF: "); Serial.println(pf); totalPower += power; totalEnergy += energy; } else { Serial.println("Error reading from device"); } } Serial.println("\n=== TOTALS ==="); Serial.print("Total Power: "); Serial.print(totalPower); Serial.println("W"); Serial.print("Total Energy: "); Serial.print(totalEnergy, 3); Serial.println("kWh"); delay(3000); } // Output: // === PZEM 0 (Address: 0x10) === // Voltage: 230.10V // Current: 0.50A // Power: 100.00W // Energy: 5.250kWh // Frequency: 50.0Hz // PF: 0.87 // // === PZEM 1 (Address: 0x11) === // Voltage: 229.80V // Current: 1.20A // Power: 250.00W // Energy: 12.100kWh // Frequency: 50.0Hz // PF: 0.91 // // === PZEM 2 (Address: 0x12) === // Voltage: 230.00V // Current: 0.30A // Power: 60.00W // Energy: 3.500kWh // Frequency: 50.0Hz // PF: 0.87 // // === TOTALS === // Total Power: 410.00W // Total Energy: 20.850kWh ``` -------------------------------- ### Read PZEM-004T v3.0 data via SoftwareSerial Source: https://github.com/mandulaj/pzem-004t-v30/blob/master/README.md Configures a software serial port on pins 11 and 12 to communicate with the PZEM-004T sensor and prints readings to the hardware serial console. ```c++ #include #include /* Use software serial for the PZEM * Pin 11 Rx (Connects to the Tx pin on the PZEM) * Pin 12 Tx (Connects to the Rx pin on the PZEM) */ SoftwareSerial pzemSWSerial(11, 12); PZEM004Tv30 pzem; void setup() { Serial.begin(115200); pzem = PZEM004Tv30(pzemSWSerial); } void loop() { Serial.print("Custom Address:"); Serial.println(pzem.readAddress(), HEX); // Read the data from the sensor float voltage = pzem.voltage(); float current = pzem.current(); float power = pzem.power(); float energy = pzem.energy(); float frequency = pzem.frequency(); float pf = pzem.pf(); // Check if the data is valid if(isnan(voltage)){ Serial.println("Error reading voltage"); } else if (isnan(current)) { Serial.println("Error reading current"); } else if (isnan(power)) { Serial.println("Error reading power"); } else if (isnan(energy)) { Serial.println("Error reading energy"); } else if (isnan(frequency)) { Serial.println("Error reading frequency"); } else if (isnan(pf)) { Serial.println("Error reading power factor"); } else { // Print the values to the Serial console Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); Serial.print("Current: "); Serial.print(current); Serial.println("A"); Serial.print("Power: "); Serial.print(power); Serial.println("W"); Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh"); Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz"); Serial.print("PF: "); Serial.println(pf); } Serial.println(); delay(2000); } ``` -------------------------------- ### Read Accumulated Energy with PZEM-004T Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves total energy consumption in kWh. Use 3 decimal places for Wh precision. ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { float energy = pzem.energy(); if (!isnan(energy)) { Serial.print("Total Energy: "); Serial.print(energy, 3); // 3 decimal places for Wh precision Serial.println(" kWh"); // Calculate cost (example: $0.12 per kWh) float cost = energy * 0.12; Serial.print("Estimated Cost: $"); Serial.println(cost, 2); } delay(5000); } // Output: // Total Energy: 7.368 kWh // Estimated Cost: $0.88 ``` -------------------------------- ### Read AC Line Frequency with PZEM-004T Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves the AC line frequency in Hz. Useful for monitoring grid stability. ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { float frequency = pzem.frequency(); if (!isnan(frequency)) { Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println(" Hz"); // Check for frequency anomalies if (frequency < 49.5 || frequency > 50.5) { Serial.println("WARNING: Frequency outside normal range!"); } } delay(1000); } // Output: Frequency: 50.0 Hz ``` -------------------------------- ### Read Active Power with PZEM-004T Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves active power in Watts. Returns NaN if the reading fails. ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { float power = pzem.power(); if (!isnan(power)) { Serial.print("Active Power: "); Serial.print(power); Serial.println(" W"); // Convert to kW if large if (power >= 1000) { Serial.print(" ("); Serial.print(power / 1000.0, 2); Serial.println(" kW)"); } } delay(1000); } // Output: Active Power: 4.50 W ``` -------------------------------- ### Set Power Alarm Threshold Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Configures the over-power alarm threshold in watts, with a maximum limit of 25000W. ```cpp #include PZEM004Tv30 pzem(Serial2); #define ALARM_THRESHOLD 100 // 100W threshold void setup() { Serial.begin(115200); // Set alarm threshold to 100W if (pzem.setPowerAlarm(ALARM_THRESHOLD)) { Serial.print("Power alarm set to "); Serial.print(ALARM_THRESHOLD); Serial.println("W"); } else { Serial.println("Error setting power alarm"); } } void loop() { float power = pzem.power(); bool alarm = pzem.getPowerAlarm(); Serial.print("Power: "); Serial.print(power); Serial.print("W | Alarm: "); Serial.println(alarm ? "TRIGGERED!" : "OK"); if (alarm) { // Take action on over-power condition Serial.println("WARNING: Power consumption exceeds threshold!"); } delay(1000); } // Output (normal): // Power: 4.50W | Alarm: OK // Output (over threshold): // Power: 150.00W | Alarm: TRIGGERED! // WARNING: Power consumption exceeds threshold! ``` -------------------------------- ### resetEnergy() - Reset Energy Counter Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Resets the internal energy counter to zero. Returns true on success. ```APIDOC ## resetEnergy() ### Description Resets the internal energy counter to zero. Returns true on success. ### Method POST (conceptual, as it's a command to the device) ### Endpoint N/A (Library function) ### Parameters None ### Request Example ```cpp #include PZEM004Tv30 pzem(Serial2); void setup() { Serial.begin(115200); float energyBefore = pzem.energy(); Serial.print("Energy before reset: "); Serial.print(energyBefore, 3); Serial.println(" kWh"); Serial.println("Resetting energy counter..."); if (pzem.resetEnergy()) { Serial.println("Energy counter reset successfully!"); } else { Serial.println("Error: Failed to reset energy counter"); } delay(500); float energyAfter = pzem.energy(); Serial.print("Energy after reset: "); Serial.print(energyAfter, 3); Serial.println(" kWh"); } void loop() {} ``` ### Response #### Success Response (200) - **success** (boolean) - True if the energy counter was reset successfully, false otherwise. #### Response Example ``` Energy counter reset successfully! ``` ``` -------------------------------- ### Measurement Functions: current() Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves the current AC line current from the connected PZEM-004T module. ```APIDOC ## current() ### Description Returns the AC line current in Amperes (A). The range is 0-10A (built-in shunt) or 0-100A (external CT) with 0.01A/0.02A resolution. ### Returns - **float** - The current in Amperes, or NaN if the reading fails. ``` -------------------------------- ### Check Power Alarm Status Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Returns true if the current power consumption exceeds the configured alarm threshold. ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { if (pzem.getPowerAlarm()) { Serial.println("ALARM: High power consumption detected!"); // Trigger relay, send notification, etc. } delay(1000); } ``` -------------------------------- ### Power Measurement API Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieve the active power consumption from the PZEM-004T v3.0 sensor. The power is returned in Watts (W) with a resolution of 0.1W. The measurement range varies based on the device's current rating (0-2.3kW for 10A, 0-23kW for 100A). ```APIDOC ## GET /power ### Description Returns the active power consumption in Watts (W). ### Method GET ### Endpoint /power ### Parameters None ### Request Example None ### Response #### Success Response (200) - **power** (float) - Active power consumption in Watts (W). #### Response Example ```json { "power": 4.50 } ``` ``` -------------------------------- ### Measurement Functions: voltage() Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves the current AC line voltage from the connected PZEM-004T module. ```APIDOC ## voltage() ### Description Returns the AC line voltage in Volts (V). The measurement range is 80-260V with 0.1V resolution and 0.5% accuracy. ### Returns - **float** - The voltage in Volts, or NaN if the reading fails. ``` -------------------------------- ### Read Voltage Measurement Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves AC line voltage. Returns NaN if the reading fails. ```cpp #include PZEM004Tv30 pzem(Serial2); void setup() { Serial.begin(115200); } void loop() { float voltage = pzem.voltage(); if (isnan(voltage)) { Serial.println("Error: Could not read voltage"); } else { Serial.print("Voltage: "); Serial.print(voltage); Serial.println(" V"); } delay(1000); } // Output: Voltage: 229.60 V ``` -------------------------------- ### Read Current Measurement Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieves AC line current. Useful for implementing threshold-based alerts. ```cpp #include PZEM004Tv30 pzem(Serial2); void loop() { float current = pzem.current(); if (!isnan(current)) { Serial.print("Current: "); Serial.print(current, 2); // 2 decimal places Serial.println(" A"); // Current threshold check if (current > 5.0) { Serial.println("WARNING: High current detected!"); } } delay(1000); } // Output: Current: 0.10 A ``` -------------------------------- ### setPowerAlarm(watts) - Set Power Alarm Threshold Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Sets the over-power alarm threshold in watts. When power exceeds this threshold, the alarm flag is set. Maximum threshold is 25000W. ```APIDOC ## setPowerAlarm(uint16_t watts) ### Description Sets the over-power alarm threshold in watts (max 25000W). When power exceeds this threshold, the alarm flag is set. ### Method POST (conceptual, as it's a command to the device) ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None #### Parameters - **watts** (uint16_t) - Required - The power threshold in watts. Maximum value is 25000. ### Request Example ```cpp #include PZEM004Tv30 pzem(Serial2); #define ALARM_THRESHOLD 100 // 100W threshold void setup() { Serial.begin(115200); if (pzem.setPowerAlarm(ALARM_THRESHOLD)) { Serial.print("Power alarm set to "); Serial.print(ALARM_THRESHOLD); Serial.println("W"); } else { Serial.println("Error setting power alarm"); } } void loop() { float power = pzem.power(); bool alarm = pzem.getPowerAlarm(); Serial.print("Power: "); Serial.print(power); Serial.print("W | Alarm: "); Serial.println(alarm ? "TRIGGERED!" : "OK"); if (alarm) { Serial.println("WARNING: Power consumption exceeds threshold!"); } delay(1000); } ``` ### Response #### Success Response (200) - **success** (boolean) - True if the alarm threshold was set successfully, false otherwise. #### Response Example ``` Power alarm set to 100W ``` ``` -------------------------------- ### Reset Energy Counter Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Resets the internal energy counter to zero. Returns a boolean indicating success. ```cpp #include PZEM004Tv30 pzem(Serial2); void setup() { Serial.begin(115200); // Read current energy float energyBefore = pzem.energy(); Serial.print("Energy before reset: "); Serial.print(energyBefore, 3); Serial.println(" kWh"); // Reset energy counter Serial.println("Resetting energy counter..."); if (pzem.resetEnergy()) { Serial.println("Energy counter reset successfully!"); } else { Serial.println("Error: Failed to reset energy counter"); } delay(500); // Verify reset float energyAfter = pzem.energy(); Serial.print("Energy after reset: "); Serial.print(energyAfter, 3); Serial.println(" kWh"); } void loop() {} // Output: // Energy before reset: 7.368 kWh // Resetting energy counter... // Energy counter reset successfully! // Energy after reset: 0.000 kWh ``` -------------------------------- ### Modbus Address Management API Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Manage the Modbus slave address of the PZEM-004T v3.0 device. This is crucial for configuring multiple devices on a single Modbus bus. ```APIDOC ## POST /address ### Description Sets a new Modbus slave address for the device. Valid addresses range from 0x01 to 0xF7. ### Method POST ### Endpoint /address ### Parameters #### Request Body - **addr** (uint8_t) - Required - The new Modbus slave address (e.g., 0x10). ### Request Example ```json { "addr": "0x10" } ``` ### Response #### Success Response (200) - **success** (boolean) - Indicates if the address change was successful. #### Response Example ```json { "success": true } ``` ``` ```APIDOC ## GET /address ### Description Reads the device's custom Modbus slave address from its memory. ### Method GET ### Endpoint /address ### Parameters #### Query Parameters - **update** (boolean) - Optional - If true, updates the internal address variable of the device. ### Request Example None ### Response #### Success Response (200) - **address** (uint8_t) - The Modbus slave address of the device. #### Response Example ```json { "address": "0x10" } ``` ``` -------------------------------- ### Set ModBUS Slave Address Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Configures a new ModBUS address. Ensure only one device is connected to the bus during this operation. ```cpp #include PZEM004Tv30 pzem(Serial2); #define NEW_ADDRESS 0x10 void setup() { Serial.begin(115200); // Read current address Serial.print("Previous address: 0x"); Serial.println(pzem.readAddress(), HEX); // Set new address (WARNING: only connect ONE device!) Serial.print("Setting address to: 0x"); Serial.println(NEW_ADDRESS, HEX); if (pzem.setAddress(NEW_ADDRESS)) { Serial.println("Address changed successfully!"); Serial.print("Current address: 0x"); Serial.println(pzem.readAddress(), HEX); } else { Serial.println("Error: Failed to set address"); } } void loop() { delay(5000); } // Output: // Previous address: 0xF8 // Setting address to: 0x10 // Address changed successfully! // Current address: 0x10 ``` -------------------------------- ### Frequency Measurement API Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Retrieve the AC line frequency in Hertz (Hz). The measurement range is between 45Hz and 65Hz with a resolution of 0.1Hz. ```APIDOC ## GET /frequency ### Description Returns the AC line frequency in Hertz (Hz). ### Method GET ### Endpoint /frequency ### Parameters None ### Request Example None ### Response #### Success Response (200) - **frequency** (float) - AC line frequency in Hertz (Hz). #### Response Example ```json { "frequency": 50.0 } ``` ``` -------------------------------- ### Read ModBUS Slave Address Source: https://context7.com/mandulaj/pzem-004t-v30/llms.txt Reads the device address from memory and optionally updates the internal library variable. ```cpp #include PZEM004Tv30 pzem(Serial2); void setup() { Serial.begin(115200); // Read address from device (don't update internal variable) uint8_t deviceAddr = pzem.readAddress(false); if (deviceAddr != 0x00) { Serial.print("Device address: 0x"); Serial.println(deviceAddr, HEX); } else { Serial.println("Error reading address"); } // Read and update internal address pzem.readAddress(true); Serial.print("Internal address now: 0x"); Serial.println(pzem.getAddress(), HEX); } void loop() {} // Output: // Device address: 0x10 // Internal address now: 0x10 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.