Hi I am trying to give a website direct on/off control of a usb relay on a chrome os device. In the past I just used some terminal scripts to run on start-up on a ubuntu device but chrome os is giving me a hard time making this work.
Asked
Active
Viewed 107 times
1 Answers
0
Based on the sample Linux code it looks like these devices are using an FTDI USB serial chip and support a very simple set of commands to turn the relays on and off. The FTDI chips are supported by ChromeOS and so you can use the Web Serial API to control the device like this:
let port = navigator.serial.requestPort();
await port.open({ baudRate: 9600 });
let writer = port.writable.getWriter();
writer.write(new Uint8Array([0xff, 0x01, 0x01]); // Turn relay on command.
await writer.close();
await port.close();
This is a very minimal example. You can improve this by passing a filter to requestPort()
to select only the USB devices you want. You also don't need to request permission every time. You can call navigator.serial.getPorts()
to get a list of ports that your site already has permission to access.
See https://web.dev/serial and https://wicg.github.io/serial for more information about how to use this API.

Reilly Grant
- 5,590
- 1
- 13
- 23
-
You might need to add a newline or carriage return character (0x0A or 0x0D) to the end of those commands to match the behavior of the Linux shell example. – Reilly Grant May 25 '22 at 01:14
-
Hi thank you for responding. As far as integrating this into my website, does my website need to be a PWA? or can it be a very simple web app. – user19182644 Jun 06 '22 at 19:18
-
The WebUSB API is available to any site loaded over a secure connection (i.e. https). – Reilly Grant Jun 07 '22 at 20:02
-
I'm trying to test the device connection to the api with webserial.io just to have proof of concept that the device can be turned on through the api, but I don't know enough about this stuff to be able to tell it to turn on through this example site. – user19182644 Jun 10 '22 at 00:07