Can webusb connect a website to a Kmtronic Usb one relay device in a ChromeOs chromebox? - webusb

Hi I am trying to give a website direct on/off control of a usb relay on a chrome os device. In the past I just used some terminal scripts to run on start-up on a ubuntu device but chrome os is giving me a hard time making this work.

Based on the sample Linux code it looks like these devices are using an FTDI USB serial chip and support a very simple set of commands to turn the relays on and off. The FTDI chips are supported by ChromeOS and so you can use the Web Serial API to control the device like this:
let port = navigator.serial.requestPort();
await port.open({ baudRate: 9600 });
let writer = port.writable.getWriter();
writer.write(new Uint8Array([0xff, 0x01, 0x01]); // Turn relay on command.
await writer.close();
await port.close();
This is a very minimal example. You can improve this by passing a filter to requestPort() to select only the USB devices you want. You also don't need to request permission every time. You can call navigator.serial.getPorts() to get a list of ports that your site already has permission to access.
See https://web.dev/serial and https://wicg.github.io/serial for more information about how to use this API.

Related

Connecting to Web Bluetooth

I'm trying to connect a bluetooth device (ESP32, peripheral) with Web Bluetooth (central). I send an addvertising packet and I can see the device on the web page, but I fail to connect to it. My code for the ESP32 looks like this: (MicroPython)
ble.gap_advertise(interval_us=100, adv_data=bytearray(b'\x02\x01\x06\x06\x08\x45\x53\x50\x33\x32\x11\x07\x26\x6c\x34\x7f\xb2\x38\x61\x82\xbe\x4d\x1d\x64\xf1\xf7\x93\x16\x02\x19\x80'),resp_data=None, connectable=True)
In Web Bluetooth (JavaScript) I just request a device and try to connect to the gatt.
Can anyone help me out?
Thanks in advance!
The JavaScript does work with another bluetooth peripheral. (RN4678)
And in JavaScript I can find the peripheral (ESP32) after filtering, but I can't connect to it.
JavaScirpt:
server = await device.gatt.connect()
The code stops at that point.
MicroPython:
The GATT is defined using:
ble.gatts.register.services()
I'm trying to use my own service, not a SIG defined one. So I advertise b'(x...') which contains the flags, the service and the device name. And it works with the nRF Connect App. I can send and recive data. But it doesn't work with the bluetooth enabled web browser.
Thanks for your help!

linux usb gadgets traffic monitoring

I am using a BeagleBone Black board (kernel 4.14.108-ti-r104) to create USB gadget using configfs/functionfs. I compose my gadget (using gadgettool) providing details about device configuration (function, vendor id, product id and ton of other params), run my userspace program that writes descriptors and strings to ep0 and connect the device to host. All works fine, I get BIND (when binding device to UDC) and ENABLE (when actually host is connected) events and my device can read from ep2 and write to ep1. Using wireshark I see the communication looks good, device and configuration descriptors as well as strings are exchanged.
The problem starts when I connect the device to another host. Unfortunately I have almost no control over that host, in particular I cannot run wireshark there, I don't even know the OS. The only thing I can do is to plug/unplug device, optionally see a message that device was detected and optionally a restart. What I see on the gadget side is that following BIND and ENABLE events I immediately get SUSPEND event and read on ep2 fails with 108 (ESHUTDOWN). Now the question is how to track the problem down.
I tried usbmon, but it seems it does not listen to traffic when device is in gadget mode. I have also seen https://github.com/torvalds/linux/blob/master/drivers/usb/gadget/udc/trace.h file which seems to define some udc trace points, but I am not really sure how they can be used.
So the final question is simple: how do I get any information about traffic on USB bus having access only to gadget side? I don't need full trace, but al least some information which packets were exchanged would be super useful. Did it fail while exchanging device descriptors, configuration/interface/endpoint descriptors or strings or something totally different?
Small update:
The whole thing is about Android Open Accessory Protocol and I am trying to write a gadget that would connect to this accessory.
I have changed my gadget composition somewhat and now I know the gadget is being identified by host (it displays manufacturer/model) so I suppose the issue is not in device descriptor and strings. I have used two additional flags in descriptors (FUNCTIONFS_ALL_CTRL_RECIP | FUNCTIONFS_CONFIG0_SETUP) and when connecting to my computer I get setup event (request 51 as expected), but when connecting to my accessory I still get SUSPEND/ESHUTDOWN. This time though it looks like the time between ENABLE and SUSPEND is much greater (over 10 seconds) which looks to me as if the host send some message, but this message was not processed by my gadget and then the host timeout out and disabled usb device. Still don't know how to find out if the accessory sent anything to gadget and what it was...

USB Printing using chrome browser on Mac

I am testing chrome.usb and chrome.printerProvider api.
I am able to connect my usb printer using chrome.usb deviceinfo (sample app ) and get my device info. I am not able to actually print using the chrome.usb usb-label-printer app. This app seems to print using a label printer but it did not work when I use my USB printer. I have the vendorId, productId, endpoint correctly configured. How do I actually print?

Connecting to Pebble watch using Arduino

I am curently looking to find out wheter it is possible to Bluetooth connect an arduino or similar micro controller to the pebble watch using the RN-42 Sparkfun RN-42 adapter
I currently am having difficulty connecting to the pebble, as the board just times out.
Thanks
The RN-42 can, using bluetooth. The following will get you paired, and the watch MAY accept a ping:
SF,1 //factory reset
SP,9999 //takes care of the pairing code
SM,6 //pairing
SA,4
SY,0000 //power
SW,0640 //sniff mode
R,1 //reset so settings are saved/active and pebble should ask to connect
To pass app messages you need to pass some information to the watch, see http://dexwatch.blogspot.com/2015/12/more-on-pebble.html for the full details.

Initiate connection from Arduino to remote device

I am trying to achieve that my arduino-built device connects automatically to a device after it is switched on. The board is an Arduino Mini Pro and the bluetooth module is the Bluetooth Mate Silver (RN42). The devices are already paired, the bluetooth module is in slave mode, the remote device (in this case a normal laptop) is discoverable. I am using the SoftwareSerial library provided with the Arduino software.
Here is my setup:
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
// bluetooth
// set baud rate to 9600
bluetooth.begin(9600);
// start command mode
bluetooth.print("$$$");
// reply delay
delay(100);
// set to slave mode
bluetooth.println("SM,0");
// reply delay
delay(100);
// set device pin to 0403
bluetooth.println("SP,0403");
// reply delay
delay(100);
// connect to pixelsense
bluetooth.println("C," + remoteMAC); // remoteMAC is a string with the mac address
// reply delay
delay(2500);
// leave command mode
bluetooth.println("---");
// reply delay
delay(100);
}
I read the command set of the RN42 and I think the commands are correct. I can connect to Arduino via bluetooth but the other direction isn't working. I also tried using the master mode or storing the remote mac in the flash instead of using a string. From the documentation it should be possible to initiate connections from the RN42, but I haven't quite found out how this works in details. Can anyone provide any help to solve my issue?
EDIT: The device does not seem to enter command mode at all. At least the flash lights don't indicate that. Is there anything wrong with the commands so that the command mode cannot be accessed?
After nearly a week, I came across a solution. Actually it seems to be important that there is any program that accepts a bluetooth connection. I tried it the whole time with the Windows 7 Bluetooth dialog menu entry "Receive a file" but that did not work. So I used a third party program that listens for incoming connections and that actually worked. Basically everything I did was fine except for the missing connection listener.
Sometimes if I upload new code to the Arduino there is the problem that the connection cannot be established. This can be solved by removing and readding the bluetooth device.
The code for the RN42 Bluetooth Client above works. The library SoftwareSerial provided with the Arduino software is used.
I think you may need to put some delay after bluetooth.begin(9600), to let set its baudrate and then issue print command.
You can enter into the command mode only in 60s after the module is powered on. You can see if the timer is still counting using the status LED(if it blink 2 per second). Sorry for my bad english.
On startup the RN-42 needs 500ms before it can enter command mode try 'delay(500);' before 'bluetooth.begin(115200);' and that should get you there. Also CTS should be tied low
as can be seen on page 45 of the document found at the link below.
https://cdn.sparkfun.com/assets/1/e/e/5/d/5217b297757b7fd3748b4567.pdf
PG 45
"3.3 INTERFACING TO A MICROPROCESSOR
Roving Networks Bluetooth devices can connect to 3.3-V (only) microprocessors using
the UART interface. When interfacing with a microprocessor, use the following guidelines:
• The Bluetooth device power, ground, RX, and TX signals must be connected and
CTS should be held low or tied to RTS.
• The Bluetooth device can go into command mode 500 ms after booting.
• The microprocessor should send $$$ with no carriage return or line feed to enter
command mode.
3.3.0.1 HOW DO I KNOW THE MODULE IS READY FOR COMMAND MODE?
500 ms after rebooting, the module is ready for command mode. You send $$$with no
carriage return.
3.3.0.2 WHICH HARD SIGNALS SHOULD I CONNECT?
You should connect power, ground, RX, and TX. CTS should be low or you can connect
or tie it to RTS."

Resources