BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Home automation relay with new blyk, Raspberry VS ESP 32

Hello blynkers! :slight_smile:

I’ve an amazing (at least for me that I’m a beginner :slight_smile: ) automation project in the old blynk App since a year now, and I’d like now to move it to the new amazing blynk cloud!!!

I’m currently using a Raspberry Pi 4 (2GB) and a couple of relay boards (5v) and an IOS phone.
I’ve been using nodeJS for this project.

What my project does:

TAB 1

  1. I have several (10 to be precise) button on the app linked to the RPI digital PIN, and the GPIO pins are connected to the relay boards to switch on and off lights and open doors.
  2. I’ve several (8 to be precise) led on the app linked with blynk virtual pin, those are needed to show on the app the status of the lights (led ON = Light ON, and vice versa). To detect the status of the lights I read the status of the GPIO with this code
    // 3) turn on of pin according to GPIO status
     
    const { Gpio } = require( 'onoff' );
  
  // set BCM 26 pin as 'input'
    const led1 = new Gpio( '26', 'in', 'both' );
    // listen for pin voltage change
    led1.watch( ( err, value ) => {
      if( err ) {
        console.log( 'Error', err );
      }
      // log pin value (0 or 1)
      blynk.virtualWrite(3, value * 255)
      console.log( 'Pin value', value );
    } );

    const led2 = new Gpio( '19', 'in', 'both' );
    led2.watch( ( err, value ) => {
      if( err ) {
        console.log( 'Error', err );
      } .... 
// and so on for each of the 8 led.....

When the GPIO it’s HIGH = 1 = LED ON, when it’s LOW = 0 = LED = OFF

TAB 2
I read with blynk API the data from another procjet with ESP32 where I’ve a distance sensor to mesure water level in a container. I do this as the relay and the container are far from each other but I wat to see all info in one APP.
Info I read from the ESP via blynk API are:

  • Distance
  • ESP status
  • ESP temperature

Here the code (for distance, for other parameter it’s the same just changing pins to read and to write)

    var request = require('request');
        //// get distance in CM
      setInterval(function() {
      request('http://blynk-cloud.com/MYTOKEN/get/v3', function (error, response, body) {
        console.log('Status:', response.statusCode);
        console.log('Headers:', JSON.stringify(response.headers));
        console.log('Response:', body);
        console.log('Response:', JSON.parse(body));
        blynk.virtualWrite(4, JSON.parse(body))
      });},5000);

TAB3
I read direclty from rasberry the CPU temperature and I report it on a chart and I’ve 2 button respectivelly to restart and turn off the raspberry.
Here the code:

    // 1) get RPi temperature
    setInterval(function () {
        child = exec("cat /sys/class/thermal/thermal_zone0/temp", function (error, stdout, stderr) {
            if (error !== null) {
                console.log('exec error: ' + error);
            } else {
                var temp = parseFloat(stdout) / 1000;
                var temp = temp.toFixed(0);
                blynk.virtualWrite(5, temp)
                console.log('RPI_Temp:' + temp + 'C');
            }
        });
    }, 5000);

    // 2) switch off or reboot RPi
    var v1 = new blynk.VirtualPin(1);
    var v2 = new blynk.VirtualPin(2);

    v1.on('write', function () {
        child = exec("sudo halt", function (error, stdout, stderr) {
        });
    });
    v2.on('write', function () {
        child = exec("sudo reboot", function (error, stdout, stderr) {
        });
    });

OFC on top of all the code there is:

    var Blynk = require('blynk-library');
    sys = require('util'),
        exec = require('child_process').exec;
    var AUTH = 'MYTOKEN';
    var blynk = new Blynk.Blynk(AUTH);

I need you blinkers to:

  1. advice me if better to redo the project from 0 on a ESP32 or I can keep in on RPi :sweat_smile: (hope yes I can keep it there)
  2. advice me on what accoriding to you could be done better (all at the moment works perfectly but maybe is not done in the best way, remember I’m a beginner :slight_smile: )
  3. advice (and guide :sweat_smile: ) me to move this code to work with the new blynk cloud platfom

Thanks :slight_smile:
I hope this post will also give some hint for blynkers that are trying to do something similar

If you want to keep the Pi i would use Node-red on it with the Blynk IOT node, Rpi gpio node and MQTT node (or use the API/http request in Node-Red). No need to code then.

Thanks a lot Bazzio!! I’ve tought about when I did the project but the I was half done already, I’ll explore further Node-red. :pray:

My script in nodeJS won’t be supported anymore from the new platform right?

@bazzio thanks again for the suggestion!
I’ve been working to move in Node-red so far and I’ve been able also to do some improvements (more stability) and some nice new functionality, i.e.: integrate with Apple home to control relay with Siri.
Qutie powerfull and better for non expert coders :slight_smile:

My only concern is that Node Red on Blynk IOT seems in Beta, only for testing so far (node-red-contrib-blynk-iot (node) - Node-RED), indeed the nodes are still limited.
Therefore I’m still keeping my nodeJS script running and the legacy app live in parallel to check if there are any issue. So far I’m seems all good :slight_smile: :crossed_fingers:

When the author of the Blynk IoT contrib was converting from the pervious Legacy ws contrib version he didn’t have access to Blynk IoT, so it was done somewhat blind.
I did quite a bit of testing and feeding-back, and some of the issues were on the Blynk side that were subsequently fixed.
I’ve been running version 0.2.0 constantly since it was launched in June ‘21 and its extremely stable, so I wouldn’t worry about the ‘beta’ status.

Pete.

2 Likes

Thank a lot Pete!!! Good news :slight_smile:
Indeed so far all seems to work pretty good :slight_smile:

1 Like