Python Forum
Webhook, post_data, GPIO partial changes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Webhook, post_data, GPIO partial changes
#3
You call this each time do_POST() runs.
        setupGPIO()
And that sets all the outputs high.

Since the message is structured text, why not pull the information out of the message? The message format is C(index)P(group)MON where index is a digit from 1-8 and group is G or V. It is easy to write a regex that not only verifies the message but extracts the index and group information.
import re

def do_POST(message):
    try:
        pin, group = re.match(r'C([1-8])P(G|V)MON', message).groups()
        print(group, pin)
    except:
        print(message, "is invalid")

while True:
    do_POST(input("Enter Message: "))
Output:
>>> python test.py Enter Message: arfarf arfarf is invalid Enter Message: C7PGMON G 7 Enter Message: C3PVMON V 3
Knowing the group and index, it is easy to map this to a particular output.
import RPi_GPIO as GPIO
import re

# Define the LED groups.
led_groups = {
    "G":(4, 5, 6, 7, 8, 9, 10, 11),
    "V":(12, 13, 14, 15, 16, 17, 18, 19)
}

# Process a message
def do_POST(message):
    try:
        pin, group = re.match(r'C([1-8])P(G|V)MON', message).groups()
        group = led_groups[group]
        pin = group[int(pin)-1]
        for p in group:
            print(p, "=", GPIO.LOW if p == pin else GPIO.HIGH)
    except:
        print(message, "is invalid")

while True:
    do_POST(input("Enter Message: "))
Output:
>>> python test.py Enter Message: C7PVMON 12 = 1 13 = 1 14 = 1 15 = 1 16 = 1 17 = 1 18 = 0 19 = 1 Enter Message:
And since there are lists of outputs, may as well use those to initialize the GPIO outputs.
import RPi_GPIO as GPIO
import re

# Define the LED groups.
led_groups = {
    "G":(4, 5, 6, 7, 8, 9, 10, 11),
    "V":(12, 13, 14, 15, 16, 17, 18, 19)
}

# Initalize outputs used to turn LED's on/off
def setupGPIO():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    for group in led_groups.values():
        for pin in group:
            GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH)


# Process a message
def do_POST(message):
    try:
        pin, group = re.match(r'C([1-8])P(G|V)MON', message).groups()
        group = led_groups[group]
        pin = group[int(pin)-1]
        for p in group:
            GPIO.output(p, GPIO.LOW if p == pin else GPIO.HIGH)
    except:
        print(message, "is invalid")

if __name__ == '__main__':
    setupGPIO()  # This setup code belongs down here, not in do_post
    while True:
        do_POST(input("Enter Message: "))
Reply


Messages In This Thread
RE: Webhook, post_data, GPIO partial changes - by deanhystad - Nov-10-2022, 09:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 685 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 896 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Partial KEY search in dict klatlap 6 1,351 Mar-28-2023, 07:24 AM
Last Post: buran
  function return boolean based on GPIO pin reading caslor 2 1,247 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 860 Jan-30-2023, 08:05 PM
Last Post: caslor
  remove partial duplicates from csv ledgreve 0 842 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Optimal way to search partial correspondence in a large dict genny92c 0 1,026 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,420 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 4,021 Feb-19-2021, 06:29 AM
Last Post: LouF
  Picture changing triggered by GPIO q_nerk 2 2,641 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020