Weird 8 relay hat results on a Raspberry Pi 4b

gwfami

New member
I have a RPi 4b which has a sequent microsystems 8 relay hat and on top of that it has lcd touchscreen. I just connected it to an Adafruit Feather RP2040 using a USB bus. The feather is monitoring an encoder so the Pi doesn't miss counts. Got the encoder working just fine, but now I have a weird issue popping up.
The relay can be ran either from code or the command line. Using python, I have coded the relays numerically, doing things in the order needed.

I have ran various code using all of the relays before connecting the Feather, and everything worked perfectly.

Now anything coded for the relay acts very strange.

So I reinstalled the lib8relind library, and it still is goofy.

Here's what it does.

From the command line, I test all the relays:
8relind 0 test
Everything works

Using the command line, I test the individual relays using: (x is the relay number between 1 and 8)
8relind 0 write x on and 8relind 0 write x off
Each one is working fine.

Here's a simple python script that only looks at one relay at a time.
#!/usr/bin/env python3 # Make sure you "chmod +x main.py" or else it won't start by itself from the command line. import os import lib8relind from getkey import getkey relay = 0 def test(): lib8relind.set(0, relay, 1) def shutdown(): # make sure all the relay is off lib8relind.set(0, relay, 0) # now shut the program off exit(0) # Runs the program print("TEST") print(" ") print("Enter a relay # (1-8)") print(" ") print("<any other key will end the program>") a = "" while True: # Breaks when key is pressed key = getkey() print(key) break if int(key) in range(1, 8): try: while True: relay = int(key) test() except KeyboardInterrupt: lib8relind.set(0, relay, 0) shutdown() else: shutdown()

Can't get much simpler then that.
When ran, the code returns this, pretty simple.

b2400@b2400:~/Desktop/b2400 $ python test6.py TEST Enter a relay # (1-8) <any other key will end the program> 1

Here's where it gets really strange when using the python code shown above. These results were verified from both the "relay active" blue light and from the relays themselves.

Relay entered Relay activated
1..............................1
2..............................7
3..............................2
4.............................. 7
5..............................4
6..............................5
7..............................3
8..............................Error (below)
Traceback (most recent call last): File "/home/b2400/Desktop/b2400/test6.py", line 46, in <module> shutdown() File "/home/b2400/Desktop/b2400/test6.py", line 16, in shutdown lib8relind.set(0, relay, 0) File "/home/b2400/lib/python3.11/site-packages/lib8relind/__init__.py", line 47, in set raise ValueError('Invalid relay number!') ValueError: Invalid relay number!

