Using C++ on a Raspberry Pi with Blynk

You would need to link the required libraries etc that’s why I used millis() in the example provided.

can you be more specific what I need to do (one example); I’m using Raspbian on RI Zero and a local server; Blynk libraries were installed yesterday. Here are some other error received while compiling:
In file included from …/src/WidgetRTC.h:18:0,
from main.cpp:13:
…/src/Blynk/BlynkWidgetBase.h: In member function ‘void BlynkWidgetBase::setLabel(Args …)’:
…/src/Blynk/BlynkWidgetBase.h:27:9: error: ‘Blynk’ was not declared in this scope
Blynk.setProperty(mPin, “label”, args…);
^~~~~
…/src/Blynk/BlynkWidgetBase.h: In member function ‘void BlynkWidgetBase::setColor(Args …)’:
…/src/Blynk/BlynkWidgetBase.h:32:9: error: ‘Blynk’ was not declared in this scope
Blynk.setProperty(mPin, “color”, args…);
^~~~~
In file included from main.cpp:13:0:
…/src/WidgetRTC.h: In static member function ‘static time_t WidgetRTC::requestTimeSync()’:
…/src/WidgetRTC.h:37:5: error: ‘Blynk’ was not declared in this scope
Blynk.sendInternal(“rtc”, “sync”);
^~~~~
main.cpp: In function ‘void clockDisplay()’:
main.cpp:29:3: error: ‘String’ was not declared in this scope
String currentTime = String(hour()) + “:” + minute() + “:” + second();
^~~~~~
main.cpp:30:10: error: expected ‘;’ before ‘currentDate’
String currentDate = String(day()) + " " + month() + " " + year();

I’m not able to run DHT too; is there a tutorial to show how to setup libraries to be able to execute C++ programs? I’m trying to run programs from examples:
https://examples.blynk.cc/?board=Raspberry%20Pi&shield=System%20default&example=More%2FDHT11

All the examples in the Sketch Builder are for Arduino, Arduino like and ESP based MCUs

FYI, RPi can work with Blynk using WiringPi (C++) or Node.js, but as stated in the Help Center, anything beyond the basic direct pin control with options require much more experience…

http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/raspberry-pi/nodejs-vs-c-library

Use Blynk with C++
If you want just to toggle a pin, it will simply work out-of-the-box.
But if you want to add more functionality, it will require some Linux, threading, sockets knowledge, modifying Makefile, etc.
So, C++ is generally for advanced developers.

In the examples pages there are raspberry pi programs to be build

I know… I have mentioned that in the past to the Developers :wink: … and it has gotten better.

In theory most can be used with the WiringPi method, as it is C++ based… but not all as drop-in solutions, as many 3rd party libraries may not actually be supported on the RPi or WiringPi, syntax for workarounds may be different, and well, an RPi is not an Arduino… period.

Personally I started with WiringPi and quickly moved toward Node.js… I found more references available with Google… but still a lot of trial and error discovering the correct way to use Blynk with it.

ok…thx…I have to move back to ESP

Don’t give up entirely… there are a few others here who have some WiringPi experience.

And search here for NodeJS stuff… there are a few small examples… plus what is available on the Help Center document I linked to. I have been actively learning to control many things on my RPi3 with Blynk and quite like the power of the RPi :smiley:

