Web USB receive from browser

dev board RobotDyn M0 mini
firmware Arduino
chip SAMD21
features WebUSB

Before starting

Dependancies

Ensure the following requirements are available:

Pre-requisites

Try these simpler examples:

Description

Turn on/off LED on-board compatible arduino hardware through the commands received H and L from the browser on the laptop which is connected to the hardware.

Use this firmware with the browser code.

Code

Download code
#include <WebUSB.h>

// Modified from example https://webusb.github.io/arduino/demos/console
WebUSB WebUSBSerial(1, "webusb.github.io/arduino/demos/console");

const int ledPin = 13;

void setup() {
  WebUSBSerial.begin(9600);
  while (!Serial) {}
  delay(100);

  SerialUSB.begin(9600);
  delay(1000);

  SerialUSB.println("Start!");
  WebUSBSerial.write("Sketch begins");
  WebUSBSerial.flush();

  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (WebUSBSerial && WebUSBSerial.available()) {
    int byte = WebUSBSerial.read();

    if (byte == 'H') {
      SerialUSB.println("LED ON");
      WebUSBSerial.write("Turning LED on.");
      digitalWrite(ledPin, HIGH);
    } else if (byte == 'L') {
      SerialUSB.println("LED OFF");
      WebUSBSerial.write("Turning LED off.");
      digitalWrite(ledPin, LOW);
    }

    WebUSBSerial.flush();
  }
}

Makefile

BOARD?=arduino:samd:mzero_bl
PORT := $(shell ls /dev/cu.usbmodem*)

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) ./

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD) ./

clean:
	rm -f .*.bin
	rm -f .*.elf
	rm -f .*.hex

References

Watch