Loading
Jeffeb3

Relay Control OctoPrint Wanhao i3

by Jeffeb3 Mar 19, 2016
Download All Files

Thing Apps Enabled

Please Login to Comment

Thanks a lot. Thanks to this post I could add an emergency switch, followed by a tasmota to turn on/off my octopi. From this point and with relays I can control the printer and the lights.
All my requirements are met !

Thanks !

Will it work in the latest version?
Correction location is different item

What changed? Which version do you have?

Do not know where to add the location

sudo nano ~/.octoprint/config.yaml

system:
actions:

  • action: pon
    command: printer_on.sh
    name: PrinterOn
  • action: poff
    command: printer_off.sh
    confirm: Are you sure you want to turn off the printer?
    name: PrinterOff
  • action: lon
    command: light_on.sh
    name: LightOn
  • action: loff
    command: light_off.sh
    name: LightOff

I have octoprint 1.3.10 and it's working fine for me

thank you
We succeeded in creating the end button
I faced other errors
/usr/local/bin/ I created a file in a folder
Where should I check? ^^

so I've got everything done and working I can ssh in and turn on and off both the light and printer. my problem is I can get the config for octoprint system menu button to work every time I type the code in it bricks octoprint and I can't get the plugin system command to work either as I can't find the right file path I'm kind of a noob to programming but I'm a quick learner

got it I also used the enclosure plugin thanks for the replay.
great job on this by the way.

Do you see anything suspicious in the octoprint log?

I was reading some of the comments before I did my wiring.
As some has mentioned, the are a couple of posts on how to connect the relay and your RPI 3, all with different ways.
I ended up following Jeffeb3's directions and they work perfectly, no need for resistors, just the 5v and Ground and then which ever GPIO pins your require.
I did not however used the scripts as what Jeffeb3 did. I used the OctoPrint-Enclosure plug-in which has an array of GPIO functions
https://plugins.octoprint.org/plugins/enclosure/

do this work with octoremote also?

I think it does. It should end up in the "hambuger menu" in the left pane.

How I have to do to write the code? Any tutorial? I´m beginner in programing. Thank you

It's just text files. Search google for tutorials on editing text files.

I did but now I have the problem that when I want to turn on the printer it doesn’t work. I revised the code and everything was good, even the wiring.

Hi,

In terms of the relay that you were using, were you able to control the power for the 3d printer and led separately?

Thank you for the feedback. I ordered the necessary parts. Will you be able to give me more info once I get all my parts in terms of wiring

At some point, you need to be responsible for understanding what is happening enough to wire it yourself. Mains electricity is dangerous and I can't be there to watch out for mistakes. My solution is to not offer any wiring advice, so you have to be responsible enough to do it right yourself.

Ok. I understand

Hi,

This is an awesome project. I came across similar relays on aliexpress (https://www.aliexpress.com/item/free-of-charge4-road-5-v-10A-optical-coupling-isolation-relay-module/32304609560.html). Does the brand matter? also will I need to get the 3.3v converter as well?

The brand doesn't matter. This one looks fine. I would try it out without a conversion at first. Since 3.3V > 2.5V, they often toggle with 3.3V.

Tha k you for the update. In terms of the led, what kind of voltage do I need to look for I.e 5v, 12v etc

If you use the relay, then it's anyrhing that the relay can switch. It also needs it's own power supply.

Based on the relay link I sent you, what voltage should I use for the led light?

Pretty much anything. The top of the relay says 10A 125VAC 10A 28VDC. I'd probably find a 12VDC, because the power supplies are common.

Please provide some more detailed instructions... it's just like I did this and that but you don't really explain how you did it (at least not for me)

I'm afraid this content doesn't lend itself to step by step instructions. It's not possible to just do each part without thinking about it because there is a lot of variance in octoprint set ups, relay boards, and raspberry pis.

Is there a particular part that you don't understand?

first of all, thanks for replying (so fast) :D
The part I don't understand is everything about the GPIO Scripts part.
So could you maybe help me with that?
Thanks in advance :)

Comments deleted.

Thank you for this information. I am trying to get this to work but am running into a weird problem. If I turn the port on the relay does turn on. But after that it will not turn off. It stays on and I can no longer control it. If i power cycle the Pi it goes off until I turn it on again. Any suggestions.

The pi is has 3.3V outputs. So the output should be switching between being pulled down to ground and pulled up to 3.3V. If the relay board is powered by 5V, that should be fine, because 3.3V is plenty over the 2.5V that most 5V systems use as their high threshold.

I'm just guessing here, but one of a few things might be happening.

1) Try powering the relay board with 3.3V. Some relay boards are more sensitive when powered with 3.3V.
2) Are your scripts keeping the pin as an output? If you were switching from output to disabled or input, then it would float, and possibly keep whatever state it was last time. Can you test your pin with a multimeter to see what voltage it is (relative to the pi's ground) when on and off? Are your scripts exactly like mine?
3) Did you connect the grounds between the pi and the relay board? They need the same reference.

