🇺🇦 We stand with Ukraine! 🇺🇦

Webcam activates LEDs?

I recently used the Enclosure plugin to get an RGB LED strip to be controlled via the GPIO pins. So this is all well and good, and easy to add the gcode to the print files. Im curious if there is any way to have that gcode run when the webcam is brought up? Just thinking that its not necessary to have the LEDs on all the time, but when logging in to check a print, it would be handy if the activation of the webcam also could turn on the LEDs.

... possible?

You can activate the LEDs by the Enclosure plugin with the start of the print and deactivate them at the end. You also can add some time the LEDs keep on after the print.

Thanks Ewald. Ya, im aware i can insert the gcode to have the lights do whatever i want during the print, but my understanding is that they will only be run as the part of the gcode is reached. This has to deal with the possibility of having the lights triggered by the camera being activated. It could be a solution that does not even require gcode! But i dont think i saw an option in the Enclosure plugin that had anything to do with the camera.

So you need the LEDs as kind of flash light?

sure. you could say that. i could leave them on all the time... its not a big deal. i was just curious if this is possible. if octoprint knows, or can be made to know, when the camera activates.

You could use a reverse proxy in front of OctoPrint and have it make a silent call to an API endpoint to turn the lights on, have it run a script to do so, or use a log watcher to turn them on when the connection is opened and off when the connection is closed. You could also add a couple of lines of code to mjpeg-streamer (or a wrapper around it) to turn them on at stream init and off at stream close via GPIO (easy) or API endpoint (slightly more difficult, but still pretty basic). With the mjpeg_streamer approach (with or without a wrapper), you'd want to use semaphores to keep track of how many streams are active and the LED state and only change the LED state when changing to or from a 0 active stream state.

1 Like

thanks for the detailed reply taz. this sounds like its a bit over my head. iv never been one for coding, and while i understand what your suggesting, i have no idea how to go about doing it. ill stick with adding the gcode into the slicer. i can always manually type it in as well thru the terminal too.

I have 2 scripts for turn on and off the led. I can simple do it from GUI.

Here is a picture;

thats neat johnnie.. could you share those scripts along with instructions on how to get them to appear in the menu like that? that would be a pretty ideal solution.

In fact it´s not difficult to add this. I know 2 ways, but sure there are even more.

First, I´ve created the scripts at a folder inside te Pi. I´m using GPIO pins to control a relay board, but if you use GPIO pins to control your color lightstrip, I think should pretty much the same.

Created my scripts to turn on and off leds at /usr/local/bin/scripts/

You should give execute permission to pi user to run the scripts.


gpio export 4 out
gpio -g write 4 1


gpio export 4 out
gpio -g write 4 0

Added to /home/pi/.octoprint/config.yaml the following lines, after server section (showing here the printer turn on/off lines too):


  • action: pon
    command: /usr/local/bin/scripts/printer_on.sh
    name: Turn Printer On
  • action: poff
    command: /usr/local/bin/scripts/printer_off.sh
    confirm: Are you sure you want to turn off the printer?
    name: Turn Printer Off
  • action: lon
    command: /usr/local/bin/scripts/light_on.sh
    name: Turn Light On
  • action: loff
    command: /usr/local/bin/scripts/light_off.sh
    name: Turn Light Off

Quoting text above it changes the "-" for a bullet, but it´s a "-".

Just do this and restart octopi.

There is a plugin called "System Command Editor" that does pretty much the same.

I hope this helps you.

1 Like

thanks Johnnie. im going to play around with this, but i know i will have some issues, so ill just put my questions down, and maybe ill figure them out on my own before you answer...

  1. when you say "export 4", does that mean gpio 4, or pin 4? im assuming that is what these #s are referring to, so i need to know what to change it to to reflect the pins/gpios that im using.

2.at the end when you put 1, or 0 (on or off i assume), will that work with PWM leds? with the Enclosure plugin i can put down "ENC O1 S255" and that will set the RED portion of the LED strip to full power. so here, can i use 255 instead of 1?

  1. i followed your instructions best i can (for a complete linux noob). i attached a screenshot of how i edited config.yaml. Im not sure i did this correctly. when i restart, and get into octoprint, i no longer have the power button menu. you can see what i wrote here...


... and when i remove the system actions stuff i added, up to temperature, then i get the power menu back.

Add a space after “-“. I don’t know very much how other stuff works, just found digging the web how to turn on and off the lights. In the example you’ve mentioned, GPIO pin 4 can be set to 1 or 0, what in my case triggers the relay on or off.

