How do I use Blynk API within my own Arduino (C++) classes?

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? :wink:

Hey,

I split my projects in to header files. Its similar but more of and ‘Arduino’ splitting method over proper CPP files.

It might help your situation.

Here is an example: https://github.com/jaminNZx/ESP8266-Blynk-Gate-Logger-And-Counter

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

BlynkSimpleEsp8266.h

...
/*
static WiFiClient _blynkWifiClient;
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);

#include <BlynkWidgets.h>
*/
#endif

MyBlynkSimpleEsp8266.h


#include "Arduino.h"
#include <BlynkSimpleEsp8266.h>                       

static WiFiClient _blynkWifiClient;
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);

#include <BlynkWidgets.h>

mycode.ino

...
#include <MyBlynkSimpleEsp8266.h>  
...
setup(){
Myclass::setObjectPtr(&Blynk);		//Send a reference to Blynk object		
}
...

Myclass.cpp

...
#include <BlynkSimpleEsp8266.h>

class Myclass{
	BlynkWifi* Blynk_ptr
	
	Myclass::setObjectPtr(BlynkWifi* ptr){
		Blynk_ptr = ptr;
	}

	void Myclass::SendData(char pin, double data) {
		Blynk_ptr->virtualWrite(pin, data);				

	}
}

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>

// My.ino
MyBlynkSimpleEsp32.h
BlynkManager.h

void setup()
{
    BlynkManager blynkManager;
    blynkManager.Begin(&blynkManager, &Blynk);
}

// 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);

Hope this helps!