If that doesn't work, feel free to post a pic. Maybe I'll see something, or maybe we'll be blessed with the presence of someone else that can spot the problem.

Greetings,

I'm trying to add a delay to the power_off script and also include shutdown of the usb port(to disable the display) by adding the following to the power_off.sh file. While it works fine from the command line, Octoprint throws errors when I try to run it from the UI. The script is shown below. Appreciate your feedback

gpio export 23 out
(sleep 150 && gpio -g write 23 1)&
sudo ./hub-ctrl -h 0 -P 2 -p 0

"octopi comes with the gpio stuff installed, so just running "gpio" on the command line gives me several options. I made 4 scripts to control the relays. They were all in "/usr/local/bin/". I had to make them as root, and I had to add permissions "sudo chmod a+rx .sh" so that the pi user could execute them."- ido not get this part i just add scritps from where to how? like where do i type it in?

Sorry, This isn't a detailed step-by-step. Maybe look into using the raspberry pi and the Linux terminal before trying this type of tutorial.

How did you make the scripts?
like sudo nano printer_on.sh ? or sudo nano /usr/local/bin/printer_on.sh ?
You didn't say how.

And would this work?

!/bin/bash

gpio export 4 out
gpio -g write 4 0
(sleep 60 && gpio -g write 4 1)&

so go on (fan) and go off after 60 sec.
Thank you

nano is just a text editor. You just write the files and save them.

https://wiki.gentoo.org/wiki/Nano/Basics_Guide

Instead of running a cable out of my power supply, I'm just going to get a short extension cord from my local store, and splice the relay into it. That way it's also re-usable for other things if I ever change it in the future.

Wall Outlet -> Extension Cord (part 1) -> Sainsmart relay -> Extension Cord (part 2) -> 3d printer power cable -> 3d printer

Sure. That works. You can also buy something like that: http://a.co/aR67D7C

Let me just say that your simple example opened up a whole new understanding of the inner workings of OctoPrint. I am new to neither electronics, software nor Raspberry Pi but the OctoPrint documentation had me going in circles. In 15 minutes I soldered up a little 2 LED board to plug into my OctoPi (after modding the OctoPrint case I mashed up) and verified all of this. Now I am going to add solid state relays to the PSU and LED driver and get on with life. Thanks!

Hi,
How can I turn off the printer as soon as I finish printing?

did you by any chance figure out how to turn the printer off after print completed?

Hello, I have this same SainSmart 4-channel 5v relay that you have. Here's my issue, I would REALLY appreciate your input (or anyone's in the know, I suppose). So there are many sources claiming that with this exact relay board, to control it using the Rpi, you need to solder 1kohm resistors over the existing ones in order for the 3.3v of the Rpi to control the 5v relays. However, there are clearly others who don't seem to have done this and are controlling it just fine. Did you need to do this? Do I need to do this? I'm trying to do exactly what you've done, I just want to be able to remotely power on and off my printer, and a light. Though my Octopi setup is powered through my 3D printer's PSU via a 12v to 5v step down converter, so I will need the main power supply to remain on at all times.

I'm not new to electronics but I am new to this whole Rpi thing, and connecting it to other stuff as well, lol, so it's a lot to take in and it doesn't help when there's sort of conflicting information out there. I just want to make sure I hook it up correctly, I literally have the 1kohm resistors and the relay board sitting in front of me and I can solder them on right now if I have to, but if I don't have to I'd obviously rather not. I hope you see this, thanks for your time. (sorry for the length, brevity is NOT my strong suit)

In case you're wondering where I'm seeing this information, here it is. The second link I just found while searching for a picture of the mod, because the image link is broken on the Github page.

https://goo.gl/gYHK4i
https://goo.gl/rjj4VB

That's good info. In the second one, he's more interested in flipping the logic, so that driving the board to 0V shuts off the relay, instead of energizing it. I don't see a good reason for that. The schematic they made (still talking about the second one) shows the LED driven by the pin, when dragged down, and the LED would presumably go off when the input is raised high enough that 5V - 3.3V isn't enough to drive the LED. That would actually explain a lot.

I think the advice I gave you still holds. If it doesn't work with 5V going to the relay board, then I bet it will with 3.3V. The LED will either light up with a small amount of voltage difference (1.7V, including the resistor voltage drop, but I doubt it), and if it's that sensitive, then hooking the relay board's power to 3.3V will make it trip consistently. The relay won't have as much voltage then, but I think it will still work.

I didn't have to change any resistors on the relay board. The board is supposedly 5V. If the pi outputs 3.3V, that is usually enough to be considered HIGH on 5V systems. If it doesn't work, you can try wiring in the resistors, or even try powering the relay from 3.3V, which might make the triggering voltage a bit lower.

It's definitely possible that they've changed the board (mine is several years old now) and they have a hefty pull-down installed (which would make the 3.3V effectively smaller, which might not flip). There's no harm in trying it without the resistor though.

