Custom module to turn relay on. RSUDP

Hello, I’m trying to make my own custom module to turn a relay on to activate a beacon light. I followed the instructions on the rsudp documentation, declared it in the client.py and the settings file, but it still wont show on the consumers when an event is detected. I was wondering if someone has made a custom module before or can point to where my mistake is on the code.

Module:

import sys
from rsudp.raspberryshake import ConsumerThread
from rsudp import printM
import RPi.GPIO as GPIO
import time

class MyModule(ConsumerThread):
    def __init__(self, q):
        super().__init__()
        self.sender = 'MyModule'
        self.alive = True
        self.queue = q
        # ... lots of other stuff to initialize your module
        printM(self.thing1, sender=self.sender)

    def getq(self):
        '''
        Reads data from the queue and returns the queue object.

        Since this function returns something, it has return
        strings (*rtype* stands for return type) so that the
        user reading the documentation knows what they'll get
        back if they call it.

        :rtype: bytes
        :return: The queue object.
        '''
        d = self.queue.get()
        self.queue.task_done()
        return d

    def run(self):
        printM('Starting.', sender=self.sender)
        # some stuff to execute here at runtime before looping
        GPIO.setmode(GPIO.BCM)
        pin_rele = 22
        GPIO.setup(pin_rele, GPIO.OUT)

        while self.alive:
            # main loop, do something until self.alive == False
            d = self.queue.get()
            self.queue.task_done()
            if 'TERM' in str(d):
                self.alive = False
                printM('Exiting.', self.sender)
                sys.exit()
            elif 'ALARM' in str(d):
                printM('Got ALARM message...', sender=self.sender)
                # Se enciende la alarma
                time.sleep(1)
                GPIO.output(pin_rele, GPIO.HIGH)
                # Se apaga la alarma
                time.sleep(5)
                GPIO.output(pin_rele, GPIO.LOW)
                GPIO.cleanup()

        self.alive = False

and in the client:

if settings['mymodule']['enabled']:
		# put settings in namespace
		q = mk_q()
        mymod = MyModule(q=q)
	    mk_p(mymod)
1 Like

UPDATE:
I finally decided to try and use the existing custom module that executes a python file with this code:

import RPi.GPIO as GPIO
import time

RELAY_PIN = 22

def setup_gpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(RELAY_PIN, GPIO.OUT)

def turn_relay_on():
    GPIO.output(RELAY_PIN, GPIO.HIGH)
    print("Relé encendido")

def turn_relay_off():
    GPIO.output(RELAY_PIN, GPIO.LOW)
    print("Relé apagado")

def cleanup_gpio():
    GPIO.cleanup()

def main():
    try:
        setup_gpio()
        turn_relay_on()
        time.sleep(4)
        turn_relay_off()

    finally:
        cleanup_gpio()

if __name__ == "__main__":
    main()

The rsudp recognizes it when an event is detected but i get an error that says:
[Custom] Code execution failed. Error: Invalid syntax (, line 1).
Wich i dont understand because line 1 is just librarys.

Hello Matiro,

It is quite unusual behavior, I agree, as I haven’t seen it before. If you haven’t already tried, I would recommend deleting your current Python environment and creating a new one with a fresh installation of RSUDP. It is possible that some files got corrupted while working and the fastest way to correct this possible problem is to reinstall.

If the issue appears again after that, this could be of help (if you haven’t already read it): 1. Program architecture and flow — rsudp documentation