(SOLVED - but still open for suggestions) Java + Blynk questions regarding my test script on RPi (Node.js) - Includes real buttons and LEDs

Any Java/Blynk/RPi experts with nothing by free time out there? :smiley:

I have been slogging through Google and Github trying to make sense out of what I guess is Java when using Node.js and Blynk.

I have a mixture of the test script provided by @vshymanskyy (showing the seconds in the App and showing a Virtual Button status on the CLI), mixed in with some direct pin manipulation (successfully controlling an LED and dual relay board) and my recent attempts to read a physical button on the RPi.

Two issues I have noticed…

First issue: (EDIT: This has been RESOLVED - see below) I added a Virtual LED to the Virtual Button toggle… and it works, sorta… it does toggle the state ON/OFF, but the colour stays black (even though I have it set to match the Button colour orange). I guess it works differently in Java. So any idea how to set the LED value in java as it is clearly not the usual Blynk type commands as listed in the DOCs for C++ :stuck_out_tongue_winking_eye:

v1.on('write', function(param) {
  console.log('V1:', param[0]);
  v10.write(param[0]);  // This turns on or off the Virtual LED - but not showing preset colour?

Second issue: I am trying to determine the state of a physical button on (Pin 22, GPIO 6, BCM 25 - it is enough to drive one bonkers :confounded:) )

After digging through the directions on this page pi-gpio - npm EDIT - This appears to be an old and near dead option… don’t use!

2nd EDIT - And has been SOLVED by using the already installed onoff library. - jump to comment below.

I was able to install the pi-gpio library and mess around with commands until I got the script to run, even though it throws an error… but of course nothing happens when I press the physical button.

EDIT - I have removed the code that I had pasted here, since it was no good anyhow… no sense confusing everyone :wink:

At this point, I don’t know my button’s state, but I am clearly in a drooling state :dizzy_face:

The LED needs a value between 0 and 1023.
For me, this is working:

LED on:
blynk.virtualWrite(10, 1023);

LED off:
blynk.virtualWrite(10, 0);

@espftw That doesn’t look like Java to me… Are you running this on an RPi using Node.js?

The node.js - version on the Pi uses javascript, not java.
And yes, this is javascript.

Try something like this:

v1.on('write', function(param) {
  console.log('V1:', param[0]);
   if (param == 0) {
        blynk.virtualWrite(10, 0);
    } else if (param == 1) {
           blynk.virtualWrite(10, 1023);
1 Like

Errr… now you are really messing me up :stuck_out_tongue_winking_eye: I didn’t realise they where two separate things. OK, I will try that code and see what happens.

They are very different. Please check my last post, I inserted a code sample that should work if your LED-Widget in the Blynk app is on virtual pin 10

I had to reverse that last } But now it is working, Thanks :+1:… OK, so Javascript will use the standard Blynk commands… just going to take some getting use to slightly (or significantly) different syntax.

I have been wondering if I even needed to install (and use) that pi-gpio library?? Wouldn’t something have already been included in the Blynk Libraries?

Maybe I am just using the wrong commands again… what is the Javascript equivalent of digitalRead()?

yep, I typed too quickly :blush:

I’m learning the javascript blynk myself at the moment.
If you need a sample using lots of virtual pins and timers, you can check my sourcecode.

EDIT: I have not tried buttons attached to the GPIOs on the Pi yet, sorry. Can’t help there

After further Googling the errors I was getting re: GPIO, it looks like I was trying to use an old and near dead quick2wire-gpio method.

I still think I need to be using something that is already included in the Blynk libraries… just need to find that breadcrumb trail… hmmm :mag:

Alas… it was staring me in the face… onoff… and here I thought that was just for the direct pin manipulation part of Blynk (e.g. setting a Button Widget to a digital pin).

OK, I now have added physical button (momentary switch) controlling the physical LED as well as another LED Widget… yahh!!

Here is my test code as it currently stands… very messy, uncoordinated and somewhat commented, but it works :smiley:

Anyone have a better way of editing Javascript in a terminal window than nano?

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:"", 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 LED on
    } else if (param == 1) {
                blynk.virtualWrite(10, 255);  // V10 Widget 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 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 LED on
    } else if (value == 1) {
                blynk.virtualWrite(2, 255);  // V2 Widget LED off

process.on('SIGINT', function () {  // This is apparently a cleaner exit upon CTRL-C... I dunno??...


why on earth you’d do that???
there are very nice ide-s from jetbrains for javascript
(actually, i think jetbrains are the BEST. also used pycharm, it was great…)

Cuz I is a Linux n00b :stuck_out_tongue:

Actually, the RPi is running headless, so I need (want?) to do everything in the CLI via SSH, and nano was there.

I also need free, as purchasing something from jetbrains is not an option.

I am sure there is something better than nano that fits the bill… I just haven’t spent any time looking yet.

WinSCP for a Windows SSH interface and then Notepad++

1 Like

Thnaks. I think you may have mentioned this before? but I am in a better mindset to understand them now ;).

Have just installed both… now need to determine if I can get super user authority when editing certain files… of should I just give my User SU powers on the Mint box?

Yes you can have root access via WinSCP but you have to set up the root account on the Pi.

this is exactly what i wanted to recommend… :wink:

there is a saying for pc games, but it applies to software too:
“actually all games are for free, but some people are paying for them…”