I broke some of my Arduino app into C++ classes in separate .CPP source files, and I cannot figure out how to call Blynk.virtualWrite (for example) within these classes. I expected to either have to #include a header file, or pass a reference to the instantiated Blynk instance, but Iām failing at both. I see another post looking for the same info, but never got answered.
Can someone help us out, and knock the C++ rust out of my brain?
Thanks. I appreciate the help. Arduino pre-compilation smashes all .INO and .H files together, forming āone-file-to-rule-them-allā before passing it along to the compiler, so, yes this works. Not so much with .CPP files. It still seems like I should be able to #include the blynk declarations and then pass in a reference, but I stopped trying. For the moment I just pass a pointer to some local Blynk wrapper function I wrote. Itās stupid, but the purist in me starts to twitch when I put function definitions (other than static inline) in a .H.
Hello:
After looking for the same solution and not finding anything. I tried many things and what worked for me was the following.
I hope it saves someone elseās time.
The solution I found is not very elegant because it modifies the library <BlynkSimpleEsp8266.h> Cutting the last lines of code and pasting them into a new file āMyBlynkSimpleEsp8266.hā.
Maybe it could be a change to make in the library
I too was looking for a way to put Blynk in a class. Expanding on ferchinasā idea, here is what I came with. Iām still a beginner in C/C++, but Iāll try to explainā¦
First attempt:
// MyBlynkSimpleEsp32.h
#include <BlynkSimpleEsp32.h>
// these are the four lines copied from BlynkSimpleEsp32.h
static WiFiClient _blynkWifiClient;
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);
#include <BlynkWidgets.h>
// BlynkManager.h
BlynkSimpleEsp32.h // modified file with last four lines commented
void BlynkManager::Begin(BlynkManager *instance, BlynkWifi *blynk);
Problem with this is 1) compiler found two Blynk definitions: one in the ino and one in BlynkManager.h, and 2) WidgetLED would not compile from BlynkManager.h. After a few tries at it, hereās what I came up with:
Working
I actually ended up deleting MyBlynkSimpleEsp32.h.
// My.ino
BlynkManager.h
// no Blynk includes here!
void setup()
{
BlynkManager blynkManager;
blynkManager.Begin(&blynkManager);
}
// BlynkManager.h
BlynkSimpleEsp32.h // same modified file
// comment/copy/paste these two lines from the bottom of BlynkSimpleEsp32.h
// line #1:
static WiFiClient _blynkWifiClient;
// line #2:
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
// only declare Blynk here - will do definition in cpp, so that compiler doesn't see two of them!
// otherwise you get two Blynks when including from the ino!
// extern is the key
extern BlynkWifi Blynk;
// this line also from the bottom of BlynkSimpleEsp32.h
// widgets include has too be exactly here to work!
#include <BlynkWidgets.h>
// again, only declare your widgets here - will also define these in cpp!
extern WidgetLED RelayLED;
class BlynkManager
{
void BlynkManager::Begin(BlynkManager *instance);
public:
static bool GetIsFirstConnect();
static bool SetIsFirstConnect(bool b);
...
private:
static BlynkManager *instance;
}
// BlynkManager.cpp
void BlynkManager::Begin(BlynkManager *i)
{
instance = i;
Blynk.config(auth);
Blynk.connect();
}
...
// an example of a Blynk function in BlynkManager.cpp
BLYNK_CONNECTED()
{
if (BlynkManager::GetIsFirstConnect())
{
Blynk.syncAll();
Blynk.notify("ESP32 Starting!");
Serial.println("Blynk connected!");
BlynkManager::SetIsFirstConnect(false);
}
}
// other example of Blynk functions
BLYNK_READ(V1)
{
}
BLYNK_WRITE(V2)
{
}
// these lines go at the very bottom of BlynkManager.cpp:
// define the instance
BlynkManager *BlynkManager::instance;
// define Blynk here (copied from BlynkSimpleEsp32.h)
BlynkWifi Blynk(_blynkTransport);
// define all widgets here
WidgetLED RelayLED(V0);