Using C++ on a Raspberry Pi with Blynk

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.