Where to save and how to run Blynk Scripts

I’ve just started to use Blynk with a Raspberry Pi 3 and love the GUI on the app, I’ve got to the point where I want to use virtual pins to read data from an I2C and publish as a labelled value, but I’m stuck at the first hurdle and just can’t figure out how to run the most basic of scripts, where to I put the file and how can I run it?

I am a noob & just need a bit of a kick to help me get going… Thanks!

@rocketfool I do not have any experience with using the Raspberry Pi 3 (or any Raspberry Pi) so I cannot give any advice on this. What I can do is point you to a video that will probably cover what you are looking for.
https://community.blynk.cc/t/raspberry-pi-video-tutorial/13490
I hope this helps.

Oh, don’t forget about all those helpful links on the top right of the screen. DOCS and ?HELP CENTER are your friends. I could not have gotten my projects working without them.

2 Likes

Yeap, at the end of video (starting from 5 minute) there is explanation on how to work with virtual pin. You can also use this.

She is doing very technical correct ( and beutifull ) presentations !
Transfer my congrats to her!

1 Like

Sure :slight_smile:

Many thanks for both your help, I can now do everything Anna shows in video with success.

I would like to read the status of a thermocouple and have a widget read this value and have tried the ‘BLYNK_READ’ function just to practice however I get an error saying “BLYNK_READ is not defined”

I’m staying simple to begin with the following;

  BLYNK_READ(V7)
{
Blynk.virtualWrite(7,100);
}

What should I use presumably at the start of my script to allow the use of this? (Using Raspberry pi 3)

Cheers

I should add I’m starting my script with;

var Blynk = require('blynk-library');
var GPIO = require('onoff').Gpio;
var led = new GPIO(13,'out');


var AUTH = '<<token no.>>';

var blynk = new Blynk.Blynk(AUTH);

var v1 = new blynk.VirtualPin(1);
var v9 = new blynk.VirtualPin(9);
var v7 = new blynk.VirtualPin(7);

Always best to include the whole script when initially posting… there can be subtle syntax errors elsewhere that cause issues.

@rocketfool you are mixing C++ and JS. Decide which you wish to use and then review the available examples.

1 Like

Thanks, I can’t find any examples that have resulted in me being able to read and write with virtual pins.

Maybe Javascript isn’t for me, I’m simply trying to replicate a function I have in Python to allow me to use the great GUI the Blynk app provides - is there a way to run a Python script instead and how I could interface with the Blynk app this way?

Apologies for how dumb this probably sounds, I’m a little out of my depth here

@rocketfool Have you tried the SKETCH BUILDER?

Set the Board to Raspberry PI and take a look at the PUSH DATA example.

Basically all you need to do is replace the

Blynk.virtualWrite(V5, millis() / 1000);

with some code to read the analog pin the thermocouple is wired to, and then pass that value to a virtual pin.

int thermo = analogRead(A0); // get value from thermocouple
Blynk.virtualWrite(V1, thermo); // pass value to virtual pin 1

On App you will need a value display linked to virtual pin 1.

Again, my PI experience is ZERO, but maybe this will help.

@Toro_Blanco Sketch Builder for Pi is C++ and it looks like @rocketfool is set up for JS.

I knocked up a guide for C++ on a Pi some time ago and it’s available at Using C++ on a Raspberry Pi with Blynk

@rocketfool with JS you can access pretty much anything on the Pi, including Python scripts.
Search posts on this site containing “child_process”.

Thanks, I was about to say I have looked at sketch builder but I only get errors back, I guess because I’m mixing languages.

I guess my question now is, what is the JS version of ‘BLYNK_READ(V[])’?

This may help me get to a solution… In the meantime I’ll also investigate child process as you suggest.

@rocketfool

Awhile back I dove head first into the whole Blynk on RPi thing… I am still not sure if my head hit the crushing depth of the deep end or sharp rocks at the shallow end :stuck_out_tongue: but I have both WiringPi (C++) and Node.JS running very basic scripts that do very simple stuff.

Here is the code for the Node.JS and the QR for the App. It is controlling two relays on GP23 & 24. An LED on GP18. shows a display of the seconds (looping 0-59). Toggles a virtual LED via virtual button and alternates two virtual LEDs via physical button on GPIO25

It is not much to go on, but I hope it helps.

PS, it is setup for my Local Server, so you will need to change this line to something different… that I can’t think of off the top of my head :confused:

var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr:"10.10.3.13", port:8442 } ) });  // Connects to my local server...  nener nener not for you!

NodeJS Test Script:

var Gpio = require('onoff').Gpio;  // links variable 'Gpio' to the all important onoff GPIO control library

var Blynk = require('blynk-library');  // Links variable 'Blynk' to the Blynk Library

var AUTH = 'cda957435ce44407bed2e9283c95018f';  // My top secret auth code... useless to anyone on the cloud ;P

