This post is in response to recent posts regarding Blynk and Raspberry Pi’s.
Most of the example sketches you will see are written for Arduino’s and ESP’s. If you have “javascript” skills there’s a small selection of scripts that can be used with nodejs on the Pi. However there are some Blynkers that are much more familiar with C++ than they are with JS.
In the “linux” directory of the Blynk libraries there is a shell script and a Makefile for compiling C code for the Pi and other linux devices. I will concentrate on the Pi here.
Copy the following files for safe keeping:
build.sh
main.cpp
If you haven’t already set up wiringPi on your Pi run the original shell script with:
./build.sh raspberry
Now edit the build.sh shell script to remove all the one off stuff (like installing wiringPi etc) so it looks something like this:
#!/bin/bash
//
case "$1" in
raspberry)
make clean all target=raspberry
exit 0
;;
linux)
make clean all
exit 0
;;
esac
echo "Please specify platform: raspberry, linux"
exit 1
We will now modify main.cpp so it compiles the following project.
It’s basically a PushData for Pi, with a few extras, using wiringPi so you can select “GPIO” pins directly in the project. Without messing around with various libraries the revised main.cpp is as follows:
// 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;
}
Once you have saved the revised main.cpp just run the script again:
./build.sh raspberry
and then start your connection to Blynk by replacing xxxxxxxxx with your token:
sudo ./blynk --token=xxxxxxxxxxxxx
Those 2 commands and a few button presses in the app will look like this:
pi@pizero:~/blynk0.4.4/libraries/Blynk/linux $ ./build.sh raspberry rm main.o BlynkDebug.o ../src/utility/BlynkHandlers.o blynk
g++ -I ../src/ -I ./ -DLINUX -c -O3 -w -DRASPBERRY main.cpp -o main.o
g++ -I ../src/ -I ./ -DLINUX -c -O3 -w -DRASPBERRY BlynkDebug.cpp -o BlynkDebug.o
g++ -I ../src/ -I ./ -DLINUX -c -O3 -w -DRASPBERRY ../src/utility/BlynkHandlers.cpp -o ../src/utility/BlynkHandlers.o
g++ main.o BlynkDebug.o ../src/utility/BlynkHandlers.o -lrt -lpthread -s -lwiringPi -o blynk
pi@pizero:~/blynk0.4.4/libraries/Blynk/linux $ sudo ./blynk --token=xxxxxxxxxxxxx
[1]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.4.4 on Linux
[5001] Connecting to blynk-cloud.com:8442
[5374] Ready (ping: 101ms).
GP17 pin status: 1
GP17 pin status: 0
V1 turned device ON
GP17 pin status: 1
V1 turned device OFF
GP17 pin status: 0
V1 turned device ON
GP17 pin status: 1