Hello blynkers!
I’ve an amazing (at least for me that I’m a beginner ) 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
- 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.
- 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:
- advice me if better to redo the project from 0 on a ESP32 or I can keep in on RPi (hope yes I can keep it there)
- 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 )
- advice (and guide ) me to move this code to work with the new blynk cloud platfom
Thanks
I hope this post will also give some hint for blynkers that are trying to do something similar