Using PZEM-004 with arduino to operate relays ON/OFF

I bought PZEM-004 energy meter module from

I tested it with arduino by using the following code. It displays the readings
Voltage, Current, Watts and Watts/hr
I wish to use this serial readings to control the relay ON/OFF by voltage or Current values
For an example if the normal voltage is 230V, if the voltage exceed above 260V it should
switch OFF the relay and when the voltage returns to 240V Switch ON the relay.
Can you guide me how to do it ?

#include <SoftwareSerial.h> // Arduino IDE <1.6.6
#include <PZEM004T.h>

PZEM004T pzem(2,3);  // RX,TX
IPAddress ip(192,168,1,1);

void setup() {

void loop() {
  float v = pzem.voltage(ip);
  if (v < 0.0) v = 0.0;
  Serial.print(v);Serial.print("V; ");

  float i = pzem.current(ip);
  if(i >= 0.0){ Serial.print(i);Serial.print("A; "); }
  float p = pzem.power(ip);
  if(p >= 0.0){ Serial.print(p);Serial.print("W; "); }
  float e =;
  if(e >= 0.0){ Serial.print(e);Serial.print("Wh; "); }


//  delay(1000);

In setup():

pinMode(relayPin, OUTPUT);

In loop():

if(v > 260) {
  digitalWrite(relayPin, relayOffState);
else if(v <= 240) {
  digitalWrite(relayPin, relayOnState);

Define relayPin, relayOffState, relayOnState as global constants according to the configuration of your device.

Thanks pert I will try this