Hello, help me Im dumb. Addon to control relays
#46
(2017-06-06, 22:51)Roman_V_M Wrote:
(2017-06-06, 21:09)ablues10 Wrote:
(2017-06-06, 20:35)el_Paraguayo Wrote: The libreelec forum suggests the append line should be:
Code:
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
Have you tried that?

ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.ImportError'>
Error Contents: dynamic module does not define init function (initGPIO)
Traceback (most recent call last):
File "/storage/.kodi/addons/script.relay/addon.py", line 7, in <module>
import RPi.GPIO as GPIO
ImportError: dynamic module does not define init function (initGPIO)
-->End of Python script error report<--
That was using the
Code:
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

This error means that a compiled dynamic library cannot be initialized as a Python module. It looks as if the binary GPIO module (_GPIO.so) is either incorrectly compiled or for a wrong platform. Make sure that you have installed this module from the official LibreELEC repo via Kodi Addon Manager. This should guarantee that you have correct binary components installed.

And for Kodi module addons you don't need to manipulate sys.path. Just add the necessary module addon to the requires section of your addon.xml and it will be added to sys.path of your addon automatically.

BTW, just out of curiosity I've installed RPi tools addon and imported it with no problems:
addon.xml (excerpt):
Code:
<requires>
    <import addon="xbmc.python" version="2.19.0"/>
    <import addon="virtual.rpi-tools" />
</requires>
main.py:
Code:
from pprint import pformat
import xbmc
import RPi.GPIO as GPIO
xbmc.log(pformat(dir(GPIO)), xbmc.LOGNOTICE)
Result:
Code:
23:33:49.107 T:1240986528  NOTICE: ['BCM',
                                             'BOARD',
                                             'BOTH',
                                             'FALLING',
                                             'HARD_PWM',
                                             'HIGH',
                                             'I2C',
                                             'IN',
                                             'LOW',
                                             'OUT',
                                             'PUD_DOWN',
                                             'PUD_OFF',
                                             'PUD_UP',
                                             'PWM',
                                             'RISING',
                                             'RPI_INFO',
                                             'RPI_REVISION',
                                             'SERIAL',
                                             'SPI',
                                             'UNKNOWN',
                                             'VERSION',
                                             '__builtins__',
                                             '__doc__',
                                             '__file__',
                                             '__name__',
                                             '__package__',
                                             '__path__',
                                             'add_event_callback',
                                             'add_event_detect',
                                             'cleanup',
                                             'event_detected',
                                             'getmode',
                                             'gpio_function',
                                             'input',
                                             'output',
                                             'remove_event_detect',
                                             'setmode',
                                             'setup',
                                             'setwarnings',
                                             'wait_for_edge']

Here is my new error:
ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.SyntaxError'>
Error Contents: ('invalid syntax', ('/storage/.kodi/addons/script.relay/addon.py', 6, 1, '<requires>\n'))
SyntaxError: ('invalid syntax', ('/storage/.kodi/addons/script.relay/addon.py', 6, 1, '<requires>\n'))
-->End of Python script error report<--
and code:
Code:
import xbmcaddon
#import xbmcgui
import os
import xbmc
import sys
<requires>
    <import addon="xbmc.python" version="2.19.0"/>
    <import addon="virtual.rpi-tools" /></requires>
# Grab a GPIO adapter for the current platform.
gpio = GPIO.get_platform_gpio

GPIO.setup(9, GPIO.OUT)

addon       = xbmcaddon.Addon()
addonname   = addon.getAddonInfo('name')

line1 = "Turned on/off a relay"
line2 = "Version: " + addon.getAddonInfo('version')

os.system("sh /storage/.kodi/addons/relay-addon-kodi-master/relay.sh")

xbmc.executebuiltin('Notification(Relay Addon, The relay was turned on/off,5000,//storage/.kodi/addons/script.relay.master/icon.png)')
#xbmcgui.Dialog().ok(addonname, line1, line2)

I did install Raspberry pi tools from the addon library located in Kodi
Reply
#47
The requires part in the angle brackets goes in the addon.xml file, not the python script.

And the path append I already have earlier in this thread, if you want to use it that way.
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#48
Thanks Roman. I was thinking about downloading LibreELEC so I could actually try and get it working myself.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#49
Here is the current settings:

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.relay" name="Relay" version="1.0.3" provider-name="Near Lg">
    <requires>
    <import addon="xbmc.python" version="2.19.0"/>
    <import addon="virtual.rpi-tools" />
