Migrating from USB to an Internet connection

Migrating from USB to an Internet connection

Yoctopuce sells USB modules designed to operate when they are directly plugged into a USB port on your computer. But it is also possible to operate your Yoctopuce modules remotely via a network connection, using a YoctoHub for example. The Yoctopuce API was designed to switch from one mode to the other in a trivial way, so much so that we never thought to explain the principle clearly. An oversight we're now putting right.


YAPI.RegisterHub()

It all comes down to the call to YAPI.RegisterHub, found in all code snippets that manage Yoctopuce modules. To use modules directly connected via USB, the call simply takes the "usb" string as a parameter. For example, in C# the call looks like the code below, but the call is similar in all other programming languages.

if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
   Console.WriteLine("RegisterHub error: " + errmsg);



Note that it's important to verify that the call has worked by checking the return value. If RegisterHub didn't work, the rest of your code won't work either.

If you want to use modules connected to a YoctoHub, all you have to do is replace the"usb" parameter with the hub's IP address. You can easily find out the IP address of a Yoctopuce hub by connecting it via USB and using a VirtualHub. Let's imagine that the address in question is 192.168.1.2:

if (YAPI.RegisterHub("192.168.1.2", ref errmsg) != YAPI.SUCCESS)
   Console.WriteLine("RegisterHub error: " + errmsg);




And that's it, there's no need to change anything else in the code.

The VirtualHub case

The IP address given as a parameter doesn't necessarily have to be that of a physical YoctoHub. It's perfectly possible to use the address of a machine running a VirtualHub, and thus remotely access the modules connected by USB to this machine.

You can even use the 127.0.0.1 address to access modules connected to the local machine. This is the recommended method for circumventing the limitation of the Yoctopuce API , which prevents more than one process on the same machine from accessing modules via a USB connection.

Your code can perfectly well connect to several hubs by making one call to RegisterHub per concerned hub.

Authentication

If the hub is password-protected, this must be specified when calling RegisterHub, in the form of a username + password pair, for example:

if (YAPI.RegisterHub("username:password@192.168.1.2", ref errmsg) != YAPI.SUCCESS)
   Console.WriteLine("RegisterHub error: " + errmsg);




Note that the username is either "user" for read-only access or "admin" for read/write access.


The case of YoctoHub GSMs

YoctoHub GSMs are a special case in that it's generally not possible to connect to them directly, as their IP address is not public, but hidden behind a NAT. So even if you know the IP address of your GSM hub, it's unlikely that you'll be able to connect to it directly.

But there's a trick: you can install a VirtualHub (for web) on a public web server, which is able to drive the GSM hub and to which your code can connect via a call to RegisterHub. Obviously, this won't be nearly as responsive as a direct connection, but it'll work.

Conclusion

A Yoctopuce USB module can be driven both by a direct USB connection and by a network connection, and switching from one mode to the other only requires the modification of a call parameter.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.