Skip to content
ONeil's Lab
ONeil's Lab
  • Home
  • The Lab
  • About Me
  • Helpful Readings
  • Gitlab

CNC Button Board

April 2, 2021April 2, 2021 AONeil

Button board for CNC enclosure control. Python script meant to be ran on RPi to watch if a button was pressed and send webhook POST on button press to home assistant to turn on / off smart outlets for cnc killswitch, vacuum, lights, fan.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# example gpiozero code that could be used to have a reboot
#  and a shutdown function on one GPIO button
#!/usr/bin/python
from time import sleep
from gpiozero import Button
from signal import pause
import requests

held_for=0.0
trigger1time = 0.5
trigger2time = 5.0

def cncbutton_rls():
        global held_for
        print("released!")
        print("released after", held_for, "seconds.")
        # example code showing what to do with this time
        if (held_for > trigger1time):
                # If held for 2 seconds...
                print("Button:CNC Held for over .30 seconds.")
                print('Toggling CNC E-STOP')
                requests.post("webhook")
                held_for = 0.0
        elif (held_for < trigger1time):
                # If held under 2 seconds...
                print("Button:CNC Held for under .30 seconds, probably noise...")
                held_for = 0.0
            
def cncbutton_hld():
        # callback for when button is held
        #  is called every hold_time seconds
        global held_for
        # need to use max() as held_time resets to zero on last callback
        held_for = max(held_for, cncbutton.held_time + cncbutton.hold_time)
        print("held for", held_for, "seconds.")

def vacuumbutton_rls():
        global held_for
        print("released!")
        print("released after", held_for, "seconds.")
        # example code showing what to do with this time
        if (held_for > trigger1time):
                # If held for 2 seconds...
                print("Button:VAC Held for over .30 seconds.")
                print('Toggling Vacuum')
                requests.post("webhook")
                held_for = 0.0
        elif (held_for < trigger1time):
                # If held under 2 seconds...
                print("Button:VAC Held for under .30 seconds, probably noise...")
                held_for = 0.0
            
def vacuumbutton_hld():
        # callback for when button is held
        #  is called every hold_time seconds
        global held_for
        # need to use max() as held_time resets to zero on last callback
        held_for = max(held_for, vacuumbutton.held_time + vacuumbutton.hold_time)
        print("held for", held_for, "seconds.")

def lightbutton_rls():
        global held_for
        print("released!")
        print("released after", held_for, "seconds.")
        # example code showing what to do with this time
        if (held_for > trigger1time):
                # If held for 2 seconds...
                print("Button:Lights Held for over .30 seconds.")
                print('Toggling Lights')
                requests.post("webhook")
                held_for = 0.0
        elif (held_for < trigger1time):
                # If held under 2 seconds...
                print("Button:Lights Held for under .30 seconds, probably noise...")
                held_for = 0.0
            
def lightbutton_hld():
        # callback for when button is held
        #  is called every hold_time seconds
        global held_for
        # need to use max() as held_time resets to zero on last callback
        held_for = max(held_for, lightbutton.held_time + lightbutton.hold_time)
        print("held for", held_for, "seconds.")

def fanbutton_rls():
        global held_for
        print("released!")
        print("released after", held_for, "seconds.")
        # example code showing what to do with this time
        if (held_for > trigger1time) and (held_for < trigger2time):
                # If held for 2 seconds...
                print("Button:Fan Held for over .30 seconds.")
                print('Toggling Fan')
                requests.post("webhook")
                held_for = 0.0
        elif (held_for > trigger2time):
                # If held under 2 seconds...
                print("Button:Fan Held for over 5 seconds.")
                print('Toggling Fan & Garage Door')
                requests.post("webhook")
                held_for = 0.0
        elif (held_for < trigger1time):
                # If held under 2 seconds...
                print("Button:Fan Held for under .30 seconds, probably noise...")
                held_for = 0.0
            
def fanbutton_hld():
        # callback for when button is held
        #  is called every hold_time seconds
        global held_for
        # need to use max() as held_time resets to zero on last callback
        held_for = max(held_for, fanbutton.held_time + fanbutton.hold_time)
        print("held for", held_for, "seconds.")

cncbutton=Button(21, hold_time=.30, hold_repeat=True, pull_up = False)
vacuumbutton=Button(20, hold_time=.30, hold_repeat=True, pull_up = False)
lightbutton=Button(19, hold_time=.30, hold_repeat=True, pull_up = False)
fanbutton=Button(16, hold_time=.30, hold_repeat=True, pull_up = False)

cncbutton.when_held = cncbutton_hld
cncbutton.when_released = cncbutton_rls

vacuumbutton.when_held = vacuumbutton_hld
vacuumbutton.when_released = vacuumbutton_rls

lightbutton.when_held = lightbutton_hld
lightbutton.when_released = lightbutton_rls

fanbutton.when_held = fanbutton_hld
fanbutton.when_released = fanbutton_rls

pause()
Posted in MPCNC, Projects

Post navigation

Updates on automated BB recon

Welcome to my corner of the internet.

This blog is my dedicated space to post about network security, homelab stuff, CNC / 3D Printer work, and various other projects I undergo. Feel free to contact me via one of the social icons on the "about me" page.

Categories

  • 3D Printing (1)
  • Homelab (3)
  • Misc (4)
  • MPCNC (4)
  • Networking (3)
  • Powershell (1)
  • Projects (3)
  • Security (4)
  • Storage (1)
  • Tutorial (3)
  • Uncategorized (1)

Archives

© 2021 ONeil's Lab