Kodi Community Forum

Full Version: URLLIB/URLLIB2 urlopen does not work in RpiGPIO event detect callback
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm having problems making an urlopen request after RpiGPIO.event_detect triggers the callback in an add-on service that I'm building to kodi (over Rpi2). The callback function is "play" and the callback is settled in the main function with "GPIO.add_event_detect".

Code:
#Python
#running on kodi over openelec
def play(button):
    #it has the same pid that is printed in the main
    xbmc.log("[Callback] > process, pid %s" % os.getpid(), level=xbmc.LOGNOTICE)

    try:
        #I'm sure that this url works.
        #I also replaced with other existent urls to doublecheck
        url = "http://localhost:3000/next/" + button

        #it goes well
        request = urllib2.Request(url)

        #I receive a NoneType
        response = urllib2.urlopen(request)

        #It prints "Responded <type 'NoneType'>"
        xbmc.log("Responded %s" % type(response), level=xbmc.LOGNOTICE)
    except Exception as exception:
        xbmc.log("Some error occurred %s " % exception, level=xbmc.LOGNOTICE)
    else:
        xbmc.log("It went well", level=xbmc.LOGNOTICE)

#Main function
if __name__ == '__main__':
    xbmc.log("[main] > process, pid %s" % os.getpid(), level=xbmc.LOGNOTICE)
    # Buttons Setup
    buttons = [
        {'gpio': 21, 'name': "button-1", 'state': False, 'led_pin': 26},
        {'gpio': 20, 'name': "button-2", 'state': False, 'led_pin': 19},
        {'gpio': 16, 'name': "button-3", 'state': False, 'led_pin': 13}
    ]

    # Starting with GPIO
    GPIO.setmode(GPIO.BCM)
    # GPIO Init
    for k in range(0, len(buttons)):
        GPIO.setup(buttons[k]['gpio'], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.add_event_detect(buttons[k]['gpio'], GPIO.RISING, callback=play, bouncetime=300)

    while not monitor.abortRequested():

        #don't need to nothing here as the callback will be called
        #when some change will occurr in the gpio

        # Sleep/wait for abort for 200 ms
        if monitor.waitForAbort(1):
            xbmc.log("Exiting...", level=xbmc.LOGNOTICE)
            # Abort was requested while waiting. We should exit
            GPIO.cleanup() # clean up after yourself
            break


The callback is triggered but the urlopen always return a type 'NoneType'.

Though, when I do the url open without using the callback it works meaning that when I do pooling over the gpis's without the event_detect function, the urlopen works.

RpiGpio event_detect callbacks triggers the callback in another thread (http://sourceforge.net/p/raspberry-gpio-...ki/Inputs/).

Any idea why this is happening? Is there any restriction on running url open on a kodi add-on service in another thread?

I'm running Kodi, over openelec, over a PI2. Pi2 has internet and url exists.
I figured out that RpiGPIO runs the callback in restricted mode meaning that I can't access to any url or file in that instance