The ability to combine Arduino or physical computing with browser technologies can give designers and technologists superpowers to prototype awesome experiences faster; easier and cheaper.
In the past, Arduino was usually connected to Processing te generate interactive art, products or services. While this is still a widely used way to connect Arduino to a computer, connecting to a browser can give a wider range of opportunities and requires less installation.
Connecting Arduino to a web browser was however still a pain to create (websockets and a server on the device) but now with the introduction of the Serial API you can connect to any Serial device from the browser with the click of a button.
One huge benefit is that software that runs in the browser is easy to share and execute on various devices and operating systems. This system should work with any Arduino that has a serial connection to the computer. Right now only Google Chrome supports web serial if experimental web platform features are enabled, but more browsers might follow.
In this video, I show how Arduino can be connected through the Serial API to the Chrome browser. The P5 sketch can be found here.
Examples of useIn my thesis on the future of learning Arduino, Arduino Action, I used this web Serial protocol to establish a connection between the Arduino and the ecosystem of phones.
This project was for experience prototyping with Ilse Pouwels , Sebastian de Cabo Portugal and Carolyn Wegner
In this prototype we did use Bluetooth communication, but if we would have known webSerial than that would have been a more optimal solution. The Camera is connected to a laptop on which P5.js runs to run a poseNet (that recognizes your pose using computer vision) and Key Nearest Neighbour (a Machine Learning model that can be trained to distinguish certain poses from others).
Steps to make it work- Get the latest version of Google Chrome
- Enable “enable-experimental-web-platform-features” go to chrome://flags/#enable-experimental-web-platform-features
- You should be able to communicate using the browser with your Arduino; you can test this at https://geertroumen.com/projectHub/serial_terminal.html (You can either upload some code to the Arduino to send data or check RX/TX lights when sending messages to confirm that it is working).
- If you want to try it yourself in P5.js go to this sketch and upload the Arduino sketch in this project to the Arduino.
In the browser the scripting language used is JavaScript, this is where you can take the values from the Arduino and do all kinds of things with it; it can also send values back to the Arduino and use other peripherals like the mouse, keyboard, camera, microphone etc.
The most important functions in the javascript code for connecting with the Arduino are the write and read function. In this example "writeToStream()" is the equivalent of Serial.print for the computer.
writeToStream("Hello")
The equivalent for the Serial.read is reader.read()
async function readLoop() {
while (true) {
const { value, done } = await reader.read();
receiveText.value += value;
}
}
In this case the value of each package of bytes is added to the recieveText field.
Here is one simple example of how you could use a button to send a specific character or string to the Arduino:
//find the element with the HTML tag id="commandButton"
commandButton = document.getElementById("commandButton")
//make a function that does the desired action
function commandFunction(){
//Add a check to see if there is an output stream
if (outputStream) {
//if there is an ouput stream
//send an A to the Arduino
writeToStream("A")
}else{
//if not
//alert the user that there is no connection
alert("no connection")
}
}
//Couple the button to the commandFunction when clicked
commandButton.addEventListener("click",commandFunction)
Troubleshooting- If Arduino is running I had some issues with finding the Serial port in the Serial API interface; just turn Arduino off, plug the Arduino in and out and you should be able to find it.
- Sometimes the connection is flaky if you can't connect; unplug the Arduino, refresh the page, click connect, plug in the Arduino and connect to it. This seems to be the most solid way (out of experience).
- Tutorial on dev.to where they connect the browser to a LED matrix through Arduinohttps://dev.to/unjavascripter/the-amazing-powers-of-the-web-web-serial-api-3ilc
- WICG's Serial API Draft: https://wicg.github.io/serial/
- WICG's Serial API Explainer: https://github.com/WICG/serial/blob/gh-pages/EXPLAINER.md
- Google's Weg Serial Codelab (here they connect the MicroBit to the browser) https://codelabs.developers.google.com/codelabs/web-serial/#0
Let us know below what you make ;)
Comments
Please log in or sign up to comment.