Thank you very much I didn't see your reply at first. That's a good point, I'll just try it out as is and see how it goes.

Comments deleted.

how would the wire go if i just use SSR? only 2 input wire is needed for 1 SSR

The SSRs I've seen work the same as the mechanical relays. Power, ground, and signal.

There are only 2 ports, that is what confusion me the most

Do you have a link?

Looks like it. It's spec'ed at 5V (all the ones I've seen are too). The raspberry pi is 3.3V, so you might try powering the relay board from 3.3V or 5V and see if that makes it work better.

Got it. Thanks!

this is not working from last update recently i made in october 2017 of octoprint server. Anyone facing such issues ? i am not able to get menu from system.

Uh oh. I haven't updated in a long time, so I can't say. What version number are you on? Maybe I'll bite the bullet and upgrade to see what's up. Also, make sure the update didn't mess with your config file. IIRC, one of the past updates added another system menu entry, so it ignored mine.

Installed: 1.3.5 Version. Can anyone tell me in which file i should paste system entry ? to get custom system menu ?

/home/pi/.octoprint/config.yaml but i don't think its making any changes in system menu. Please can anyone help ? with 1.3.5 version to use this feature ?

I am able to toggle lights using putty command line.

Can you attach your config file here or paste a link to it uploaded on drive or something?

It will only respond to one "system:" tag, so if there are two, you might be editing the one it's ignoring.

There's also a log file where it will post errors that it has in parsing your file. If there's an error, it might be failing, and posting the notice in the log, but otherwise working.

Please find attached config.yaml file its original file after update. if i write anything instead of system: {} octoprint fails to start.

Can you please edit the file and give me ?

also another attached file if you need.

That's not really how this works. I'm happy to help when I can, but I'm not going to write it for you. You'll get a lot more help if you learn, and do the work yourself.

Those files look fine (from a yaml/syntax perspective). Is there anything interesting in the log files that indicates what the problem is?

Thanks for help, i don't mean to write for me, actually i tried my way and was working till last update so thought lets see where i am making mistake. Never mind.. i have attached log file if anything is useful.

Thank you very much for this awesome instructable Jeff. I have set up my system and it is working fantastic. I have even expanded on the relay and code to include a light for the extruder also. This helped me so much!! Thank you again!! God Bless!!

Thanks for the kind words. Glad you're having fun.

When you talk about using pins 18 and 23 are you talking about the GPIO pins or the pin numbers? I assume the 5volts in supplies the power to the led strip but I see 3 connectors on each relay. How do I know which 2 to use and which is + and -? I'm using the same relay you suggested.

Thanks

GPIO pins:

http://raspi.tv/wp-content/uploads/2014/07/Raspberry-Pi-GPIO-pinouts.png

So in this diagram, it's pins 12/16, or GPIO18/GPIO23.

They are just doing basic output, so you can pick from any of the GPIOs. Make sure your scripts match what you wired it to though.

Awesome! Can you also explain how to wire in the relay to just connect the power and a 5volt led strip? My first time using a relay and all the great info you've posted is a bit overwelming when I just want to power just a 5 volt led strip. Any help you can provide would be greatly appreciated. I bought the relay you listed and with all the research I've done I still can't figure it out. Thanks so much!

Comments deleted.

Hey there!

With the help of Google and this Thing, I've made up this mod to my printer since the very firsts days. However it would be great if the printer could shutdown automatically after five minutes except when I start a new print.

In theory a simple block like "while printer is not in Printing state, count five minutes, then shutdown" should do the trick. I don't really know how to do this practically with OctoPrint only. Any clue?

What google links did you use? I'm struggling with the wiring.

I would ask in the octoprint forums. I doubt there is a basic event, but I would guess there is a way to it if you wrote your own plugin.

Hi! :)

I figured out yesterday and made it a small tutorial:
https://www.thingiverse.com/thing:2478522

Regards,
Chris.

Automatically shut down a printer after a while if idle

Thank you for this writeup!

You're welcome.

Hi, very useful guide, but i'm no expert, can i also use a 2 channel relay?

Hi, I hope someone can help me, everything works fine but my printer still ON with the USB cable... Is there a way to avoid this ?

You can modify the USB cable, to cut the 5V line.

So cut the RED WIRE 5V will be enough ? I don't have a spare USB cable so I would like to be sure before to avoid a boo-boo...

Well, if I made a cable, I'd use the red for 5V, but who knows what they used. If you can't live without the USB, then wait until you have a backup. You could also cut into an extension cable instead.

Hi, finally it doesn't work... Relay OFF, so printer OFF, I plug the USB with some (lot) tape on 5V PIN and the printer turn ON with LCD ON and FAN ON...
I don't understand... Someone can help me please ?

I doubt you can block the 5V pin with tape. It needs to have the wire cut. Go but a cheap USB cable to hack up

Hi, I did it and things are so weird...

  • I cut the 5v then nothing happen
  • I cut the GND then LED ring almost OFF, almost because 2 leds still ON and printer screen still ON

