Cannot Call Function from Python Code in Android Main Activity

68 Views Asked by At

Wondering if you guys have ever encountered a problem with calling a function written inside a python code using chaquopy. Below is my python code

import OPi.GPIO as GPIO
import time

def gpio_control_on():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(16, GPIO.OUT)
    GPIO.setup(24, GPIO.OUT)
    GPIO.setup(13, GPIO.OUT)

    GPIO.output(24, GPIO.LOW)
    GPIO.output(16, GPIO.HIGH)  # Set GPIO 16 to HIGH
    GPIO.output(13, GPIO.HIGH)  # Set GPIO 13 to HIGH
    time.sleep(2)  # Delay
    GPIO.output(16, GPIO.LOW)  # Set GPIO 16 to LOW
    GPIO.output(13, GPIO.LOW)  # Set GPIO 13 to LOW

    GPIO.cleanup()

def gpio_control_off():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(13, GPIO.OUT)
    GPIO.setup(24, GPIO.OUT)
    GPIO.setup(16, GPIO.OUT)

    GPIO.output(24, GPIO.HIGH)
    GPIO.output(13, GPIO.HIGH)  # Set GPIO 13 to HIGH
    GPIO.output(16, GPIO.HIGH)  # Set GPIO 16 to HIGH
    time.sleep(2)  # Delay
    GPIO.output(13, GPIO.LOW)  # Set GPIO 13 to LOW
    GPIO.output(16, GPIO.LOW)  # Set GPIO 16 to LOW

    GPIO.cleanup()

def control_gpio():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(8, GPIO.IN)

    pin8_value = GPIO.input(8)

    if pin8_value == GPIO.HIGH:
        gpio_control_on()
    else:
        gpio_control_off()

    time.sleep(1)

    GPIO.cleanup()

# Call the gpio_kawal function repeatedly
while True:
    control_gpio()

The way I call the python code built into the main code:

  // Load the python JNI library
            Python py = Python.getInstance();

            // Get the Python sys module
            PyObject sys = py.getModule("sys");

            // Get the current sys.path list
            PyObject sysPath = sys.get("path");

            // Append the Python directory to the path
            sysPath.callAttr("append", "app/src/main/python/gpio_control");

            // Load the python module
            PyObject module = py.getModule("gpio_control");

            // Call the function named "control_gpio" from the module "gpio_control"
            module.callAttr("control_gpio");
        } catch (Exception e) {
            // Log the error
            Log.e("YourTag", "Error calling Gpio Control function: " + e.getMessage());
            e.printStackTrace();
        }

system error occured when calling the function of control_gpio which is located inside the gpio_control python code. Does anyone know what is the problem? I have checked all the possible error that I can think of. But, the system error keeps on showing. why? the gpio_control python code can be obtained by the application but the function inside the gpio_control python code cannot be identify. why? the error: Error calling Gpio Control function: AttributeError: module 'gpio_control' has no attribute 'control_gpio' This is the new python code

import OPi.GPIO as GPIO
import time

def control_gpio():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(8, GPIO.IN) #setting pin 8 as input
    GPIO.setup(16, GPIO.OUT) #setting pin 16 as output
    GPIO.setup(24, GPIO.OUT) #setting pin 24 as output
    GPIO.setup(13, GPIO.OUT) #setting pin 13 as output

    pin8_value = GPIO.input(8) #check the condition of pin 8

    if pin8_value == GPIO.HIGH:
        gpio_control_on() #if high, call the function
    else:
        gpio_control_off() #if low, call the function

    GPIO.cleanup()

    time.sleep(1)

def gpio_control_on():
    GPIO.output(24, GPIO.LOW) #if input high, pin 24 low
    GPIO.output(16, GPIO.HIGH) #pin 16 high
    GPIO.output(13, GPIO.HIGH) #pin 13 high
    time.sleep(2) #delay 2 seconds
    GPIO.output(16, GPIO.LOW) #pin 16 low
    GPIO.output(13, GPIO.LOW) #pin 13 low

    GPIO.cleanup()

def gpio_control_off():
    GPIO.output(24, GPIO.HIGH) #if input low, pin 24 high
    GPIO.output(13, GPIO.HIGH) #pin 13 high
    GPIO.output(16, GPIO.HIGH) #pin 16 high
    time.sleep(2) #delay 2 seconds
    GPIO.output(13, GPIO.LOW) #pin 13 low
    GPIO.output(16, GPIO.LOW) #pin 16 low

    GPIO.cleanup()

while True:
    control_gpio() #looping for control_gpio function while app runs

but still, the error shows the same 'Attribute Error' I have tried both method, but I still cannot find a way to call the function from the python code built. Is there anything that can be used to pass the function into the Main Code?

1

There are 1 best solutions below

6
On

There's no need to add anything to sys.path to make your app/src/main/python directory accessible. The fact that you've done that, plus the fact that your getModule call did not get stuck in the infinite while True loop at the top level of the Python module, makes me think that you may have multiple versions of this module in your app, and the one that's actually running isn't the same as the one in your question.

If you need more help, please edit your question to include the full stack trace.