</requires>
    <extension point="xbmc.python.script" library="addon.py">
        <provides>executable</provides>
    </extension>
    <extension point="xbmc.addon.metadata">
        <platform>all</platform>
        <summary lang="en">Relay addon</summary>
        <description lang="en">Addon to turn on/off a relay</description>
        <license>GNU General Public License, v2</license>
        <language></language>
        <forum>http://forum.kodi.tv/showthread.php?tid=209948</forum>
        <source>https://github.com/zag2me/script.hello.world</source>
        <email>[email protected]</email>
    </extension>
</addon>

Thats the XML^

Here is the Python script:
Code:
import xbmcaddon
#import xbmcgui
import os
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/python.RPi.GPIO/lib')

import RPi.GPIO as GPIO
gpio = GPIO.get_platform_gpio

GPIO.setup(9, GPIO.OUT)

addon       = xbmcaddon.Addon()
addonname   = addon.getAddonInfo('name')

line1 = "Turned on/off a relay"
line2 = "Version: " + addon.getAddonInfo('version')

os.system("sh /storage/.kodi/addons/relay-addon-kodi-master/relay.sh")

xbmc.executebuiltin('Notification(Relay Addon, The relay was turned on/off,5000,//storage/.kodi/addons/script.relay.master/icon.png)')
#xbmcgui.Dialog().ok(addonname, line1, line2)

And here is my error log:

https://pastebin.com/HRCYQCri
Reply
#50
Wink 
Some progress! Except the voltage is not changing. Im reading 0.00VDC instead of the +3.14vdc I need. The addon does not log any errors so I think its a matter of telling the pins to turn on HI or something. Here is the script:


Code:
import xbmcaddon
#import xbmcgui
import os
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.OUT)

addon       = xbmcaddon.Addon()
addonname   = addon.getAddonInfo('name')

line1 = "Turned on/off a relay"
line2 = "Version: " + addon.getAddonInfo('version')

os.system("sh /storage/.kodi/addons/relay-addon-kodi-master/relay.sh")

xbmc.executebuiltin('Notification(Relay Addon, The relay was turned on/off,5000,//storage/.kodi/addons/script.relay.master/icon.png)')
#xbmcgui.Dialog().ok(addonname, line1, line2)

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.relay" name="Relay" version="1.0.3" provider-name="Near Lg">
    <requires>
    <import addon="xbmc.python" version="2.19.0"/>
    <import addon="virtual.rpi-tools" />
</requires>
    <extension point="xbmc.python.script" library="addon.py">
        <provides>executable</provides>
    </extension>
    <extension point="xbmc.addon.metadata">
        <platform>all</platform>
        <summary lang="en">Relay addon</summary>
        <description lang="en">Addon to turn on/off a relay</description>
        <license>GNU General Public License, v2</license>
        <language></language>
        <forum>http://forum.kodi.tv/showthread.php?tid=209948</forum>
        <source>https://github.com/zag2me/script.hello.world</source>
        <email>[email protected]</email>
    </extension>
</addon>
Reply
#51
I'd get rid of the os.system line of that script is just meant to do stuff with the pins. Use the GPIO module.

If you've got no errors now then it sounds like the module is importing ok. Your script just sets up pin 18 as an output but doesn't set it high. Add this line to do so:
Code:
GPIO.output(18, GPIO.HIGH)
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#52
(2017-06-08, 09:00)el_Paraguayo Wrote: I'd get rid of the os.system line of that script is just meant to do stuff with the pins. Use the GPIO module.

If you've got no errors now then it sounds like the module is importing ok. Your script just sets up pin 18 as an output but doesn't set it high. Add this line to do so:
Code:
GPIO.output(18, GPIO.HIGH)

Awesome! I got 3.30v however, when I click the addon again, it does not turn off
Reply
#53
You haven't told it to do so, have you?

The command to turn the pin low is of course:

Code:
GPIO.output(18, GPIO.LOW)

but you'll need to expand your script to either take an input from somewhere as to whether you're turning it high or low, or check its current status (either directly or using a script variable) and switch it the other way.
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#54
You could do something like this:
Code:
import xbmcgui
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

import RPi.GPIO as GPIO

# Set pin number once
RELAY_PIN = 18

GPIO.setmode(GPIO.BCM)

GPIO.setup(RELAY_PIN, GPIO.OUT)

# read the current state of the pin
current_state = GPIO.input(RELAY_PIN)

if current_state == GPIO.HIGH:

    # Pin is on so we should turn off
    GPIO.output(RELAY_PIN, GPIO.LOW)
    state = "off"

else:

    # Pin is off so we should turn on
    GPIO.output(RELAY_PIN, GPIO.HIGH)
    state = "on"

