2 mote hubs, 1 pi

Does anyone have a code sample of how to instantiate the MOTE() class in Python for more than one hub on the same machine? I can see them both when i query the usb ports. I’m a newbie with these devices and need the documentation to see if there is an overload for instantiating a new mote

ie:

can i do: Mote1 = Mote()
Mote2 = Mote()
?
TIA!!

Found my own answer!

http://docs.pimoroni.com/mote/#module-mote

have to ref the USB port in full /dev/tty, then i can have 2 instances of Mote at the same time.

This does make me wonder if I can make it smart enough to automatically find the next unused USB port when you use:

mote1 =  Mote()
mote2 = Mote()

Since I suspect the hard-coded paths could be unset by just plugging in another USB device.

ooo. a discovery routine. I like that idea, because yes, the hard coded ports WILL change if you plug something else in, or even unplug and replug the MOte in.

i think i can build something that can :scan the ports, return a list of motes on the ports, and use that list to dynamically instantiate the hubs. For myself, it would have to be the first two hubs found, but such a thing could be expanded N hubs.

Import time
from mote import Mote

import serial.tools.list_ports
list = serial.tools.list_ports.comports()

#connected = []

#Find the MOTE Hubs! This machine has 2 installed
var1 = 1
for element in list:
if ‘Mote’ in element.description:
if var1 == 1:
mote1 = Mote(port_name=element.device)
var1 = 2
elif var1 == 2:
mote2 = Mote(port_name=element.device)

#clear channels on both hubs
mote1.configure_channel(1, 16, False)
mote1.configure_channel(2, 16, False)
mote1.configure_channel(3, 16, False)
mote1.configure_channel(4, 16, False)

mote2.configure_channel(1, 16, False)
mote2.configure_channel(2, 16, False)
mote2.configure_channel(3, 16, False)
mote2.configure_channel(4, 16, False)

mote1.clear()
mote2.clear()

#make one blue and one green… so we see all 8 strips lit up on both.
for channel in range(1, 5):
for pixel in range(16):
mote1.set_pixel(channel, pixel, 0, 0, 255)
mote2.set_pixel(channel, pixel, 0, 255, 0)

mote1.show()
mote2.show()

#but only do this for 5 seconds
time.sleep(5)

#then turn off the demo
mote1.clear()
mote1.show()
mote2.clear()
mote2.show()

Lost all my indents… sorry… not sure how to paste code here.

You should use either 4 spaces before each line (which is a pain if you’re copying and pasting) or three backticks, like so:

```
# Your code goes here
```

Much better! This is cheerlights with 2 hubs on board, with dynamic port discovery

import time
from colorsys import hsv_to_rgb
from sys import exit

from mote import Mote


try:
    import requests
except ImportError:
    exit("This script requires the requests module\nInstall with: sudo pip install requests")


import serial.tools.list_ports
list = serial.tools.list_ports.comports()


#Find the MOTE Hubs!   This machine has 2 installed
var1 = 1
for element in list:
    if 'Mote' in element.description:
        if var1 == 1:
            mote1 = Mote(port_name=element.device)
            var1 = 2
        elif var1 == 2:
            mote2 = Mote(port_name=element.device)
 
#Configure the 16 LED's, clear channels on both hubs
mote1.configure_channel(1, 16, False)
mote1.configure_channel(2, 16, False)
mote1.configure_channel(3, 16, False)
mote1.configure_channel(4, 16, False)

mote2.configure_channel(1, 16, False)
mote2.configure_channel(2, 16, False)
mote2.configure_channel(3, 16, False)
mote2.configure_channel(4, 16, False)


mote1.clear()
mote2.clear()

try:
    while True:
        r = requests.get('http://api.thingspeak.com/channels/1417/feed.json')
        j = r.json()
        f = j['feeds'][-8:]

        f = [element for index, element in enumerate(f) if index%2==0]

        print(f)

        channel = 1
        for col in f:
            col = col['field2']
            ## Older versions of Python may need this line
            ##r, g, b = tuple(ord(c) for c in col[1:].lower().decode('hex'))
            r, g, b = tuple(c for c in bytes.fromhex(col[1:]))
            for pixel in range(mote1.get_pixel_count(channel)):
                mote1.set_pixel(channel, pixel, r, g, b)
                mote2.set_pixel(channel, pixel, r, g, b)
            channel += 1        

        mote1.show()
        mote2.show()

        time.sleep(5)

except KeyboardInterrupt:
    mote1.clear()
    mote2.clear()
    
    mote1.show()
    mote2.show()
    time.sleep(0.1)

1 Like

Nice work! I might have made it extensible to any number of motes and approached it like:

motes = []

for port in serial.tools.list_ports.comports():
    if 'Mote' in comport.description:
        motes.append(Mote(port_name=comport.device))

motes[0]...

motes[1]...

You can do it with list comprehension, but that’s generally an ugly unreadable mess:

motes = [Mote(port_name=comport.device for comport in serial.tools.list_ports.comports() if 'Mote' in comport.description]

This lets you do bulk updates too:

for mote in motes:
    mote.set_pixel(channel, pixel, r, g, b)
    mote.show()
1 Like
import time
from colorsys import hsv_to_rgb
from sys import exit

from mote import Mote


try:
    import requests
except ImportError:
    exit("This script requires the requests module\nInstall with: sudo pip install requests")


# use cmd window: python -m serial.tools.list_ports -v


import serial.tools.list_ports
list = serial.tools.list_ports.comports()

#Find the MOTE Hubs!   This machine has 2 installed
motes = []
for element in list:
    if 'Mote' in element.description:
        motes.append(Mote(port_name=element.device))
 
  
#clear channels on both hubs
for mote in motes:
    mote.configure_channel(1, 16, False)
    mote.configure_channel(2, 16, False)
    mote.configure_channel(3, 16, False)
    mote.configure_channel(4, 16, False)
    
for mote in motes:
    mote.clear()



trys = 1
try:
    while True:
        try:
            r = requests.get('http://api.thingspeak.com/channels/1417/feed.json')
            j = r.json()
            f = j['feeds'][-8:]
        except:
            
            for mote in motes:
                mote.clear()
                mote.show()
    
            time.sleep(0.1)
            break
        f = [element for index, element in enumerate(f) if index%2==0]

        print(f)
        trys += 1
        print(trys)
        channel = 1
        for col in f:
            col = col['field2']
            ## older python method: 
            ##r, g, b = tuple(ord(c) for c in col[1:].lower().decode('hex'))
            r, g, b = tuple(c for c in bytes.fromhex(col[1:]))
            for pixel in range(motes[0].get_pixel_count(channel)):
              
                for mote in motes:
                    mote.set_pixel(channel, pixel, r, g, b)
                    time.sleep(0.001)
         
                for mote in motes:
                    mote.show()
                
            channel += 1        


        time.sleep(3)

except KeyboardInterrupt:
    for mote in motes:
        mote.clear()
        mote.show()
    time.sleep(0.1)

Challenge Accepted!

1 Like