
UART.js Library
This library is designed to provide a consistent API for accessing Serial/Bluetooth devices from the web using Web Serial and Web Bluetooth.
It is designed to more or less mimic the Puck.js library, but adds support for Web Serial devices.
To work, it needs to be run from a website that's served over HTTPS (not HTTP). While you can set one up yourself with Let's Encrypt, we're not going to cover that here. Instead, we'll use GitHub Pages.
- Log in or create an account on GitHub.com
- Click on the
Repositories
tab, and clickNew
- Enter
UARTTest
as the name, make sure you checkInitialize this repository with a README
, and clickCreate
(if you don't, you'll have to use command-line tools to create a new file) - Click on the
Settings
tab in the top right - Scroll down to
GitHub Pages
, underSource
choosemaster branch
and clickSave
- Now go back to the
Code
tab, and clickCreate new file
in the top right - Enter
test.html
as the name - Now Copy and paste the following code and click
Commit new file
at the bottom:
<html>
<head>
</head>
<body>
<script src="https://www.espruino.com/js/uart.js"></script>
<button onclick="UART.write('LED1.set();\n');">LED On!</button>
<button onclick="UART.write('LED1.reset();\n');">LED Off!</button>
</body>
</html>
You'll now have your own webpage at: https://your_username.github.io/UARTTest/test.html
You can always just click 'Try Me!' above
As well as UART.write
for sending data, UART.eval
is available for reading back data:
<html>
<head>
</head>
<body>
<script src="https://www.espruino.com/js/uart.js"></script>
<script>
function getTemperature() {
UART.eval('E.getTemperature()', function(t) {
document.getElementById("result").innerHTML = t;
});
}
</script>
<button onclick="getTemperature()">Get Temperature</button>
<span id="result"></span>
</body>
</html>
For further details, check out the Web Bluetooth page, the UART.js repository, and the UART.js code itself.
This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.