14. Utilisation du Yocto-PowerRelay-V2 en JavaScript / EcmaScript
}
}
relay = YRelay.FindRelay(serial+".relay1");
if(await relay.isOnline()) {
document.getElementById('msg').value = '';
} else {
document.getElementById('msg').value = 'Module not connected';
}
setTimeout(refresh, 500);
}
window.sw = function(state)
{
relay.set_output(state ? YRelay.OUTPUT_ON : YRelay.OUTPUT_OFF);
};
startDemo();
</script>
</head>
<body>
Module to use:
<input
<input
id='msg' style='color:red;border:none;' readonly><br>
Relay
<a
href='javascript:sw(0);' >OFF</a>
</body>
</html>
Aucune installation n'est nécessaire pout utiliser cet exemple, il suffit d'ouvrir la page HTML avec un
navigateur web.
14.4. Contrôle de la partie module
Chaque module peut-être contrôlé d'une manière similaire, vous trouverez ci dessous un simple
programme d'exemple affichant les principaux paramètres d'un module et permettant d'activer la
balise de localisation.
"use
strict";
require('yoctolib-es2017/yocto_api.js');
async function startDemo(args)
{
await YAPI.LogUnhandledPromiseRejections();
// Setup the API to use the VirtualHub on local machine
let
errmsg
= new YErrorMsg();
if(await YAPI.RegisterHub('127.0.0.1', errmsg) != YAPI.SUCCESS) {
console.log('Cannot contact VirtualHub on 127.0.0.1:
return;
}
// Select the relay to use
let
module
= YModule.FindModule(args[0]);
if(await module.isOnline()) {
if(args.length
if(args[1] == 'ON') {
await module.set_beacon(YModule.BEACON_ON);
} else {
await module.set_beacon(YModule.BEACON_OFF);
}
}
console.log('serial:
console.log('logical name:
console.log('luminosity:
console.log('beacon:
?'ON':'OFF'));
console.log('upTime:
console.log('USB current:
console.log('logs:');
console.log(await module.get_lastLogs());
} else {
console.log("Module not connected (check identification and USB
}
await YAPI.FreeAPI();
}
106
id='serial'>
> 1) {
'+await module.get_serialNumber());
'+await module.get_logicalName());
'+await module.get_luminosity()+'%');
'+(await
'+parseInt(await
'+await
module.get_usbCurrent()+'
/
<a
href='javascript:sw(1);'>ON</a><br>
'+errmsg.msg);
module.get_beacon()==YModule.BEACON_ON
module.get_upTime()/1000)+'
sec');
mA');
cable)\n");
www.yoctopuce.com