I cant seem to post my serial monitor results to blynk terminal

Hi im currently seriously stuck as I’m new to blynk application.I need help to post my serial monitor results to terminal.I am using Arduino mega and esp8266(esp-01) wifi module. below i have provided my code.Can someone help?

**#include <ModbusMaster.h>**

**#define SERIAL_OUTPUT 1**

**#   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   // Arduino has only one serial port, Mega has 3 serial ports.
// if use Serial 0, remember disconect Tx (pin0) when upload sketch, then re-conect
#define MB_BAUDRATE       2400          // b 2400
#define MB_BYTEFORMAT     SERIAL_8N2    // Prty n
#define TxEnablePin       17

#define ID_1  1                       // id 001  modbus id of the energy monitor
#define REFRESH_INTERVAL  5000        // refresh time, 5 SECONDS
#define WRITE_INTERVAL 20000UL        // values send to serial port, 1 minute ( 60 * 1000)
#define KWH_2_WS 36000000

// Direcciones registros de datos solo lectura. Valores tipo float.
// Utilizar funcion 04 lectura, numero de registros 16-bits 2.

#define VOL_ADR 0x0000    // VOLTAGE.
#define CUR_ADR 0x0006    // CURRENT.
#define POW_ADR 0x000C    // ACTIVE POWER. 
#define APO_ADR 0x0012    // Apparent power.
#define PFA_ADR 0x001E    // Power factor.
#define FRE_ADR 0x0046    // Frequency.
#define PEN_ADR 0x0048    // IMPORTED ENERGY KWH
#define REN_ADR 0x004A    // Export Energy.
#define TEN_ADR 0x0156    // Energy activa Total.
#define TRE_ADR 0x0158    // Total reactive energy.

// multiplication factor, store value as an integer
#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 to poll, process and send values
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);

uint16_t voltage, maxVoltage, minVoltage; // integer, factor x10
uint16_t current, maxCurrent, minCurrent; // integer, factor x100
uint16_t power, maxPower, minPower;       // integer, factor x10
uint16_t lastEnergy, energy, avgPower;    // integer, factor x100
uint16_t frequency, maxFreq, minFreq;     // integer, factor x100
uint16_t aPower, maxApower, minApower;    // integer, factor x10
uint16_t powerFactor, maxPF, minPF;       // integer, factor x100

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
boolean       firstData;

void setup() {
  SERIAL_PRINTLN("time(s), maxVolt(V), minVolt(V), maxCurr(A) minCurr(A), maxPower(W), minPower(W), maxApPower(VA), minApPower(VA), maxFreq(Hz), minFreq(Hz), AvgPower (W), Energy(Kwh)");

  firstData = false;
  power = 0;
  maxPower = 0;    // in case it has been recorded, use it
  minPower = 0;
  lastEnergy = 0;  // in case it has been recorded, use it
  energy = lastEnergy;

void loop() {
  if (MBserial.available()) {
    voltage = volt.read(VOL_FAC);
    current = curr.read(CUR_FAC);
    power = pwr.read(POW_FAC);
    aPower = aPwr.read(POW_FAC);
    frequency = freq.read(FRE_FAC);
    energy = enrg.read(ENE_FAC);

    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;

 currentMillis = millis();
  if (currentMillis - previousMillis >= WRITE_INTERVAL) {
    previousMillis = currentMillis;
    avgPower = (energy - lastEnergy) * KWH_2_WS / (WRITE_INTERVAL / 1000); //average power KWh/s to W
    lastEnergy = energy;
    firstData = true;

    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)avgPower / ENE_FAC, 2);
    SERIAL_PRINTLN((float)energy / ENE_FAC, 2);


@MinioDiv please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:


@PeteKnight yes done.Thank you.Sorry I have never post to blynk forum before.

There is so much wrong with your sketch that it’s difficult to know where to start.
It’s clearly not your complete sketch, as there are missing libraries and a call to a non-existent function, so I’d suggest starting with you complete code, and indicating whether or not it is actually working when trying to read your Modbus device and outputting the readings to your serial monitor (I have my doubts).

Is there a valid reason for using the __VA_ARGS__ pre-processor for your serial output? It certainly makes your task of outputting the serial output to Blynk.

Also, your code makes no mention of what virtual pin your terminal widget is attached to, and you have no code to write any data to that widget. Clarification around this would be useful.


If you want me to look at your code then it needs to be posted somewhere other than on a Google drive account.

I’m not really interested in the modbus library files, a simple link to their GitHub page, included in your sketch, would be more than sufficient.

I’ve made more than 13,000 posts to this group, you’ll need to be more specific!

Why are you using SoftwareSerial when your Mega has three hardware serial ports?

I’d also recommend that you fix the warning about the missing .h files in the Blynk library before you go much further.

I asked you quite a few questions in my post about your initial code, none of which you’ve answered.


yes it is neccessary for my arduino to understand the incoming results from SDM120CT and TTL convertor

because I realized my esp8266 not functioning when its plugged in with the arduino mega 2560

Modbus-Energy-Monitor-Arduino/Stable version/

My ESP-01 is not respoding with Arduino mega(solved)

so I should write

#include <HardwareSerial.h>
HardwareSerial EspSerial(18, 19); // RX, TX

sorry and thanks.

The code that you’ve linked to on the GitHub site contains no Blynk libraries or code, and doesn’t have any features other than outputting the readings to the serial monitor.
It also contains this function call in the void loop…

but there is no void sei() in the .ino file or the accompanying .h files.

Hence my question (which remains unanswered)…

Personally, I doubt that that is true, but you really ought to be using a different Modbus library.
You probably ought to be using different hardware too - something with in-built IoT connectivity.

I use Modbus to read data from my solar charge controllers, and push the data to Blynk.
I’d start by reading this if I were you…

Then look at other topics relating to epever devices.


1 Like

its ok i have settled my case.Thank you also to you for helping me about the software serial.Thank you