var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr:"10.10.3.13", port:8442 } ) });  // Connects to my local server...  nener nener not for you!

var v1 = new blynk.VirtualPin(1);  // Setup Button Widget on V1 with variable 'v1'
var v9 = new blynk.VirtualPin(9);  // Setup Display Widget on V9 with variable 'v9'
var v10 = new blynk.VirtualPin(10);  // Setup LED Widget on V10 with variable 'v10'


v1.on('write', function(param) {  // Watches for V1 Button
  console.log('V1:', param[0]);  // prints value to CLI
if (param == 0) {
        blynk.virtualWrite(10, 0);  // V10 Widget (ORANGE) LED on
    } else if (param == 1) {
                blynk.virtualWrite(10, 1023);  // V10 Widget (ORANGE) LED off
        }
});

v9.on('read', function() {   // I don't understand why this says read??
  v9.write(new Date().getSeconds());  // but this sends the seconds 0-59 to the Display Widget 
});

  led = new Gpio(18, 'out'),  // Sets up the BCM pin 18 as an output for the LED and assigns it to variable "led"
  button = new Gpio(25, 'in', 'both');  // Sets up the BCM pin 25 as an input registering both rising and falling for the variable "button"

button.watch(function (err, value) {  // Watches for button press and assigns 0/1 to value
  if (err) {
    throw err;
  }
led.writeSync(value);  // Sends value (0/1) to Physical LED
if (value  == 0) {
        blynk.virtualWrite(2, 0);  // V2 Widget (GREEN) LED off
	blynk.virtualWrite(3, 1023);  // V3 Widget (RED) LED on
    } else if (value == 1) {
                blynk.virtualWrite(2, 1023);  // V2 (GREEN) Widget LED on
		blynk.virtualWrite(3, 0);  // V3 (RED) Widget LED off
        }
});

process.on('SIGINT', function () {  // This is apparently a cleaner exit upon CTRL-C... I dunno...
  led.unexport();
  button.unexport();
});

1 Like

hey guys how can I make this script to run at boot?
thank you!!

@ishben1 Personally, I Googled something like “how to run scripts on Linux at boot” and tried a few differing methods, like rc.local and crontab until I found one I liked (i.e. sorta understood and seemed to work reliably :wink: ).

The actual path and command you use will depend entirely on where you save the script, what you call it, how you want it to start and the aforementioned choice of the various options. Ah Linux, I love it and hate it in equal parts :stuck_out_tongue:

I’ve finally got what I had intended to do working, far from optimal but thought I’d share some buts.

The actual reading from the thermocouples is done in Pythin scripts which are spawned from a node file

The result is a fan actuating on/off to control a BBQ to a stable temperature (smoked pork butt here I come!)

Javascript below;

    var Blynk = require('blynk-library');
    var GPIO = require('onoff').Gpio;
    require('events').EventEmitter.defaultMaxListeners = Infinity;
    var util = require("util");

    var spawn = require("child_process").spawn;
    var fan_actuation = spawn('python',["BBQ_Lite.py"]);
    var BBQ_temp1 = spawn('python',["Read_TC1.py"]);
    var BBQ_temp2 = spawn('python',["Read_TC2.py"]);


    var AUTH = '########################';

    var blynk = new Blynk.Blynk(AUTH);

 
    var v12 = new blynk.VirtualPin(12);//assigned to a label
    var v20 = new blynk.VirtualPin(20);//assigned to a label
    


    //Read Data from thermocouple 1 
    BBQ_temp1.stdout.on('data',(function temp1(temp){ // executes child process (Python TC read)
    var BBQ17 = temp.toString('utf8') // assigns temp as a string to variable
    var BBQ1=parseInt(BBQ17);// then assigns string as integer for display
    blynk.virtualWrite(12,BBQ1) // writes integer to Blynk label 
    }));


    //Read Data from thermocouple 2 
    BBQ_temp2.stdout.on('data',(function temp2(temp){ // executes child process (Python TC read)
    var BBQ27 = temp.toString('utf8') // assigns temp as a string to variable
    var BBQ2=parseInt(BBQ27); // then assigns string as integer for display
    blynk.virtualWrite(20,BBQ2) // writes integer to Blynk label 
    }));

the ‘BBQ_Temp’ functions read different thermocouples, I use MAX31855 sensors but couldn’t get the libraries installed via npm so gave up and used the Python versions.

the main fan actuation is done via the ‘BBQ_Lite.py’ script which I didn’t realise at first but isn’t called how I thought it needed to be, but also read temps and turns fan on/off via GPIO on demand.

The result is a very satisfactory temperature control and temperatures reported to the Blynk app to allow remote monitoring.

This forum was a lot of help to kick me off so happy to answer any questions or take any recommendations for better coding!

1 Like