Toggle relay with push button and remote control (telegram bot)


I have a 3d printer with octoprint and i want to improve its control.
I want a way to turn on and off the printer by pushing a button next to the printer or using the telegram bot when I'm far from the printer.

I came from C++ and Arduino so i'm not very familiar with python and raspberry.
I created a script that reverse the state of a pin to turn on and off the printer and it works. Now i want to add the push button but I have some problem to develop the script because it works only one time I push the button, then the file "" is already imported and I can't reuse it. Do you have any suggestions? Maybe this is not the best forum where ask something like this but who knows :stuck_out_tongue:

These are the scripts:

import RPi.GPIO as GPIO
import time



GPIO.output(17, not(GPIO.input(17)))


import RPi.GPIO as GPIO
import time



while True:
	buttonState = GPIO.input(27)
	if buttonState == False: #false = pressed
		import Toggle_Relay
1 Like

Does this actually work? You've told the GPIO pin that it's an output pin in the first step. Then in the next step you're trying to query the same GPIO (as an input type) and then set it as an output type.

I dunno, but it might be necessary to do:

toggleState = not(GPIO.input(17))
GPIO.output(17, toggleState)
1 Like

In Arduino it's not needed so i wrote directly this and worked. In fact my issue is only about the push button

I think that it is simpler to use gpiozero library that rpio.gio
declaring input and output is much simpler for beginners.

I am not sure if you really need to use "toggle" for what you want ... if is running you can turn-off the raspberry pi. But if it off... you can't turn it on (unless you have more hardware involved). But in case that your relay it is not related to power the raspberry pi... gpiozero have a toggle function built-in within the library.

I haven't explore much the telegram plugin for octprint, not sure how easy is to add more stuff... But, if telegram plugin for octoprint does not provide enough options ... to directly access the gpio you can use python telegram bot

If you have telegram plugin for octoprint, then you will have two bots... another one will control the relay.

1 Like

Thank you for the library, I'll study it in the week end :slight_smile:

I need toggle because I want to turn on and off the printer when I'm next to it or when I'm on a different network using the telegram bot. The relay doesn't turn off raspberry so I can talk every time with raspberry when I use push button or telegram bot.

I have edited the yaml file of octoprint to call the script so I could call the toggle easly using the command /sys on telegram bot. What I need is to trigger the same action but using a different input, the push button.

It was a very easy thing: I just replaced the line "import Toggle_Relay" with the code that changes the state of the pin and it worked..

Now I have another strange problem that I don't know ho to figure out: the relay change its status randomly even if I don't push the button or call the function from the python script (using the telegram bot). Most of the time is like the relay change its status 2 times in a small time so at the end if turned off it stays off but I don't want this situation. What could I do?

I haven't been around the forum lately. Did you solve your issue with the relay?
This will be a debugging process, you should monitor where is the problem. It is the signal from the GPIO really dropping or it is the relay doing false triggering? Most relay for DIY are 5V input while the raspberry pi uses 3.3V signals. What I have trouble before using raspberry pi and relays...

  1. Powering from 3.3V most relay need being powering from 5V.
  2. Setting the relay to high voltage triggering (signal), some have jumper selector for low voltage (3.3v) trigger. This makes a big diference.
  3. Bad wiring. Are the jumpers tight?