How to move blynk operations into a class

@wolph42, Classes are great and are the “essence” of C++, I think… They are everywhere: in libraries, in more advanced projects… Unfortunately I know little about them, and currently only know how to share/access variables. Know nothing about functions. But IF YOU WILL handle that, please share!
P.S. All my "knowledge"in this aspect is no more than 2 weeks old, as I needed to adopt one of the common ESP firmwares to correctly support PMS sensor… That’s all…

…But perhaps sharing functions is not much different then variables?? For variables I’m using extern "C"

well that certainly makes me the senior expert then as my experience is exactly 2x as big :rofl:
Its exactly one month ago when I started with this. This being EVERYTHING related to Arduino, including electronics, schemes, and coding in C++. Before that I wrote some visual basic scripts for excel and created some scripts for rptools’ maptool. Granted, I do have a Masters in Quantum Physics, but thats >15 years ago.

well, I was hoping I was, but I can’t find ANY inventor of ANY wheel ANYwhere. If you can point me to him/her or to the wheel that would be great!

@everyone: yes i will share if I get something OOP working with Blynk.

The best person to ask would be @vshymanskyy

thnx. I’ve send him a PM.

It is better to ask the question here in order to gain the answer all the Blynk community… Just put a @ + his name and he will get the message

I pointed him to this topic in the message. No sense in writing a question twice.

1 Like

@vshymanskyy should you be checking this. I’ve created a class by I got stuck on constructing the widgetbridge objects. Below is part of the code which gives you the idea I went with. The error I got was:

no matching function for call to ‘WidgetBridge::WidgetBridge()’

Links to the full error message and code are below.

class Room {
private:
  
  //initializer list of internal objects
  WidgetBridge bridge_thermostat;
  WidgetBridge bridge_relay;

public:
  //Constructor of the class:
  Room() : bridge_thermostat(V100), bridge_relay(V107){}

  void initBridges(String authThermostat, String authRelay){
      bridge_thermostat.setAuthToken(authThermostat);
      bridge_relay.setAuthToken(authRelay);
  }
  
  void receiveCurrentT(float param){
    currentT = param;
    Blynk.virtualWrite(V10, currentT);
    timer.restartTimer(thermostatTimer );          //reset isDead timer for thermostat
    Blynk.setProperty(V17, "color", BLYNK_GREEN);    //change LED color
    Blynk.virtualWrite(V17, 200);
  }
} livingRoom;
  
BLYNK_CONNECTED() {
  Blynk.syncAll();
  livingRoom.initBridges("xxx", "xxxx");  //auth of: thermostat, relay
}
BLYNK_WRITE(V10){ livingRoom.receiveCurrentT(param.asFloat());        } //receive currentT from thermostat

Full code
Full error message

edit: reading it, I just realised the big unintentional pun: “class room”…

still no one?
I’ve asked around a bit on other fora and some more experienced coders, but no one so far knows the answer…

I have only the bareset understanding of ‘classes’ but after watching a video about how to make your own library (turns out you use classes :stuck_out_tongue: ) in order to ‘clean up’ your code, I think I understand a bit better.

Issue I see though is that you are trying to take an existing library (Blynk) and shoehorn it into yet another form of class or library format… so, if even possible… to what benefit? aside from you own aesthetics of your code?

As I mentioned earlier, reinventing the wheel :stuck_out_tongue:

Well, @Gunner then remember yourself, that in similar manner works for example the DS1820 sensor library, which uses WIRE (if i recall) for it’s needs… And many, many more…

That is libraries working together, not conscripting one another into themselves or breaking themselves into seperate segments.

As I said, i have only a bare understanding… but taking Blynk functions that already work with the Blynk library and turning them into separate libraries/classes of their own doesn’t make sense. If it did, then I think a developer might have already chimed in… and still might?

not the answer you’re looking for, but for such cases when “classes” would be needed, i use arrays instead.
lets take your example, with the room, and say each room has the following members:

  • temperature actual
  • temperature target
  • heater relay gpio pin

i would create 3 arrays with the max number of the rooms (let’s say we have 10 rooms), with the needed types:

float roomActualTemp[ROOMS];
float roomTargetTemp[ROOMS];
byte roomHeater[ROOMS];       // output pins for heater relays

because all the arrays have the same length and indexing order, one can easily refer and write logic to any room any “member”, in all possible combinations:

#define HYST   1  // hysteresis
#define ROOMS 10

void checkRoomsTemp()
{
  for (byte i = 0, i < ROOMS, i++) {
    if (roomTemp[i] < roomTargetTemp[i])             digitalWrite(roomHeater[i], HIGH);
    else if (roomTemp[i] > roomTargetTemp[i] + HYST) digitalWrite(roomHeater[i],  LOW);
  }
}

you can do the same for timers, timestamps, buttons, etc. for more complex projects you can even use multi dimensional arrays, like this:

int rooms[ROOMS][3];

rooms array has 3 registers, where:
[0] = actual temp
[1] = target temp
[2] = heater relay gpio pin

then, the above code example would look like this:

void checkRoomsTemp()
{
  for (byte i = 0, i < ROOMS, i++) {
    if (rooms[i][0] < rooms[i][1])             digitalWrite(rooms[i][2], HIGH);
    else if (rooms[i][0] > rooms[i][1] + HYST) digitalWrite(rooms[i][2],  LOW);
  }
}

hope this makes sense… i used this strategy in several bigger projects and it worked great. if one designs the code carefully, it is very easy to scale the project:

for example, if you need to go from 10 room to 100, just replace the #define ROOMS 10 and ready to go. you do not have to change anything else in the code!

also, a bit different topic, but it is helpful for code organising and fragmenting, to use the tabs functionality in the arduino ide. i have wrote about this some time ago, here:

3 Likes

@Wanek: that’s really useful. I still want to check out the class method as its basically how c++ is intended, so IF you’re using c++ its a bit silly not to use its main strength.
On your method though: how do you pass on multidimensional arrays onto functions and return those? I’ve done some experimental coding to learn C and one of the things I tried was passing myVar[10][3] to a function and returning it with an operation, but that failed miserably. In the end I just made it global so I could continue.

to use the above method, the arrays must store persistent values, so you should either declare them static or global. from the memory footprint pov it doesn’t really matter actually, so i always declare them global.

i’m not sure what you ask here? afaik, c++ functions can’t return arrays…

well that explains my failure :smiley: I’m still learning. But declaring it global is indeed the way to go.

And what about pointer to array? I barely remember, but it was possible?

yes, that is possible.

small update. I’ve managed to get the widget bridge class called in my room class. The next challenge is the timer class, that one actually poses a real problem which most likely means I have to write it up myself to embed it straight into the class. More later.

1 Like

@Wanek: thank you very much for the array suggestion! That worked perfectly!

1 Like

@wolph42 - Did you ever resolve the Blynk.Write(Vx) inside a class…
I have implemented a Finite State model for a garden watering system, written Classes and sub classes and using events , pointers etc.
Ideally I would wish to imbed the Blynk. Write command (and the subsequent param.asInt(). ) inside an event( ie a class).
for this interested in the garden automation project using a State Model, I will happily share my code in due course