Phidget Network Server: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Programming]]
[[Category:Programming]]
__TOC__
{|
|style="vertical-align:middle; width: 60%;"|
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.
 
If you haven't read them yet, we recommend first reading the pages on [[What is a Phidget?]] and [[Phidget Programming Basics]] to better understand the contents of this page.
|{{TOC limit|2}}
|}


==General Overview==
==General Overview==
The Phidget Network Server is a feature of Phidgets that makes it possible to control or interact with Phidgets connected to other computers. To understand how it works, let's first take a look at what a system looks like without the Network Server enabled:
[[Image:Phidgets_without_NetworkServer.jpg|link=|500px|right]]
Begin with a local computer with the Phidget software installed and a number of Phidgets and/or [[What_is_VINT?|VINT Hubs]] connected. Connected to the VINT Hubs could be VINT devices.
All of these connected Phidgets have various channels that can be attached, which would allow a program running on the local computer to control or read data from them.


Since the Network Server is disabled in this diagram, the local computer is the only one that will be able to access the connected Phidgets.
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.


<br clear="all">
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.


Enabling the Phidget Network Server will allow software on a remote computer to connect to the local computer and receive the list of available Phidget devices attached.  The channels from the remote computer will appear to be local to the local computer.
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.


[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]
[[Image:NetworkServer_PhidgetServer.jpg|link=|800px]]


Software can set the {{Code|IsLocal}} and {{Code|IsRemote}} properties of a channel to control if remote or local channels should be considered during matching.
When you open a Phidget, you have the option of opening it '''locally''' or '''remotely'''.  


Specifying '''IsLocal''' would instruct the Phidget software to only match Phidgets physically connected to the local computer.
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.


Specifying '''IsRemote''' would instruct the Phidget software to only match Phidgets exported by a remote computer.
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.


