Javascript
Last updated
Last updated
Download the magic.js npm package by running the following command in your terminal.
Include the package in your file by creating a reference to it.
If you are planning on using a USB connection, you will need to download the USB driver for your system and follow the install wizard. . We recommend this during development.
In order to pair directly to a piece of hardware, a user has to initialize the connection the first time they use your site. This user flow is a requirement set in place by underlying browser guardrails.
Note: Browser based serial connections are only supported by Chrome, Edge, and Opera. This means a hardware connection will not work when a website is opened on Safari or Firefox.
The easiest way for a user to initialize the connection is by providing a "connect button" for them to click.
A popup will appear for the user to choose the "serial port" and then click connect.
For USB on a Mac, the port will be named "CP2102N USB to UART Bridge Controller (cu.SLAB_USBtoUART)."
For USB on Windows, the port will be named "CP2102N USB to UART Bridge Controller (COM#).
Disconnecting from the hardware can happen programmatically and does not have to be initialized by the user. You must disconnect before a user navigates away from a page or pairs again, otherwise there will be unexpected errors. When a page is reloaded it will automatically disconnect.
If you have multiple of the same module plugged in, it will choose the module on the highest numbered port. To specify a module on a specific port you can pass the port number in your reference like so.
To access the data of a specific port and not as a module you can reference the hardware.
All modules have a simplified mapped version of their data, to access the raw data stream of a module you can do so by referencing its raw property.
For Bluetooth on a Mac, and then click: "cu.Device" in the popup window on the browser or "cu.{replace with unique name of their hardware}."
For Bluetooth on Windows, and then click: "Device" in the popup window on the browser or "{replace with unique name of their hardware}."
You can access module data in multiple ways. The simplest way is to access the module and its property directly. For example, in the case of a , to access how close an object is you can use the following code.
For all module properties you can visit a .