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

Help with my C++ code optimization on Raspberry Pi 3

rpi

#1

I’m new to creating my own code for BLYNK.
I decided to use C ++ to build my own code using virtual pins because they are very useful, right.
I would like to ask you for help in optimizing my code or improving it.
For now everything works as it should, I use my project to control the 8 channel switch (2 channels are empty, I use only 6).
Pin V10 controls pins V2, V3, V4, pin V10 is switched on then these 3 are turned on etc.
I have a question if I can use the Blynk.sync () command; for the selected pin, for example:
Blynk.sync (V2); or Blynk.sync (V2, V3, V4);
Thank u

//#define BLYNK_DEBUG
#define BLYNK_PRINT stdout
#ifdef RASPBERRY
  #include <BlynkApiWiringPi.h>
#else
  #include <BlynkApiLinux.h>
#endif
#include <BlynkSocket.h>
#include <BlynkOptionsParser.h>

static BlynkTransportSocket _blynkTransport;
BlynkSocket Blynk(_blynkTransport);

static const char *auth, *serv;
static uint16_t port;

#include <BlynkWidgets.h>

BlynkTimer tmr;

BLYNK_CONNECTED() {
	Blynk.syncAll();
}

BLYNK_WRITE(V10) {
	if (param[0] == 1) {
		Blynk.virtualWrite(V2, 1);
		Blynk.virtualWrite(V3, 1);
		Blynk.virtualWrite(V4, 1);
		if (digitalRead(17) == LOW) {
			printf("Monitor: ON\n");
            digitalWrite(17, HIGH);
		}
		if (digitalRead(27) == LOW) {
			printf("Głośniki: ON\n");
            digitalWrite(27,HIGH);
		}
		if (digitalRead(22) == LOW) {
			printf("Ambilight: ON\n");
            digitalWrite(22, HIGH);
		}
	} else {
		Blynk.virtualWrite(V2, 0);
		Blynk.virtualWrite(V3, 0);
		Blynk.virtualWrite(V4, 0);
		if (digitalRead(17) == HIGH) {
			printf("Monitor: OFF\n");
			digitalWrite(17, LOW);
		}
		if (digitalRead(27) == HIGH) {
			printf("Głośniki: OFF\n");
			digitalWrite(27, LOW);
		}
		if (digitalRead(22) == HIGH) {
			printf("Ambilight: OFF\n");
			digitalWrite(22, LOW);
		}
	}
}

BLYNK_WRITE(V1) {
    if (param[0] == 1) {
		printf("PC power: OFF\n");
		digitalWrite(18, HIGH);
	} else {
		printf("PC power: ON\n");
		digitalWrite(18, LOW);
	}
}

BLYNK_WRITE(V2) {
	if (param[0] == 1) {
		printf("Monitor: ON\n");
		digitalWrite(17, HIGH);
	} else {
		printf("Monitor: OFF\n");
		digitalWrite(17, LOW);
	}
	if (digitalRead(17) == LOW && digitalRead(27) == LOW && digitalRead(22) == LOW)
		Blynk.virtualWrite(V10, 0);
	else if (digitalRead(17) == HIGH && digitalRead(27) == HIGH && digitalRead(22) == HIGH)
		Blynk.virtualWrite(V10, 1);
}

BLYNK_WRITE(V3) {
	if (param[0] == 1) {
		printf("Głośniki: ON\n");
		digitalWrite(27, HIGH);
	} else {
		printf("Głośniki: OFF\n");
		digitalWrite(27, LOW);
	}
	if (digitalRead(17) == LOW && digitalRead(27) == LOW && digitalRead(22) == LOW)
		Blynk.virtualWrite(V10, 0);
	else if (digitalRead(17) == HIGH && digitalRead(27) == HIGH && digitalRead(22) == HIGH)
		Blynk.virtualWrite(V10, 1);
}

BLYNK_WRITE(V4) {
	if (param[0] == 1) {
		printf("Ambilight: ON\n");
		digitalWrite(22, HIGH);
	} else {
		printf("Ambilight: OFF\n");
		digitalWrite(22, LOW);
	}
	if (digitalRead(17) == LOW && digitalRead(27) == LOW && digitalRead(22) == LOW)
		Blynk.virtualWrite(V10, 0);
	else if (digitalRead(17) == HIGH && digitalRead(27) == HIGH && digitalRead(22) == HIGH)
		Blynk.virtualWrite(V10, 1);
}

BLYNK_WRITE(V5) {
	if (param[0] == 1) {
		printf("Ledy: ON\n");
		digitalWrite(23, HIGH);
	} else {
		printf("Ledy: OFF\n");
		digitalWrite(23, LOW);
	}
}

BLYNK_WRITE(V6) {
	if (param[0] == 1) {
		printf("Lampka: ON\n");
		digitalWrite(24, HIGH);
	} else {
		printf("Lampka: OFF\n");
		digitalWrite(24, LOW);
	}
}

void setup() {
	pinMode(18, OUTPUT);
	pinMode(17, OUTPUT);
	pinMode(27, OUTPUT);
	pinMode(22, OUTPUT);
	pinMode(23, OUTPUT);
	pinMode(24, OUTPUT);
	Blynk.begin(auth, serv, port);
	tmr.setInterval(1000, [](){
		Blynk.virtualWrite(V0, BlynkMillis()/1000);
	});
}

void loop() {
	Blynk.run();
	tmr.run();
}


int main(int argc, char* argv[]) {
	parse_options(argc, argv, auth, serv, port);

	setup();
	while (true) {
		loop();
	}

	return 0;
}

On the app it looks like this:


#2

Yes… just like it is stated in the Documents

http://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncvirtualvpin

Not sure I understand this… the void setup() runs at boot and the void loop() runs constantly by default… I see no need to manually call them… ever.

EDIT perhaps it is a WiringPi thing?.. I long ago gave up on using that on the RPi, just use JS and Python now :wink:


#3

Oh it’s Blynk.syncVirtual();
Thanks

This was in the main main.cpp file in linux folder. I have not changed anything in this case.
Maybe it’s a matter of WiringPi, I do not know exactly. But I use C ++ by getting used to the environment of arduino and for now it is difficult for me to move to Js