# Trigger on USB Drive Insertion - Raspberry Pi

**URL:** https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096
**Category:** General
**Created:** [13 December 2019 03:12 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096 "2019-12-13T03:12:16Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![jeff8houses](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff8houses/32/15094_2.png) [@jeff8houses](https://discourse.nodered.org/u/jeff8houses)
#### Post date: [13 December 2019 03:12 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/1 "2019-12-13T03:12:17Z")

</div>

Hey friends! I'm working on a Node-RED Pi Zero project and am looking for a way to monitor when a USB drive is plugged in. The aim is to auto-copy a bunch of files when a drive is inserted then unmount the drive. Copying and unmounting are relatively easy, but I'm having a tough time finding a way to have the OS notify Node-RED that a drive has been inserted/mounted.

The only thing I can think of right now is to have Node-RED constantly pinging the mount point to see if something is there and continue on if if finds a drive. While not resource-intensive it seems a bit wasteful and inefficient. But maybe this is the best way to go?

I'm not great at this stuff and could use a pointer or two! Thanks!

---

<div class="post-metadata">

### Author: ![droberts](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/droberts/32/40295_2.png) [@droberts](https://discourse.nodered.org/u/droberts)
#### Post date: [13 December 2019 08:21 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/2 "2019-12-13T08:21:50Z")

</div>

Maybe some kind of Python script running in the background on the PI - [GitHub](https://stackoverflow.com/questions/469243/how-can-i-listen-for-usb-device-inserted-events-in-linux-in-python) - (I haven't tried that particular script).

You can then modify it using the PiGPIO library to set one of the pins to high or low when it detects a USB event.

Node Red has `node-red-node-pi-gpio` which can then monitor the state of the pins and you can trigger an action off that.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [13 December 2019 10:02 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/3 "2019-12-13T10:02:23Z")

</div>

I think periodically checking sounds a reasonable way to tackle the issue if a drive is always mounted the same way each time

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [13 December 2019 10:23 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/4 "2019-12-13T10:23:35Z")

</div>

Or run that script using the node-red-node-daemon node - which will then provide an output of whatever the script reports which you can then use to trigger whatever is required.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [13 December 2019 12:09 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/5 "2019-12-13T12:09:56Z")

</div>

There is also a javascript possible way even though it's not perfect

1. Install this package:  
[https://www.npmjs.com/package/usb-detection](https://www.npmjs.com/package/usb-detection)

```auto
sudo npm install usb-detection

```

1. Edit your NR settings.js and add the line with the usbDetect:

```auto
    functionGlobalContext: {
	//tellduslive:require('telldus-live'),
        //noble:require('noble'),
        SunCalc:require('suncalc'),
        usbDetect:require('usb-detection'),

```

1. Restart NR

2. Import the flow below, click ONCE only on "Start usb monitoring"

```auto
[{"id":"7b3a1756.5c8518","type":"function","z":"60f50465.20a53c","name":"","func":"var usbDetect = global.get('usbDetect');\n\nif(msg.payload){\n usbDetect.startMonitoring();\n node.warn('started usb monitoring');\n \n usbDetect.on('add', function(device) {\n device.state = \"connected\";\n node.send(device);\n });\n \n usbDetect.on('remove', function(device) {\n device.state = \"removed\";\n node.send(device);\n });\n}\n\nif(!msg.payload){\n usbDetect.stopMonitoring();\n node.warn('stopped usb monitoring');\n}\n\n","outputs":1,"noerr":0,"x":1020,"y":130,"wires":[["b7c8d674.1671c8"]]},{"id":"b7c8d674.1671c8","type":"debug","z":"60f50465.20a53c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1200,"y":130,"wires":[]},{"id":"a3913d80.f3765","type":"inject","z":"60f50465.20a53c","name":"Start usb monitoring","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":780,"y":130,"wires":[["7b3a1756.5c8518"]]},{"id":"523afc6c.afbbb4","type":"inject","z":"60f50465.20a53c","name":"Stop usb monitoring","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":780,"y":230,"wires":[["7b3a1756.5c8518"]]}]

```

1. Remove & then connect a usb device and check the result in the debug window. I tested with a usb memory stick and a usb camera, I received a notification for both. In the function node I have added the property state so that you easily can identify if the device was "connected" or "removed"

PLEASE note that you can only start the monitoring once. If you change the code in the function node after the monitoring has started, you may have to reboot the Pi before those changes works. Read more here why: [https://github.com/MadLittleMods/node-usb-detection/issues/53](https://github.com/MadLittleMods/node-usb-detection/issues/53)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/58013fd6d9a024d42f56078b8b308ca1b2654f0f.png)

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [13 December 2019 13:17 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/6 "2019-12-13T13:17:11Z")

</div>

Wrapping that up looks like a great "my first contrib node" project for someone !

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [13 December 2019 17:41 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/7 "2019-12-13T17:41:58Z")

</div>

Not sure about the JS tooling. However, the "normal" way to do something on usb drive attachment is to configure udev to detect when the device comes active and then run a shell script.

Since a shell script could update a file, output to a UNIX pipe or even call an HTTP endpoint, there would seem to be lots of ways to efficiently do something useful in Node-RED.

> <https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device>

[https://www.thegeekdiary.com/how-to-run-a-script-when-usb-devices-is-attached-or-removed-using-udev/](https://www.thegeekdiary.com/how-to-run-a-script-when-usb-devices-is-attached-or-removed-using-udev/)

---

<div class="post-metadata">

### Author: ![jeff8houses](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff8houses/32/15094_2.png) [@jeff8houses](https://discourse.nodered.org/u/jeff8houses)
#### Post date: [13 December 2019 22:39 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/8 "2019-12-13T22:39:04Z")

</div>

Found a simple Node-RED solution! Thanks for all the suggestions. Here's a pretty simple "in-house" solution for this case (or any Linux implementation):

Using the [watch] object you can easily monitor the **/dev/disk/by-uuid/** directory which changes when a drive is plugged in or removed. [watch] can then trigger anything down the flow. You can watch any of the /dev/disk/ sub-directories but the bonus of /by-uuid is that the [watch] object kicks out msg.file as the uuid of the inserted drive, which can then be used to map the mount point, trigger the transfer, and then unmount the drive. Still need to make it so the flow it only executes on drive insertion but I think I can work that out.

I'm still tweaking the final result and will post the solution here when I get closer. Thanks again for all the suggestions! Cheers!

---

<div class="post-metadata">

### Author: ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)
#### Post date: [22 December 2020 22:28 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/9 "2020-12-22T22:28:11Z")

</div>

Hi @jeff8houses are you still using this flow? If so, would you share it?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [22 December 2020 22:31 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/10 "2020-12-22T22:31:01Z")

</div>

You can use UDEV rules to run a script when a USB device is inserted.

[linux - Using udev rules to run a script on USB insertion - Super User](https://superuser.com/questions/305723/using-udev-rules-to-run-a-script-on-usb-insertion)

Just get the script to notify Node-RED (e.g. using curl to trigger a Node-RED http endpoint).

---

<div class="post-metadata">

### Author: ![jeff8houses](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeff8houses/32/15094_2.png) [@jeff8houses](https://discourse.nodered.org/u/jeff8houses)
#### Post date: [22 December 2020 23:07 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/11 "2020-12-22T23:07:41Z")

</div>

I will indeed post the flow! It's on the pi (shame on me for not backing it up elsewhere) so I need to go dig it up. I'll post it here in a bit!

---

<div class="post-metadata">

### Author: ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)
#### Post date: [23 December 2020 09:28 UTC](https://discourse.nodered.org/t/trigger-on-usb-drive-insertion-raspberry-pi/19096/13 "2020-12-23T09:28:32Z")

</div>

> [@jeff8houses](#):
>
> I will indeed post the flow! It's on the pi (shame on me for not backing it up elsewhere) so I need to go dig it up. I'll post it here in a bit!

Thanks im looking forward to it 🙂
