Page 1 of 1

Looking for a small hardware device to trigger a python script on my RPi

Posted: Sun Mar 24, 2019 8:57 pm
by sapnho
I would like to have a little button to trigger a python script on a nearby device (bluetooth range). Initially, I was thinking of the Amazon Dash Button but Amazon has stopped selling them.

Would anyone have an idea of what simple battery powered device I could use?

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Sun Mar 24, 2019 9:39 pm
by rin67630
Maybe a bluetooth remote for selfies?

https://www.ebay.de/itm/Selfie-Remote-B ... 680397966

It seems to be easy to interface, it should act as a keyboard:

https://www.childs.be/blog/post/bluetoo ... on-mapping

Viel Glück, mein Freund!
Laszlo

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Sun Mar 24, 2019 9:42 pm
by mattmiller
The amazon dash technique is just a program running that detects a device joining a LAN- it just waits for a specific MAC address to appear.

You can use something like a WEMOS D1 mini and use its reset button to make it disconnect and reconnect to your LAN and then detect it rejoining

Alternatively, you could add a little bit of electronics so that the WEMOS is off normally and pressing a button starts it up - stays on a for a few seconds - and then switch itself off again

Code for python detector (has stuff in to send MQTT message to a broker)

Code: Select all

from scapy.all import *
import paho.mqtt.client as mqtt
import time

def arp_detect(pkt):
    if pkt[ARP].op == 1: #network request
        #if pkt[ARP].hwsrc == 'b4:7c:9c:f9:1b:8e':#dash button
        #if pkt[ARP].hwsrc == '48:5a:3f:1d:79:0d': #samsung droid phone
        if pkt[ARP].hwsrc == 'dc:4f:22:1c:90:ba':#wemos 
            publish("dash","pressed")
            print time.strftime("%b %d %Y %H:%M:%S")
            return "Button detected!"
            
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))


def publish(topic,payload):
    client.publish(topic,payload)
    print("sending: " +topic + " " + str(payload))


client = mqtt.Client()
client.on_connect = on_connect
client.connect("192.168.0.23")
client.loop_start()


try:
    print sniff(prn=arp_detect, filter="arp", store=0)
except KeyboardInterrupt:
    print
    pass

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Mon Mar 25, 2019 8:42 am
by sapnho
Thanks, Laszlo and Matt! I am looking for an off-the-shelf hardware button so I may give the Bluetooth remote a try first, I guess. I wonder if there may be a ready-made button product for the WEMOS D1 as well?

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Mon Mar 25, 2019 12:49 pm
by rin67630
sapnho wrote:
Mon Mar 25, 2019 8:42 am
Thanks, Laszlo and Matt! I am looking for an off-the-shelf hardware button so I may give the Bluetooth remote a try first, I guess. I wonder if there may be a ready-made button product for the WEMOS D1 as well?
The Wemos is an astonishing powerful device, for your need it seems overkill, albeit they are very cheap. But you need to solder and get into the guts, especially if you want to run months on battery. I don't know about ready made cases for them.

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Mon Mar 25, 2019 1:53 pm
by scruss
To be honest, this is going to be quite a bit of work — unless there's a magic "launch a command on an event" tool I don't know about.

Via GPIO, you could use something like a Simple RF receiver along with a keyfob remote. These give you four different buttons. You'll need to connect the receiver via a logic level converter: they're 5 V devices. One could write a simple systemd service that ran a program to watch GPIO pins and launch something when the pin goes high. I've used one of these for a commercial installation: cheap, reliable, but no security. Someone in the same room with the same type of fob can trigger your device.

Another option is a BT selfie button. I dug out a selfie stick I was given, turned it on, associated it through the desktop BT menu, then saw the following interesting events in xev:

Code: Select all

