How does 235 become 282 ?

I am facing a problem that is driving me nuts…

Setup is this :

  1. A Nano acquires the 3 Phase AC voltages , scales them properly and sends them as a string via a Serial link to a connected Node MCU . This process is happening with no issues. I have seen the data being sent and it is clean as below :

<234,236,233>
<235,236,237>
<234,232,236>

  1. The connected Node MCU sleeps for 1 minute, wakes up , links to a WiFi station, sends a “z” to the connected Nano and gets the above data. It Parses the string and posts only the three voltages to a Thingspeak channel. Then goes to sleep.

  2. So far so good. But the problem is the values I see on ThingSpeak are approximately 50V above the actual … so 235V reads as 285 or so.

I am giving the Node MCU code alone below and would like to know where could be the issue …

#include <ESP8266WiFi.h>
#include <ThingSpeak.h>

boolean debug = true;                  // true = more messages

int rPhaseVolt, yPhaseVolt, bPhaseVolt ;
bool started = false;                  //True: Message is started
bool ended   = false;                  //True: Message is finished
bool newVoltData = false;
char incomingByte ;                    //Variable to store the incoming byte
char dataFromNano[15] ;                //Voltage data from Nano
byte charIndex ;                       //Index of array

byte wifi_NoLink = D5;
byte wifi_LinkOK = D6;
bool ledState, linkActive;

unsigned long sleepTimeS = 120;         // Time to sleep in second after posting ...120
unsigned long sleepTimeR = 15;         // Time to Retry in second when WiFi link fails .. 15

unsigned long myChannelNumber = 123456;
const char * myWriteAPIKey = "XXXXXXXXXXX";

const char *ssid = "SSID";
const char *password = "PASSWORD";
//IPAddress ip(192, 168, 0, 170);
//IPAddress gateway(192, 168, 0, 1);
//IPAddress subnet(255, 255, 255, 0);
WiFiClient  tsclient;


//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void setup()
{
  Serial.begin(9600);
  delay(2000);
  Serial1.begin(9600);                     // Available on D4 ...only use for TxD
  Serial1.println( "Waking up after deep sleep...!");
  pinMode( wifi_NoLink, OUTPUT);
  pinMode( wifi_LinkOK, OUTPUT);

  digitalWrite(wifi_NoLink, HIGH);
  digitalWrite(wifi_LinkOK, HIGH);

  connectWifi();                           // Establish WIFi link...

  ThingSpeak.begin(tsclient);
  Serial.print("z");                       // Send a request to the Nano
  delay(100);
  readAndPost();                           // Read the response and post to cloud
  Serial1.println("ESP8266 in sleep mode after posting..");
  digitalWrite(wifi_NoLink, LOW);
  digitalWrite(wifi_LinkOK, LOW);
  ESP.deepSleep(sleepTimeS * 1000000);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void loop()
{
  // Do Nothing in the Loop.
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// FUNCTION to READ AND POST VOLT data..

void readAndPost()
{

  readSerial();                               // Get the voltage value from Serial...

  ThingSpeak.setField(1, rPhaseVolt);
  ThingSpeak.setField(2, yPhaseVolt);
  ThingSpeak.setField(3, bPhaseVolt);

  Serial1.println( "Posting data to ThingSpeak.com ....");

  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);   // Write the fields that you've set all at once.
}

//***************************************

void readSerial()
{
  while (Serial.available() > 0)                  // Is there any data in Serial buffer ??
  {
    incomingByte = Serial.read();                 // Read the incoming byte .. this removes it from Serial buffer

    if (incomingByte == '<')                      // Wait for the start marker..
    {
      started = true;                             // Got the start marker. Set receive boolean to true..
      charIndex = 0;
      dataFromNano[charIndex] = '\0';              // Throw away any incomplete characters
    }

    else if (incomingByte == '>')                 // Wait for the end marker
    {
      ended = true;                               // Got the end marker ...
      break;                                      // Stop reading - exit from while loop!
    }

    else                                          // Read the message from the Serial buffer !
    {
      if (charIndex < 30)                         // Make sure there is room
      {
        dataFromNano[charIndex] = incomingByte;    // Add char to array
        charIndex++;
        dataFromNano[charIndex] = '\0';            // Add NULL to end.. keep on adding
      }
    }
  }

  if (started && ended)                           // All data read from the Serial buffer... process it.
  {
    Serial1.println(dataFromNano);
    dataFromNano[charIndex] = '\0';
    parseData();                                   // Read and store the Voltage info in variables.
    charIndex = 0;
    started = false;
    ended = false;
  }
}

//***************************************

void parseData()
{
  char * strtokIndx;                                // This is used by strtok() as an index

  strtokIndx = strtok(dataFromNano, ",");           // Get the first part - R phase volt. The delimiter comma is replaced with a NULL
  rPhaseVolt = atoi(strtokIndx);                    // Convert to integer
  strtokIndx = strtok(NULL, ",");                   // Previous comma is now NULL .. get the data till the next comma whihc Y Phase volt
  yPhaseVolt = atoi(strtokIndx);
  strtokIndx = strtok(NULL, ">");                   // Get the last part of data..B Phase Volt
  bPhaseVolt = atoi(strtokIndx);
}

//***************************************

// FUNCTION for WiFi handling

void connectWifi()
{
  Serial1.print("Connecting as wifi client to SSID: ");
  Serial1.println(ssid);

  //WiFi.config(ip, gateway, subnet);

  WiFi.begin ( ssid, password );

  if (debug ) WiFi.printDiag(Serial1);

  // ... Give ESP 10 seconds to connect to station.
  unsigned long startTime = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
  {
    delay(100);
    Serial1.print(".");
  }

  // Check connection
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial1.print("WiFi connected; IP address: ");
    Serial1.println(WiFi.localIP());

    digitalWrite( wifi_NoLink, LOW);
    digitalWrite( wifi_LinkOK, HIGH);
  }
  else
  {
    digitalWrite( wifi_NoLink, HIGH);
    digitalWrite( wifi_LinkOK, LOW);

    Serial1.print("WiFi connect failed to ssid: ");
    Serial1.println(ssid);
    Serial1.print("WiFi password <");
    Serial1.print(password);
    Serial1.println(">");
    Serial1.println("Check for wrong typing!");

    Serial1.println("Unable to connect.. going to sleep ! ");
    digitalWrite(wifi_NoLink, LOW);
    digitalWrite(wifi_LinkOK, LOW);
    ESP.deepSleep(sleepTimeR * 1000000);
    
  }
}
  readSerial();                               // Get the voltage value from Serial...

Do rPhaseVolt, yPhaseVolt, and bPhaseVolt contain valid values after this statement executes?

If the values printed after this do not match what ThingSqueak shows, then the problem is in the ThingSqueak library or in ThingSqueak .

PaulS:

  readSerial();                               // Get the voltage value from Serial...

Do rPhaseVolt, yPhaseVolt, and bPhaseVolt contain valid values after this statement executes?

If the values printed after this do not match what ThingSqueak shows, then the problem is in the ThingSqueak library or in ThingSqueak .

Yes that's a good point. Will bring in a Serial1 print to read the voltages just after the readSerial() to nail the issue.

Thanks !!