Now if I change the python code from
lib8relind.set(0, relay, 1)
to
os.system("8relind 0 write 1 on")
(changing the "1" to the relay #) the python works fine.

Anyone have any ideas?
 

alexburcea

Moderator
Staff member
I think your problem is the conversion of the key value, please instead "print(key)" to use "print(int(key))" which will tell you the actual value used for the relay number.
 

gwfami

New member
I think your problem is the conversion of the key value, please instead "print(key)" to use "print(int(key))" which will tell you the actual value used for the relay number.
Could be, I'll try it later tonight or tomorrow. Not quite sure how that would affect the issue as I had originally had each relay coded like this and it still caused errors.
# relay hat definitions actuator_relay = 7 brake_relay = 8 eject_relay = 2 collect_motor_relay = 4 down_relay = 5 up_relay = 6 relay_on = 1 relay_off = 0
 

gwfami

New member
The issue isn't with the input of the key value. I just boiled down the code to activate only relay #4, which when ran always activates relay #7.
#!/usr/bin/env python3 import lib8relind try: while True: lib8relind.set(0, 4, 1) except KeyboardInterrupt: lib8relind.set(0, 4, 0) exit(0)
 

gwfami

New member
But this works.

#!/usr/bin/env python3 import lib8relind import os try: while True: os.system("8relind 0 write 4 on") except KeyboardInterrupt: os.system("8relind 0 write 4 off") exit(0)
 

gwfami

New member
Just noticed I forgot to include the Pi os version.

Operating System: Debian GNU/Linux 12 (bookworm)
Kernel: Linux 6.6.47+rpt-rpi-v8
Architecture: arm64
 

gwfami

New member
I am able to call the cli for almost everything that I need, but code like this.

Python:
if lib8relind.get(0, actuator_relay) == relay_off:
                lib8relind.set(0, actuator_relay, relay_on)

However, I finally found an exact match to what I'm experiencing. Same Problem.

I tried the instructions you gave for that problem ("Uninstall SM8relind and lib8relind in thonny. Then reinstall SMrelind."). But it is causing me some problems.

1. I uninstall SM8relind and lib8relind from the command line, not sure why you use thonny, I didn't use it to install them.
2. I can still run 8relind from the command line after restart.

It seems that something isn't getting uninstalled as 8relind can still be ran after deleting them from the system.

Any ideas?
 

alexburcea

Moderator
Staff member
The 8relind is a program that has been copied to the system folder at the install and has nothing to do with the python. If you need to remove this program you have to go to the "8relind-rpi" folder and run "sudo make uninstall"
I do not know anything about Thonny, never worked with it, could you be more specific about that?

Alex.
 

gwfami

New member
Ok, I have done the sudo make uninstall and then sudo make install.

I don't use Thonny, but apparently Thonny has its own package installer (Tools -> Manage Packages) for adding python modules. I prefer to do things from the command line.

I was able to get the cli to return what I wanted using this code, so that's no longer an issue.

Python:
            result = subprocess.run(["8relind", "0", "read", str(actuator_relay)], capture_output=True, text=True)
            if int(result.stdout) == relay_off:

I converted all of the code to use the os.system command for turning the relays on and off and that works. I'll have to make a copy of the code and reconvert it all back to the lib8relind commands to see if "sudo make uninstall" and "sudo make install" worked.

Thanks.
 

alexburcea

Moderator
Staff member
Ok, let's reset here, have you done the instructions from the readme and still not able to run lib8relind ?
If this is the case, please go to the python folder of the "8relind-rpi" (in the terminal run "cd 8relind/python/") and install it manually by running "sudo python setup.py install"
Let me know if after that you are able to import the library.
 

gwfami

New member
"Ok, let's reset here, have you done the instructions from the readme..."
- Yes, I have followed the readme instructions.

"...and still not able to run lib8relind ?"
- I can load and execute the lib8relind library, but it doesn't do what it is supposed too. I wrote a simple piece of code to activate/deactivate each relay in order.
Python:
#!/usr/bin/env python3
import time

import lib8relind
from getkey import getkey

relay = 0
relay_on = 1
relay_off = 0

def shutdown():
    exit(0)

print("TEST")
print("1 to start ")
print("<any other key will end the program>")

a = ""
while a == "":  # Breaks when key is pressed
    key = getkey()
    print(key)
    break

try:
    while True:
        relay += 1
        if relay > 8:
            relay = 1
        print("Testing relay: " + str(relay))
        lib8relind.set(0, relay, relay_on)
        time.sleep(1)
        lib8relind.set(0, relay, relay_off)
except KeyboardInterrupt:
    lib8relind.set(0, relay, relay_off)
    print("Stopped")
    shutdown()

Here's a video of what happens with the board when the above code is ran.


"If this is the case, please go to the python folder of the "8relind-rpi" (in the terminal run "cd 8relind/python/") and install it manually by running "sudo python setup.py install""

- There is no 8relind directory, just an 8relind-rpi directory.
Code:
b2400@b2400:~ $ ls
b2400@b2400:~ $ ls -l
total 4484
drwxr-xr-x  8 b2400 b2400    4096 Oct 22 10:42 8relind-rpi
drwxr-xr-x  3 b2400 b2400    4096 Sep 26 16:15 bin
drwxr-xr-x  2 b2400 b2400    4096 May 20 11:07 Bookshelf
drwxr-xr-x  3 b2400 b2400    4096 Oct  9 11:29 Desktop
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Documents
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Downloads
-rwxr-xr-x  1 b2400 b2400    2406 Aug 11  2014 gpiotest
-rw-r--r--  1 b2400 b2400    1012 Oct 18  2017 gpiotest.zip
drwxr-xr-x  2 b2400 b2400    4096 Mar 12  2024 img_backup
drwxr-xr-x  3 b2400 b2400    4096 Jan  3  2024 include
drwxr-xr-x  3 b2400 b2400    4096 Dec 23  2023 lib
lrwxrwxrwx  1 b2400 b2400       3 Dec 23  2023 lib64 -> lib
-rw-r--r--  1 b2400 b2400 2993705 Jan  2  2024 master.zip
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Music
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Pictures
drwxr-xr-x 10 b2400 b2400    4096 Jan  2  2024 pigpio-master
drwxr-xr-x  2 b2400 b2400    4096 Jan  2  2024 PISCOPE
-rw-r--r--  1 b2400 b2400  419840 Apr 14  2020 piscope.tar
-rw-r--r--  1 b2400 b2400  419840 Apr 14  2020 piscope.tar.1
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Public
-rw-r--r--  1 b2400 b2400     150 Dec 23  2023 pyvenv.cfg
drwxr-xr-x  4 b2400 b2400    4096 Mar 12  2024 RonR-RPi-image-utils
drwxr-xr-x  3 b2400 b2400    4096 Feb 13  2024 share
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Templates
-rw-r--r--  1 b2400 b2400     452 Sep 30 17:40 test.py
drwxr-xr-x  5 b2400 b2400    4096 Dec 23  2023 thonnyvenv
drwxr-xr-x  2 b2400 b2400    4096 Dec 23  2023 Videos
drwx------  3 b2400 b2400    4096 Mar 14  2024 vnc_bk
-rwxr-xr-x  1 b2400 b2400  647400 Mar 13  2024 vncpasswd
-rw-r--r--  1 b2400 b2400    1701 Mar 13  2024 vncpasswd.1.gz

Inside the 8relind-rpi is:
Code:
/8relind-rpi $ ls -lart
total 1776
-rw-r--r--  1 b2400 b2400    1478 Oct 22 10:42 README.md
-rw-r--r--  1 b2400 b2400     960 Oct 22 10:42 Makefile
-rw-r--r--  1 b2400 b2400    1077 Oct 22 10:42 LICENSE
-rw-r--r--  1 b2400 b2400     508 Oct 22 10:42 .gitignore
-rw-r--r--  1 b2400 b2400 1739325 Oct 22 10:42 Sequent-3D-Enclosure.zip
drwxr-xr-x  3 b2400 b2400    4096 Oct 22 10:42 node-red-contrib-sm-8relind
drwxr-xr-x  2 b2400 b2400    4096 Oct 22 10:42 readmeres
drwxr-xr-x  4 b2400 b2400    4096 Oct 22 10:42 python
drwxr-xr-x  3 b2400 b2400    4096 Oct 22 10:42 node-red
drwxr-xr-x  8 b2400 b2400    4096 Oct 22 10:42 .git
drwxr-xr-x  2 b2400 b2400    4096 Oct 22 10:42 src
drwxr-xr-x  8 b2400 b2400    4096 Oct 22 10:42 .
-rwxr-xr-x  1 root  root    73392 Oct 22 10:42 8relind
drwx------ 30 b2400 b2400    4096 Oct 28 07:38 ..

Inside the python is:
Code:
drwxr-xr-x 2 b2400 b2400 4096 Oct 22 10:42 lib8relind
-rw-r--r-- 1 b2400 b2400 1086 Oct 22 10:42 LICENSE
-rw-r--r-- 1 b2400 b2400  303 Oct 22 10:42 Makefile
-rw-r--r-- 1 b2400 b2400 1561 Oct 22 10:42 README.md
drwxr-xr-x 2 b2400 b2400 4096 Oct 22 10:42 res
-rw-r--r-- 1 b2400 b2400   26 Oct 22 10:42 setup.cfg
-rw-r--r-- 1 b2400 b2400 1116 Oct 22 10:42 setup.py

Running the "sudo python setup.py install" command gives:
Code:
b2400@b2400:~/8relind-rpi/python $ sudo python setup.py install
running install
/usr/lib/python3/dist-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
/usr/lib/python3/dist-packages/setuptools/command/easy_install.py:146: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
running bdist_egg
running egg_info
creating SM8relind.egg-info
writing SM8relind.egg-info/PKG-INFO
writing dependency_links to SM8relind.egg-info/dependency_links.txt
writing requirements to SM8relind.egg-info/requires.txt
writing top-level names to SM8relind.egg-info/top_level.txt
writing manifest file 'SM8relind.egg-info/SOURCES.txt'
reading manifest file 'SM8relind.egg-info/SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'SM8relind.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-aarch64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/lib8relind
copying lib8relind/__init__.py -> build/lib/lib8relind
creating build/bdist.linux-aarch64
creating build/bdist.linux-aarch64/egg
creating build/bdist.linux-aarch64/egg/lib8relind
copying build/lib/lib8relind/__init__.py -> build/bdist.linux-aarch64/egg/lib8relind
byte-compiling build/bdist.linux-aarch64/egg/lib8relind/__init__.py to __init__.cpython-311.pyc
creating build/bdist.linux-aarch64/egg/EGG-INFO
copying SM8relind.egg-info/PKG-INFO -> build/bdist.linux-aarch64/egg/EGG-INFO
copying SM8relind.egg-info/SOURCES.txt -> build/bdist.linux-aarch64/egg/EGG-INFO
copying SM8relind.egg-info/dependency_links.txt -> build/bdist.linux-aarch64/egg/EGG-INFO
copying SM8relind.egg-info/requires.txt -> build/bdist.linux-aarch64/egg/EGG-INFO
copying SM8relind.egg-info/top_level.txt -> build/bdist.linux-aarch64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/SM8relind-1.0.3-py3.11.egg' and adding 'build/bdist.linux-aarch64/egg' to it
removing 'build/bdist.linux-aarch64/egg' (and everything under it)
Processing SM8relind-1.0.3-py3.11.egg
Removing /usr/local/lib/python3.11/dist-packages/SM8relind-1.0.3-py3.11.egg
Copying SM8relind-1.0.3-py3.11.egg to /usr/local/lib/python3.11/dist-packages
SM8relind 1.0.3 is already the active version in easy-install.pth

Installed /usr/local/lib/python3.11/dist-packages/SM8relind-1.0.3-py3.11.egg
Processing dependencies for SM8relind==1.0.3
Searching for smbus2==0.4.2
Best match: smbus2 0.4.2
Adding smbus2 0.4.2 to easy-install.pth file

Using /usr/lib/python3/dist-packages
Finished processing dependencies for SM8relind==1.0.3


After this, I reran the simple python code from above, and the results are exactly the same, with relay 1 = relay 1, but the other relays are not being put to the correct relay number (see movie above).
 

alexburcea

Moderator
Staff member
But you told me that you are not able to import the library which clearly is not the case.
Could you send me a picture of your card?
 

gwfami

New member
I'll send a picture, but I never said that I couldn't import the library. Here's what I said in the first post

Code:
"Here's where it gets really strange when using the python code shown above.  These results were verified from both the "relay active" blue light and from the relays themselves.


Relay entered          Relay activated

1..............................1

2..............................7

3..............................2

4.............................. 7

5..............................4

6..............................5

7..............................3

8..............................Error (below)

Traceback (most recent call last):  File "/home/b2400/Desktop/b2400/test6.py", line 46, in <module>    shutdown()  File "/home/b2400/Desktop/b2400/test6.py", line 16, in shutdown    lib8relind.set(0, relay, 0)  File "/home/b2400/lib/python3.11/site-packages/lib8relind/__init__.py", line 47, in set    raise ValueError('Invalid relay number!') ValueError: Invalid relay number!



Now if I change the python code from

lib8relind.set(0, relay, 1)

to

os.system("8relind 0 write 1 on")

(changing the "1" to the relay #) the python works fine.

Which seems to be an exact match with a problem reported to you earlier. Same Problem.

As you requested, here's a picture of the card.

20241028_134230.jpg
 

3lv

New member
Hi everyone!
For me, gwfami's python code is working fine.
Could you try the following script and share your full output?

```python
Python:
#!/usr/bin/env python3

import time
import lib8relind
print(lib8relind.__file__) # Check if lib8relind is the correct one
from getkey import getkey

relay = 0
relay_on = 1
relay_off = 0

def shutdown():
    exit(0)

print("TEST")
print("1 to start ")
print("<any other key will end the program>")

a = ""
while a == "":  # Breaks when key is pressed
    key = getkey()
    print(key)
    break

try:
    while True:
        relay += 1
        if relay > 8:
            #relay = 1
            break # No need for more than one cycle
        print("Testing relay: " + str(relay))
        lib8relind.set(0, relay, relay_on)
        print("DEBUG: ", relay, lib8relind.get(0, relay), lib8relind.get_all(0)) # Debug print
        time.sleep(1)
        lib8relind.set(0, relay, relay_off)

except KeyboardInterrupt:
    lib8relind.set(0, relay, relay_off)
    print("Stopped")
    shutdown()

```
 

alexburcea

Moderator
Staff member
Hi,

Sorry, I mixed things up. So the only problem is that the Python library does not turn the right relay even the CLI does?
 
Top