And I have to say if I remove USB then LED and printer screen are both OFF... So I think it's my mother board who is weird and if I continue to play like this it will need a new RPI...
No choice I will connect my RPI Octoprint on a relay manage by my RPI smarthome...
If you have more idea tell me

I am getting an error: Command failed with return code 127:
STDOUT:
STDERR: /bin/sh: 1: printer_on.sh: not found. I seen someone else had this but it was due to a name issue, this is not.
The name is right, and the file is placed in /usr/local/bin. I even gave it 777 perms and I still get this error. I am on latest version OctoPi...maybe its not looking in /usr/local/bin?
Anyone lend a hand?

Linux uses the PATH environment to determine where to look for files. Linux is also case sensitive, so make sure it's exactly the same name as it's looking for.

You can also define the path absolutely in the config file, like /usr/local/bin/printer_on.sh

Yes I checked PATH and /usr/local/bin in there, and the file name is correct and spelled proper case, just never executes. It will execute if manually ran from a terminal, but from within OctoPrint UI, I always get this error.
I do have that Safety User Authentication enabled on the PI so people cant log in and mess, perhaps the user logged in doesnt have permissions?
I tried to log into the UI as "pi", but its an invalid login. Must only be good for ssh.

The good thing is, it's not magic, there is a simple explanation. But I don't know what it is yet.

The Linux permissions and ssh permissions are the same. The octoprint permissions are different. Octoprint is running as the pi user, AFAIK. So when you log into octoprint as changedsoul, it still runs your script as pi.

The first part at play is the permissions, which you say are 777, and you can run it via ssh as pi, so the folders must be accessible. The second part is the environment, which I think is the same as for pi also. So if you can run it via command line, I don't know why octoprint can't.

I forgot to add, I also placed the full path into the config file, and got the same result.

Do you have an extra colon in the config file (at the end of printer_on.sh)?

If you cant tell what I am doing, I am wiping egg off my face :p
I was about to post the directory listing and permissions, as well as the config file contents when I spotted the error of my ways. I must be tired, because the file was called poweron.sh, and the config was calling printeron.sh DOH. I need to go to bed hahaha
Thanks for trying to help, this is a classic case of ID10T error.
Looking forward to implementing this relay stuff on my printer. Will be nice to go to bed and have it shut itself down when done.

See? A simple explanation. NBD, we've all been there. Thanks for sharing the "fix".

Hello

Who could help me please?

I followed this tutorial "http://furick.com/icbd/2016/12/setup-relay-control-on-a-new-raspberry-pi-3-b/" but allways does not wants to start more and puts me a message of errors. Beginner on rasberry I have to pass a step?

I'm sorry, but I did not write that tutorial. Also, I don't know how to help, since I don't understand what your errors are.

thank you for the answer
Ok but can you tell me if this seems right?
Because with your explanations I do not get there, I do not understand how to write the script?

Do you know if all 4 relays can be powered and controlled by the Pi (or a Pi ZeroW)?
I have not received my relays yet to check them out but my thoughts are the current required to control all the relays may be too high for the GPIO? I am guessing if this is the case I could potentially use an external 5v source to control the relays?

You could certainly use an external power supply to power the relays, but they don't take much power, so I would guess it's ok, but you'd have to find the numbers.

Had everything working for a few days. Now when I power on/off through octoprint, I can hear the relay click but the printer stays on. I switched over to another relay channel and it's working fine. I imagine that first channel burnt out or fused closed. Is that a possibility. Switch stuck closed? By the way. Thanks for this tutorial. Very clear and worked well for me

Glad you liked the tutorial. I'm not an expert on relays. They can fuse closed but the ones that I've seen do that didn't click anymore. That must have been what happened though.

I like how you've gotten 283 downloads of your STL....

I hope there is some leech trying to sell it on ebay.

Good tutorial, and plugin :)
But you should check my plugin github.com/vitormhenrique/OctoPrint-Enclosure
It has much more configuration, you can add delays on the start and finish of the printer, you can also add inputs and events that can be triggered on temperature events etc....

I'll also create a "thing" for my plugin :)

That looks like a great plugin. Feel free to "remix" my "thing" so that people can see that as a related solution.

Do i get the Wiring of the Power Supply right?:

Outlet ------ Cable --------> Relay Board ---------Cable --------> Printer Power Supply (My Printer does not have a switch)

Does it matter in which way I connect the Cables to the Relay?
The Screw Terminal is build like that I believe : [Normally Open] [Common Ground] [Normally Closed]
Is the Left Cable going to your Power Supply or the Outlet?
Looking forward for a reply.

The relay just opens and closes, like a switch. Of you aren't sure, then this project might not be for you. Mains AC can kill.

SOLVED.

I really confused, i can confirm that i can toggle the GPIO on and off by reading logical value using "gpio readall" but when connected to a relay after typing "gpio export 23 out" the relay is immediately activated and manually set the GPIO value high/low doesn't make a different. Should i put a pull down resistor on the relay?

