Phidget Network Server: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
Line 135: Line 135:
</syntaxhighlight>
</syntaxhighlight>
|-|
|-|
C/C++=<syntaxhighlight lang=cpp>
C=<syntaxhighlight lang=cpp>
PhidgetLightSensorHandle ch;
PhidgetLightSensorHandle ch;
PhidgetLightSensor_create(&ch);
PhidgetLightSensor_create(&ch);

Revision as of 15:00, 29 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.