Template:PT3 JS ANY NODE: Difference between revisions

From Phidgets Support
(Created page with "<div class="phd-slide-deck" data-deck="PT3_JS_ANY_NODE"> <div class="phd-slide-deck-header"><h3>Language - JavaScript</h3></div> <div class="phd-slides"> <div class="phd-slide...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 19: Line 19:
<h2>Requirements</h2>
<h2>Requirements</h2>


<p>First, make sure you have the following installed:</p>
<p>
 
If you're on MacOS and plan on using any USB Phidgets that use the HID stack, you'll need to install the
<p>● Phidgets Drivers on the computer that will be running the server (see Part 1 of this user guide)</p>
<span>[https://www.phidgets.com/docs/OS_-_macOS Phidget drivers for MacOS]</span>. You can check which stack your USB Phidgets use by checking the 'USB Stack'
in the product specifications. If all of your Phidgets use the PHIDUSB stack or you're on Windows or Linux, you can skip installing the drivers since they're
packaged with the Javascript libraries.
</p>


<p>● <span>[https://nodejs.org/ Node.js]</span></p>
<p>● <span>[https://nodejs.org/ Node.js]</span></p>
Line 27: Line 30:
<br />
<br />


<h2>Version Change</h2>
<h2>Version History</h2>
 
<p><b>1.x.x</b> - Initial Release (unstable- highly recommend updating to 2.x.x or newer)</p>


<p>
<p><b>2.x.x</b> - Fixed stability issues</p>
<b>Note:</b> The Phidgets JavaScript library has been bumped to version 2.x.x following a rewrite. The version 2 API is mostly identical to version 1, but does
 
have some breaking changes. It is highly recommended that any code written against version 1 be updated to version 2, as version 1 is considered unstable.
<p><b>3.x.x</b> - Added WebUSB support for VINT devices</p>
</p>
 
<p>Each release has potential breaking changes, so you should always revisit your code when updating to a new major release.</p>
</div>
</div>


<div class="phd-slide">
<div class="phd-slide">
<h2>Phidget Network Server</h2>
<h2>JavaScript using the Phidget Network Server</h2>


<p>
<p>
The Phidgets JavaScript library requires the Phidget Network Server. Go to the page below and select the tab with your OS to get the Network Server set up:
Go to the <span>[https://www.phidgets.com/docs/Phidget_Network_Server#Using_The_Network_Server this page]</span> and select the tab with your OS to get the
Network Server set up.
</p>
</p>
<p>● <span>[https://www.phidgets.com/docs/Phidget_Network_Server#Using_The_Network_Server Phidget Network Server]</span></p>
<br />


<p>
<p>
Line 53: Line 56:
<p>
<p>
The Phidget Server Webserver can be used to serve files - such as the Phidget JavaScript library, or your own projects. By default, it serves the JavaScript
The Phidget Server Webserver 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
control panel files.
Browser. The Node.js library uses raw sockets to connect to the Phidget Server, and so does not require the Webserver or Websockets.
</p>
 
<p>
In order to connect remotely, you need to use the <b>NetworkConnection</b> object. When you download a code sample later in this guide, make sure the
<b>Remote</b> box is checked.
</p>
 
<h2>JavaScript using WebUSB</h2>
 
<p>
For library version 3.x.x or newer, you can connect to Phidgets plugged in locally via USB without having the Network Server running. This feature is only
supported on Phidgets that use the PHIDUSB stack. You can see which stack your USB Phidgets use by checking the specifications table.
</p>
 
<p>
In order to connect using USB, you need to use the <b>USBConnection</b> object. When you download a code sample later in this guide, make sure the
<b>Remote</b> box is unchecked.
</p>
</p>
</div>
</div>
Line 102: Line 121:
<p>Once you've made your selections, click the <b>Download Example</b> button to download a sample script.</p>
<p>Once you've made your selections, click the <b>Download Example</b> button to download a sample script.</p>


<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_JS_ANY_NODE/-x320/50/JavaScript_Sample_Code_Download.png"></div>
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_JS_ANY_NODE/-x310/50/JavaScript_Sample_Code_Download.png"></div>
</div>
</div>



Latest revision as of 21:34, 17 December 2024

JavaScript in Node.js

Welcome to using Phidgets with JavaScript! By using JavaScript, you will have access to the complete Phidget22 API, including events.

Node.js is an open-source, cross-platform JavaScript run-time environment that allows programs written in JavaScript to be run locally.

Requirements

If you're on MacOS and plan on using any USB Phidgets that use the HID stack, you'll need to install the Phidget drivers for MacOS. You can check which stack your USB Phidgets use by checking the 'USB Stack' in the product specifications. If all of your Phidgets use the PHIDUSB stack or you're on Windows or Linux, you can skip installing the drivers since they're packaged with the Javascript libraries.

Node.js


Version History

1.x.x - Initial Release (unstable- highly recommend updating to 2.x.x or newer)

2.x.x - Fixed stability issues

3.x.x - Added WebUSB support for VINT devices

Each release has potential breaking changes, so you should always revisit your code when updating to a new major release.

JavaScript using the Phidget Network Server

Go to the this page and select the tab with your OS to get the Network Server set up.

The Phidget Server includes a built-in Webserver. This must be enabled when using the JavaScript library in browser, but can be left disabled when using the library from Node.js.

The Phidget Server Webserver 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.

In order to connect remotely, you need to use the NetworkConnection object. When you download a code sample later in this guide, make sure the Remote box is checked.

JavaScript using WebUSB

For library version 3.x.x or newer, you can connect to Phidgets plugged in locally via USB without having the Network Server running. This feature is only supported on Phidgets that use the PHIDUSB stack. You can see which stack your USB Phidgets use by checking the specifications table.

In order to connect using USB, you need to use the USBConnection object. When you download a code sample later in this guide, make sure the Remote box is unchecked.

JavaScript Control Panel

The JavaScript control panel is a Browser version of our Phidget control panel. This can be used to view and control all Phidgets attached to a Phidget server. The JavaScript control panel is installed by default on Windows, macOS and PhidgetSBC. You can also download the source here.

Make sure the Phidget Server - Webserver is enabled, and running, then navigate to http://localhost:8989. (If you changed the port setting on the Webserver, replace '8989' with your selected port)

JavaScript Control Panel

You will now see a program written with JavaScript/HTML that mimics the Phidget Control Panel. It will show all the Phidgets attached to your machine. By double-clicking on the Phidgets, and example will launch.

Using the Code Samples

Now that you've confirmed the webserver is running properly by testing your Phidgets through the JavaScript Control Panel, you can try running some of our sample code. On the Code Samples page and select your device from the drop-down menu.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the Download Example button to download a sample script.

Using the Code Samples

Next, unpack the example and open the command prompt in the folder you extracted to and enter the following commands:

npm install phidget22
npm update

Then enter the following command to run the example (replacing example.js with your example name):

node example.js

Success! Your program is now running with Phidgets.

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

«
»