Displaying a Analog Value to Blynk APP via Virtual pins

Hi,

Thanks for your detailed explanation. I’ve managed to setup blynk using the push data example.
It’s been up and running for close to 24hr without loosing connection.

Integrating any other code to the Blynk example causes massive delays in data sending to the blynk server that it overload and I get buffer overloading errors. I’m trying to avoid having any other code except for the Blunk.run() and timer.run() in my void loop() but as soon as I add any code to it, the errors start, and connection to servers gets irradical.

@willerpw actually I notice from your OP that you are not using an ESP in standalone mode you are using the inferior connection method of adding an ESP to an Arduino.

Much of what has been written in this thread still holds true but you have to be even more careful when throwing an Arduino into the mix.

Hi Costas,

I’m building a generator controller. The Uno is used to handle the generator code, running the LCD display etc, I want to be able to start the generator from my phone as well as monitor the electricity quality when running. So I thought using a esp8266 as Wifi link to connect through Blynk would do.

That should be well within the capability of a well written Blynk project but I’m not familiar with the Emon library. Ad hoc libraries like Emon were written without the constraints imposed by the internet and connecting a Smartphone to a server etc. So if the author wanted to take a rest for 20s he could simply add delay(20000) and a standalone Arduino would be fine but not an ESP connected to Blynk and an Arduino.

If you want to post your latest formatted sketch then Blynkers might be able to suggest why you are having problems.

As an aside I would always think of adding extra boards to the rock solid ESP Blynk connection method than adding an ESP as a WiFi shield to an Arduino. So if you need 4 analogue ports on your ESP just buy the expansion board rather than risking the perilous journey of WiFi shields. Obviously if an individual already has the ESP’s and Arduino’s then that is what they are going to try and use but sometimes it’s better to cut your losses and forget Arduino’s altogether.

Will appreciate some help from the guys with experiece,

I;m new to the Forum, how do I attached code on the Forum?

Animated gif on sketch posting to the forum at [README] Welcome to Blynk Community!

Hi, so below is my code.

The screen doesn’t always work. several program restarts is sometimes necessary.
Virtual wire successfully updates to Blynk App.
I tried adding the float values to virtual wires cant get them to work. Please help. Code is below

#include <EmonLib.h>
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "jjjjjjjjjjjjjjjjjjjjjjjjj";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ppppppr";
char pass[] = ",ljljljljljljljl";

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

SimpleTimer timer;
EnergyMonitor emon1;            // Create an instance
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


void setup()
{
  emon1.current(0, 111.1);                                           // Current: input pin, calibration.
  emon1.voltage(1, 271.00, 1.43);                                    // Voltage: input pin, calibration, phase_shift
  lcd.begin(20, 4);                                                  // enable the LCD display
  
  Serial.begin(9600);                                               // Set console baud rate
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);                                    // Set ESP baud
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  
}

void myTimerEvent()
{
 Blynk.virtualWrite(V5, millis() / 1000);
}

void loop()
{
  energy();    // call function
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  
}

