Problem working with Serial Communication

Hello, I am implementing a project that is the continuation of an existing one. I am working with a totalizer that sends through a bluetooth module to an app the consumption data of different electrical charges that can be connected to it.
The improvement that I try to make is to take that same data and send it to a web page hosted on a local server that I designed for that purpose. The website is ready, however, being new to Arduino, I have encountered many difficulties to program it.
The board that has the original project is an Arduino Mega, of which the 6 Rx and Tx pins are used. My idea is to communicate it with a Wifi Module NodeMCU v3.0, via serial, I will do this through the Sofware.Serial library, using 2 digital pins. I enclose the original code modified by me (I think it’s fine).


#   define SERIAL_BEGIN(...) Serial.begin(__VA_ARGS__)
#   define SERIAL_PRINT(...) Serial.print(__VA_ARGS__)
#   define SERIAL_PRINTLN(...) Serial.println(__VA_ARGS__)
#   define SERIAL_BEGIN(...)
#   define SERIAL_PRINT(...)
#   define SERIAL_PRINTLN(...)

#include "ModbusSensor.h"

#define MB_SERIAL_PORT &Serial1   

#define MB_BAUDRATE       2400        // VELOCIDAD EN BITS * SEG  
#define MB_BYTEFORMAT     SERIAL_8N2    
#define TxEnablePin       17          // PIN CONMUTADOR TX RX

#define ID_1  1                       
#define WRITE_INTERVAL 200UL        
#define KWH_2_WS 36000000

#define VOL_ADR 0x0000    // VOLTAJE.
#define CUR_ADR 0x0006    // CORRIENTE.
#define POW_ADR 0x000C    // POTENCIA ACTIVA. 
#define APO_ADR 0x0012    // Potencia Aparente.
#define PFA_ADR 0x001E    // Factor de potencia.
#define FRE_ADR 0x0046    // Frecuencia.
#define PEN_ADR 0x0048    // ENERGIA IMPORTADA KWH
#define REN_ADR 0x004A    // Energia exportada.
#define TEN_ADR 0x0156    // Energia activa Total.
#define TRE_ADR 0x0158    // Energia reactiva Total.

// factor de division
#define VOL_FAC 10   
#define CUR_FAC 100
#define POW_FAC 10
#define PFA_FAC 100
#define FRE_FAC 10
#define ENE_FAC 100

modbusMaster MBserial(MB_SERIAL_PORT, TxEnablePin);  // instance to collect data using Modbus protocol over RS485

//variables a pedir formato para comunicarse con scketch modbus sensor y pedir datos
modbusSensor volt(&MBserial, ID_1, VOL_ADR, CHANGE_TO_ZERO);
modbusSensor curr(&MBserial, ID_1, CUR_ADR, CHANGE_TO_ZERO);
modbusSensor pwr(&MBserial, ID_1, POW_ADR, CHANGE_TO_ZERO);
modbusSensor enrg(&MBserial, ID_1, PEN_ADR, HOLD_VALUE);
modbusSensor freq(&MBserial, ID_1, FRE_ADR, CHANGE_TO_ZERO);
modbusSensor aPwr(&MBserial, ID_1, APO_ADR, CHANGE_TO_ZERO);
modbusSensor pwrFact(&MBserial, ID_1, PFA_ADR, CHANGE_TO_ONE);
// generamos variables que puedan contener 16 bits dentro de ellas
uint16_t voltage, maxVoltage, minVoltage; //  factor x10    
uint16_t current, maxCurrent, minCurrent; //  factor x100
uint16_t power, maxPower, minPower;       //  factor x10
uint16_t lastEnergy, energy, avgPower;    //  factor x100
uint16_t frequency, maxFreq, minFreq;     //  factor x100
uint16_t aPower, maxApower, minApower;    //  factor x10
uint16_t powerFactor, maxPF, minPF;       //  factor x100
// cuenta en milisegundos para hacer los intervalos de refresco escritura
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
// variable utilizada para ver si hay datos
boolean       firstData;
// variable para enviar datos a travez de puerto serial
char buffer[10];
int contador=0;