basic functionality for buttons works ok, but I want to try simple things like RTC or DHT 22 - and not able to solve it. Basically I want to be able to connect sensors to RPI (movement, Temp, Light, distance, magnetic etc…RPI is much better due to flexibility to update the program, connectivity etc. I have elimitated a lot of errors during the build, but still errors I cannot fix, so I cannot enjoy RTC or DHT on my RPI Zero. Switching to NodeJS…it’s like starting with the left foot…

Hello @Costas first thank you for the post.

Do you have any C++ example that fits in this case using the serial.read?

Example: I want to read serial information from Arduino which is connected to the Raspberry USB port and put on Blynk virtual pin?

@Clucas this is what I always recommend for MCU to MCU interfacing https://forum.arduino.cc/index.php?topic=396450.0

Hello @Costas, thank you but this is more related to interface Arduino <-> Arduino, this interface I am able to do. But how could I modify the example below to receive the serial data from Arduino on the Raspberry USB port?

The port in raspberry is /dev/ttyACM0 which somehow needs to be declared?!

=/

// Blynk "gp" numbers are BCM numbers, so gp17 is physical pin 11 
// #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);

#include <BlynkWidgets.h>

unsigned int uptime;      		// 1 second intervals
unsigned int pinStatus;   		// status of BCM 17
unsigned int lastpinStatus = 0; // to toggle

void myTimerEvent()       		// button widget on V0 or direct access gp17 button
{
  uptime = (millis() / 1000);
  Blynk.virtualWrite(V1, uptime);
  pinStatus = digitalRead(17);
  if(pinStatus != lastpinStatus){
	lastpinStatus = pinStatus;
	printf("GP17 pin status: %i\n", pinStatus);
	if(pinStatus == 1){    // this is to synchronise V1 button if gp17 button is pressed
		Blynk.virtualWrite(V0, 1);
	}
	else{
		Blynk.virtualWrite(V0, 0);
	}
  }
}

void setup()
{
  //nothing to go here yet
}

BLYNK_WRITE(V0)  // button set at PUSH frequency
{
  if(param[0] == 1){
	printf("V1 turned device ON\n");
	digitalWrite (17, HIGH) ;  
  }
  else{
	printf("V1 turned device OFF\n");
	digitalWrite (17, LOW) ;
  }
}

void loop()
{
  Blynk.run();
  if(millis() >= uptime + 1){  // 1 second intervals
	myTimerEvent();
  }
}

int main(int argc, char* argv[])
{
    const char *auth, *serv;
    uint16_t port;
    parse_options(argc, argv, auth, serv, port);
    Blynk.begin(auth, serv, port);
    while(true) {
		loop();
    }
    return 0;
}

Using NodeJS I am able to receive the data from Arduino, but, I am unable to allocate them on the Virtual Pins (So I believe its easier to try to make it work with C++ above):

const SerialPort = require(‘serialport’);
const port = new SerialPort(’/dev/ttyACM0’, () => {
console.log(‘Port Opened’);
});
const parsers = SerialPort.parsers;

const parser = new parsers.Readline({
delimiter: ‘\n’

});

port.pipe(parser);

parser.on(‘data’, console.log);

I assume you know that the Pi has a serial port and how you enable it?

I haven’t tested this yet, so there could be a syntax error or three… but I believe this should do the trick… using terminal, but any Display should work within display limits.

var term = new blynk.WidgetTerminal(13);  // Setup Terminal Widget on vPin 13

const SerialPort = require(‘serialport’);
const port = new SerialPort(’/dev/ttyACM0’, () => {
console.log(‘Port Opened’);
});
const parsers = SerialPort.parsers;
const parser = new parsers.Readline({
delimiter: ‘\n’
});

port.pipe(parser);
parser.on(‘data’, function(serdata));
 term.write(serdata); // Send serial data to terminal on vPin 13
});

Hello @Gunner, sorry I didn’t explain this correctly…

On the link below is another topic only about the NodeJS code and better explanation about it:

Hello @Costas, yes the serial port on Raspberry PI Interfaces is enabled. But do you know how can I adapt the C++ Code to read the data?

OK, got it… you are correct that it isn’t Blynk specific, (Hint, neither is this topic :wink: ) but I left a few links that should put you on track.

The same principle should work for this topic as well… parse your data stream out into individual components and send them to the App via Blynk.virtualWrite(vPin, value) However the precise parsing process with WireingPi might be different then true C++ on another Arduino/ESP.

Google, trial, error, try again, success.

Found this after a quick Gargle… Not sure if it is what you have in mind…

http://wiringpi.com/reference/serial-library/

Rebuilding the library just to compile the application code is crazy.

make clean all target=raspberry

Maybe not use “clear”, because there is such an opportunity?

@Un_ka It’s 4 years since the last post in this topic, and Wiring Pi was deprecated over 2 years ago, so I’m closing this topic.

Pete.