Sketches with Multiple .cpp Files

Hi All. I’m brand new to Blynk and am currently reading through the documentation and forums. I have a question about sketch organization.

I typically divide my larger sketches input multiple .cpp files in the Arduino IDE. That makes organizing large projects easier than having one huge .ino file. It also allows me to take advantage of file-level scoping of variables and functions.

So, my question is – will the Blynk callback functions such as BLYNK_READ() and BLYNK_WRITE() work as expected if they are placed in other (.cpp) files rather than the main .ino file? Obviously, I’ll put the proper #include statements in the .cpp files.

I see that this topic was discussed here:

But I was wondering if there has been any new information since it was posted last year? Are there any examples posted?



No, You have to put all Blynk- related functionality on one file currently.
Actually, it is much easier to maintain - as you can have Blynk integration and your other logic separated.

OK, thanks, can it be a .cpp file (i.e. blynk.cpp), or must it be the main .ino file?

it can be a cpp.
you can create blynk_setup() and blynk_loop() functions and call them from main .ino

That will work. Thanks.

You can also have multiple .ino files, which will be concatenated together as a single file before compiling. See the Multi-file sketches section of the Arduino Build Process document. Note that what the document refers to as tabs with no extension actually have a .ino extension “under the covers”.

True, but that doesn’t give you the file-level variable and function scoping that are advantageous in large projects.

Can’t you use namespaces for that?

Probably, but I’m an old-guy C coder. Haven’t started learning C++ yet.

You can follow as @vshymanskyy suggested. I implemented similar way in my multi file project and it just works perfect. All the Blynk callback functions are also in a cpp file.

Yes, that’s what I’ve done. Works fine.

hi @gfvalvo,
do you have any examples that you could show me of how you did this. I have made separate files for the blynk setup and blynk run, but for some reason only one of them works. I have used the same process in each of my setup and run .h and .cpp files but its still not working.

@talb85 This is a very old topic. Please create a new topic with details and your code, thanks.