Triggering an action on a Pi when the button is pressed #2204
Replies: 14 comments
-
Posted at 2017-01-07 by @joakim I'm trying to figure out the same thing. See this comment for my thoughts on how it might be solved. I'm sure Gordon will be here soon with a helpful answer, as always :) If you're comfortable with Node, you might try using noble and Espruino's UART service over BLE like I did (node code, espruino code). Though, as Gordon pointed out, a continuous BLE connection uses much more power, so I'm looking for a better solution. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-01-07 by knolleary @joakim thanks for that. A couple questions... where is the I've got the node.js code working to the extent it connects to my puck, gets the appropriate characteristic and waits for data to be sent over. However nothing ever arrives from the puck. I've confirmed the puck side is working as, in the web ide, when I press the button, the |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-01-07 by knolleary @joakim finally found the mistake I'd made in the node.js code... missed off the subscribe call on the rx characteristic. All working now. Next task is, as you've said, figure out if there's a more energy efficient way of doing this. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-01-09 by @gfwilliams
Strange. Personally I haven't had it working, but I have definitely done it on Desktop linux so I wonder what's up. Could it be that something like EspruinoHub is effectively 'claiming' the BLE interface and breaking things? IIRC for Bleno to work you basically have to disable bluetoothd, and I guess it's bluetoothd that handles all the HID stuff.
Hmm. Looks like it isn't. I'll fix that, but yes - it's basically just a 'Serial' object Glad you got it working with Node.js in the end. I will eventually add something that'll get lower power consumption on the Puck when it's connected. Personally I'd say to use |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-01-09 by knolleary Not sure about the HID stuff - wasn't running EspruinoHub at the time. Have moved over to
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-01-09 by @gfwilliams Great, thanks! And what are you using on Puck.js for that? Just something like:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-01-09 by knolleary Pretty much, yes. I set it to 10, 20 or 30 depending on whether I single, double or triple tap the button, resetting it back to 0 after 3 seconds. Need to tidy up the code as it is littered with previous attempts and experiments, but will share it later on. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-07 by joshfarrant @knolleary Did you ever make any code publicly available? Got the basics working in the same way as your example above, but would be really useful to see some more complete code if you've got it. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-07 by knolleary @joshfarrant here you go: https://github.com/knolleary/puck-remote |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-07 by joshfarrant Thanks, @knolleary, that's really useful! One question - Did you see somewhat erratic times between the Not ideal when I'm trying to use the Puck.js as a light switch! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-08 by @gfwilliams How far away is your Puck.js device? It could be the signal strength is low enough that you're right on the edge of what can be reliably received. You can fix that to some extent by using Also, in other posts I've suggested advertising a value that is the number of button presses - so even if you miss an advertising packet you still know the button has been pressed at some point. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-08 by joshfarrant Currently the puck is right next to the MacBook, maybe 15 cm away. When I come to actually use it, however, it will be on top of a table with a Raspberry Pi 3 underneath the table, maybe 40cm away. I'll have a look at the If it's of any use at all, here's a gist of the code. Regarding the button presses, I'm currently doing the same sort of thing as knolleary suggested, which is to advertise 0, increment to 10 on first push, 20 on second etc, then reset to 0 after a few seconds. Maybe I will try having a basic counter that increments the number by 1 on each press, then just look for increases in my node script. At least I can guarantee that I'll notice the button press, no matter how long it takes. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-08 by @gfwilliams Ahh great, I didn't realise that's basically what knolleary was doing already :) Have a look at http://www.espruino.com/Reference#l_NRF_setAdvertising Basically all you need to do is change It's actually possible that your Mac's WiFi is causing slightly intermittent reception - could you have been downloading anything in the background? WiFi and BLE usually use the same aerial I think, so when WiFi is sending data it's unlikely BLE will be able to receive anything. If you're on a Pi 3 with Ethernet (or at least very rare WiFi comms) the situation will be miles better. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2017-05-08 by joshfarrant Great, thanks, I'll try that this evening. I wasn't knowingly downloading anything, but of course the OS could have been doing something in the background. I've got a Pi 3 coming in the next few days, so we'll see how it goes with that too. Massive thanks for your help, both! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2017-01-07 by knolleary
Hi,
I'm trying to figure out the most effective way to trigger an action on a Raspberry Pi when the button is pressed on a puck. It feels like an obvious thing to do, but I couldn't find an example in the forum or the tutorials.
My first attempt involved turning the puck into a HID keyboard and capture the corresponding key event on the pi.
I used the first example code from here: https://www.espruino.com/Puck.js+Keyboard, verified it worked when connected as a HID keyboard to my Mac. That worked. But then I couldn't get it to pair as a HID keyboard with the Pi. Following various guides, I've got to the point where
bluetoothctl
reports the devices as follows:You can see it is both connected and paired and that it has a HID UUID - but I see no new keyboard events when I hit the button.
Question #1 - has anyone get the HID Keyboard stuff to work with a Pi (in this instance, a Pi3 and its onboard bluetooth).
I've also tried EspruinoHub and doing something hacky around NRF.setServices() and overloading the 'temperature' value, but in some quick experiments I couldn't get that to work - the value would never change.
Question #2 - Is there a better approach I should be using going down the EspruinoHub route?
Beta Was this translation helpful? Give feedback.
All reactions