Control a normally open solenoid valve with a relay off Raspberry Pi Zero W Node.js.
For readers of this post. This is my second node.js to Blynk sketch, so I’m not too familiar. I worked through a bunch of javascript errors, but not sure what to do with this one. I haven’t written the setinterval() loop yet.
/index.js:
var Blynk = require('blynk-library');
var AUTH = 'myspecialauthcode';
var blynk = new Blynk.Blynk(AUTH);
var GPIO = require('onoff').Gpio;
// Relay and LED //
var CH1 = new GPIO(18, 'out'); // Connect Digital Pin 18 on Raspi to CH1 on Relay Module
var LEDgreen = new GPIO(23, 'out', 'high'); // Connect Digital Pin 23 on Raspi to Green LED (+ 330 ohm resistor) and then to "NO" terminal on relay module
var LEDyellow = new GPIO(24, 'out', 'high'); // Connect Digital Pin 24 on Raspi to Yellow LED (+ 330 ohm resistor) and then to "NC" terminal on relay module
// Timing setup
const defaultDuration = 30; // Default duration in minutes
var wateringActiveState = 0; // Global: Virtual button state: Store the state of the watering, is it on or off?
var wateringSliderSelect = defaultDuration; // Global: Set how long you want the watering system to run for, default 30 mins
var wateringDuration = defaultDuration; // Global: Store the preset and programmed number of minutes to run the system
// Timing active
var remainingMins = defaultDuration; // Remaining number of minutes for the countdown
var elapsedMins = 0; // Minutes completed so far since the countdown started
var expiryTimeMins = 0; // Calculated as current time + watering duration, in minutes - the time the counter is due to finish
var displayedMinute = 0; // Don't write to the display constantly, store the current minute and update it when it changes
// Dynamic text variables for display lcd
var displayV6 = "";
var displayV7 = "";
// Static text strings for display lcd
const _WELCOME = "WATER FOR";
const _MINS = " mins";
const _ON = "ON ...";
const _MINS_TO_GO = " mins to go";
const _OFF = "OFF";
const _MIN_COMPLETE = " mins watered";
//
var v2 = new blynk.VirtualPin(2);
var v3 = new blynk.VirtualPin(3);
var v4 = new blynk.VirtualPin(4);
var v6 = new blynk.VirtualPin(6);
var v7 = new blynk.VirtualPin(7);
//time
var d = new Date();
var millis = d.getTime();
// Helper functions
function millis(){
var d = new Date();
var millis = d.getTime();
return millis;
}
function minutesToMillis(mins){
//mins = milliseconds × 60000
return mins * 60000;
}
function millisToMinutes(milli){
//milliseconds = minutes / 60000
return milli / 60000;
}
// Blynk virtual buttons
// V2 Start/Stop Button
// On press, write to Blynk server
v2.on('write', function(param)
{
if(wateringDuration == 0){
v2.write(LOW);
wateringActiveState = false;
} else {
wateringActiveState = param.asInt();
}
expiryTimeMins = 0;
expiryTimeMins = millisToMinutes(millis()) + wateringDuration;
});
// V3 Slider
// On press, write to Blynk server
v3.on('write', function(param)
{
wateringSliderSelect = param.asInt();
if(!wateringActiveState){
wateringDuration = wateringSliderSelect;
v4.write(wateringDuration);
v6.write(_WELCOME);
v7.write(wateringDuration + _MINS);
}
});
function loop()
{
// Calculate time remaining
remainingMins = expiryTimeMins - millisToMinutes(millis());
// ACTIVE WATERING SYSTEM
if(wateringActiveState){
// Set the 5V relay
// If off, turn it on
if(!digitalRead(CH1)){
CH1.write(HIGH);
v2.write(HIGH);
}
// Has signal been on for as long as specified?
// Yes, turn it off
if (remainingMins <= 0) {
// Turn off
wateringActiveState = false;
// No, leave it run
} else {
// Display
if(displayV6 != _ON){
displayV6 = _ON;
v6.write(displayV6);
}
if(displayedMinute != remainingMins){
displayedMinute = remainingMins;
displayV7 = remainingMins + _MINS_TO_GO;
v7.write(displayV7);
}
}
// INACTIVE WATERING SYSTEM
} else {
// Set the 5V relay
// If on, turn it off
if(digitalRead(CH1)){
CH1.write(LOW);
v2.write(LOW);
// DISPLAY
if(displayV6 != _OFF){
displayV6 = _OFF;
v6.write(displayV6);
}
var b = wateringDuration - remainingMins;
if(displayV7 != b + _MIN_COMPLETE){
displayV7 = b + _MIN_COMPLETE;
v7.write(displayV7);
}
// Reset display minute
displayedMinute = 0;
}
}
}
// init
function setup()
{
// Pass Blynk default values
v2.write(wateringActiveState);
v3.write(wateringSliderSelect);
v4.write(wateringDuration);
// Display
v6.write(_WELCOME);
v7.write(defaultDuration + _MINS);
// Defaults
expiryTimeMins = millisToMinutes(millis()) + wateringDuration;
//begin
loop();
}
setup();
Returns:
pi@raspberrypi:~/blynk/garden $ node index.js
OnOff mode
Connecting to: blynk-cloud.com 8441
fs.js:786
return binding.writeBuffer(fd, buffer, offset, length, position);
^
Error: EINVAL: invalid argument, write
at Error (native)
at Object.fs.writeSync (fs.js:786:20)
at Object.fs.writeFileSync (fs.js:1343:24)
at new Gpio (/home/pi/blynk/garden/node_modules/onoff/onoff.js:122:10)
at Object.<anonymous> (/home/pi/blynk/garden/index.js:9:17)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)