void setup() {
  //inicia serial 1 con caracteristicas especificadas antes

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // Debido a que los pines TX y RX se encuentran utilizados, se asignan como pines virtuales RX y TX, 10 y 11 respectivamente.

   mySerial.begin(9600); //inicia comunicación serial con Node Mcu
// inicializa datos en cero
  firstData = false;
  power = 0;
  maxPower = 0;    
  minPower = 0;
  lastEnergy = 0; 
  energy = lastEnergy;

void loop() {
  // abilita interrupciones en el programa
  // si puerto serial1 disponible hace
  if (MBserial.available()) {
    // leemos variables desde modbus sensor y guardamos en variables ejemplo leemos volt que tiene un formato predefinido lo pedimos con un factor que puede ser 10 o 100 dependiendo de la variable para guardar como entero esta y lo guardamos en voltaje
    voltage =;
    current =;
    power =;
    aPower =;
    frequency =;
    energy =;
    powerFactor =;

    if (!firstData) {
      if (maxVoltage < voltage) maxVoltage = voltage;
      if (minVoltage > voltage) minVoltage = voltage;
      if (maxCurrent < current) maxCurrent = current;
      if (minCurrent > current) minCurrent = current;
      if (maxPower < power) maxPower = power;
      if (minPower > power) minPower = power;
      if (maxApower < aPower) maxApower = aPower;
      if (minApower > aPower) minApower = aPower;
      if (maxFreq < frequency) maxFreq = frequency;
      if (minFreq > frequency) minFreq = frequency;
      if (maxPower < power) maxPower = power;
      if (minPower > power) minPower = power;
    else {
      maxVoltage = voltage;
      minVoltage = voltage;
      maxCurrent = current;
      minCurrent = current;
      maxPower = power;
      minPower = power;
      maxApower = aPower;
      minApower = aPower;
      maxFreq = frequency;
      minFreq = frequency;
      firstData = false;
// se saca una muestra de millisegundos para comprara 
  currentMillis = millis();
  //si los milisegundos son mayores o iguales a intervalo se hace
  if (currentMillis - previousMillis >= WRITE_INTERVAL) {
    previousMillis = currentMillis;

    firstData = true;
contador = contador+1;
// realentiza envio de buffer 
if (contador >= 20){
    SERIAL_PRINT(currentMillis / 1000);
    SERIAL_PRINT((float)maxVoltage / VOL_FAC, 1);
    SERIAL_PRINT((float)minVoltage / VOL_FAC, 1);
    SERIAL_PRINT((float)maxCurrent / CUR_FAC, 2);
    SERIAL_PRINT((float)minCurrent / CUR_FAC, 2);
    SERIAL_PRINT((float)maxPower / POW_FAC, 2);
    SERIAL_PRINT((float)minPower / POW_FAC, 2);
    SERIAL_PRINT((float)maxApower / POW_FAC, 2);
    SERIAL_PRINT((float)minApower / POW_FAC, 2);
    SERIAL_PRINT((float)maxFreq / FRE_FAC, 2);
    SERIAL_PRINT((float)minFreq / FRE_FAC, 2);
    SERIAL_PRINT((float)powerFactor / ENE_FAC, 2);
    SERIAL_PRINTLN((float)energy / ENE_FAC, 2);

     Serial.println("enviando datos");
    sprintf(buffer, "%d,%d,%d,%d,%d,%d",energy,maxVoltage,maxCurrent,maxPower,powerFactor,maxFreq);

        sprintf(buffer, "%d,%d,%d,%d,%d,%d",energy,maxVoltage,maxCurrent,maxPower,powerFactor,maxFreq);
    contador = 0;

However, I have problems developing the code of the NodeMCU board, since it has to connect to Wi-Fi, receive the data via serial and take that information and upload it to the website. In addition to the data must arrive separately, so the code is further complicated. Could you suggest some code that would serve for this purpose? Beforehand thank you very much.

#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>

String str = "";
const char separator = ',';
const int dataLength = 3;
int data[dataLength];

void setup()
  Serial.println("Inicio. Intentando conectar a red.");
  while ((WiFi.status() == WL_CONNECTED)){



void loop()

if (Serial.available())
      str = Serial.readStringUntil('\n');
      for (int i = 0; i < dataLength ; i++)
         int index = str.indexOf(separator);
         data[i] = str.substring(0, index).toInt();
         str = str.substring(index + 1);
         Serial.print("Recibido: ");
         Serial.println(incomingByte, DEC);



Modbus.ino (6.54 KB)

ModbusSensor.cpp (12.2 KB)

ModbusSensor.h (3.81 KB)

nodemcu.ino (875 Bytes)