Trying to read data from Web Serial API

Hey, so basically I'm using Serial.available() to wait for a message from the browser, after which it should break out of a while loop and execute the next line. I'll show the code below. I am using the Web Serial API, and I can connect to the Arduino and write to the serial port, but the program isn't behaving how I would expect.

This is the code in my loop() function:

while(Serial.available() == 0){ 
// repeat until something is written to serial port
}
myServo.write(0); // move position of servo motor

And this is the javascript code in the browser:

async function getReader() {
        port = await navigator.serial.requestPort({});
        await port.open({ baudRate: 9600 });

        const textEncoder = new TextEncoderStream();
        const writableStreamClosed = textEncoder.readable.pipeTo(port.writable);

        const writer = textEncoder.writable.getWriter();
        await writer.write("hello");

        writer.releaseLock();
        
}

The javascript code seems to be working fine, but the Arduino code never breaks out of the loop.Looking for any help, thanks.

What evidence do you have that you can? Do you see the prompt from requestPort({})?

Opening that port will reboot the arduino, so I don’t know where your snippet is but possibly the message is sent before your Serial.begin() is even called.

Try to delay a bit in JavaScript before you send the data, may be a couple seconds possibly

1 Like

Thanks for the reply.
I know the port is open for two reasons, one is that Javascript throws an exception if you call port.open() and it doesn't work. The second is that on some occasions I have actually been able to read the 'hello' message and write it to the serial monitor using Serial.read() and Serial.println(). But even on those occasions, the servo motor doesn't move.

It may be as you say, that the message is being written to the serial port before the Arduino can read it. I will experiment with your solution, thanks for the suggestion!

You can’t have the serial monitor open and at the same time connect to the same port from another application.

Did you try the delay?

Can you blink the built in led 10 times in the setup? This way you’ll know if opening the port from the web API does actually reboot your arduino

Yes you're right you can't have the serial monitor open when you open the connection, but you can open it afterwards and see if anything was written, that's how I saw the message was received. I'm going to try the delay later as well some other things, I'll update the thread with anything I find

That’s weird, the bytes were sent to the app that has opened the port and if this app disconnects and you connect the serial monitor then the arduino reboots… so I’ve doubts on this, but happy to be proven wrong…

Your solution worked! Here's a screenshot showing what I mean. The connection is still open (I haven't disconnected), but no more values are being written. You can see the ASCII values for 'hello' in the serial monitor.

It would be interesting to see if there is a reboot

How do you mean?

Print booting in the setup and blink the led 10 times

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.