How to fix nan error in serial monitor with nodemcu 1.0 (ESP12E-Module)

Hi! I'm a beginner at using arduino and I'm trying to make an energy consumption meter that would transmit data via wifi to ThingSpeak, but can't seem to get proper results in the serial monitor. What would seem to be the problem?

I am using an NodeMCU v1.0 (ESP12E-Module) with a CH340 chip and a PZEM-004T 100A v3.0
Any replies would be appreciated!

Here is my code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PZEM004Tv30.h>

PZEM004Tv30 pzem(Serial, 14, 12);

const char *ssid = "SSID";
const char *password = "PASSWORD";
const char *myWriteAPIKey = "";

WiFiClient client;


unsigned long lastTime = 0;
unsigned long timerDelay = 5000;

void setup() {

  pzem.resetEnergy();

  delay(1000);
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  Serial.println("");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(5000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}

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);
  }


  delay(5000);  //Post Data at every 5 seconds
}

Here is what is appearing on the serial monitor:
image

You can not use 'Serial' for both the energy meter & Debug output. Also you can not define the TX & RX pins of the Serial port to any pin like you can on an ESP32. You can use the swap() pins (13 & 15) if you want, but that will disable Serial output to the USB port.

You should probably use swSerial. Try the swSerial example.

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