I'm using two Arduino Megas in a project to read data from a bunch of analog sensors and then output PWMs on digital pins according to their value. These analog signals are also being plotted on a dash app.
I'm using pyfirmata, and the program runs perfectly with either one of the two boards connected. However, when I connect both boards, The second board (second in terms of its
pyfirmata.util.Iterator object being instantiated second), will always report
nonetype objects when calling
I found essentially the exact same problem here
Putting the iterators on separate processes fixed the nonetype issue for me too. However I want to know why this would fix things, because I want to be confident there isn't a threading-based solution to the nonetype problem before I start reprogramming everything to work with processes.
My instinct tells me it has to do with this code:
class Iterator(threading.Thread): def __init__(self, board): super(Iterator, self).__init__() self.board = board self.daemon = True def run(self): while 1: try: while self.board.bytes_available(): self.board.iterate() time.sleep(0.001) except (AttributeError, serial.SerialException, OSError): # this way we can kill the thread by setting the board object # to None, or when the serial port is closed by board.exit() break except Exception as e: # catch 'error: Bad file descriptor' # iterate may be called while the serial port is being closed, # causing an "error: (9, 'Bad file descriptor')" if getattr(e, "errno", None) == 9: break try: if e == 9: break except (TypeError, IndexError): pass raise except (KeyboardInterrupt): sys.exit()
found on the pyfirmata github repo here
I've been searching around for days, but still have no clue why this would cause whichever board is tied to the second instance of the iterator object to only give nonetype objects as analog reads. It's not a serial communications problem, because
some_digital_pin.write(pwm) works for both boards when both plugged in.
Any help or insight into this issue would be greatly appreciated, as well as tips on how to bypass this issue to allow both boards to read without using multiprocessing.