Exploring Web Serial API
|Chrome introduced support for Web Serial API which in Origin trials state(Available on all desktop platforms). This opens up lot possibilities to control physical devices which provide a serial interface. In this post we will see how to control the onboard led present in the arduino board. I am using wio terminal, any arduino compatible device with onboard led should work.
First enable the serial api using the following flag.
chrome://flags/#enable-experimental-web-platform-features
How to access serial port from web page
Serial API provides asynchronous methods to access the serial port. First we need to request for a serial port (browser will prompt the user to a select a serial port), once the port object is available we can open it with required baud rate.
const port = await navigator.serial.requestPort();
await port.open({ baudRate
: 9600 });
Now we can access read/write streams on it
const writer = port.writable.getWriter();
const reader = port.readable.getReader();
For more details on the web serial API see this page
We will be using a simple serial protocol to switch on/off the led. The arduino code will be listening to serial part for valid integers, in case it receives 1 it will switch on the led and for -1 it will switch off the led.
Load above code to arduino and click on connect button below, chrome will prompt with available serial ports, select arduino port and click connect. Once connected send either 1 or -1 to control the led.
Some context on security concerns:
https://mozilla.github.io/standards-positions/#webserial
https://github.com/mozilla/standards-positions/issues/336#issuecomment-678066631
How is it different from arduino web serial library ?
I think arduino web serial lets you to send a message from esp32 and see it in the web page, for this to work both need to be in the same network. Web serial standard is completely different from it. Chrome web serial lets us access any serial device from browser. With this feature a web page can connect to arduino uno any device that has a serial port and physically connected to computer.
i need some help. this code work localy perfect. but not work online server
The example works, but I think the tutorial is missing some code. Even if I use the code from the linked page, I can connect but not write. https://i.ibb.co/pW0NKnQ/01.png
Can you please add the code to write the 1 and -1, that the scetch expects.