Phidget Network Server: Difference between revisions
Line 326: | Line 326: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|-| | |-| | ||
JavaScript= | JavaScript=<syntaxhighlight lang=javascript> | ||
const conn = new phidget22.NetworkConnection(5661, 'localhost') | |||
await conn.connect() | |||
const ch = new phidget22.LightSensor() | |||
ch.deviceSerialNumber = 37299 | |||
ch.hubPort = 0 | |||
await ch.open() | |||
</syntaxhighlight> | |||
</tabber> | </tabber> | ||
Revision as of 20:25, 25 April 2022
15 . Phidget Network Server
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers on your local network.
General Overview
Once the Phidget Network Server is enabled, your computer hosts a Phidget Server which broadcasts all connected Phidgets to other computers on your network. When another computer tries to open a channel, these Phidgets will be included in the list of channels that can be attached.
Phidgets attached over a Phidget Network server can be addressed, opened, and attached in much the same way as local Phidgets, so long as the program has access to the Network Server hosting the Phidgets.
Channels that are attached remotely may be opened by multiple programs simultaneously using the Phidget Network Server. There are some exceptions, such as motor controllers, that will never match more than one channel at a time for safety reasons.
When you open a Phidget, you have the option of opening it locally or remotely.
Opening a Phidget locally means communicating with it directly. You can only locally open Phidgets that are physically connected to the computer running your program.
Opening a Phidget remotely means communicating with it using the Network Server. You can remotely open any Phidget on your network, even ones that are physically connected to the computer running your program.
IsLocal and IsRemote serve as additional Addressing Properties when a Phidget Network Server is used in your application, and are used to specify if a Phidget should be attached locally or remotely. If you are opening a Phidget your computer is physically connected to and your program uses the Phidget Network Server, it is strongly recommended to specify if you intend to connect to it locally or remotely.
Using The Network Server
The specifics of running a Phidget Network Server on your machine depends on the operating system it is using. Select your operating system below for instructions:
Select an Operating System for Instructions
Windows
In order to try the Phidget Network Server out, first download and install Bonjour for Windows on your host computer and your client computer.
- Host computer: the computer that is physically connected to the Phidgets via USB and is running the Phidget Network Server.
- Client computer: a computer running a Phidgets application that accesses Phidgets connected to the host computer.
If you only have one computer, don't worry, your machine can act as both a host and a client.
Next, open the Phidget Control Panel on your host computer. Do this by double-clicking on the icon in your taskbar, or by searching for Phidget Control Panel in the start menu.
Navigate to the Network Server tab, you will see something like this:
From here, you can start, stop, or restart the Network Server. You can also decide how and when the Network Server will start.
Select Automatic as the startup type from the list and press the start button. By enabling automatic startup, the Network Server will run immediately after Windows boots, so you won't have to manually start it again.
Now that you have the Network Server running on your host computer, the next step will be accessing the Phidgets connected to your host computer from your client computer. Follow the steps below:
Open the Phidget Control Panel on your client computer. You will see something like this:
As you can see from the image above, Phidget servers are listed under the following heading:
- Remote Server: Servername (link local address)
You now have access to the Phidgets that are connected to your host computer. Try double-clicking one of them to bring up an example:
Note the label in the example indicating a remote connection. This means the example is using the Phidget Network Server in order to communicate with the device.
macOS
In order to try the Phidget Network Server out, you will need a host computer, and a client computer:
- Host computer: the computer that is physically connected to the Phidgets via USB and is running the Phidget Network Server.
- Client computer: a computer running a Phidgets application that accesses Phidgets connected to the host computer.
If you only have one computer, don't worry, your machine can act as both a host and a client.
Next, open the Phidget Control Panel on your host computer. Do this by double-clicking on the icon in your applications folder. Navigate to the Network Server tab, you will see the following:
From here, you can start or stop the Network Server. You can also decide when the Network Server will start.
Check the automatic start box and enter a server name. By enabling automatic startup, the Network Server will run immediately after macOS boots, so you won't have to manually start it again. You can verify the Network Server is running by looking at the label beside the start/stop button.
Now that you have the Network Server running on your host computer, the next step will be accessing the Phidgets connected to your host computer from your client computer. Follow the steps below:
Open the Phidget Control Panel on your client computer. You will see something like this:
As you can see from the image above, Phidget servers are listed under the following heading:
- Remote Server: Servername
You now have access to the Phidgets that are connected to your host computer. Try double-clicking one of them to bring up an example:
Note the label in the example indicating a remote connection. This means the example is using the Network Server in order to communicate with the Phidget.
Linux
In order to try the Phidget Network Server out, you will need a host computer, and a client computer with the Phidget libraries installed:
- Host computer: the computer that is physically connected to the Phidgets via USB and is running the Phidget Network Server.
- Client computer: a computer running a Phidgets application that accesses Phidgets connected to the host computer.
If you only have one computer, don't worry, your machine can act as both a host and a client.
Next, open the terminal on the host computer and type in the following command to run the Network Server in the terminal:
phidget22networkserver
Or, as a daemon:
phidget22networkserver -D
The Network Server is now running on your machine with the default configuration. If you would like to change the Network Server configuration, you can do so by modifying the following file:
- /etc/phidgets/phidget22networkserver.pc
You can also simply point towards your own configuration file with the following command:
phidget22networkserver -c /path/to/configuration/file
Now that you have the Network Server running on your host computer, the next step will be accessing the Phidgets connected to your host computer from your client computer. Follow the steps below:
Open the terminal on the client computer and type in the following command (if you are using a single machine, simply open a new terminal session and continue):
phidget22admin -s
This will list all of the Phidget Servers on your network. Confirm that you can see the Network Server running on your host computer.
After you have confirmed your Network Server is running, type in the following command:
phidget22admin -R -d
This command will list all remote Phidgets on the network. Confirm that you can see the Phidgets attached to your host computer:
Run Phidget Network Server on Boot
If you installed the Phidget drivers using the Package Install, your Network Server will automatically start on boot. If you installed manually using the Source Install, you can follow the steps below to run the Network Server on boot.
Running the Phidget Network Server on Boot can be done a number of ways depending on which Linux distro you have. Some distros have easy built-in ways to run scripts and commands on startup, so we recommend you check that first.
In most cases, you can follow these steps to run the Network Server on startup:
- Create a script file containing
phidget22networkserver
and save it in /etc/init.d/ - Make the script executable:
sudo chmod 755 /etc/init.d/myscript
- Add the script to run at startup:
sudo update-rc.d myscript defaults
Restart the computer and you should be able to see the phidget22networkserver process running when you call top
.
PhidgetSBC
In order to try the Phidget Network Server out, you will need a host computer, and a client computer with the Phidget libraries installed:
- Host computer: the computer that is physically connected to the Phidgets via USB and is running the Phidget Network Server.
- Client computer: a computer running a Phidgets application that accesses Phidgets connected to the host computer.
If you only have one computer, don't worry, your machine can act as both a host and a client.
The Phidget SBC comes with the Phidget Network Server installed, and the SBC automatically starts the Network Server on boot. In order to change any of this default behaviour, navigate to Phidgets->phidget22NetworkServer using the SBC Web Interface:
Connecting to a Network Server
There are two ways to gain access to a Phidget server that's being hosted on your network. If the server is discoverable, you can simply enable automatic server discovery in your program. Select your programming language below to see a sample of how this is done.
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
Net.EnableServerDiscovery(ServerType.DeviceRemote);
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICEREMOTE);
Net is the object that is used for Phidget Networking. You can find a full list of methods and properties available use with the network in the Phidget22 API by selecting Networking API in the drop-down menu.
If the Phidget server is not discoverable, you can connect to it by adding it specifically. This is done using AddServer
, which takes a number of parameters that help specify the server to connect to (e.g. IP address, port, password). See below for an an example of adding a specific server, and take a look at the Networking API in the Phidget22 API for details.
Net.addServer("ServerName", "192.168.2.20", 5661, "passwd", 0)
Net.addServer("ServerName", "192.168.2.20", 5661, "passwd", 0);
Net.AddServer("ServerName", "192.168.2.20", 5661, "passwd", 0);
PhidgetNet_addServer("ServerName", "192.168.2.20", 5661, "passwd", 0);
const conn = new phidget22.NetworkConnection(5661, "192.168.2.20")
await conn.connect()
Enabling the Web Server
The Phidget Network Server includes a built-in Webserver. It can be used to serve files - such as the Phidget JavaScript library, or your own projects. By default, it serves the JavaScript control panel files. The main purpose of the Webserver is to support a Websockets connection for the Browser library - because regular sockets cannot be used in Browser. If you're on Windows or Mac, you can enable the Webserver in the Phidget Control Panel:
If you're using Linux, you can enable it in the Network Server config file located at:
/etc/phidgets/phidget22networkserver.pc
by changing enabled to 'true' in the www section.
Network Server on a Phidget Single Board Computer
The Phidget Single Board Computer (SBC) can provide a compact, inexpensive way to easily run the Network Server. It runs the Network Server in the background automatically from the moment you turn it on, and allows you to remotely read from and control all Phidgets attached to it:
In this example, a Phidget SBC is connected to a VINT Hub, which in turn is connected to a VINT device. By using the Network Server, it makes all of these channels available to any device connected to the same network. The Network Server on the SBC is discoverable by default.
This is convenient because it allows the Phidgets and sensors to be in a remote location, like mounted on a wall or inside some kind of assembly, rather than sitting on your computer desk. The channels of this system could be conveniently accessed by a home computer on the network, a phone running some Phidgets code, or even another Phidget SBC in a different location.
For more information on controlling Phidgets with your phone, have a look at the mobile section of our operating system support page, or read this article where we use iOS to control a robot full of Phidgets.
Examples
Below are some quick examples showing how simple it is to open a Phidget remotely over the Network Server. In each example, a light sensor Phidget is being remotely opened on port 0 of a VINT Hub with serial number 37299.
ch = LightSensor()
ch.setDeviceSerialNumber(37299)
ch.setHubPort(0)
ch.setIsRemote(1)
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
ch.open();
LightSensor ch = new LightSensor();
ch.setDeviceSerialNumber(37299);
ch.setHubPort(0);
ch.setIsRemote(true);
Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
ch.open();
LightSensor ch = new LightSensor();
ch.DeviceSerialNumber = 37299;
ch.HubPort = 0;
ch.IsRemote = true;
Net.EnableServerDiscovery(ServerType.DeviceRemote);
ch.Open();
PhidgetLightSensorHandle ch;
PhidgetLightSensor_create(&ch);
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);
Phidget_setHubPort((PhidgetHandle) ch, 0);
Phidget_setIsRemote((PhidgetHandle) ch, 1);
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICEREMOTE);
Phidget_open((PhidgetHandle) ch);
const conn = new phidget22.NetworkConnection(5661, 'localhost')
await conn.connect()
const ch = new phidget22.LightSensor()
ch.deviceSerialNumber = 37299
ch.hubPort = 0
await ch.open()
For more information, have a look at the Phidget22 API and select your language from the drop-down menu. You can learn more about opening Phidgets on the Programming Basics page.
Troubleshooting
When using the Network Server, both the client and server should have the same version of the Network Server installed. The easiest way to ensure this is to update your libraries on both ends.
Sometimes viewing the Network Server log file can provide additional information if something isn't working properly. If you contact Phidgets support for a Network Server issue, it will be helpful if you attach the log file. You can find the log file depending on your operating system:
When using the Phidget Control Panel in Windows, you can find the log file at
C:\ProgramData\Phidgets\logs\Phidget22NetworkServer_networkserver.log
Which can also be found by clicking on the "Logs" link in the bottom right corner of the Control Panel window.
/Library/Logs
on the left. Then, click on phidget22networkserver.log
in the list on the right.
/var/log/phidget22networkserver.log
For other troubleshooting tips, try our Network Server Troubleshooting page.