PZEM-004T with SIM800L (DTMF) for Power usage monitoring library issues using arduino promini

Hi, I am trying to use PZEM-004T for monitoring power consumption and at the same time automatically cutoff the power once the usage is above 500 watts. Then once the relay is off, I can use DTMF relay control to power it ON again. The issue I am having is that the two libraries do not work together. if i comment out the codes for the PZEM, the dtmf code will work. and if I comment out the DTMF codes, that of PZEM will work. but not both. If I run both, dtmf will stop working. The sim800l will ring, but serial communication will not show the change. No command will work.
Below is my iterated code



#include <PZEM004Tv30.h>
#include <SoftwareSerial.h>

#if defined(ESP32)
#error "Software Serial is not supported on the ESP32"
#endif

/* Use software serial for the PZEM
 * Pin 12 Rx (Connects to the Tx pin on the PZEM)
 * Pin 13 Tx (Connects to the Rx pin on the PZEM)
*/
#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
#define PZEM_RX_PIN 5
#define PZEM_TX_PIN 6
#endif


SoftwareSerial pzemSWSerial(PZEM_RX_PIN, PZEM_TX_PIN);
PZEM004Tv30 pzem(pzemSWSerial);

#define rxPin 7
#define txPin 8
SoftwareSerial sim800L(rxPin, txPin);

#define RELAY_1 2
boolean relay1_state = false;

// previous time for the tasks depending upon time.
unsigned long prevTime_T1 = millis();
long interval_T1 = 10;

//stores incomming data from sim800l
String buff;
String dtmf_cmd;
boolean is_call = false;
float voltage = 0.0, current = 0.0, power = 0.0, energy = 0.0, frequency = 0.0, pf = 0.0;


void setup() {
  /* Debugging serial */
  Serial.begin(9600);
  pinMode(RELAY_1, OUTPUT);
  digitalWrite(RELAY_1, LOW);
  //Begin serial communication with (SIM800L)
  sim800L.begin(9600);
  Serial.println("Begin serial communication with (SIM800L)");
  delay(2000);

  sim800L.println("AT");  //send AT
  delay(500);

  sim800L.println("AT+DDET=1");  //Enable DTMF
  delay(500);
}

void loop() {
  // unsigned long currentTime = millis();
  // if (currentTime - prevTime_T1 > interval_T1) {
  //   measure();

  //   prevTime_T1 = currentTime;
  // }
   if (power >= 500.0) {
      digitalWrite(RELAY_1, HIGH);
      //delay(100);
    }

    else if (power < 500.0) {
      digitalWrite(RELAY_1, LOW);
      //delay(100);
    }

  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  receiveCall();
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  if (Serial.available()) {
     measure();
    sim800L.println(Serial.readString());
   
  }
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  //
}




void measure() {
  //Serial.print("Custom Address:");
  pzem.readAddress();

  // // Read the data from the sensor
  // voltage = pzem.voltage();
  // current = pzem.current();
  power = pzem.power();
  // energy = pzem.energy();
  // frequency = pzem.frequency();
  // pf = pzem.pf();

  // if (power >= 500.0) {
  //   digitalWrite(RELAY_1, HIGH);
  //   delay(100);
  // }

  // else if (power < 500.0) {
  //   digitalWrite(RELAY_1, LOW);
  //   delay(100);
  // }
  // // 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);


  //  else if (power < 20.0 && dtmf_cmd == "2") {
  //   digitalWrite(RELAY_1, LOW);
  //   delay(500);
  // }

  // else if (power >= 20.0 && relay1_state == false) {
  //   digitalWrite(RELAY_1, HIGH);
  //   delay(500);
  // }
  //  else if (power < 20.0 && relay1_state == true) {
  //   digitalWrite(RELAY_1, LOW);
  //   delay(500);
  // }
  //}

  //Serial.println();
  //delay(2000);
}

void receiveCall() {

  //measure();
  if (sim800L.available()) {
    buff = sim800L.readString();
    Serial.println(buff);

    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    if (is_call == true) {
      //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
      if (int index = buff.indexOf("+DTMF:") > -1) {
        index = buff.indexOf(":");
        dtmf_cmd = buff.substring(index + 1, buff.length());
        dtmf_cmd.trim();
        Serial.println("dtmf_cmd: " + dtmf_cmd);
        doAction();
      }
      //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
      if (buff.indexOf("NO CARRIER") > -1) {
        sim800L.println("ATH");
        is_call = false;
      }
      //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
    }
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    if (buff.indexOf("RING") > -1) {
      delay(2000);
      sim800L.println("ATA");
      is_call = true;
    }
    //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  }




  //------------------------------------------------------
}

void doAction() {
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

  if (dtmf_cmd == "1") {
    relay1_state = !relay1_state;
    digitalWrite(RELAY_1, relay1_state);
    if (relay1_state == true) {
      Serial.println("Relay 1 has been ON");
    } else {
      Serial.println("Relay 1 has been OFF");
    }
  }
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
}

Using 2 SoftwareSerial might be the problem. Try using a controller having 2 free hardware serials, like a Mega.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.