Paso 4: codificación
Cree un nuevo proyecto en el IDE Ideino, elija nombre y modificar los archivos package.json y server.js como sigue:
package.json (puede personalizar los campos nombre, versión, Descripción y autor con su información)
{"name": "Ideino Project", "version": "0.0.1", "description": "Ideino example project", "author": { "name": "Ideino Team" } }
Server.js y eso es todo! Leemos comentarios en el código para saber qué cambiar para adaptar el sistema a las necesidades de su planta.
var linino = require('ideino-linino-lib'), board = new linino.Board(), light = board.pin.analog.A5, //a5 --> I5 light sensor lamp = board.pin.digital.D9, //D9 --> 02 lamp water = board.pin.digital.D5, //water active = 1, waterOn = 0, waterTime = 10000, // set water time dayWater = '0', day = '0'; // function to convert time in a convenient format, for example 10:03 will be 1003, 8:00 will be 800. function addZero(i) { if (i < 10) { i = "0" + i; } return i; } // function to check system time function checkTime (){ var d = new Date(); day = d.getDate(); var h = d.getHours(); var m = addZero(d.getMinutes()); var time = Number(h+1 + '' + m); // added +1 to h for italian time now if (time >= 800 && time <= 1800) { // time of System ON, now it will be active from 8:00 to 18:00 active = 1 ; } else { active = 0; console.log('System OFF'); board.digitalWrite(lamp, board.LOW); // turn off the lamp, system OFF } console.log('Time: ' + Number(h+1) + ':' + m); if (time >= 1700 && time <= 1800) { // time of water, now between 17:00 and 18:00 waterOn = 1 ; } else { waterOn = 0; } } board.connect(function(){ board.pinMode(lamp, board.MODES.OUTPUT); board.pinMode(water, board.MODES.OUTPUT); checkTime(); // first check system time board.analogRead(light, function(value){ console.log('Light Sensor Value: ' + value); if (active === 1){ console.log('System ON'); if (value < 100){ board.digitalWrite(lamp, board.HIGH); // light the lamp } else { board.digitalWrite(lamp, board.LOW); // turn off the lamp } } else { board.digitalWrite(lamp, board.LOW); // turn off the lamp } }); // Check system time setInterval(function() { checkTime(); if (waterOn === 1 && dayWater != day){ // This control allow to water one time at day console.log('It is time to water, I do for '+ waterTime/1000 + ' seconds'); board.digitalWrite(water, board.HIGH); setTimeout(function() { board.digitalWrite(water, board.LOW); waterOn = 0; console.log('Stop water now'); done = true; dayWater = day; }, waterTime); // waterTime specific time of watering, set it in global variables } },61000); // Time to update check system time and water time, 61 seconds now });