Where do you connect your led strips? GPIO pins directly?

well, i tried changing 1 and 0 to 255 and 0. it didnt work. i also tried using the pin #, and the gpio #. again, neither worked. i got the menu items to appear with the System Command Editor, but after assigning the 'light_on.sh' script to each button, pressing the button does not activate the lights. perhaps i need different code working in the scripts.

my lights are using 3 gpio pins because i need 1 for each RGB color. those pins pass thru 1 mosfet each, and then go on to connect to the led strip. so, via the enclosure plugin, i can get it to work by sending a command to set each pin to anywhere from 1-255. i end up having to use 3 commands total.

i also did the chmod a+x for the 2 light files to give execute privledges, but still nothing. so im guessing the scripts are not set up properly for RGB, as they arent a simple 1/0 as a relay is.

do i need anything additional installed to get access to the gpio functions? enclosure plugin gives me access, but maybe i need something else installed to get those commands to work?

EDIT: i looked up a few of the many gpio led posts on google. while the scripting isnt the same as what you have, its similar, and when i follow it, nothing happens. so something is missing. again, i tried working with the pin# and the gpio#, setting values of 1, and 255, and nothing each time.

just to be clear, i went to sys/class/gpio/gpio33/ echo out direction, echo 1 value. i was doing this thru putty, not thru octoprint or enclosure or anything. putty is the only other way i know how to interface with the pi.

Are these Neopixels? Assuming yes, for a moment... Mine have three pins per section (+, -, data). Note that if there's more than one light then it's one in a "serial bus" so you'd need to address the pixel that you're trying to talk to.

The data logic level is 5V (Arduino). The Raspberry has a 3.3V data logic level so you'd need something like a logic level converter to talk to it.

not, just boring regular RGB led strips, so i just get RGB and +. after my experiment to do it thru command line, following a couple tutorials, im a bit confused as to why its not working. Im wondering if somehow enclosure plugin has locked it up somehow and is taking sole control of those gpio pins i assigned to the leds?

but also, reading thru things alot of posts were taling about wiringPi. i never installed that. i dont know if enclosure installed it for me, or if it works without it, but perhaps since i never installed it that is why i cant get it to work via command line? though, i did read 1 tutorial that said nothing additional was required... just direct commands... so.....?

Can you link a tutorial like this here? maybe I could help you better this way.

sure, iv looked at several now, but here are a few...

since you started with bash, i tried...

this, which is basically the same thing, and ends with a very similar script to what you posted above, but its dependant on another script...

tried a python version, setting Command plugin to look up light_on.py, but then i get a permission error...

another very similar to yours, which states i need wiringPi, which i dont think i have unless its included with the octopi image files...

so ya, nothing is working, and there are too many variables for me to figure out why. bash, python, wiringPi, uppercase, lowercase, permissions... kinda frustrating. so, any light you can shine on this would be greatly appreciated. thanks!

Setting GPIO pins (and maybe even reading them) requires root access, btw. Prepend with sudo and it will be happier.

Not sure which one of us is confused here but echo and cat are included with Raspbian. The greater-than symbol is basic command line redirection. Don't forget the details: The following commands should be run as root (type 'sudo bash' to become root).

thanks guru, i did find out about the root access as i read more, so i tried running it with sudo, but i still get the permission error. im trying to run the command via the System Command Editor plugin. I dont know if this has anythign to do with the issue? So from the octorpint power menu, iv added a "Light On" button via this plugin, and the plugin was set to run the command...

sudo /usr/local/bin/scripts/light_on.py

or as Johnnie had initally said "light_on.sh", minus the sudo, and written as he had outlined above.

Im only 3 or 4 weeks into octoprint, rpi, linux, etc... so this is all new to me.

Okay. Next, the script itself has to be marked so that it can be executed.

  1. root needs to run GPIO commands
  2. the script needs to be marked so that it is executable
  3. if this weren't a root-related thing, we would also need to check the rights of the file for the user who would be running it
  4. most shells by default prevent you from running a script in the same directory so you have to include the path, so assuming that you're in the same folder then you'd prepend ./light_on.py for the "this folder" path
chmod a+x /usr/local/bin/scripts/light_on.py     # Add the executable right
which light_on.py                                # Make sure that it's in your path
light_on.py                                      # You would think this would work

To the best of my knowledge, though, /usr/local/bin is in your PATH but not that scripts subfolder. So give it the full path to your Python script.