Reading the pins shouldn't change the state. Not sure what's going on there, but ignoring that for a moment...

gpio export 23 out changes the state of the output, to "activate" the relay. But then going high or low doesn't turn it off.

You shouldn't need a pull up or down resistor, since the pi will be actively choosing 0V or 3.3V. pull ups/downs are usually for inputs, not outputs.

What relay are you using? Is it a premade board like I'm using from sainsmart? Are you sure it will trigger with 3.3V? It sounds like at either state (0, 3.3V) it's causing the relay to be in one state, but when the pin is floating (when the pi isn't controlling the output) it's able to deactivate the relay.

As a hack/workaround, you could just gpio export 23 out in the printer_on script and gpio export 23 in in the printer_off script. One possible problem with that is that if the relay isn't stable when the pi is in gpio export 23 in, then it might toggle on.

thanks for a quick respons, i have tried plugging in a 5mm LED with resistor in series to the GPIO and succesfully switch it on and off and i also tried a Fotek SSR and it also work (the light indicator on the SSR switches on and off). The relay in question is similar to what you have in the description the different is i bought it off ebay not sure of the quality and mine only 1 channel instead of 4, but i had the same relay work with arduino before. So not sure what is going on here. I'll try the method you are using i'll report back.

Best Regards
Rizki

UPDATE: Connected my Relay to 3.3v Pi Pin (Pin 1) instead of 5V and it work like it should, switching it on and off no problem. Not sure why maybe the voltage differential between 3.3v active high and 5v source is enough to trigger the relay?

Lol that hack with just gpio export 23 out and gpio export 23 in actually toggle the relay and i can hear a sounding "click" i am not sure i will continue on with this as i am not sure how reliable it will be. Something is wrong with my relay but i have two and all behaves the same. i am not sure how to continue with this problem.

You connected the power of the relay to 3.3V and it triggered like it should? That's a good workaround.

Yeah this is how i connect my relay (working)
Relay VCC --> VDC 3.3v (Raspi Pin #1)

Previously My relay is connected like this:
Relay VCC --> VDC 5v (Raspi Pin #4)

Still scratching my head :)

If you do this:
Relay VCC -> VDC 3.3V (pi #1)
Relay GND -> GND (pi #6)
Relay IN -> OUT (pi #23)

That's perfectly fine. If that works, you're done.

The reason that's working is that the input on the relay is comparing the input to the VCC, so when it's lower, at 3.3V, the input looks higher.

They may not be broken, just configured for a different voltage. The pi outputs 3.3V. Most arduinos use 5V. Using 3.3V with an output is usually ok with 5V devices because 3.3V > 2.5V (the halfway point to 5V). You can confirm this by connecting the relay straight to the 3.3V, GND, and 5V on the pi, and see what states the relay is in.

I almost didn't mention that caveat to the hack, because if the relay has an internal pullup (which it should), then it's not going to be erratic at all. If you want to be sure, you can combine that export in/out hack with a pull up resistor to 5V. Don't use the pullup with on/off, just the out/in. That's probably unnecessary because the relay board probably already has one.

If you want to avoid the export out/in hack, then you would need to add a transistor, controlled by the 3.3V output of the pi, and it will switch the relay from 5 to 0V. I'm not going to explain that in this comment section, there's plenty about it on the internet already.

update again: bought a 4 channel relay with opto-coupler, now with 5V it work beautifully.

Are the plugs wired to the relays normally closed or normally open? I did not see a picture of the finished/wired relay board.

Either way, I used Normally Open, because if something happened to the power of the pi, or the relay board, I'd rather it switch it off than switch it on.

It's really up to you. I could see a pretty good argument for using the normally closed on the printer, so that some flaky wire to the relay board doesn't ruin a print. It's really up to you.

very, very, very useful tutorial. Thanks a lot!

Instead of using a relay board, could you do this by publishing an mqtt topic for a home automation system to turn the printer/light on/off?

Sure. You're essentially just calling a script, and you could put anything you want in that script. mosquitto (notice the two 't's) can be used in a python script, or you can call the mosquitto_pub directly from the bash script:

Look at the "Examples" Section here (Sorry if you're reading this in 1000 years and this page doesn't say examples anymore):
https://mosquitto.org/man/mosquitto_pub-1.html

So instead of

#!/bin/bash
gpio export 23 out
gpio -g write 23 0

You might do:

#!/bin/bash
mosquitto_pub -h 192.168.1.1 -p 1885 -t octoprint/printer_switch -m "ON"

Great job on the instructions. Got this all hooked up and running. Could not be happier.

Hello!
After finishing the task on Orange PI PC, getting 0 on the ports and relay works for switching light and the 3d printer. What to do about this?

I don't understand what the question is.

Sorry, this is Google translator.
0 - turn on the printer.
1 - turn off the printer power
I use gpio22 and gpio23
At the conclusion of OrangePi (sudo shutdown -h now) at the outputs 22 and 23 = 0 and switched relay, switched on the light and power of the printer.

You're saying that when you turn off the computer, the states of the relays are inverted? You should be able to switch which side of the relay you use. One side will be connected when there is power, and the other side will be connected when there is no power. You will have to invert the logic in the scripts as well.

Thank you for the article, came in very handy

You sir are a legend! Thank you very much for this!

I was getting crazy for the last two evenings to get the relay board working. I've read every tutorial and finally found the solution by myself (note: total GPIO noob).

The IN1 & IN2 of the board are connected to the pins 12 and 16 (GPIO 18 & 23). So I've tried to trigger GPIO 18 and 23 accordingy. Nothing happened.. I've read things about external 5V power supplys and insufficent power to run the board and stuff.. But I've seen than other people are running it and didn't want to give up.

Then I've found a solution while trying.

gpio readall

SURPRISE! The physical pins 12 & 16 are marked as gpio 1 and 4 in octopi...

Edited the code aaaaaaand done!

Hardware:
2011 Raspberry Model B Rev1, OctoPrint 1.3.1, 2 channel relay board with opto's.

Hi!
Your intructable worked great thanks!
I also use pushbullet.
At night I use the Light_off command while printing.
When it is finished I get a push message witch is fine. The problem is that I get a overexposed picture of the finished print. It seems that the the light is turned on again for a short moment when pushbullet triggers the webcam.
The cam has too less time to focus and sends me that blurry and overexposed picture.

Does anyone know how to fix that? (I don't want to leave the light on.)
Maybe also a sleep command for the light_on.sh file?

Yep. The last section in the tutorial covers that.

I already have the last section implemented, so it does not solve that. I think the light is turned on at the same time as the picture is shot. So the cam has a wrong focus. The picture should be taken some seconds later. Or the light should be turned on a few seconds earlier.

Oh, I see. You want the light to come on a few moments before the picture is taken, but it's off when the printer is working... Hmmm.

I don't know of a way to get the PB plugin to slow down. Maybe there's an earlier event you could try to turn the light on with?

http://docs.octoprint.org/en/master/events/index.html#server

I also thought about that, but I could not find a event that matched.
Is it there a way to use those commands in the gcode-file?

I highly doubt that. Probably wouldn't be terribly difficult to add a pause in the Pushbullet code. It's in your octoprint folder somewhere. Any edits would get deleted on an update.

Ok thanks!

The LCD on my Prusa i3 Hephestos is on when it is connected to the Pi. I think I will cut the 5V line and use a relay in between. That will solve this problem.
But maybe there is a more eligant way to do that. I read about that it is possible to switch all usb ports off from the software side.
Is this possible with octopi?

I have no idea.

The update to Octoprint 1.3.0 deleted my "system:" section. So you'll probably want to back up your config.yaml before doing that upgrade.

The update to Octoprint 1.3.0 deleted my "system:" section. So you'll probably want to back up your config.yaml before doing that upgrade.

The update to Octoprint 1.3.0 deleted my "system:" section. So you'll probably want to back up your config.yaml before doing that upgrade.

How do i do that?

Comments deleted.

I'm not sure it's applicable any more. But to copy a file, you can use the cp command.

How does that preserve it after the update? Or are you saying just to copy and paste it after the update?
P.S. I just got this error after the Octoprint Config Part One
"Command failed with return code 127: /bin/sh: 1: printer_on.sh: not found"
Not sure how to fix. The relay works on command line entries.

Nevermind the P.S. I'm a new Linux user and added the colon to the file names.
Just got rid of those and it works fine.
The question about the Update still stands though.

So you're asking about how to backup and restore for an update?

I'm not sure every update will break it, but to be sure, after you finish editing config.yaml or before you update, make a copy of the file. Then, if an update destroys your changes, you can look at the copy to remember what you did to make the config.yaml work.

Ok so just evaluate the update and try to make something like your "thing" work for it, got it. I'll give it a go and update the thread when I have.
Have looked through the files of 1.3.2 and have any advice? I'm still new at Linux so any help is much appreciated.

Sorry, I haven't been using my printers in the last few months. I just finished my low rider CNC machine, and I've been working on other projects. Please let us know how it goes. Also, welcome to Linux. We were all once noobs.

Thanks it soothes my OCD

I'm using octoPi on the RPi 3. Just got events working but I did notice that for system events I had to put all my commands on a single line.

This syntax cause my octoprint to fail to start

  • command:
    • printer_on.sh
    • light_off.sh
      event: Startup
      type: system

This syntax is working correctly

  • event: PrintDone
    command: lights_off.sh && fans_off.sh
    type: system

Hmmm. That may be. This comment section has pretty difficult to read syntax, but it's possible the commands can't be stored as a list. YAML also cares about whitespace, and the "command", "event" and "type" should all have the same number of spaces. The ticks for the command should have more. Let me see if I can figure out this syntax here in the comments:

  - command:
        - printer_on.sh
        - light_off.sh
    event: Startup
    type: system

Also, doing the && is fine, but be aware that if the first one fails, the second will not run. So your fans might stay on. Here is that section, with the &&s, but fixed width formatted:

  - command: printer_on.sh && light_off.sh
    event: Startup
    type: system

(BTW, to do fixed width, you have to have an empty line before and after, and indent each line with 4 spaces, plus whatever spaces you want it to have).

You might be able to use a semicolon, "lights_off.sh; fans_off.sh". That would run the fans_off.sh even if the first one failed. But minor nit.

Looking at my config file, I don't have two commands, I only have the one, so I'm not perfectly sure the two commands works anymore.

The documentation says the commands can be a list of strings but the example only shows type: gcode. all the type: system examples are a single line. The && operator works great

I'm assuming that has something to do with it although I didn't confirm the gcode example works correctly

(The comments section did mess with my white space and dashes)

Comments deleted.

I have 1.2.16, and my config is still working for me.

I'm not sure what the difference you are trying to show is, but the order of the items doesn't matter, they all end up in the same dictionary. Maybe you had a different syntax error, and accidentally fixed it when you reordered it?

Hello nice tutorial!
I'm going to try it.
Do you know if it's feasible to integrate these commands into telegram plugin commands?
Thanks
Pietro

Thank you.

No, I haven't used the telegram plugin.

Hello
I set all up in a breeze! Thanks.
Only problem was editing of config.yaml, you must respect indention and no empty row in the text.
All works and with my big surprise all commands are available direct in Telegram plugin, under /sys commands!
Really thanks!

This is a very slick "Thing"!

Just a question for you. Do the -action: pon, poff, etc have relevance elsewhere ? Or are they like internal names for the .yaml file?

Also, once you edit the .yaml file, is a restart of the pi (reboot) needed, or just an octoprint restart?

Thanks again!

Thanks.

Pon and poff are just yaml names.

You just have to restart octoprint.

Got it all working great! Thanks again for the walkthrough!

Hi, Thanks for this. I've been getting this set up myself. One point... what kind of power supply are you using for the RPi? I have RPi3, and at startup it switches all the relays off. Unfortunately, that sucks so much power that it kills the Pi :(

Thanks for a great post! But I've tried to write the scripts as an admin but for some reason it doesn't work out. I think I don't know how I need to write the scripts or what command I need, this is the first time I'm in the ssh on a raspberry and mabye I am missing something? Thanks for the help!

Well, I'm trying to avoid doing a tutorial on how to use Linux, or raspberry pi's. There are much better tutorials out there for that.

There are a thousand ways to edit files on the command line in linux. I use vi, but you should use probably use nano:

nano .octoprint/config.yaml

Also, you don't need to be root (linux admin) to edit the config.yaml file. It is owned by the pi user. So you should just ssh in as the user 'pi'.

copy the file first, so you can revert if you do something wrong:

cp .octopi/config.yaml .octopi/config.yaml.orig

Thanks for fast replay! I think you misunderstood me, I meant these section:

"GPIO Scripts
octopi comes with the gpio stuff installed, so just running "gpio" on the command line gives me several options. I made 4 scripts to control the relays. They were all in "/usr/local/bin/". I had to make them as root, and I had to add permissions "sudo chmod a+rx .sh" so that the pi user could execute them.

printer_on.sh:

!/bin/bash

gpio export 23 out
gpio -g write 23 0
printer_off.sh:

!/bin/bash

gpio export 23 out
gpio -g write 23 1
light_on.sh:

!/bin/bash

gpio export 18 out
gpio -g write 18 0
light_off.sh:"

!/bin/bash

gpio export 18 out
gpio -g write 18 1

Thanks again!

Ah, yeah, so you would need to use nano in combination with the sudo command, which will give pi root access. So something like this for the printer_on.sh script:

sudo nano /usr/local/bin/printer_on.sh

... type or paste in the script contents... write out, and exit...

sudo chmod a+rx /usr/local/bin/printer_on.sh

Then you should be able to type 'printer_on.sh' as the pi user, and turn on that pin.

Hi again! One last question, in the picture with the relay on the wiring, you have connected the power L from the outlet to normally open and then commen to the psu. But after searching, I have seen that people connect L from outlet to common and then normally open to the psu. But maybe it doesn't matter how I connect it ?

The only difference is that the normally closed side will be connected to common when it's "off". I'm a little confused on your nomenclature, but the safest thing would be to connect the power from the wall to the normally open, and the power to the power supply, or printer to the common. That way, if by some random chance you shorted the normally closed to something, it wouldn't have wall power.

Some other applications might have something else they want powered in the normally closed, and so you'd put the input power on the common.

Thanks! But I think you are misunderstanding me, I know that NC are connected to COMMON when it is off. My question was if I connect the Live (+) cable from the outlet to the COMMON or to the NO on the relay ? What would be the difference between NO and COMMON. Thanks agian for response!

I think you are misunderstanding me.

The printer will work either way, the only difference is that the NC will be live if you connect the live wire to the common.

Okey now I understand! Thanks for your patience!

Comments deleted.

Thanks for sharing, Jeff!

Each one of the four relays is rated at 10 Amp, but the printer draws 20 Amps with the heated bed on, to the best of my knowledge. How did you go about that?

Thanks

You scared me there! I thought maybe I messed up.

After thinking about it though, I'm switching the input to the power supply, which is 120VAC. The output is 12VDC, 20A.

There's no way the printer takes 20A input current, I only have a 15A breaker for the whole room.

I'm not at the printer right now, but I'm guessing it's close to 3A.

:D No worries Jeff! The printer will certainly take up to 10-16 amps when it's at fully operational mode for ABS (bed=90, heater = 240) it might take 3 amp when bed and extruder aren't powered. The controller alone might take up to 2 amps and the rpi II will take up to 1.2 amps I haven't measured the watts yet but that is another important thing to consider.

on a different note, I keep getting the following error
"Command failed with return code 127: /usr/local/bin/light_on.sh: line 2: gpio: command not found
/usr/local/bin/light_on.sh: line 3: gpio: command not found"

I think it has to do with the GPIO wiring library but I still have to look it up.

Thanks again for sharing, it really helped get my feet wet with Octoprint and now I'm working on a smoke/fire protection with MQ sensors and image processing.

Yeah, you have to install wiring first.

You're talking about the output amps, and I'm switching the input. The input amps are lower than the output amps, because the input voltage is 10x higher.

So I'm not in any danger of overloading that relay.

Right, I'm talking about the output amps. I will measure the input and see how much it takes.

Having just read the above, I've measured the Amps used on my UK 240v supply at the wall socket.

Standby : ie just Raspberry Pi3 running OctoPi and a charger to a tablet ( charged) - 0.15 Amp
Idle : as above, plus Wanhao i3 v2.1 powered on and ready, extruder fan spinning and two 500mm strips of LED lighting - 0.4 Amp
Full On : as above plus bed heating, extruder heating and both Z axis servos running - 2.0 Amp

After a 4 hour print the maximum recorded Amp's Used was 2.05 Amp.

That's not much at all. Although I wouldn't expect those numbers to be the same on 120V.

Good tutorial. Thanks!

Have you had any difficulty when printing from Octoprint? When using Octoprint, it seems that my printing is off by several mm to one side of the x axis. I believe it's related to the start gcode but haven't been able to zero it in.

I haven't had that issue. The octoprint thingiverse group or octoprint Google group would probably be the place to ask.

My understanding is that it should home to 0, and then move from that edge. If you center something in your slicer program, it's assuming the bed width is what you set it up for, say 200mm. The center should then be 100mm from the X stop. That might not be the real center of the bed. You can do a home x, then move the head to where you think the center is, and see what x that is. On my printer, 0 is a little bit off of the left edge of the bed.

What did you do to have the ServerRestart and Shutdown in the Menu??

I added more items to the system menu. I didn't put them in the guide, so you'll have to merge these items with the others if you are doing the relay control:

system:
  actions:
  - action: shutdown
    command: sudo shutdown -h now
    confirm: You are about to shutdown the system
    name: shutdown
  - action: reboot
    command: sudo shutdown -r now
    confirm: You are about to reboot the system
    name: Reboot
 - action: srestart
    command: sudo service octoprint restart; sudo service octopipanel restart
    confirm: You are about to restart octoprint.
    name: ServerRestart

That goes in the .octopi/config

Also note that I have an "octopipanel" service, and you probably don't, so you can edit that line too.

Can you tell me more about the Push Bullet script please?

If you're asking about how I got pushbullet to work, that's not my script, that's a plugin:
https://github.com/OctoPrint/OctoPrint-Pushbullet
If you have questions about that, I would ask in the google group for octoprint: https://groups.google.com/forum/?hl=en#!forum/octoprint

If you're talking about how I changed my light_off.sh script to work with it, I changed this:

gpio -g write 18 1

into this:

(sleep 5 && gpio -g write 18 1)&

"sleep 5" waits five seconds. "&&" means that the first command has to complete with success, and then the second command will be called, so "sleep 5 && command" will wait five seconds, and call "command".

Putting that thing in parenthesis, and adding a "&" at the end means the whole thing will get run in the background, so that octoprint can keep on doing whatever it was doing before calling that script (the octoprint developers said that I shouldn't have a long running script, because it will block octoprint, but I'm skeptical that's the case).

As an interesting side note, I first had this:

sleep 5
gpio -g write 18 1

And that worked just fine, so I'm not sure if it's really a problem, but foosel told me that it was, so I went with the slightly more complicated version.

Since I've written this up, I might as well put it in the main description...

Yes, thanks. Those are the ones.

Do you have a link to the pi case that you used? I searched for quite a while and couldn't find one exactly like that..

Thanks

I've made the part "remixed from" the pi case.