Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Connecting a web app to a Circuit Python board via USB
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Connecting a web app to a Circuit Python board via USB

by kmevans on Sat Apr 11, 2020 10:03 pm

I was wondering if anyone had worked on a web app which talks to a CircuitPython board. I've only seen the CircuitPython IDE here - https://github.com/Mr-Coxall/CircuitPython-IDE but no other examples. (I haven't really dug into what MakeCode is doing.)

Assuming a CircuitPython board (without WiFi or Bluetooth) is connected via USB to a host running a web app in a browser, I only know of two ways that bidirectional communications could occur between the board and the web app - WebUSB, and Web Serial. USB HID works well for controlling web apps such as games, but I don't believe it has any back channel so it's a one-way communication. When it works though it's great. WebUSB doesn't appear to be supported by CircuitPython. So it seems like Web Serial is the only possibility. I've seen some relevant threads related to serial communication over USB:

runtime.serial_bytes_available (key for serial polling):
https://circuitpython.readthedocs.io/en ... or.Runtime

Disable REPL to use usb as serial pipe:
viewtopic.php?f=60&t=164078

About using serial to talk to a CircuitPython board:
https://github.com/adafruit/circuitpython/issues/231

Receive commands from the computer via USB
viewtopic.php?f=60&t=146394

reading from serial - circuitpython
viewtopic.php?f=60&t=146236

As far as I can tell CircuitPython doesn't have the equivalent of MicroPython's micropython.kbd_intr(chr). So any serial protocol needs to be escaped, or have to avoid sending CTRL-C by design. Is that still true?

So I was wondering if people had any favorite extendable/flexible yet simple protocols for such a connection.

There's a list of possibilities here - https://en.wikibooks.org/wiki/Embedded_ ... _Protocols

I saw that Google has a Web Serial example which communicates to a Micro:bit running Espruino. https://codelabs.developers.google.com/ ... -serial/#0 Rather than invent a protocol they appear to just send javascript code to the REPL. (David Whale used a similiar idea but not for a web app where he sends python code to the MicroPython REPL https://github.com/whaleygeek/bitio) This seems interesting, but I believe it means that the CircuitPython board would just be in the REPL and not able to run a user program. So it's not really ideal for every scenario.

The types of applications I'm thinking about are mostly educational in nature. For example a student has to wire up an LED to act as a getting hotter/getting colder indicator for a game. Or perhaps sensor data is sent to a web app for logging. Both sides could have simple libraries to handle sending and receiving messages, and make it simple for students to use. Sort of like the Micro:bit's MicroPython radio calls.

kmevans
 
Posts: 30
Joined: Mon Feb 06, 2017 4:32 pm

Re: Connecting a web app to a Circuit Python board via USB

by siddacious on Fri Apr 24, 2020 5:57 pm

This is a great idea and we're working on some stuff that will help make this possible! It's still early days so I don't have anything to point you to but we'll certainly announce it very loudly when we have something to show.

siddacious
 
Posts: 270
Joined: Fri Apr 21, 2017 3:09 pm

Re: Connecting a web app to a Circuit Python board via USB

by kmevans on Fri Apr 24, 2020 9:37 pm

Ok thanks for the response. I'll be curious to see what you do.

Someone separately pointed me towards firmata as a potential protocol, but given that it's a binary protocol the control characters would be a problem as they could break into REPL. Something like micropython.kbd_intr(chr) could fix that, but there must be a reason it hasn't been implemented. Maybe it was viewed as a liability because one might get locked out of the REPL until overwriting the code that's calling it? One idea would be to use the firmata protocol as the basis, but not as the binary protocol that's currently defined. There are probably several approaches to that.

kmevans
 
Posts: 30
Joined: Mon Feb 06, 2017 4:32 pm

Re: Connecting a web app to a Circuit Python board via USB

by kmevans on Fri Apr 24, 2020 11:42 pm

I should try things before posting micropython.kbd_intr(-1) seems to work fine for me. So perhaps a binary protocol is fine.

kmevans
 
Posts: 30
Joined: Mon Feb 06, 2017 4:32 pm

Re: Connecting a web app to a Circuit Python board via USB

by siddacious on Sat Apr 25, 2020 4:02 pm

You may be able to glean some useful information from Melissa's guide here as well:
https://learn.adafruit.com/bluefruit-da ... th-chrome/

Additionally Thea of Winterbloom wrote this neat tool for updating her CircuitPython based 'Sol' Eurorack module using what I think is WebSerial:
https://github.com/theacodes/Winterbloo ... restore.js

siddacious
 
Posts: 270
Joined: Fri Apr 21, 2017 3:09 pm

Re: Connecting a web app to a Circuit Python board via USB

by kmevans on Sat Apr 25, 2020 4:28 pm

I've never run across that project before. https://winterbloom.com/store/winterbloom-sol for anyone who wants to see what it can do. I really appreciate what you're all doing for CircuitPython. I only recently bothered to dig into it, and it's been a really positive experience. Would be great to see a BBC Micro:bit update that was more like your Express boards. (I'm aware of the Clue - almost bought one, and then the inventory went to zero LoL.)

kmevans
 
Posts: 30
Joined: Mon Feb 06, 2017 4:32 pm

Re: Connecting a web app to a Circuit Python board via USB

by siddacious on Sun Apr 26, 2020 4:13 am

You can still get a CLUE at DigiKey ;)
https://www.digikey.com/products/en/rf- ... 859?k=CLUE

siddacious
 
Posts: 270
Joined: Fri Apr 21, 2017 3:09 pm

Please be positive and constructive with your questions and comments.