BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Mega and nodemcu to blynk

#1

i have a problem here. i have arduino mega and nodemcu , the mega act as reading the sensor data. i use SCT013 100:50mA for current sensor and voltage transformer single phase module zmpt101b for reading the voltage value. the problem that i had is im able to send the reading value throught Serial1. on the nodemcu side, the value is there but i cant make it upload to blnyk. sometime blynk will crash or not getting reading at all. is there any ideas here please let me know

here is the code for mega

#include "EmonLib.h"         
#define SAMPLE_COUNT 5
#define VOLT_CAL 256.8    //voltage calibration
#define CURRENT_CAL1 66.8 //sensor 1 calibration
EnergyMonitor emon1;              
//arrays to hold the sample data
double volts1[SAMPLE_COUNT];
double amps1[SAMPLE_COUNT];
double watts1[SAMPLE_COUNT];
const int currentPin1 = 0;
const int voltagePin = 1;

//counter to keep track of the current sample location
int counter = 0;

void setup()
{  
  Serial.begin(115200);
  Serial1.begin(115200);
  
  emon1.voltage(voltagePin, VOLT_CAL, 1.7);  // Voltage: input pin, calibration, phase_shift
  emon1.current(currentPin1, CURRENT_CAL1);       // Current: input pin, calibration.
}

void loop()
{
  
  
  //reset the var that keeps track of the number of samples taken 
  //(loop back around to 0 on the array for our running total)
  if(counter >= SAMPLE_COUNT){
    counter = 0;
  }

  //calculate the most recent readings
  emon1.calcVI(20,5000);               

  //save the voltage, current, watts to the array for later averaging
  amps1[counter] = emon1.Irms;
  volts1[counter] = emon1.Vrms;
  watts1[counter] = emon1.Vrms * emon1.Irms;
  counter++;
  
  //setup the vars to be averaged
  double wattAvg1 = 0;
  double voltAvg1 = 0;
  double ampAvg1 = 0;
  //add em up for averaging
  for(int i = 0; i < SAMPLE_COUNT; i++){
    wattAvg1 += watts1[i];
    voltAvg1 += volts1[i];
    ampAvg1 += amps1[i];
  }

  //get the final average by dividing by the # of samples
   wattAvg1 /= SAMPLE_COUNT;
   ampAvg1 /= SAMPLE_COUNT;
   voltAvg1 /= SAMPLE_COUNT;
   
  //calculate the total amps and watts
  double totalAmp = ampAvg1;
  double totalWatt = wattAvg1;
  
  //send the power info to the ESP module through Serial1
  sendPowerInfo (voltAvg1, totalAmp, totalWatt);
}


//send the power info to the ESP module through Serial1 (comma separated and starting with *)
void sendPowerInfo(double Volts, double Amps, double Watts){
  Serial1.print("*");
  Serial1.print(Volts);
  Serial1.print(",");
  Serial1.print(Amps);
  Serial1.print(",");
  Serial1.println(Watts);
}

code for nodemcu

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define AVG_COUNT 50

char auth[] = "xxx";
char ssid[] = "xxx;
char pass[] = "xxxx";

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

void calcEnergy()
{
  int count = 0;

  //where to store the data to be averaged
  double watts[AVG_COUNT];
  double volts[AVG_COUNT];
  double amps[AVG_COUNT];

  //vars to maintain averages for all data points
  double wattAvg = 0;
  double voltAvg = 0;
  double ampAvg = 0;

  //the serial buffer of 64 bytes
  char serialBuf[64];

  while(1)
  {

    //reset the count when we hit the max. The average acts and a rolling average
    if(count >= AVG_COUNT){
      count = 0;
    }

    //get data from serial line
    while(Serial.available()){
      // '*' marks the beginning of a transmission
      bool start = Serial.find('*');

      //parse out the floats
      if(start){
        volts[count] = Serial.parseFloat();
        amps[count] = Serial.parseFloat();
        watts[count++] = Serial.parseFloat(); 
        break;
      }
      delay(1);
    }

    //calculate averages
    wattAvg = 0;
    ampAvg = 0;
    voltAvg = 0;
    for(int i = 0; i < AVG_COUNT; i++)
    {
      wattAvg += watts[i];
      voltAvg += volts[i];
      ampAvg += amps[i];
    }
    wattAvg /= AVG_COUNT;
    ampAvg /= AVG_COUNT;
    voltAvg /= AVG_COUNT;
    Blynk.virtualWrite(V4, millis()/1000);
    Blynk.virtualWrite(V5,ampAvg);
    Blynk.virtualWrite(V6,voltAvg);
    Blynk.virtualWrite(V7,wattAvg);
    yield();
  }
}

void loop()
{
  Blynk.run();
  calcEnergy();
}

is there is something wrong in nodemcu coding ?

0 Likes

#2

Yes, the ‘while’ loop is blocking, so causing disconnections.

Is there a reason why you can’t connect your sensors direct to the NodeMCU?

Pete.

0 Likes

#3

ohh… i see . basically i need 2 analog input. nodemcu only have 1 input. so that is why i used mega to read all the sensor and send it to nodemcu. okay i will try to remove the ‘while’ and see how the result.

EDIT: even remove ‘while’ nothing can be done. blynk will disconnect. i think i have not enough knowledge on how to read data from Serial of mega in nodemcu then i cant manipulate it to be send to blynk… i need help on this

0 Likes

#4

Check the ADS1115… small, powerful and easy to use attached to the Esp

0 Likes

#5

i would like to use my mega instead of buying other part. could u help me?

0 Likes

#6

I’m sorry, this was my best advise… :wink: I have never been playing with the old mega + esp this way.
If I were you I’d reconsider the use of the Ads1115…

@Gunner did a really good job posting examples using that combo with Blynk. Check his posts

0 Likes

#7

i have trouble here. could you lend some help ? my mega use to read all sensor value. there is 2 sensor that i attach to mega and i use hardware Serial1 for the nodemcu. the nodemcu can read the data , the problem is i cant make the data appear on blynk . i need some advise here. i would like to stick with mega and nodemcu because it is my final year project

0 Likes