WebUSB on teensy

dawjs24

Member
Hi, i am working on project that can communicate with teensy using webUSB, but the only thing I found about it was 4 years old topic on this forum. Maybe dev can add support for webusb?
 
Last edited:
I saw this post, but I have an application written for webUSB and it works with teensy on macOS, but on Windows you can't connect to teensy. I think the reason is that Windows is installing its drivers for Teensy, which makes it impossible to connect to Teensy via webusb.

Most of the users of my application use windows, so it must work well, and I don't want to give up teensy...
 
@dawjs24 Out of interest, I'm looking into WebUSB as well. So I started with this library and an Adafruit Itsybitsy M4 board (since I had the impression that that was supposed to work). Well, long story short, I didn't work... Using this sketch, I got up to the point where it recognized the board:
1707042155566.png



but entering H or 'L' showed this:
1707041816060.png

Since you already made it work on a Teensy and MacOS, would you mind sharing what you did to get to that point?

Paul

PS: an Arduino Zero didn't work either - different error message though:
1707047898240.png
 
Last edited:
@PaulS, Maybe I didn't understand this correctly, but I always thought WebUSB is a low level API where you directly communicate to endpoints / interfaces etc. Thus, wouldn't you need a corresponding driver on the Teensy side to make this work? When you just need to exchange data over USB between a browser and a Teensy I'd assume some high level API like WebHID or WebSerial might be better suited. But maybe I just don't understand the use case for a low level USB communication using a web browser.
 
I saw this post, but I have an application written for webUSB and it works with teensy on macOS, but on Windows you can't connect to teensy. I think the reason is that Windows is installing its drivers for Teensy, which makes it impossible to connect to Teensy via webusb.

Most of the users of my application use windows, so it must work well, and I don't want to give up teensy...

WebHID is (probably) your path of least resistance. Yeah, it means changing some of your existing code. I can't see how difficult your code is, but I really doubt it could be worse than dealing with the Windows issues. WebHID really should be worth another look.
 
But maybe I just don't understand the use case for a low level USB communication using a web browser.
Hallo Luni, actually I have no use case, other than satisfying my curiosity about this unfamiliar WebUSB stuff.
But as you explained, WebUSB is apparently a low level API while WebHID/WebSerial is probably better to dive into. I will check out your link above.

Thanks,
Paul
 
I just gave WebSerial a try. This is also very simple to use:

Here a simple test web page:

HTML:
<!DOCTYPE html>
<html>
<head>
    <title>Page Title2</title>
</head>
<body>
    <h1>WebSerial Tester</h1>

    <button type="button" id="connectButton">Connect to Teensy</button>
    <p></p>
    <button type="button" id="sendButton">Say hello to Teensy</button>

    <script src="js/main.js"></script>
</body>
</html>

And here the corresponding java script (main.js)
JavaScript:
let connectButton = document.getElementById('connectButton');
let writeButton = document.getElementById('sendButton');

// allow connection to the port
connectButton.addEventListener("click", async () => {
    const filters = [
        { usbVendorId: 0x16C0 }
      ];
      port =  await navigator.serial.requestPort({filters});
});

// write button opens the port and writes a string to the Teensy
writeButton.addEventListener("click", async () => {
    await port.open({ baudRate: 9600 }); // baudrate ignored
    const writer = port.writable.getWriter();
    let utf8Encode = new TextEncoder();
    const data = new Uint8Array(utf8Encode.encode("hello World\n")); // hello

    await writer.write(data);

    writer.releaseLock();
    port.close();
});

The sketch on the teensy side is trivial. I used USB mode "Dual Serial". The Teensy listens for data on the first port and outputs it on the second.
C++:
void setup()
{  
   while (!SerialUSB1);
 
   SerialUSB1.println("Start");
}

void loop()
{
  if(Serial.available())
  {
    SerialUSB1.write(Serial.read());
  }
}

1707060105643.png
 
WebHID is (probably) your path of least resistance. Yeah, it means changing some of your existing code. I can't see how difficult your code is, but I really doubt it could be worse than dealing with the Windows issues. WebHID really should be worth another look.
I'm trying this now and it almost works fine, the problem I have is that teensy doesn't always connect correctly, here:
Zrzut ekranu 2024-02-5 o 00.42.29.png

everything works fine, however here:

Zrzut ekranu 2024-02-5 o 00.42.46.png

there is a communication problem, I tried using void usb_init, however such a function does not exist, but I found usb_rawhid_configure however it did not help. In my code I only use usb_rawhid_recv and usb_rawhid_send for hid communication.
When a problem occurs, I have to disconnect and reconnect the Teensy until it connects properly
 
When Teensy implements RawHID, it actually has 2 HID interfaces.

Usage FFAB:0200 is the RawHID interface.

Usage FFC9:0004 is the serial emulation interface. This lets you still use Serial.print() to the Arduino Serial Monitor, even though you're not using USB serial.

I'm not familiar with WebHID, so I can't say how... but I can say you probably need to tell it to open only the FFAB:0200 interface and somehow ignore the FFC9:0004 interface.
 
When Teensy implements RawHID, it actually has 2 HID interfaces.

Usage FFAB:0200 is the RawHID interface.

Usage FFC9:0004 is the serial emulation interface. This lets you still use Serial.print() to the Arduino Serial Monitor, even though you're not using USB serial.

I'm not familiar with WebHID, so I can't say how... but I can say you probably need to tell it to open only the FFAB:0200 interface and somehow ignore the FFC9:0004 interface.
Fixed man! awesome!
 
When Teensy implements RawHID, it actually has 2 HID interfaces.

Usage FFAB:0200 is the RawHID interface.

Usage FFC9:0004 is the serial emulation interface. This lets you still use Serial.print() to the Arduino Serial Monitor, even though you're not using USB serial.

I'm not familiar with WebHID, so I can't say how... but I can say you probably need to tell it to open only the FFAB:0200 interface and somehow ignore the FFC9:0004 interface.
Coming back to the topic, I have a question about increasing the package size, I use webhid and it works properly, but the package size of 64 bytes limits me. Is it possible to increase it to 128 bytes, which would be satisfactory for me? If so, will teensy be able to handle it without causing any problems?
 
Back
Top