Undeclared meaasage

Hi, this is probably a simple fix but is frustrating the hell out of me, I’m not a pro at this by any means. All I was after is a simple code that I can use two CT clamps to compare wattage (with out a voltage sensor so I’ll multiply by 240v). I have suscessfully used the arduino sketch from emonlib.h and adapted for 2x ct clamps. The serial monitor gives me the values fine on esp32 now trying to go mobile with blynk app, every you tube videos says it’s easy but I’m struggling, especially how to implement virtual pins with real pins and how they don’t correlate.

Please find my code and error message any help would be greatly appreciated

/*************************************************************

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - ESP32 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPLQPQz8rzC"
#define BLYNK_DEVICE_NAME           "Quickstart Template"
#define BLYNK_AUTH_TOKEN            "ROm1XzZNz8BMEEFeWQN4LA0JD2FRJ6g8"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial\


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "EmonLib.h"                   // Include Emon Library

#define EnergyMonitor emon1.current;                   // Create an 1st instance
#define EnergyMonitor emon2.current;                   // Create an 2nd instance




char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

void myTimerEvent() {

  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V7, emon1.current);
  Blynk.virtualWrite(V6, emon2.current);
}


void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

 emon1.current(32, 111.1);             // Current: input pin, calibration.58.6
 emon2.current(33, 111.1);             // Current: input pin, calibration.58.6

timer.setInterval(1000L, sendSensor);
}

void loop()
{
  
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

  double Irms1 = emon1.calcIrms(2280);  // Calculate Irms only
  double Irms2 = emon2.calcIrms(2280);  // Calculate Irms only
  

  
  
  Serial.println(Irms1*240.0);  
  Serial.println(Irms2*240.0);
  

  Blynk.run();
  timer.run();
}

Error message

C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino: In function 'void myTimerEvent()':
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:51:26: error: 'emon1' was not declared in this scope
   Blynk.virtualWrite(V7, emon1.current);
                          ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:52:26: error: 'emon2' was not declared in this scope
   Blynk.virtualWrite(V6, emon2.current);
                          ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino: In function 'void setup()':
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:66:2: error: 'emon1' was not declared in this scope
  emon1.current(32, 111.1);             // Current: input pin, calibration.58.6
  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:67:2: error: 'emon2' was not declared in this scope
  emon2.current(33, 111.1);             // Current: input pin, calibration.58.6
  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:69:26: error: 'sendSensor' was not declared in this scope
 timer.setInterval(1000L, sendSensor);
                          ^~~~~~~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino: In function 'void loop()':
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:79:18: error: 'emon1' was not declared in this scope
   double Irms1 = emon1.calcIrms(2280);  // Calculate Irms only
                  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:80:18: error: 'emon2' was not declared in this scope
   double Irms2 = emon2.calcIrms(2280);  // Calculate Irms only
                  ^~~~~
Multiple libraries were found for "WiFi.h"
  Used: C:\Users\MuscatP\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\libraries\WiFi
  Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1

Compilation error: 'emon1' was not declared in this scope

You’re declaring Irms1 and Irms2 as Double precision integers inside your void loop.
This makes these variables LOCAL to your void loop and therefore invisible within the rest of your code.
Hence the message that these variables were undeclared within the scope of the myTimerEvent function.

The solution is to declare these variables near the top of your code (not within a function) then assign the readings to these variables without re-declaring them. This means dropping the Double from the start of these lines of code…

However, your sketch has other issues…

You have a timer set-up to cal the sendSensor function once per second…

But you don’t have a function called sendSensor so you’ll get a compilation error.

You do have a function called myTimerEvent, but there is no timer set-up to call this function, so it will never execute.

Also, the code that takes these readings and assigns the results to the Irms1 and Irms2 variables is in your void loop. This isn’t good practice as far as Blynk is concerned, and it should really be in a function that is called with a timer (myTimerEvent once it’s actually being called, or renamed as sendSensor).

It’s not clear what your virtual pin question is, but maybe reading this would help…

Pete.

Hi Pete, thanks for your reply, I read the write up you did with interest and appreciate the link to it. I guess my issue is my understanding from Arduino to Blynk, I had same sketch as above (with out the blynk stuff) and works fine on Arduino and ESP32 (serial monitor) but me trying to add the code stuff to get to work with Blynk is doing my head in, I did what you suggested and just kept getting same errors, then tried working through the code each time and rerun to keep getting varied errors to the point the code doesn’t look like the original arduino code. I’ve tried utilising the DHT11 sensor sketch and modifying with my sketches info but it doesn’t work and confuses me. I just wanted to sent two CT clamp readings to an app. Has been very frustrating. Sounds dumb but can you spell out my mistakes clearer please. It still not making sense.
The virtual pin issue I think is only a part of my issue on this, hence just wanting to send two ct clamp values to an app.
Thanks Pete

Updated code?

Maybe explaining what your “virtual pin issue” actually is would be helpful, because so far I’m struggling to understand what exactly that issue is, and how it relates to your project.

Pete.

EnergyMonitor emon1;
EnergyMonitor emon2;

That might help with some of your errors.

Hi distans, tried what you suggested and didn’t help, I now get Compilation error: expected initializer before ‘.’ token. I know the code thinks the . doesn’t belong but I can’t figure how to articulate the code to make it work.
I also changed the timer to same as DHT11 humidity/temp code found on Arduino IDE. But error doesn’t go that far yet.
Thanks any help appreciated

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPLQPQz8rzC"
#define BLYNK_DEVICE_NAME           "Quickstart Template"
#define BLYNK_AUTH_TOKEN            "ROm1XzZNz8BMEEFeWQN4LA0JD2FRJ6g8"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial\


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "EmonLib.h"                   // Include Emon Library

EnergyMonitor emon1.current;                   // Create an 1st instance
EnergyMonitor emon2.current;                   // Create an 2nd instance




char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

void sendSensor()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V7, emon1.current);
  Blynk.virtualWrite(V6, emon2.current);
}


void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

 emon1.current(32, 111.1);             // Current: input pin, calibration.58.6
 emon2.current(33, 111.1);             // Current: input pin, calibration.58.6

timer.setInterval(1000L, sendSensor);
}

void loop()
{
  
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

 Irms1 = emon1.calcIrms(2280);  // Calculate Irms only
 Irms2 = emon2.calcIrms(2280);  // Calculate Irms only
  

  
  
  Serial.println(Irms1*240.0);  
  Serial.println(Irms2*240.0);
  

  Blynk.run();
  timer.run();
}
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:17:20: error: expected initializer before '.' token
 EnergyMonitor emon1.current;                   // Create an 1st instance
                    ^
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:18:20: error: expected initializer before '.' token
 EnergyMonitor emon2.current;                   // Create an 2nd instance
                    ^
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino: In function 'void sendSensor()':
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:36:26: error: 'emon1' was not declared in this scope
   Blynk.virtualWrite(V7, emon1.current);
                          ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:37:26: error: 'emon2' was not declared in this scope
   Blynk.virtualWrite(V6, emon2.current);
                          ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino: In function 'void setup()':
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:51:2: error: 'emon1' was not declared in this scope
  emon1.current(32, 111.1);             // Current: input pin, calibration.58.6
  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:52:2: error: 'emon2' was not declared in this scope
  emon2.current(33, 111.1);             // Current: input pin, calibration.58.6
  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino: In function 'void loop()':
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:64:2: error: 'Irms1' was not declared in this scope
  Irms1 = emon1.calcIrms(2280);  // Calculate Irms only
  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:64:10: error: 'emon1' was not declared in this scope
  Irms1 = emon1.calcIrms(2280);  // Calculate Irms only
          ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:65:2: error: 'Irms2' was not declared in this scope
  Irms2 = emon2.calcIrms(2280);  // Calculate Irms only
  ^~~~~
C:\Users\MuscatP\OneDrive - DOTCS\Documents\Arduino\libraries\Solar_Diverter_Simple\Solar Diverter Simple_Blynk\Solar Diverter Simple_Blynk.ino:65:10: error: 'emon2' was not declared in this scope
  Irms2 = emon2.calcIrms(2280);  // Calculate Irms only
          ^~~~~
Multiple libraries were found for "WiFi.h"
  Used: C:\Users\MuscatP\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\libraries\WiFi
  Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1

Compilation error: expected initializer before '.' token

Hi Pete, my issue is I can get readings to serial monitor but as soon as I try and incorporate code for wifi and Blynk (using Virtual Pins) the code craps itself and I get errors. I am trying to understand how to incorpporate virtual pins into an existing arduino code so I can set up Blynk app. To me it seems when I try and use code from other projects (you tube or example sketches like DHT11) I get the errors. I will keep trying but below is the basic arduino code that does work on serial monitor, hopefully you may see how I am trying to add code for Blynk app/wifi/Virtual pins (first post) and how I am stuffing it up as I can’t see what I’m doing wrong.

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an 1st instance
EnergyMonitor emon2;                   // Create an 2nd instance
//int hwsPin=11;                         // Must use PWM pin for enough power to drive SSR


void setup()
{  
  Serial.begin(115200);
  
 emon1.current(1, 58.6);             // Current: input pin, calibration.58.6
 emon2.current(2, 58.6);             // Current: input pin, calibration.58.6
}

void loop()
{
  double Irms1 = emon1.calcIrms(2280);  // Calculate Irms only
  double Irms2 = emon2.calcIrms(2280);  // Calculate Irms only
  

//  if (Irms1*240.0>Irms2*240.0){analogWrite(hwsPin,HIGH);}
//  if (Irms1*240.0<=Irms2*240.0){analogWrite(hwsPin,LOW);}
  
    Serial.print ("CT INV Watts = "); Serial.print(Irms1*240.0);  Serial.print(" ");      Serial.print ("CT MAINS Watts = "); Serial.print(Irms2*240.0);Serial.println(" ");
  //Serial.print(Irms1*235.0);	                                         
  //Serial.println(" ");                                                   
  //Serial.print ("Amps = ");                                             
  //Serial.println(Irms1);		       // Irms                              

  //Serial.print ("CT MAINS Watts = ");
  //Serial.print(Irms2*235.0);         // Apparent power
  //Serial.println(" ");
  //Serial.print ("Amps = ");
  //Serial.println(Irms2);          // Irms
  delay (10000);
}
CT INV Watts = 126.91 CT MAINS Watts = 799.38 
CT INV Watts = 13.68 CT MAINS Watts = 805.75 
CT INV Watts = 1.47 CT MAINS Watts = 884.41 
CT INV Watts = 0.16 CT MAINS Watts = 869.70 
CT INV Watts = 0.02 CT MAINS Watts = 862.17

This is nothing at all to do with virtual pins.

It’s all about variable scope in C++ and I’ve explained to you that you need to declare Irms1 and Irms2 as global variables (near the top of your code and not inside a function) and not re-declare them as Doubles within your void loop. You’ve not taken that advice, so your problem persists.

You should probably read this mini tutorial about variable scope in C++

https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/

In this case it’s Integers rather than Double precision variables that are being declared, but the principal is the same.

Pete.

According to the new(?) code you posted you still declare EnergyMonitor emon1.current when you should omit the current like this: EnergyMonitor emon1. Then you can use emon1.current. Loot at the authors example:

I recommend everyone to use timers that are at least on a 5 second intervall. Using the DHT11 at 1 Hz is suicide! Search the forum…

Too tired to write in “good english” and with elaborated answers, but I hope you get the gist. If not, listen to @PeteKnight His word is gospel!