Hi all, i’m using my raspberry pi 3 with relays to heat my chicken coop. It’s coming colder here in Canada 
My chicken cool is on the edge of my wifi range and my problem is my script crash when the pi loose the wifi signal.
Is there a solution to avoid the crash from blynk… and let the script try to reconnect to blynk-cloud later?
I’m using blynk-library on node.
This is my code feel free to optimize it if you want… i’m new to coding 
var gpio = require('rpi-gpio');
var blynkLib = require('blynk-library');
var sensorLib = require('node-dht-sensor');
var AUTH = 'b8ab71b111a44231a2d7de9a858b4a3a';
// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);
// Setup Relais
var v0 = new blynk.VirtualPin(0);
// ACTIVATION LAMPE VIA PUSH BUTTON -- WORKING
var lamp = 1
v0.on('write', function() {
if (lamp == 0) {
lamp = 1;
gpio.setup(11, gpio.DIR_OUT, write);
function write() {
gpio.write(11, true, function(err) {
});
}
console.log ('Heater On');
} else {
lamp = 0;
gpio.setup(11, gpio.DIR_OUT, write2);
function write2() {
gpio.write(11, false, function(err) {
});
}
console.log ('Heater Off');
}
});
// LAMPE D'APPOINT
gpio.setup(13, gpio.DIR_OUT, write6);
function write6() {
gpio.write(13, false, function(err) {
});
}
blynk.virtualWrite(5, 0);
// Setu2sensor, exit if failed
var sensorType = 22; // 11 for DHT11, 22 for DHT22 and AM2302
var sensorPin = 4; // The GPIO pin number for sensor signal
if (!sensorLib.initialize(sensorType, sensorPin)) {
console.warn('Failed to initialize sensor');
process.exit(1);
}
var d = new Date();
var lampe_appoint = 0;
if ( d.getHours() > 5 && d.getHours() < 20 ) {
lampe_appoint = 0;
blynk.virtualWrite(5, 1);
} else if( d.getHours() < 5 || d.getHours() > 19 ) {
lampe_appoint = 1;
blynk.virtualWrite(5, 0);
}
console.log('Lampe de jour = ' + lampe_appoint);
console.log(d.getHours());
var readout = sensorLib.read();
blynk.virtualWrite(3, readout.temperature.toFixed(1));
blynk.virtualWrite(4, readout.humidity.toFixed(1));
var heat_relay = 0;
if ( readout.temperature.toFixed(1) < 18 ) {
heat_relay = 0;
//blynk.virtualWrite(0, 1);
console.log('Température Froide');
//} else if ( readout.temperature.toFixed(1) > 22 ) {
// heat_relay = 0;
// blynk.virtualWrite(0, 0);
// console.log('Température Chaude');
} else {
heat_relay = 1;
//blynk.virtualWrite(0, 0);
console.log('Température OK');
}
// Automatically update sensor value every 2 seconds
setInterval(function() {
var dh = new Date();
if (dh.getHours() > 5 && dh.getHours() < 20 && lampe_appoint == 0 ) {
gpio.setup(13, gpio.DIR_OUT, write7);
function write7() {
gpio.write(13, true, function(err) {
});
}
blynk.virtualWrite(5, 1);
lampe_appoint = 1;
console.log('Activation lampe d\'appoint');
} else if (dh.getHours() < 5 || dh.getHours() > 19 && lampe_appoint == 1) {
gpio.setup(13, gpio.DIR_OUT, write8);
function write8() {
gpio.write(13, false, function(err) {
});
}
lampe_appoint = 0;
blynk.virtualWrite(5, 0);
}
if (dh.getHours() > 5 && dh.getHours() < 20 && lampe_appoint == 1 ) {
console.log('Heure plus grande que 5 et plus petit que 20');
console.log(dh.getHours());
} else {
console.log('Heure plus petit que 6 et plus grand que 20');
console.log(dh.getHours());
}
if (lampe_appoint == 1) {
console.log('----------------------------\nLumière d\'appoint ON\n----------------------------');
console.log(lampe_appoint);
} else {
console.log('----------------------------\nLumière d\'appoint OFF\n----------------------------');
console.log(lampe_appoint);
}
// AFFICHAGE DES VALEURS DANS L'APPLICATION BLYNK - HUMIDITY & TEMPERATURE
var readout = sensorLib.read();
blynk.virtualWrite(3, readout.temperature.toFixed(1));
blynk.virtualWrite(4, readout.humidity.toFixed(1));
console.log('Temperature:', readout.temperature.toFixed(1) + 'C');
console.log('Humidity: ', readout.humidity.toFixed(1) + '%');
console.log(heat_relay);
// ACTIVATION DE LA LAMPE CHAUFFANTE EN FONCTION DE LA TEMP…RATURE INTERIEUR
if ( readout.temperature.toFixed(1) > 22 && heat_relay == 1 ) {
heat_relay = 0;
gpio.setup(11, gpio.DIR_OUT, write3);
function write3() {
gpio.write(11, false, function(err) {
});
}
blynk.virtualWrite(0, 0);
} else if ( readout.temperature.toFixed(1) < 18 && heat_relay == 0) {
heat_relay = 1;
gpio.setup(11, gpio.DIR_OUT, write4);
function write4() {
gpio.write(11, true, function(err) {
});
}
console.log('Activation de la lampe chauffante');
blynk.virtualWrite(0, 1);
}
if (heat_relay == 1) {
console.log('----------------------------\nLumière chauffantee ON\n----------------------------');
console.log(heat_relay);
} else {
console.log('----------------------------\nLumière chauffantee OFF\n----------------------------');
console.log(heat_relay);
}
}, 2000);
Thanks all



