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

Cant call Blynk from a class file


#1

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?


#2

BTW, a workaround is to create helper functions that are placed in the ino file, and declare them in a .h file which is included in the class file that needs to call Blynk. - thanks to @JRobert

Still no direct call, but the class calls the helper, and the helper, calls Blynk.

Another workaround (even less elegant) is to create a special function in the class, which we call from the main loop() asking for pending virtualWrites that need to be done and getting the data out by reference, then calling Blynk. Downside is latency and order of execution plus having to store data for later execution, plus of course all the extra calls.


#3

Since no solution is provided, I have re-categorized this as an issue.