KeyRelease event, serial 48, synthetic NO, window 0x2000001,
    root 0x25d, subw 0x0, time 331326881, (104,95), root:(950,163),
    state 0x0, keycode 123 (keysym 0x1008ff13, XF86AudioRaiseVolume), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyPress event, serial 48, synthetic NO, window 0x2000001,
    root 0x25d, subw 0x0, time 331334771, (104,95), root:(950,163),
    state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES,
"   XLookupString gives 1 bytes: (0d) "
"   XmbLookupString gives 1 bytes: (0d) "
    XFilterEvent returns: False

KeyRelease event, serial 48, synthetic NO, window 0x2000001,
    root 0x25d, subw 0x0, time 331334775, (104,95), root:(950,163),
    state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES,
"   XLookupString gives 1 bytes: (0d) "
    XFilterEvent returns: False
So it seems to be sending two (three for the pedantic) keyboard events: a Return key press and release followed by a Raise Volume key release. I'm not sure if keyboard shortcuts/launchers would work for what you need.

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Mon Mar 25, 2019 2:06 pm
by sapnho
Thanks much! I have ordered a BT camera remote button and will report on my progress! 8-)

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Mon Mar 25, 2019 3:12 pm
by PhatFil
for an off the shelf solution with minimal h/w interference and simple straight forward configuration, i use 433mhz alarm/doorbell buttons of which there are lots to choose from https://www.aliexpress.com/wholesale?ca ... mhz+button to listen for all 433mhz signals it can decode i use a sonoff rf bridge reflashed with tasmota that will echo any signals it can decode as a wifi mqtt topic/payload.

node red sits alongside mosquitto mqtt broker on my pi-0-w and subscribes to the bridge/result topic and on acts on any known codes in a very simple flow.

once set up adding new buttons etc is simple, open the bridge web interface and navigate to its console window, press/activate your 433mhz device and its code will appear in the console, I then go to node-red with the code and add a new switch condition based on that code to direct flow to a node to fire up whatever is relevant.

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Mon Mar 25, 2019 9:27 pm
by sapnho
A doorbell is certainly a nice idea hardware-wise, but I am looking for a rather simple solution which does not require any additional hardware tinkering on the RPi.

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Tue Mar 26, 2019 6:00 am
by ghp
Found a nice solution using esp8266. https://community.openhab.org/t/esp8266 ... tton/40770

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Tue Mar 26, 2019 6:54 am
by sapnho
Yes, that looks nice but I don't have a 3D printer! :?

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Tue Mar 26, 2019 8:20 am
by Heater
This might be a little bit pricey but what about an Espruino Puck.js
https://shop.espruino.com/puckjs

It's a really neat, nice feeling Smart Bluetooth button, with LEDs built, runs off a coin cell for ages. It has a ton of capability that you might like to play with. Programming it is drop dead simple:

Loads of simple getting started turorials/examples here:
http://www.espruino.com/Puck.js#tutorials

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Tue Mar 26, 2019 9:25 am
by sapnho
Yes, a little pricey indeed. I am looking for something which is widely available and costs less than €10. :roll:

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Thu Apr 04, 2019 8:34 am
by martinohanlon
Have a look at BlueDot, its a Bluetooth remote and Python library.

https://bluedot.readthedocs.io/en/latest/index.html

You can use an android phone or another Raspberry Pi as the remote.

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Thu Apr 04, 2019 10:55 am
by sapnho
Also an interesting approach, thanks!

Re: Looking for a small hardware device to trigger a python script on my RPi

Posted: Fri Apr 05, 2019 12:46 am
by PhatFil
i still like the 433mhz botton devices, they come in 1x button to 10x buttons and more.. and have a greater range than most bluetooth devices, and all you need for wifi/mqtt translation of the codes is a £10 usb device flashed with some free firmware.. once set up adding a new code from a button or alarm device like pir or door/window sensor takes a few minutes
https://github.com/arendst/Sonoff-Tasmo ... Bridge-433

your python script needs only sit and respond to the relevant mqtt publications..
i use node red for all that tho