void energy()
{
  
   
  emon1.calcVI(20,2000);                          // Calculate all. No.of half wavelengths (crossings), time-out
  float Irms            = emon1.Irms;             //extract Irms into Variable
  float realPower       = emon1.realPower;        //extract Real Power into variable
  float apparentPower   = emon1.apparentPower;    //extract Apparent Power into variable
  float powerFActor     = emon1.powerFactor;      //extract Power Factor into Variable
  float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable

 
  
  lcd.setCursor(0,0);                    // Code for LCD Display //
  lcd.print(supplyVoltage);
  lcd.setCursor(0,1); 
  lcd.print(Irms);
  lcd.setCursor(0,2); 
  lcd.print(apparentPower);
  lcd.setCursor(0,3); 
  lcd.print(powerFActor);
  lcd.setCursor(6,0); 
  lcd.print("  V");
  lcd.setCursor(6,1); 
  lcd.print("  A");
  lcd.setCursor(6,2); 
  lcd.print("  VA");
  lcd.setCursor(8,3); 
  lcd.print("pF");
  delay(500);                       // Delay to make LCD readable between updates
}
```cpp

Problem #1. Remove that delay in your last line of the energy() function. As I mentioned earlier
delays are bad, use SimpleTimer instead.

Call your energy function from a timer instead.

in setup add

// Call the energy function every 500 milliseconds
timer.setInterval(500L, energy);

and remove its call from the loop()

Reason beeing:
Your delay(500) halts the entire unit and blocks ESP and Blynk from doing what it has to to keep the connection going

@willerpw don’t do anything in loop() other than Blynk and SimpleTimer so move energy() into the 1second SimpleTimer loop e.g.

void myTimerEvent()
{
 Blynk.virtualWrite(V5, millis() / 1000);
 energy();
}

I don’t know how frequently energy() should be called as I haven’t studied “Emon” but perhaps leave at 1s (1000L in timer.setInterval(1000L, myTimerEvent). Some projects only need to call functions on a hourly or even daily basis.

Physical LCD’s were invented before Blynk came along. Personally I see no purpose to them when you now have free, portable LCD’s on your Smartphone.

1 Like

Having seen the post by @Fettkeewl and as the original author thought 0.5s delay was required then change 1000L to 500L.

can we use this virtual pin to insert any number from blynk application in our phone.
I mean user can insert any value from blynk application than from that th application can notify based on our number?
could you help me in developing the coding?

@fazlianayunus Yes, virtual pins are used to send data to and from App & Hardware. I recommend you start in the documents for further information on everything Blynk:

http://docs.blynk.cc/#blynk-main-operations-virtual-pins

And try out the Example Browser for actual examples that help demonstrate the various functions. For a simple example of using a virtual pin to send data to app, look at GettingStarted/PushData where it will send the uptime, of your hardware, in seconds to a display widget on your app.

http://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FPushData

Hi, so I managed to change my code by running energy() from simple timer. I also played around with the timer length etc,
What I noticed is that the LCD screen only enables if Wifi is connected and running? When connection is lost the LCD stops updating or at least is updating very radical.
I still have issues with “Buffer Overflow” I’m also experimenting with switching on an LED with Blynk when running this code, it cause a “Buffer Overflow”.

#include <EmonLib.h>
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "40ee80f235714637a96";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

EnergyMonitor emon1;            // Create an instance
SimpleTimer timer;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


void setup()
{
  emon1.current(0, 111.1);                                           // Current: input pin, calibration.
  emon1.voltage(1, 271.00, 1.43);                                    // Voltage: input pin, calibration, phase_shift
  
  
  Serial.begin(9600);                                               // Set console baud rate
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);                                    // Set ESP baud
  delay(10);

  timer.setInterval(1000L, energy); 
  Blynk.begin(auth, wifi, ssid, pass);
  timer.setInterval(500L, myTimerEvent);
  
  lcd.begin(20, 4);                                                  // enable the LCD display
  
}

void myTimerEvent()
{
  energy();
  Blynk.virtualWrite(V5, millis() / 1000);
  
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  
}

void energy()
{
  
   
  emon1.calcVI(20,2000);                          // Calculate all. No.of half wavelengths (crossings), time-out
  float Irms            = emon1.Irms;             //extract Irms into Variable
  float realPower       = emon1.realPower;        //extract Real Power into variable
  float apparentPower   = emon1.apparentPower;    //extract Apparent Power into variable
  float powerFActor     = emon1.powerFactor;      //extract Power Factor into Variable
  float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable

 
  
  lcd.setCursor(0,0);                    // Code for LCD Display //
  lcd.print(supplyVoltage);
  lcd.setCursor(0,1); 
  lcd.print(Irms);
  lcd.setCursor(0,2); 
  lcd.print(apparentPower);
  lcd.setCursor(0,3); 
  lcd.print(powerFActor);
  lcd.setCursor(6,0); 
  lcd.print("  V");
  lcd.setCursor(6,1); 
  lcd.print("  A");
  lcd.setCursor(6,2); 
  lcd.print("  VA");
  lcd.setCursor(8,3); 
  lcd.print("pF");
                        
}

As an aside… since your posted code looks a little strange… when formatting your code, you only need the three backticks just before and three just after your entire code segment. not every line (if that is what you are doing).

Think it looks better now.

Everything looks good now, as far as formatting the posting code :+1:

That is correct… in fact nothing in your program is running when the WiFi connection to the server is down. There are some ways of changing that, but I don’t know if introducing that now will help or hinder as you are just learning how Blynk works… well lets try it

In this line… Blynk.begin(auth, wifi, ssid, pass);… Change Blynk.begin to Blynk.config

This will still be due to getting the timing just right… the ESP is very picky about getting it’s fair share of attention :slight_smile:

So let’s start with combining both recommendations… copy and paste this to replace what you currently have for your timer setup and start of Blynk: Take note of the commenting to help explain the commands

// Put these two timer lines before the Blynk.config line.
timer.setInterval(2000L, energy);  // This gets called every 2 seconds... test until works as needed.
timer.setInterval(1000L, myTimerEvent); //  This only needs to be called every second.

Blynk.config(auth, wifi, ssid, pass); // this means that your hardware code will still run even if communication with Blynk server is lost.

This isn’t tested (that’s your job :stuck_out_tongue_winking_eye: ) but lets see how it works.

EDIT!! I made a mistake in the code with the timers… I have fixed it, so make sure you copy/paste whatever is there now.

I’ve always thought that using intervals that could collide was not a good thing, how is your experience in this matter?

I mean if we just look at it from a short period
Numbers are seconds and function calls are X and Y.

  1. X
  2. X, Y // risk of wdt timeout?
  3. X
  4. X, Y // risk of wdt timeout?
  5. X

@willerpw perhaps all your lcd writes could be a culprit, instead of setting the cursors all around the place, try making a single string with your values and printing just that, once.

Or maybe a yield is in place, what say you on this @Gunner, could it be that the EMON1.CALCVI function just takes to long. Deleted some shit need to read a bit more about emon.

  emon1.calcVI(20,2000);                          // Calculate all. No.of half wavelengths (crossings), time-out
  yield(); // release control to esp for internal stuff
  float Irms            = emon1.Irms;             //extract Irms into Variable
  float realPower       = emon1.realPower;        //extract Real Power into variable
  float apparentPower   = emon1.apparentPower;    //extract Apparent Power into variable
  float powerFActor     = emon1.powerFactor;      //extract Power Factor into Variable
  float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable

If you mean having multiple timers run at the same time?.. I guess it depends on the duration of each timer’s function. I have ran multiple timers that are the same, but not more than 2 or 3 in testing. Otherwise I just combine the same-time actions into a single timer function.

I did change my suggestions to 1 & 2 seconds, as the up time is only displaying every second anyhow (thus didn’t need to be 500ms), and I am just guessing on the energy timer, but that is the one writing to the LCD, so I spaced it out a little longer.

1 Like