npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ongzzzzzz/p5.web-serial

v1.0.0

Published

A p5.js library for using the Web Serial API to access devices like Arduino, no setup required

Downloads

12

Readme

p5.web-serial

A p5.js library for using the Web Serial API to access devices like Arduino, no setup required.

What is this and why?

This library is a wrapper for the Web Serial API, which enables you to communicate with devices like Arduino without having to run a local server!

arduino + p5.js

The Web Serial API is available on all desktop platforms (Chrome OS, Linux, macOS, and Windows) in Chrome 89. More about compatibliity can be found here.

Getting Started

To use this library, create a new p5.js sketch and import the library in the index.html file.

You can download p5.web-serial.js and use it directly in your code:

<script language="javascript" type="text/javascript" src="p5.web-serial.js"></script>

Or, you can also use a CDN link available via jsdelivr:

<script language="javascript" type="text/javascript" src="https://cdn.jsdelivr.net/gh/ongzzzzzz/p5.web-serial/lib/p5.web-serial.js"></script>

After importing the library, head over to your script.js file, and make sure the setup() function looks like:

let port, reader, writer;
async function setup() {
	createCanvas(windowWidth, windowHeight);

    // additional code here...

	noLoop();
	({ port, reader, writer } = await getPort());
	loop();
}

Note the async keyword, definitions of port, reader, writer and the noLoop() and loop() function calls.

Examples

Check out the examples folder for more details.

Sending data from the browser to the Arduino:

make sure to put \n at the end of every writer.write()! (so arduino can know where it needs to read until)

async function draw() {
	if (port) {
		try {
			if (mouseIsPressed) {
                // do something...
				await writer.write("clicked!\n");
			}
			else {
                // do something...
				await writer.write("not clicked!\n");
			}
		} catch (e) { console.error(e) }
	}
}
void loop() {
    val = "";
    if (Serial.available()) {
        val = Serial.readStringUntil('\n');
        val.trim();
    }

    if (val == "clicked!") digitalWrite(ledPin, HIGH); 
    else if (val == "not clicked!") digitalWrite(ledPin, LOW);
}

Sending data from the Arduino to the browser:

reading data on the p5js side is a little uhhhh messy (lol) for now, but it works!

async function draw() {
	try {
		while (true) {
			const { value, done } = await reader.read();

			if (done) {
				// Allow the serial port to be closed later.
				reader.releaseLock();
				break;
			}
			if (value == "69420") {
                // do something...
                console.log("nice");
            }
		}
	} catch (e) { console.error(e) }
}

remember to use Serial.println() and not Serial.print() - this is to let the browser know where it needs to read until!

void loop() {
    if (digitalRead(buttonPin) == LOW) {
        Serial.println("69420");
        delay(150);
    }
}

Resources

Issues

Report issues in the issues tab :3

Contributing

just open a pull request :D ~free labour~ contributions always welcome!

Screenshots / GIFs

License

MIT License