Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when used with MCP3008 #605

Open
dpritt opened this issue Nov 19, 2017 · 10 comments
Open

Error when used with MCP3008 #605

dpritt opened this issue Nov 19, 2017 · 10 comments
Assignees
Labels

Comments

@dpritt
Copy link

dpritt commented Nov 19, 2017

Ben,

I have a couple of PIs with MCP3008 analogue devices, both are throwing the following exception.
Python 3.5 running on Stretch.

Exception ignored in: <bound method GPIOBase.__del__ of <gpiozero.MCP3008 object using <weakproxy at 0xb3b35d20 to NoneType at 0x3e5788>>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 37, in close
    if self._spi:
ReferenceError: weakly-referenced object no longer exists

Any suggestions on how to stop this?

Regards

Dave

@bennuttall
Copy link
Member

Can you post the code you're using? (please use fenced code blocks)

I have seen other people report of SPI not working on Stretch but I can't reproduce. The following works fine for me:

from gpiozero import MCP3008

pot = MCP3008()

while True:
    print(pot.value)

@dpritt
Copy link
Author

dpritt commented Nov 26, 2017 via email

@gkovacs81
Copy link

gkovacs81 commented Jan 10, 2018

I have a similar problem in my project. When the application stops I get three exceptions:

Exception ignored in: <bound method MCP3008.__del__ of <gpiozero.MCP3008 object using <weakproxy at 0xb55e5900 to LocalPiSoftwareSPIShared at 0xb5541770>>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 38, in close
    self._spi.close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
    old_close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 175, in close
    super(LocalPiSoftwareSPI, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
    super(SourceMixin, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 393, in close
    self._pin.close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
    GPIO.cleanup(self.number)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Exception ignored in: <bound method MCP3008.__del__ of <gpiozero.MCP3008 object using SPI(closed)>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 38, in close
    self._spi.close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
    old_close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 175, in close
    super(LocalPiSoftwareSPI, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
    super(SourceMixin, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 393, in close
    self._pin.close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
    GPIO.cleanup(self.number)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Exception ignored in: <bound method LocalPiSoftwareSPIShared.__del__ of SPI(closed)>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 137, in __del__
    super(SharedMixin, self).__del__()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
    old_close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 175, in close
    super(LocalPiSoftwareSPI, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
    super(SourceMixin, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 393, in close
    self._pin.close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
    GPIO.cleanup(self.number)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

I wrote a basic application to reproduce the problem:

mcp_adapter.py:

from gpiozero import MCP3008
class Adapter(object):

    def __init__(self):
        self.pot = MCP3008()
        self.p2 = MCP3008()
        self.p3 = MCP3008()

    def get_value(self):
        return self.pot.value

monitor.py:

from time import sleep
from threading import Thread
from mcp_adapter import Adapter

class Monitor(Thread):

    def __init__(self, stop):
        super(Monitor, self).__init__()
        self.a = Adapter()
        self.st = stop

    def run(self):
        while self.st.is_set():
            print(self.a.get_value())
            sleep(1)

worker.py:

from monitor import Monitor
from threading import Event

stop = Event()
m = Monitor(stop)
m.start()

while True:
    try:
        print("test-")
    except KeyboardInterrupt:
        print("exit")
m.join()

sys.exit(1)

If it is less complex the exception doesn't appear.

Environment:
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 8.0 (jessie)
Release: 8.0
Codename: jessie

Package: python-gpiozero
Source: gpiozero
Version: 1.4.0

@gkovacs81
Copy link

gkovacs81 commented Sep 22, 2018

gpiozero==1.4.1

from gpiozero import MCP3008

m = MCP3008(channel=3, clock_pin=21, mosi_pin=16, miso_pin=20, select_pin=1)
print(m.value)
m = MCP3008(channel=2, clock_pin=21, mosi_pin=16, miso_pin=20, select_pin=1)
print(m.value)

0.0004885197850512668
0.0004885197850512668
Exception ignored in: <bound method GPIOBase.__del__ of <gpiozero.MCP3008 object using <weakproxy at 0xb6180660 to NoneType at 0x3e5788>>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 37, in close
ReferenceError: weakly-referenced object no longer exists

@bennuttall
Copy link
Member

What's happening is you're creating a reference to an SPI object, and then throwing it away by creating another with the same name. They're supposed to be connected, as they use a shared bus. But if you throw one away you have problems.

You need to use separate variable names (e.g. m1, m2):

m1 = MCP3008(channel=3, clock_pin=21, mosi_pin=16, miso_pin=20, select_pin=1)
m2 = MCP3008(channel=2, clock_pin=21, mosi_pin=16, miso_pin=20, select_pin=1)

while True:
    print(m1.value, m2.value)

@gkovacs81
Copy link

gkovacs81 commented Sep 23, 2018

Thanks for the answer!
Good idea, but unfortunately it's just an example to reproduce the problem.
Let's take another case without named variables.

from gpiozero import MCP3008

sensors = []
for i in range(0,6):
    sensors.append(MCP3008(channel=3, clock_pin=21, mosi_pin=16, miso_pin=20, select_pin=1))

for s in sensors:
    print(s.value)
0.5007327796775769
0.5007327796775769
0.5007327796775769
0.49975574010747437
0.5007327796775769
0.5007327796775769
Exception ignored in: <bound method GPIOBase.__del__ of <gpiozero.MCP3008 object using <weakproxy at 0xb61cd360 to LocalPiSoftwareSPIShared at 0xb60ae7f0>>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 38, in close
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 173, in close
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 394, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Exception ignored in: <bound method GPIOBase.__del__ of <gpiozero.MCP3008 object using SPI(closed)>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 38, in close
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 173, in close
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 394, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Exception ignored in: <bound method SharedMixin.__del__ of SPI(closed)>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 137, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 173, in close
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 394, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

@gkovacs81
Copy link

gkovacs81 commented Sep 23, 2018

OK, another mistake not using the variable 'i'!

from gpiozero import MCP3008

sensors = []
for i in range(0,6):
    sensors.append(MCP3008(channel=i, clock_pin=21, mosi_pin=16, miso_pin=20, select_pin=1))

for s in sensors:
    print(s.value)
0.0004885197850512668
0.002442598925256556
0.0014655593551538004
0.5007327796775769
0.002442598925256556
0.0014655593551538004
Exception ignored in: <bound method GPIOBase.__del__ of <gpiozero.MCP3008 object using SPI(closed)>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 38, in close
    self._spi.close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
    old_close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 173, in close
    super(LocalPiSoftwareSPI, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
    super(SourceMixin, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 394, in close
    self._pin.close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
    GPIO.cleanup(self.number)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Exception ignored in: <bound method GPIOBase.__del__ of <gpiozero.MCP3008 object using <weakproxy at 0xb6141360 to LocalPiSoftwareSPIShared at 0xb6022810>>>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/spi_devices.py", line 38, in close
    self._spi.close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
    old_close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 173, in close
    super(LocalPiSoftwareSPI, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
    super(SourceMixin, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 394, in close
    self._pin.close()
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
    GPIO.cleanup(self.number)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Exception ignored in: <bound method SharedMixin.__del__ of SPI(closed)>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 137, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 82, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/local.py", line 173, in close
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 77, in close
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 394, in close
  File "/usr/lib/python3/dist-packages/gpiozero/pins/rpigpio.py", line 106, in close
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

@retroriff
Copy link

retroriff commented Jan 29, 2019

I have the same issue when I stop a script which reads multiple MCP3008 devices. It only gives the error when using non SPI pins, for example:

for i in range(4): 
    device1[i] = MCP3008(channel=i, clock_pin=21, mosi_pin=20, miso_pin=19, select_pin=7)

It works using default SPI pins (gpio8, gpio9, gpio10, gpio11)

device1[i] = MCP3008(i)

@lurch
Copy link
Contributor

lurch commented Feb 11, 2019

Re-opening and assigning to @waveform80 , as he wrote all the SoftwareSPI stuff 🙂

@waveform80
Copy link
Member

Yup, looks like there's definitely something iffy in the life-span of stuff using SPISoftwareBus (which doesn't surprise me too much - I vaguely recall there's some very quick hacks around the pin reservation stuff in there - probably time it got sorted out "properly"). Useful to know that it's not occurring with the hardware-driven (spidev) implementation though.

I'll dig into this...

@bennuttall bennuttall added bug and removed question labels Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants