From 88bfe9851df6d4134fdde583794b3889e6d8c363 Mon Sep 17 00:00:00 2001 From: jerick Date: Sat, 22 Oct 2022 20:57:07 -0400 Subject: [PATCH 1/2] Fixed GPIO assignation and curl command --- lightSwitch.py | 15 ++++++++++----- lightSwitch.sh | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) mode change 100644 => 100755 lightSwitch.sh diff --git a/lightSwitch.py b/lightSwitch.py index 563d355..1b777e8 100644 --- a/lightSwitch.py +++ b/lightSwitch.py @@ -6,11 +6,14 @@ import RPi.GPIO as GPIO #Don't have to check if on or off #curl -k -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwYWRkMGMxZmFlNWQ0YWNmYThjNTdmMDRhNWIwODBjNiIsImlhdCI6MTY2NjQ2NzMzMCwiZXhwIjoxOTgxODI3MzMwfQ.Egk1vDF0LC_WRlorhCHLPihkPICIEwz6nbfJR7Y1Prs" -H "Content-Type: application/json" -d '{"entity_id": "switch.lamp_plug"}' http://192.168.0.196:8123/api/services/switch/turn_on +#Find an accurate broadcom pinout here +#https://www.etechnophiles.com/raspberry-pi-3-b-pinout-with-gpio-functions-schematic-and-specs-in-detail/ -touchPin = 31 + +touchPin = 21 GPIO.setmode(GPIO.BCM) -GPIO.setup(touchPin, GPIO.IN, pullUpDown=GPIO.PUD_UP) +GPIO.setup(touchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) def touchDet(pin): touch = GPIO.input(pin) @@ -18,12 +21,14 @@ def touchDet(pin): try: while True: - if touchDet(touchPin): print('['+time.ctime()+'] - '+'Touch Detected') - time.sleep(0.2) + if touchDet(touchPin): + print('['+time.ctime()+'] - '+'Touch Detected, Light Switch Toggled') + subprocess.run('./lightSwitch.sh') + time.sleep(0.2) except KeyboardInterrupt: print('interrupted!') GPIO.cleanup() -#subprocess.run('./lightSwitch.sh') \ No newline at end of file +#subprocess.run('./lightSwitch.sh') diff --git a/lightSwitch.sh b/lightSwitch.sh old mode 100644 new mode 100755 index 87101bb..a263436 --- a/lightSwitch.sh +++ b/lightSwitch.sh @@ -1,3 +1,3 @@ #!/bin/bash -curl -k -X POST - "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwYWRkMGMxZmFlNWQ0YWNmYThjNTdmMDRhNWIwODBjNiIsImlhdCI6MTY2NjQ2NzMzMCwiZXhwIjoxOTgxODI3MzMwfQ.Egk1vDF0LC_WRlorhCHLPihkPICIEwz6nbfJR7Y1Prs" -H "Content-Type: application/json" -d '{"entity_id": "switch.lamp_plug"}' http://192.168.0.196:8123/api/services/switch/toggle +curl -k -X POST -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwYWRkMGMxZmFlNWQ0YWNmYThjNTdmMDRhNWIwODBjNiIsImlhdCI6MTY2NjQ2NzMzMCwiZXhwIjoxOTgxODI3MzMwfQ.Egk1vDF0LC_WRlorhCHLPihkPICIEwz6nbfJR7Y1Prs" -H "Content-Type: application/json" -d '{"entity_id": "switch.lamp_plug"}' http://192.168.0.196:8123/api/services/switch/toggle From b8656917f5d7c8a5cf9cb2be7d60ce2e0607750a Mon Sep 17 00:00:00 2001 From: jerick Date: Sat, 22 Oct 2022 21:05:18 -0400 Subject: [PATCH 2/2] fixed shell file path --- README.md | 2 ++ lightSwitch.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) mode change 100644 => 100755 lightSwitch.py diff --git a/README.md b/README.md index e69de29..8f8eec0 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,2 @@ +Service File can be found at /lib/systemd/system on Pi + diff --git a/lightSwitch.py b/lightSwitch.py old mode 100644 new mode 100755 index 1b777e8..be74c9c --- a/lightSwitch.py +++ b/lightSwitch.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import subprocess import time import RPi.GPIO as GPIO @@ -23,7 +24,7 @@ try: while True: if touchDet(touchPin): print('['+time.ctime()+'] - '+'Touch Detected, Light Switch Toggled') - subprocess.run('./lightSwitch.sh') + subprocess.run('./home/pi/homeassistantPi/lightSwitch.sh') time.sleep(0.2) except KeyboardInterrupt: