Javascript and USB

Javascript and USB

Javascript is a very powerful language, it can turn a pathetic static web page into something very similar to a native application. But there is a slight difference between web-based applications and native ones: Javascript is held captive by its own browser. Javascript is well known as unable to interact with the real world.

It turns out that the libraries provided by Yoctopuce will allow you to drive a USB Yoctopuce device using Javascript. A web page, five lines of Javascript, and you can already see your device working. No kidding.. wanna see an example ?


The Yocto-PowerRelay is a USB relay. Use it to switch one input to one out of two outputs. Here is the minimal HTML page to make it work:

<HTML>
<HEAD>
 <TITLE>Yocto-PowerRelay, minimal example</TITLE>
 <SCRIPT type="text/javascript" src="yocto_api.js"></SCRIPT>
 <SCRIPT type="text/javascript" src="yocto_relay.js"></SCRIPT>
 <SCRIPT language='javascript1.5' type='text/JavaScript'>
  yRegisterHub("http://127.0.0.1:4444/");
  var relay = yFindRelay("RELAYHI1-00055.relay1");
 
  function switchRelay(bool_powered)
  { relay.set_state(bool_powered ? Y_STATE_B : Y_STATE_A); }
 </SCRIPT>
</HEAD>  
<BODY>
  <a href='javascript:switchRelay(false);'>output A</a><br>
  <a href='javascript:switchRelay(true);'>output B</a>
</BODY>
</HTML>
 



You'll notice that we didn't lie: only 5 lines of Javascript.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.