Giter Site home page Giter Site logo

ongzzzzzz / p5.web-serial Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 1.0 105 KB

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

License: MIT License

JavaScript 100.00%
p5js p5-library p5-js p5 processing arduino serial serialport physical-computing web-serial

p5.web-serial's Introduction

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

p5.web-serial's People

Contributors

imgbotapp avatar ongzzzzzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

khorweiheng

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.