WebUSB / Headphone connection to Espruino #5801
Replies: 1 comment
-
Posted at 2017-07-21 by @gfwilliams There's been some talk about it, but it's quite a lot of work. My reasoning for not attempting it is:
So for me, it's not looking good. Urish has made a web bluetooth polyfill for Chrome on windows which uses a plugin API (https://github.com/urish/web-bluetooth-polyfill). IMO I'd be much better off making my own plugin for each major platform that handled serial, since the plugin API seems to work on a few different browsers. It'd stop me getting in this stupid situation where I'm dependent on one browser maker. Posted at 2017-07-21 by dave_irvine Bummer didn't realise it would be so difficult I assumed it was basically just setting https://wicg.github.io/webusb/#webusb-platform-capability-descriptor and away we go. My use case was not to use it for flashing code to the board but rather being able to connect to the Espruino though a web-ui in Chrome and be able to update files on the SD card. This would let me have someone configure the Espruino WiFi with their WiFi credentials via a web-app and then send this info into the Espruino. Posted at 2017-07-21 by @gfwilliams Sadly I don't think it's that easy - if the operating system has recognised the device as a serial port then it'll 'claim' it and create a serial port device with it. Once that's happened, I don't think Chrome will be able to get access to it, even if it thinks it's a Web USB device... So you can have one, or the other. I could create a separate set of USB endpoints just for chrome, but it starts to get really scary at that point. I could be wrong about that, but that's the distinct impression I got when I looked into it. If I created a bunch of plugins for different platforms and a single library you could use to get serial access, that might even work better for your use case though? Posted at 2017-07-21 by dave_irvine Unfortunately I don't think I could get away with people installing plugins, it was something that would have to work with absolutely zero effort. I have a final idea involving the web audio api and the ADC's on the Espruino but its such a bad idea... Posted at 2017-07-22 by @gfwilliams It's a really annoying situation. Electric Imp devices used to have a light sensor, and would listen to a certain pattern of flashes on the screen of whatever device was trying to communicate with them. That could work out pretty well for small amounts of data, and could then be used straight from any device, without setup? Posted at 2017-07-24 by dave_irvine Well, whaddaya know, you've already done what I was gonna do: http://www.espruino.com/Headphone this will work fine :D Posted at 2017-07-24 by @gfwilliams Yep - the only gotcha is that some phones seem to have the audio wired backwards, but it's not a big deal. Just do it once one way, and again the other... Or if you're doing two-way comms you can just check and see which option works. Posted at 2017-07-31 by dave_irvine @gfwilliams just realised I can't really use audio-to-serial directly as my WiFi pins for USART are taken up with something else. I see there is code for going from audio-to-serial https://espruino.github.io/EspruinoOrion/serial_to_audio.js, do you have anything for using the ADC pins and Posted at 2017-07-31 by @gfwilliams You should be able to use normal digital pins and If you did want to use software serial there's some code for it here though: espruino/Espruino#549 (comment) Posted at 2017-08-01 by dave_irvine @gfwilliams hmm, I've tried setting the pinMode but I'm not getting any response out of setWatch. I've used various tone generators but nothing ever seems to trigger.
I'm relatively sure my circuit is right, can you think of anything I might be missing? Attachments: Posted at 2017-08-01 by @gfwilliams Maybe you could check the voltage of RX (A5) relative to GND (in fact you might just be able to use analogRead) - it should be somewhere around 1.6v (or 0.5 on analogRead). It might also be worth giving a single-direction one a go first? It might be your phone/PC that's confused about the type of device that's plugged in - and using a standard 3 wire headphone jack would remove that possibility I guess. Posted at 2017-08-01 by dave_irvine I got the guys in the shop to look at it with an oscilloscope, there is definitely voltage on the line, with about 1.5v swing.
Attachments: Posted at 2017-08-01 by @gfwilliams The voltage swing is good, but as far as I can tell from that trace (I'm not 100% sure where 0v is on the scope) the 'resting' voltage is 0v, so it's oscillating between -0.7 and 0.7v... That sort of implies that the pullup on the pin isn't working for some reason? Ideally it should sit at around 1.6v, so would go between 0.9v and 2.3v depending on the signal. Posted at 2017-08-01 by dave_irvine Well I guess when I measured it on the scope it wasn't connected to the Espruino so the internal voltage pullup wouldn't have been in effect. I'm guessing on A5 I would need to call Posted at 2017-08-01 by @gfwilliams Ahh, yes. You'd need it connected to the Espruino, and with the pullup. That's the important time to measure the voltage :) Posted at 2017-08-01 by dave_irvine I'll try and do some more tests tomorrow Posted at 2017-08-02 by dave_irvine Ok so here our resting voltage is 0v, and with the pullup set, we're getting 1v - 2.5v. Espruino says:
when using I've gotta be doing something stupid here... Attachments: Posted at 2017-08-02 by dave_irvine If I plug A5 into Espruino 3.3v output, I get the following:
If I then set the pinmode...
Posted at 2017-08-02 by dave_irvine Ok if I use Posted at 2017-08-02 by dave_irvine Ahhh the secret is a square wave not a sine wave. Posted at 2017-08-02 by @gfwilliams Thinking about it, I've just looked up the IO pin characteristics again, and I get the attached. It looks like you're just on the edge of the acceptable input voltage for a 0 reading at 1v. Please could you try using a lower value resistor for the pull-down? You've got 47k at the moment, but something like a 33k or 39k resistor might just get the voltage down enough that a '0' was registered. It's odd because it had worked for me, but there's some variance in all the components and I guess this time it was just too close to work... If it works I'll update the resistor values in the docs. When you get that done, just try something simple like:
Attachments: Posted at 2017-08-02 by dave_irvine @gfwilliams I'm getting fairly reliable readings now. I guess my next question is how do I generate sounds of varying length. Out of my depth here I think. I thought that this would generate 0.5seconds of...something, and then 0.5 seconds of silence, but I just get a click
Posted at 2017-08-02 by @gfwilliams I think the click comes from the transition from 1 to -1 (and maybe at the beginning and end too), but basically that's all you need to do - just outputting -1 usually with bursts of 1 of varying lengths. Doing something like Posted at 2017-08-03 by dave_irvine So after a bit of fiddling, I've come up with the following that seems to work. I'm sure this has been done before and in a better and more reliable way, but this is what I have. PC Sound Generator:
Espruino Receiver:
So now my question @gfwilliams, is how do I reverse this? i.e generate sound on Espruino. I'm guessing I want to just be doing digitalWrite to the pin I have wired to my headphone socket, can I reuse any of the code I have or should I start again? Posted at 2017-08-03 by @gfwilliams Great - glad it's going! To send from Espruino I'd just use To read it back you could take a look at the little oscilloscope page I made: https://github.com/espruino/webaudio-oscilloscope It basically just detects the large changes in the signal caused by the transition from 0 to 1 and vice versa. Currently you're using buffers - so you might be able to do something similar I guess (the oscilloscope reads continuously) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2017-07-21 by dave_irvine
Well it seems like it wouldn't be a day of the week if I wasn't requesting some crazy feature for Espruino, but I'm wondering @gfwilliams if you would consider adding support for WebUSB? Arduino implementation:
https://github.com/webusb/arduino
Beta Was this translation helpful? Give feedback.
All reactions