Phidget Network Server

From Phidgets Support
Revision as of 22:16, 13 December 2017 by Patrick (talk | contribs)

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:



Phidgets without NetworkServer.jpg


Begin with a local computer with the Phidget software installed and a number of Phidgets and/or 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.


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.

NetworkServer PhidgetServer.jpg

Software can set the IsLocal and IsRemote properties of a channel to control if remote or local channels should be considered during matching.

Specifying IsLocal would instruct the Phidget software to only match Phidgets physically connected to the local computer.

Specifying IsRemote would instruct the Phidget software to only match Phidgets exported by a remote computer.

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.

NetworkServer Local Remote.jpg

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.

Using The Network Server

Each Operating System page has a section on how to use the Phidget Network Server:

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).

Connecting to a Network Server

There are two ways to access to a Phidget Network Server. First, 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 enableServerDiscovery() method is used. To connect to a specific server the addServer() method is used. A list of networking methods can be found in the "Network API" section of the Phidget22 API.

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 read from and control all Phidgets attached to it:

Network server sbc.jpg

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.

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 full operating system on which to develop code, serve web pages, and control Phidgets.

For more information on controlling Phidgets with your phone, have a look at the mobile section of our languages 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.

C
PhidgetLightSensorHandle ch;
PhidgetLightSensor_create(&ch);

PhidgetNet_enableServerDiscovery(PHIDGETSERVER_DEVICEREMOTE);

Phidget_setDeviceSerialNumber((PhidgetHandle) ch, 37299);
Phidget_setHubPort((PhidgetHandle) ch, 0);
Phidget_setIsRemote((PhidgetHandle) ch, 1);

Phidget_open((PhidgetHandle) ch);
C#
LightSensor ch = new LightSensor();

Net.EnableServerDiscovery(ServerType.DeviceRemote);

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

ch.Open();
Java
LightSensor ch = new LightSensor();

Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);

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

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

Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE);

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

ch.open();

For more information, please refer to the Phidget22 API, and the Programming Basics page.

Configuring the Phidget Network Server

There are two different ways to configure the Phidget Network Server:

  • via the Phidget Control Panel (for Windows and macOS users). This provides a simple way for you to make changes to the Phidget Network Server, however, not all settings are available.
  • modifying the Phidget Network Server configuration file directly.


Find the configuration file on your machine by referencing the following table:

Operating System Location File
Linux /etc/phidgets/ phidget22networkserver.pc
Windows C:/ProgramData/Phidgets phidget22networkserver.pc
macOS /Library/Preferences/Phidget22NetworkServer/ phidget22networkserver.pc

Now that you have the configuration file on hand, we will go through each setting and describe it.

Settings

phidget{
   enabled: true
}
phidget {
enabled enable the Phidget Server and provide access to Phidget channels over the network.


phidget{
    auth {
        password: 'temp123'
    }
}
auth {
password set a password for your Phidget Server.


phidget{
    pidfile: '/var/run/phidget22networkserver.pid'
}
phidget {
pidfile where the UNIX pid file is written
phidget{
    logging {
        level: info
	file: 'c:/tmp/networkserver.log'
	maxfiles: 4
        maxsize: 1048576
	network {
	    enabled: true
	    port: 5771
	}
	source {
	    phidget22net {
	        level: info
	    }
	    _phidget22usb {
	        level: info
	    }
	    _phidget22match {
	        level: error
	    }
	    _phidget22channel {
	        level: error
	    }
	    _phidget22bridge {
		level: error
	    }
	    _phidget22disp {
		level: error
	    }
        }
    }
}

The use of level throughout this section refers to log level. See the Phidget Control Panel logging section for more information.

logging {
file choose the log file location and name
maxfiles the maximum number of log files to keep before rotation deletes them.
maxsize the maximum file size of a log file before rotation.
network {
enabled enable network logging.
port tcp port that the server listens on
phidget{
    network {
        keepalive: 30000
	ipv4 {
	    address: localhost
	    port: 5661
	}
	datagram {
	    enabled: true
	}
	publish {
	    enabled: true
	    name: 'Phidget22 Server'
	}
	resolveaddrs: false
    }
}
network {
keepalive the server is constantly sending keepalive packets to the client, if it doesn't get a response in the keepalive milliseconds, it will close the connection (0 turns off, so server won't send keep alive)
resolveaddrs if addresses should be resolved to names (affects performance)
ipv4 {
address the address to bind
port server port
datagram {
enabled if UDP for events is allowed
publish {
enabled publish your Phidget Network Server to mDNS
name the name that will be published (defaults to computer name).


phidget{
    feature {
        control {
	    enabled: true
	}
	stats {
	    enabled: true
	}
	dictionary {
	    enabled: true
	    directory: '/etc/phidgets/dictionary.d'
	}
    }
}
control {
enabled enables controls dictionary, which is the network one
stats {
enabled enables the stats dictionary
dictionary{
enabled this enables the use of dictionaries on your server
directory points to the dictionary.d file


phidget{
    filter {
        enabled: true
	clients {
	    default: allow
	    allow {
	        enabled: false
		verbose: false
		list: ['192.168.2.159', '192.168.3.120' ]
	    }
	    deny {
		enabled: false
		verbose: false
		list: ['192.168.2.158', '192.168.3.119' ]
	    }
	}
    }
}
filter {
enabled enable filter to limit what clients are allowed to connect
clients {
defaults choose between allow or deny. Controls if the default is to allow or deny connections.
allow {
enabled if allow rules should be processed
verbose if clients that are allowed to cause a log message
list list of addresses to allow
deny {
enabled if deny rules should be processed
verbose if clients that are denied cause a log message
list list of addresses to deny


phidget{
    www {
    enabled: true
    serverhost: 'my-pc'
    docroot: '/var/phidgets/www'
    mimetypes: '/etc/phidgets/mimetypes.kv'
    logging {
        level: debug
	accesslog: '/var/log/phidget22access.log'
    }
    network {
        cachectrl: nocache
	ipv4 {
	    address: 192.168.1.1
	    port: 8080
	}
	publish {
	    enabled: true
	    name: 'Phidget22 Server'
	}
    }
    phidgets {
        enabled: true
	passwd: 'temp123'
        }
    }
}
www {
enabled enables the Phidget Webserver
serverhost the host name to use for redirects etc. This should not be specified unless there is a reason to override the host name (e.g. server being accessed through a firewall)
docroot where your webpage/files exist.
mimetypes location of mimetypes file
logging {
level See the Phidget Control Panel logging section for more information about different log levels.
accesslog specify the access log location. This file lets you know who has accessed your webserver.
network {
cachectrl prevents clients from caching results (for testing)
ipv4 {
address address to bind
port specify your port
publish {
enabled if the WWW server should be published to mDNS
name the name to publish (defaults to computer name)
phidgets {
enabled contorls if we allow websocket upgrades for Phidgets
passwd the server password

Troubleshooting

When using the Network Server, both the client and server should have the same version of the Phidget software library installed.

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.