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

CNC Button Board

April 2, 2021July 20, 2021 AONeil

This project is a python script meant to be running on a Raspberry Pi to trigger webhooks to my home assistant setup if a button was pressed. I currently have the following plugs on a smartswitch: cnc killswitch, vacuum, lights, fan. The center toggle switch is the laser kill switch.

#!/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 3D Printing, MPCNC, Projects

Post navigation

Updates on automated bug bounty recon framework
MPCNC Hot-Swappable Z-Axis

Austin

Here you'll find my dedicated space to post about the various other projects I undergo. Feel free to contact me via one of the social links on my about me page.

Categories

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

Archives

© 2023 ONeil's Lab

Proudly powered by WordPress | Theme: x-blog by wpthemespace.com