# Send notification
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
It's more verbose than it needs to be but it should help you understand what's going on.

Edit: I'd strongly recommend that you also look at the RPi.GPIO documentation.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#55
(2017-06-08, 15:20)el_Paraguayo Wrote: You could do something like this:
Code:
import xbmcgui
import xbmc
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

import RPi.GPIO as GPIO

# Set pin number once
RELAY_PIN = 18

GPIO.setmode(GPIO.BCM)

GPIO.setup(RELAY_PIN, GPIO.OUT)

# read the current state of the pin
current_state = GPIO.input(RELAY_PIN)

if current_state == GPIO.HIGH:

    # Pin is on so we should turn off
    GPIO.output(RELAY_PIN, GPIO.LOW)
    state = "off"

else:

    # Pin is off so we should turn on
    GPIO.output(RELAY_PIN, GPIO.HIGH)
    state = "on"

# Send notification
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
It's more verbose than it needs to be but it should help you understand what's going on.

Edit: I'd strongly recommend that you also look at the RPi.GPIO documentation.

Awesome! This worked! I had to delete the:
Code:
# Send notification
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
but now I just need to get a message if its on or off.

My next question is how can I duplicate this 4 more times? DO I just copy the add-on folder and change the pinouts?
Reply
#56
(2017-06-08, 15:35)ablues10 Wrote: Awesome! This worked! I had to delete the:
Code:
# Send notification
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
but now I just need to get a message if its on or off.

My next question is how can I duplicate this 4 more times? DO I just copy the add-on folder and change the pinouts?

Why did you delete it? That line is to give you a message if it's on or off, just like you wanted.

As for your second question, there are a number of ways of doing this but I wouldn't copy the addon!

You could:
- Let your addon accept arguments when you run it (this could be done binding commands to spare keys on your keyboard or remote);
- You could have a list of relays to select from when you run the script. Two ways of doing this:
- Use a select dialog; or
- Use something like pyXBMCt to create a menu.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#57
I got this error:
Quote:ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.NameError'>
Error Contents: name 'xbmcgui' is not defined
Traceback (most recent call last):
File "/storage/.kodi/addons/script.relay/addon.py", line 33, in <module>
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
NameError: name 'xbmcgui' is not defined
-->End of Python script error report<--
(2017-06-08, 15:56)el_Paraguayo Wrote:
(2017-06-08, 15:35)ablues10 Wrote: Awesome! This worked! I had to delete the:
Code:
# Send notification
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
but now I just need to get a message if its on or off.

My next question is how can I duplicate this 4 more times? DO I just copy the add-on folder and change the pinouts?

Why did you delete it? That line is to give you a message if it's on or off, just like you wanted.

As for your second question, there are a number of ways of doing this but I wouldn't copy the addon!

You could:
- Let your addon accept arguments when you run it (this could be done binding commands to spare keys on your keyboard or remote);
- You could have a list of relays to select from when you run the script. Two ways of doing this:
- Use a select dialog; or
- Use something like pyXBMCt to create a menu.

So I would like to use pyXBMCt. Is there a step by step on how to start this from scratch? Would I just make more addon.py modules so pyXBMCt can control them?
Reply
#58
Quote:ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.NameError'>
Error Contents: name 'xbmcgui' is not defined
Traceback (most recent call last):
File "/storage/.kodi/addons/script.relay/addon.py", line 33, in <module>
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
NameError: name 'xbmcgui' is not defined
-->End of Python script error report<--
Did you remove the # from the import xbmcgui line like I had in the code I posted?
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#59
Quote:So I would like to use pyXBMCt. Is there a step by step on how to start this from scratch? Would I just make more addon.py modules so pyXBMCt can control them?
Everything you need should be here: https://forum.kodi.tv/showthread.php?tid=174859

No, you don't need more addon.py modules. You can have one script that creates a menu and then performs different actions depending on which menu items you select.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#60
(2017-06-08, 16:32)el_Paraguayo Wrote:
Quote:ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.NameError'>
Error Contents: name 'xbmcgui' is not defined
Traceback (most recent call last):
File "/storage/.kodi/addons/script.relay/addon.py", line 33, in <module>
xbmcgui.Dialog().notification("Relay", "Relay is now {0}!".format(state))
NameError: name 'xbmcgui' is not defined
-->End of Python script error report<--
Did you remove the # from the import xbmcgui line like I had in the code I posted?
You are F'N amazing! It works flawlessly!
Reply

Logout Mark Read Team Forum Stats Members Help
Hello, help me Im dumb. Addon to control relays0