Hello =)
My Question is: How to enable Blynk debugging in Javascript? Details below
I have built a home automation system with many ESP8266 and one Raspberry Pi, which is a Blynk client that handles the bridges to all the ESPs.
I use the Pi because the ESPs stop working, if I add more then 4 bridge-widgets on them.
On the Pi I use the node.js Blynk client and forever to keep it running all the time.
My Script works flawlessly for a few days and then stops working.
It starts disconnecting from Blynk-Cloud and reconnecting every second. The forever logfile shows:
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
Connecting to: blynk-cloud.com 8441
Connecting to: blynk-cloud.com 8441
SSL authorization...
SSL authorization...
Connected
Connected
My Script is this. Notice: I use 2 blynk clients for different projects. But since the script is working for a few days, I donât think that that causes the problem.
var BlynkLib = require('blynk-library');
//DHT22
var sensorLib = require('node-dht-sensor');
// Setup sensor, 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 DHT22 sensor');
process.exit(1);
}
// Standard Blynk
var blynk = new BlynkLib.Blynk('xxx');
// Pi-Projekt Blynk
var blynk2 = new BlynkLib.Blynk('xxx');
// Blynk terminal
var terminal = new blynk.WidgetTerminal(126);
// Pi-Projekt Blynk terminal
var terminal2 = new blynk2.WidgetTerminal(124);
// Bridges
var bridge_board1 = new blynk.WidgetBridge(100);
var bridge_board2 = new blynk.WidgetBridge(101);
var bridge_socket_1_stehlampe = new blynk.WidgetBridge(102);
var bridge_normal1 = new blynk.WidgetBridge(103);
var bridge_th16_1_haustuer = new blynk.WidgetBridge(104);
var bridge_TH16_2_Spiegelschrank = new blynk.WidgetBridge(105);
var bridge_TH16_3_Schuppen = new blynk.WidgetBridge(106);
var bridge_touch_bad = new blynk.WidgetBridge(107);
var bridge_touch_1_flur = new blynk.WidgetBridge(108);
var bridge_touch_2_wohnzimmer1 = new blynk.WidgetBridge(109);
var bridge_touch_3_kueche = new blynk.WidgetBridge(110);
var bridge_touch_4 = new blynk.WidgetBridge(111);
// Switch all lights
var allLightsOn = new blynk.VirtualPin(1);
var allLightsOff = new blynk.VirtualPin(2);
allLightsOn.on('write', function(param) {
switchAllLightsOn(param);
});
function switchAllLightsOn(param) {
terminal.write('Turn on all Lights. Param: ' + param + '\n');
// Send value 1 to vPin 122 on the ESPs to turn on the light
bridge_socket_1_stehlampe.virtualWrite(122, param);
bridge_normal1.virtualWrite(122, param);
bridge_th16_1_haustuer.virtualWrite(122, param);
bridge_TH16_2_Spiegelschrank.virtualWrite(122, param);
bridge_TH16_3_Schuppen.virtualWrite(122, param);
bridge_touch_bad.virtualWrite(122, param);
bridge_touch_1_flur.virtualWrite(122, param);
bridge_touch_2_wohnzimmer1.virtualWrite(122, param);
bridge_touch_3_kueche.virtualWrite(122, param);
bridge_touch_4.virtualWrite(122, param);
}
allLightsOff.on('write', function(param) {
switchAllLightsOff(param);
});
function switchAllLightsOff(param) {
terminal.write('Turn all lights off. Param: ' + param + '\n');
// Send value 1 to vPin 123 on the ESPs to turn off the light
bridge_socket_1_stehlampe.virtualWrite(123, param);
bridge_normal1.virtualWrite(123, param);
bridge_th16_1_haustuer.virtualWrite(123, param);
bridge_TH16_2_Spiegelschrank.virtualWrite(123, param);
bridge_TH16_3_Schuppen.virtualWrite(123, param);
bridge_touch_bad.virtualWrite(123, param);
bridge_touch_1_flur.virtualWrite(123, param);
bridge_touch_2_wohnzimmer1.virtualWrite(123, param);
bridge_touch_3_kueche.virtualWrite(123, param);
bridge_touch_4.virtualWrite(123, param);
}
// Floor-Lights on
var floorLights = new blynk.VirtualPin(3);
floorLights.on('write', function(param) {
terminal.write('Switch Floorlights. Param: ' + param + '\n');
// turn on the lights in the floor
if (param == 1) {
bridge_socket_1_stehlampe.virtualWrite(122, 1);
bridge_touch_2_wohnzimmer1.virtualWrite(122, 1);
bridge_touch_1_flur.virtualWrite(122, 1);
bridge_touch_bad.virtualWrite(122, 1);
} else if (param == 0) {
// turn off the lights in the floor
bridge_socket_1_stehlampe.virtualWrite(123, 1);
bridge_touch_2_wohnzimmer1.virtualWrite(123, 1);
bridge_touch_1_flur.virtualWrite(123, 1);
bridge_touch_bad.virtualWrite(123, 1);
}
});
//Alarm-Modus
var timer_alarmModus;
var i = 0;
var alarmModus = new blynk.VirtualPin(0);
alarmModus.on('write', function(param) {
terminal.write('Alarm-Modus. Param: ' + param + '\n');
if (param == 1) {
timer_alarmModus = setInterval(function() { lightshow() }, 200);
} else if (param == 0) {
clearInterval(timer_alarmModus);
allLightsOff(1);
}
});
// Let all the lights blink after each other and then togehter
function lightshow() {
if (parseInt(i) == parseInt(0)) {
bridge_touch_2_wohnzimmer1.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(1)) {
bridge_touch_2_wohnzimmer1.virtualWrite(123, 1);
bridge_socket_1_stehlampe.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(2)) {
bridge_socket_1_stehlampe.virtualWrite(123, 1);
bridge_touch_1_flur.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(3)) {
bridge_touch_1_flur.virtualWrite(123, 1);
bridge_TH16_3_Schuppen.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(4)) {
bridge_TH16_3_Schuppen.virtualWrite(123, 1);
bridge_th16_1_haustuer.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(5)) {
bridge_th16_1_haustuer.virtualWrite(123, 1);
bridge_touch_3_kueche.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(6)) {
bridge_touch_3_kueche.virtualWrite(123, 1);
bridge_normal1.virtualWrite(122, 1);
i ++;
} else if (parseInt(i) == parseInt(7)) {
bridge_normal1.virtualWrite(123, 1);
bridge_touch_bad.virtualWrite(122, 1);
i++;
} else if (parseInt(i) == parseInt(8)) {
bridge_touch_bad.virtualWrite(123, 1);
switchAllLightsOff(1);
i++;
} else if (parseInt(i) == parseInt(9)) {
switchAllLightsOn(1);
i++;
} else if (parseInt(i) == parseInt(10)) {
switchAllLightsOff(1);
i++;
} else if (parseInt(i) == parseInt(11)) {
switchAllLightsOn(1);
i++;
} else if (parseInt(i) == parseInt(12)) {
switchAllLightsOff(1);
i++;
} else if (parseInt(i) == parseInt(13)) {
switchAllLightsOn(1);
i++;
} else if (parseInt(i) == parseInt(14)) {
switchAllLightsOff(1);
i = 0;
}
}
// Window states
//Bathroom
var windows_bath_isOpen;
var timer_windows_bath_isOpen;
var timer_humidity_warning_bathroom;
// Receive the window-state from a ESP with a reed-switch mounted to the window
var vPinWindow = new blynk.VirtualPin(22);
vPinWindow.on('write', function(param) {
terminal.write('Window state received. Param: ' + param + '\n');
//Bathroom
if (param == 0) {
// Fenster geoeffnet
terminal.write('Window opened' + '\n');
windows_bath_isOpen = true;
// Start 30min Timer that notifies, if the windows has not been closed yet
timer_windows_bath_isOpen = setTimeout(notify_window_bathroom, 1800000);
// Stop timer because window has been opened
clearTimeout(timer_humidity_warning_bathroom);
} else if (param == 1) {
// Window has been closed
terminal.write('Window closed' + '\n');
windows_bath_isOpen = false;
// Stop the timer
clearTimeout(timer_windows_bath_isOpen);
} else if (param == 3) {
// RTC Memory state of the ESP is undefined
blynk.notify("ERROR! Undefined RTC state at the ESP at the window!");
}
});
// Method notifies if the window is still open
function notify_window_bathroom() {
console.log("Send Blynk.notify, window is still open.");
blynk.notify("Window bathroom is still open");
}
// Battery state
var battery_ESP_reed1_bathroom;
var vPinBattery_bathroom = new blynk.VirtualPin(25);
vPinBattery_bathroom.on('write', function(param) {
terminal.write('Received Battery state. Reed1_Bathroom. Param: ' + param + '\n');
console.log('Received Battery state. Reed1_Bathroom:', param);
battery_ESP_reed1_bathroom = param;
// Send battery state to Blynk-Pi-Project
blynk2.virtualWrite(50, parseInt(battery_ESP_reed1_bathroom) / parseInt(10));
});
// Temperatures, humidity etc
var temp_kitchen;
var humid_kitchen;
var temp_outside;
var humid_outside;
var pressure_outside;
var temp_door;
var temp_bath;
var humid_bath;
var temp_bedroom;
var humid_bedroom;
var temp_floor;
var humid_floor;
var pressure_floor;
var vPin_Temp_Outside = new blynk.VirtualPin(50);
var vPin_Humid_Outside = new blynk.VirtualPin(51);
var vPin_Pressure_Outside = new blynk.VirtualPin(52);
var vPin_Temp_door = new blynk.VirtualPin(55);
var vPin_Temp_Bathroom = new blynk.VirtualPin(60);
var vPin_Humid_Bathroom = new blynk.VirtualPin(61);
var vPin_Temp_bedroom = new blynk.VirtualPin(80);
var vPin_Humid_bedroom = new blynk.VirtualPin(81);
var vPin_Temp_Floor = new blynk.VirtualPin(85);
var vPin_Humid_Floor = new blynk.VirtualPin(86);
var vPin_Pressure_Floor = new blynk.VirtualPin(87);
vPin_Temp_Outside.on('write', function(param) {
terminal2.write('Temp received: ' + param + '\n');
temp_outside = param;
});
vPin_Humid_Outside.on('write', function(param) {
terminal2.write('Humidity received: ' + param + '\n');
humid_outside = param;
});
vPin_Pressure_Outside.on('write', function(param) {
terminal2.write('Airpressure hpa received: ' + param + '\n');
pressure_outside = param;
});
vPin_Temp_door.on('write', function(param) {
terminal2.write('Temp received: ' + param + '\n');
temp_door = param;
});
vPin_Temp_Bathroom.on('write', function(param) {
terminal2.write('Temp received: ' + param + '\n');
temp_bath = param;
});
vPin_Humid_Bathroom.on('write', function(param) {
terminal2.write('Humidity received: ' + param + '\n');
humid_bath = param;
checkVentilateBathroom();
});
vPin_Temp_bedroom.on('write', function(param) {
terminal2.write('Temperature received: ' + param + '\n');
temp_bedroom = param;
});
vPin_Humid_bedroom.on('write', function(param) {
terminal2.write('Humidity received: ' + param + '\n');
humid_bedroom = param;
});
vPin_Temp_Floor.on('write', function(param) {
terminal2.write('Temp received: ' + param + '\n');
temp_floor = param;
});
vPin_Humid_Floor.on('write', function(param) {
terminal2.write('Humid received: ' + param + '\n');
humid_floor = param;
});
vPin_Pressure_Floor.on('write', function(param) {
terminal2.write('Airpressure hpa received ' + param + '\n');
pressure_floor = param;
});
//DHT22 directly attached to the Pi
// Automatically update sensor value every 30 seconds
setInterval(function() {
var readout = sensorLib.read();
temp_kitchen = readout.temperature.toFixed(1);
humid_kitchen = readout.humidity.toFixed(1);
blynk.virtualWrite(48, readout.temperature.toFixed(1));
blynk.virtualWrite(46, readout.temperature.toFixed(1));
blynk.virtualWrite(44, readout.temperature.toFixed(1));
blynk.virtualWrite(49, readout.humidity.toFixed(1));
blynk.virtualWrite(47, readout.humidity.toFixed(1));
blynk.virtualWrite(45, readout.humidity.toFixed(1));
terminal2.write('Temperature kitchen:' + readout.temperature.toFixed(1) + 'C' + '\n');
terminal2.write('Humidity Kitchen: ' + readout.humidity.toFixed(1) + '%' + '\n');
checkVentilateKitchen();
checkColdKitchen();
checkFrostKitchen();
}, 30000);
// Methods for checking if warings are neccessary
// Humidity
function checkHumid(humid_in, warned_humid_in) {
var warn_humid = warned_humid_in;
if (parseFloat(humid_in) > parseFloat(63.0)) {
warn_humid = true;
} else if (parseFloat(humid_in) < parseFloat(58.0)) {
warn_humid = false;
}
return warn_humid;
}
// Cold
function checkCold(temp_in, warned_temp_in) {
var warn_temp = warned_temp_in;
if (parseFloat(temp_in) < parseFloat(12.0)) {
warn_temp = true;
} else if (parseFloat(temp_in) > parseFloat(16.0)) {
warn_temp = false;
}
return warn_temp;
}
// Frost
function checkFrost(temp_in, warned_frost_in) {
var warn_frost = warned_frost_in;
if (parseFloat(temp_in) < parseFloat(2.0)) {
warn_frost = true;
} else if (parseFloat(temp_in) > parseFloat(6.0)) {
warn_frost = false;
}
return warn_frost;
}
// Ventilation-Notices
// Bathroom
var warned_humid_bathroom = false;
function checkVentilateBathroom() {
// Too high humidity in bathroom
if (checkHumid(humid_bath, warned_humid_bathroom)) {
terminal.write('Too high humidity in bathroom.' + '\n');
// Start timer that notifies if the window isnt opened within 20 minutes
if (!windows_bath_isOpen) {
terminal.write('Window is not opened.' + '\n');
// Start timer only if humidity outside is less than inside
if (parseFloat(humid_outside) < parseFloat(humid_bath)) {
terminal.write('Humidity outside is below inside.' + '\n');
if (!warned_humid_bathroom) {
terminal.write('Not warned yet. Start Timer.' + '\n');
warned_humid_bathroom = true;
timer_humidity_warning_bathroom = setTimeout(notifiy_humidity_bathroom, 1200000);
}
}
}
} else if (!checkHumid(humid_bath, warned_humid_bathroom)) {
terminal.write('Humidity bathroom OK.' + '\n');
if (warned_humid_bathroom) {
terminal.write('Timer has already been started, stop the timer.' + '\n');
warned_humid_bathroom = false;
// Humidity OK in bathroom
// Stop timer, humidity is back to normal
clearTimeout(timer_humidity_warning_bathroom);
}
}
}
// Method notifies and tells to ventilate the bathroom
function notifiy_humidity_bathroom() {
console.log("Sending blynk.notifiy to ventilate the bathroom");
terminal.write('Sending blynk.notifiy to ventilate the bathroom.' + '\n');
blynk.notify("Humidity too high in bathroom, please ventilate!");
}
// Kitchen
// Humidity
var warned_humid_kitchen = false;
var timer_warn_humidity_kitchen;
function checkVentilateKitchen() {
// Humidity too high
if (checkHumid(humid_kitchen, warned_humid_kitchen)) {
terminal2.write('Humid kitchen too high.' + '\n');
// Start timer if humidity outside is below inside
if (parseFloat(humid_outside) < parseFloat(humid_kitchen)) {
terminal2.write('Humidity outside is below inside.' + '\n');
if (!warned_humid_kitchen) {
terminal2.write('Not yet warned. Start Timer Kitchen.' + '\n');
warned_humid_kitchen = true;
// Let the Sonoff Touch-LED in the kitchen blink
// Send param 1 to vPin 5 on the Sonoff touch
bridge_touch_3_kueche.virtualWrite(5, 1);
timer_warn_humidity_kitchen = setTimeout(notify_humidity_kitchen, 600000);
}
// }
}
} else if (!checkHumid(humid_kitchen, warned_humid_kitchen)) {
terminal2.write('Humidity kitchen OK.' + '\n');
if (warned_humid_kitchen) {
terminal2.write('Timer has been started, stop timer kitchen.' + '\n');
warned_humid_kitchen = false;
// Stop the blinking LED on the Sonoff touch
// Send param 0 to vPin 5 of the Touch
bridge_touch_3_kueche.virtualWrite(5, 0);
// Stop the timer because humidity is OK now
clearTimeout(timer_warn_humidity_kitchen);
}
}
}
// Method warns if humidity is too high in the kitchen
function notify_humidity_kitchen() {
console.log("Sending blynk.notify humid kitchen");
blynk.notify("Humidity in kitchen too high, please ventilate!");
}
// Temperature (Cold)
var warned_cold_kitchen = false;
function checkColdKitchen() {
// too cold
if (checkCold(temp_kitchen, warned_cold_kitchen)) {
terminal2.write('Too cold in the kitchen.' + '\n');
if (!warned_cold_kitchen) {
terminal2.write('Not warned yet, notify now.' + '\n');
warned_cold_kitchen = true;
notify_cold_kitchen();
}
} else if (!checkCold(temp_kitchen, warned_cold_kitchen)) {
terminal2.write('Not too cold in the kitchen.' + '\n');
if (warned_cold_kitchen) {
terminal2.write('Already warned.' + '\n');
warned_cold_kitchen = false;
}
}
}
// Method sends blynk.notify if too cold in the kitchen
function notify_cold_kitchen() {
console.log("Sending Blynk.notify");
blynk.notify(unescape("Too cold in the kitchen!"));
}
// Temperature (Frost)
var warned_frost_kitchen = false;
function checkFrostKitchen() {
// Frost!!!
if (checkCold(temp_kueche, warned_frost_kitchen)) {
terminal2.write('Way too cold in the kitchen!!!' + '\n');
if (!warned_frost_kitchen) {
terminal2.write('Not yet warned, send blynk notify.' + '\n');
warned_frost_kitchen = true;
// Let the LED on the Sonoff Touch blink
bridge_touch_3_kueche.virtualWrite(5, 1);
notify_frost_kitchen();
}
} else if (!checkCold(temp_kitchen, warned_frost_kitchen)) {
terminal2.write('No Frost in the kitchen.' + '\n');
if (warned_frost_kitchen) {
terminal.write('Already notified about frost in the kitchen.' + '\n');
warned_frost_kitchen = false;
// Stop blinking LED on the Sonoff Touch
bridge_touch_3_kueche.virtualWrite(5, 0);
}
}
}
// Method sends blynk.notify if its way too cold in the kitchen
function notify_frost_kitchen() {
console.log("Sending Blynk.notify");
blynk.notify(unescape("WARNING!!! Frost in the Kitchen!!!"));
}
blynk.on('connect', function() {
// Connect Bridges
bridge_board1.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_board2.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_socket_1_stehlampe.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_normal1.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_th16_1_haustuer.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_TH16_2_Spiegelschrank.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_TH16_3_Schuppen.setAuthToken("xxx");
bridge_touch_bad.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_touch_1_flur.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_touch_2_wohnzimmer1.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_touch_3_kueche.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
bridge_touch_4.setAuthToken("xxx"); // Place the AuthToken of the second hardware here
terminal.write('Raspberry Pi ready.' + '\n');
//blynk.syncAll();
});
blynk.on('disconnect', function() { console.log("Raspberry Pi disconnected"); });
blynk2.on('connect', function() {
terminal2.write('Raspberry Pi-Project ready.' + '\n');
//blynk.syncAll();
});
blynk2.on('disconnect', function() { console.log("Raspberry Pi-Project disconnected"); });