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 ?