Phidget device channels normally can only be attached to a single user channel; however, device channels that are exported by the Phidget Network Server may attach to more than one remote user channel.  There are some exceptions (for example motor controllers) where safety could be an issue.
'''''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.


[[Image:NetworkServer_Local_Remote.jpg|link=|800px]]
==Using The Network Server==


As can be see in this example, there is a Phidget with four channels connected to a local computer.  The local computer has enabled the Phidget Network Server.  When the local computer attaches to channel 1, a remote computer is unable to attach to channel 1; furthermore, the local computer cannot attach a second user channel to channel 1 using the network server because channel 1 is already attached locally. On the other hand, both computers are able to attach to channel 2 remotely, because channel 2 hasn't been attached locally by the local computer. Both the local computer and the remote computer could attach several user channels to channel 2.
Each Operating System page has a section on how to use the Network Server on that operating system:


==Using The Network Server==
Notably, on Windows and OS X this can be handled from the '''Phidgets Control Panel''', under the ''Network Server'' tab.
 
Each Operating System page has a section on how to use the Phidget Network Server:


* [[OS - Windows#Phidget Network Server|Windows]]
* [[OS - Windows#Phidget Network Server|Windows]]
Line 45: Line 36:
* [[OS - Linux#Phidget Network Server|Linux]]
* [[OS - Linux#Phidget Network Server|Linux]]
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]
* [[OS - Phidget SBC#Phidget Network Server|Linux on the Phidget SBC]]
* [[OS - iOS#Phidget Network Server|iPhone/iPad iOS]]


The OS pages have examples of how to set up the Phidget Network Server and how to use it to remotely control and gather data from Phidgets. The OS pages also describe how to start and stop the Phidget Network Server, and how to run it with or without mDNS (Bonjour, avahi, etc).
The operating systems pages will tell you how to start and stop the Network Server on your computer, and how to run it with or without mDNS (Bonjour, avahi, etc).


== Connecting to a Network Server ==
== Connecting to a Network Server ==


There are two ways to access to a Phidget Network ServerFirst, if '''publish''' is enabled in a server, the server will broadcast its existence, and software can enable server discovery to dynamically discover and connect to the server.  Second, software can connect directly to a server. To enable server discovery the {{Code|enableServerDiscovery()}} method is used.  To connect to a specific server the {{Code|addServer()}} method is usedA list of networking methods can be found in the "Network API" section of the {{Phidget22API}}.
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.
 
<tabber>
Java=<syntaxhighlight lang=java>
Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
</syntaxhighlight>
|-|
Python=<syntaxhighlight lang=python>
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
</syntaxhighlight>
|-|
C#=<syntaxhighlight lang=cSharp>
Net.EnableServerDiscovery(ServerType.DeviceRemote);
</syntaxhighlight>
|-|
  C=<syntaxhighlight lang=c>
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICEREMOTE);
</syntaxhighlight>
</tabber>
 
'''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 {{Phidget22API}} 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 {{code|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 {{Phidget22API}} for details.
 
<tabber>
Java=<syntaxhighlight lang=java>
Net.addServer("ServerName", "192.168.2.20", 5661, "passwd", 0);
</syntaxhighlight>
|-|
Python=<syntaxhighlight lang=python>
Net.addServer("ServerName", "192.168.2.20", 5661, "passwd", 0)
</syntaxhighlight>
|-|
C#=<syntaxhighlight lang=cSharp>
Net.AddServer("ServerName", "192.168.2.20", 5661, "passwd", 0);
</syntaxhighlight>
|-|
  C=<syntaxhighlight lang=c>
PhidgetNet_addServer("ServerName", "192.168.2.20", 5661, "passwd", 0);
</syntaxhighlight>
</tabber>


==Network Server on a Phidget Single Board Computer==
==Network Server on a Phidget Single Board Computer==
 
The [{{SERVER}}/products.php?product_id=SBC3003 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:
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 read from and control all Phidgets attached to it:


[[Image:network_server_sbc.jpg|link=|900px]]
[[Image:network_server_sbc.jpg|link=|900px]]


In this example, a Phidget SBC physically connected to a VINT Hub which is connected to a VINT device. The SBC itself has its own channels corresponding to the on-board ports it has. By using the network server, the SBC makes all of these channels available to any computer connected to the network.
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 of an assembly. 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.  


The SBC runs Linux, which provides a [[OS - Phidget SBC|full operating system]] on which to develop code, {{ARTICLE|WebPageOnSBC|serve web pages}}, and {{ARTICLE|PhidgetsWirelesslyWithSBC|control Phidgets}}.
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 [[Software_Overview#Language_Support|languages]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.
For more information on controlling Phidgets with your phone, have a look at the mobile section of our [[Software_Overview#Operating System Support|operating system support]] page, or read {{ARTICLE|MurvRobotIOS|this article}} where we use [[OS_-_iOS|iOS]] to control a robot full of Phidgets.


== Examples ==
== 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.
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.


{{hiddenh3|C}}
<tabber>
Java=<syntaxhighlight lang='Java'>
LightSensor ch = new LightSensor();
 
ch.setDeviceSerialNumber(37299);
ch.setHubPort(0);
ch.setIsRemote(true);


<syntaxhighlight lang=cpp>
Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
PhidgetLightSensorHandle ch;
PhidgetLightSensor_create(&ch);


PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICEREMOTE);
ch.open(5000);
</syntaxhighlight>
|-|
Python=<syntaxhighlight lang='python'>
ch = LightSensor()


Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);
ch.setDeviceSerialNumber(37299)
Phidget_setHubPort((PhidgetHandle) ch, 0);
ch.setHubPort(0)
Phidget_setIsRemote((PhidgetHandle) ch, 1);
ch.setIsRemote(1)


Phidget_open((PhidgetHandle) ch);
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)


ch.openWaitForAttachment(5000);
</syntaxhighlight>
</syntaxhighlight>
 
|-|
{{hiddenh3|C#}}
C#=<syntaxhighlight lang='CSharp'>
 
<syntaxhighlight lang='CSharp'>
LightSensor ch = new LightSensor();
LightSensor ch = new LightSensor();
Net.EnableServerDiscovery(ServerType.DeviceRemote);


ch.DeviceSerialNumber = 37299;
ch.DeviceSerialNumber = 37299;
Line 96: Line 130:
ch.IsRemote = true;
ch.IsRemote = true;


ch.Open();
Net.EnableServerDiscovery(ServerType.DeviceRemote);


ch.Open(5000);
</syntaxhighlight>
</syntaxhighlight>
|-|
C/C++=<syntaxhighlight lang=cpp>
PhidgetLightSensorHandle ch;
PhidgetLightSensor_create(&ch);


{{hiddenh3|Java}}
Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);
Phidget_setHubPort((PhidgetHandle) ch, 0);
Phidget_setIsRemote((PhidgetHandle) ch, 1);


<syntaxhighlight lang='Java'>
PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICEREMOTE);
LightSensor ch = new LightSensor();


Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
Phidget_open((PhidgetHandle) ch);
 
ch.setDeviceSerialNumber(37299);
ch.setHubPort(0);
ch.setIsRemote(true);
 
ch.open();


</syntaxhighlight>
</syntaxhighlight>
</tabber>


{{hiddenh3|Python}}
For more information, have a look at the {{Phidget22API}} and select your language from the drop-down menu. You can learn more about opening Phidgets on the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.
 
<syntaxhighlight lang='python'>
ch = LightSensor()
 
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE);
 
ch.setDeviceSerialNumber(37299)
ch.setHubPort(0)
ch.setIsRemote(1)
 
ch.open();
 
</syntaxhighlight>
 
For more information, please refer to the {{Phidget22API}}, and the [[Phidget_Programming_Basics#Opening_the_Phidget|Programming Basics]] page.
 
{{NetworkServerConfig}}


== Troubleshooting ==
== Troubleshooting ==


When using the Network Server, both the '''client and server should have the ''same version''''' of the Phidget software library installed.
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.


For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].
For other troubleshooting tips, try our General Troubleshooting page, in its [[General_Troubleshooting#Network_Server_Troubleshooting|Network Server section]].

Revision as of 18:48, 26 October 2018

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.

If you haven't read them yet, we recommend first reading the pages on What is a Phidget? and Phidget Programming Basics to better understand the contents of this page.

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.

NetworkServer PhidgetServer.jpg

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

Each Operating System page has a section on how to use the Network Server on that operating system:

Notably, on Windows and OS X this can be handled from the Phidgets Control Panel, under the Network Server tab.

The operating systems pages will tell you how to start and stop the Network Server on your computer, and how to run it with or without mDNS (Bonjour, avahi, etc).

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(ServerType.DEVICE_REMOTE);
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
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);

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:

Network server sbc.jpg

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.

LightSensor ch = new LightSensor();

ch.setDeviceSerialNumber(37299);
ch.setHubPort(0);
ch.setIsRemote(true);

Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);

ch.open(5000);
ch = LightSensor()

ch.setDeviceSerialNumber(37299)
ch.setHubPort(0)
ch.setIsRemote(1)

Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)

ch.openWaitForAttachment(5000);
LightSensor ch = new LightSensor();

ch.DeviceSerialNumber = 37299;
ch.HubPort = 0;
ch.IsRemote = true;

Net.EnableServerDiscovery(ServerType.DeviceRemote);

ch.Open(5000);
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);

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.

For other troubleshooting tips, try our General Troubleshooting page, in its Network Server section.

Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

Using Multiple Phidgets - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.

Polling vs. Events - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.

Logging, Exceptions, and Errors - Learn about all the tools you can use to debug your program.

Best Phidgets Practices - Good programming habits that will save you from common problems when writing code for your Phidgets.