I have created a class for some functionality I am using across multiple projects. The class consists of two files, a .cpp file and its corresponding .h file.
I wish to call Blynk.virtualWrite() from one of the functions in the class.
The compiler complains that ‘Blynk’ was not declared in this scope.
If I add #include <BlynkSimpleEsp8266.h> to the module, the compiler complains: multiple definition of `Blynk’
I cannot find a way to include access to Blynk from the class file.
To demonstrate the problem I have a zip file showing the problem in a minimalist project, but I am prevented from uploading zip files so I present all three files in code below, and anyone wanting to see the problem can copy and paste them into files.
It uses the sample class used to show people how to create a class library from:
https://www.arduino.cc/en/Hacking/libraryTutorial
@JRobert attempted to describe the problem last year, first opening a thread talking about multiple modules, it was then renamed to be about IDE tabs.
and s/he also opened an issue on GitHub (Issue #312) which was also closed.
It was closed as it was demonstrated that the Blynk library #include did not have to be included in code in multiple tabs in the Arduino IDE - which is not the same thing at all.
Yes, a library does appear in its own tab, but not all tabs are the same. Libraries appear in tabs but not all tabs are libraries.
Here are the three demo files:
Morse.ino
#include "Morse.h"
#include <BlynkSimpleEsp8266.h>
Morse morse(13);
// place a widget that can take an input on V1 such as Numeric Input
// set the range to allow input > 20
// set the value to something other than 10 each time you run
void setup()
{
Blynk.begin("xx", "xx", "xx");
Blynk.virtualWrite(V1,10); // works fine
}
void loop()
{
Blynk.run();
morse.dot();
}
Morse.cpp
/*
Morse.cpp - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#include "Morse.h"
//#include <BlynkSimpleEsp8266.h> // will result in multiple definition of `Blynk'
Morse::Morse(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
void Morse::dot()
{
digitalWrite(_pin, HIGH);
delay(250);
digitalWrite(_pin, LOW);
delay(250);
// Blynk.virtualWrite(V1,20); // will result in 'Blynk' was not declared in this scope
}
void Morse::dash()
{
digitalWrite(_pin, HIGH);
delay(1000);
digitalWrite(_pin, LOW);
delay(250);
}
Morse.h
/*
Morse.h - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#ifndef Morse_h
#define Morse_h
#include "Arduino.h"
class Morse
{
public:
Morse(int pin);
void dot();
void dash();
private:
int _pin;
};
#endif
So who can tell me how to call Blynk from a cpp class library?