Switching the Mugic to Wifi Client Mode (no app)

#Linux #Ligne de commande #Musique #Python #Web

I’ve been experimenting for some time with the Mugic, a very nice 9-axis IMU (accelerometer, magnetometer and gyroscope, 3 axes each) based on the BNO055 “smart” sensor. For instance, the following video shows a motion-controlled harmonizer allowing to play 4-part jazz harmonies with a single flute, using the player’s position to select the quality of the chords (and the dice allows to select which notes are played, allowing to harmonize in 3rds, 5ths, 7ths or combinations thereof).

This sensor really opens up interesting perspectives and that’s why we’ve decided to include it in Les Chemins de Traverse’s teaching at the Summer Academy of the La Côte Flûte Festival.

So we ordered 7 more sensors so that each student in the Augmented Instruments workshop could use one simultaneously.

The default setting of the mugic is to provide its own wifi access point (AP). This is convenient for quick testing, but is a little bit fragile - and doesn’t scale to several devices. So I had to switch the devices to “Wifi client mode”, where it can connect to an existing wifi. The mugic team provides an application for this, but it’s not available for Linux. It’s also supposed to be possible via a web browser (I wouldn’t have bought it otherwise), but I soon noticed it did not work as advertised.

Luckily, the mugic team has been very helpful (we spent more than two hours on the phone trying to fix things) and we finally found a way that works. I put it here, more as a reminder for myself when I need to do it again, but if it can be useful to others…

import sys
from urllib.request import urlopen
from urllib.parse import quote

mugic_index = int(sys.argv[1])
device_url = '192.168.4.1'

setup = {
    'prefix': f'mugic/{mugic_index}',
    'wifi_ssid' : '<SSID>',
    'wifi_pwd' : '<PASSWORD>',
    'wifi_ip' : '192.168.10.10', # The IP you want to send to
    'wifi_port' : '4000',
    'wifi' : 'c',
    'button' : '1',
}

for k, v in setup.items():
    v = quote(v)
    url = f'http://{device_url}/set?{k}={v}'
    print(url)
    urlopen(url)

url = f'http://{device_url}/commit'
print(url)
urlopen(url)
from urllib.request import urlopen
from bs4 import BeautifulSoup

for i in range(100,200): # adapt to your router's dhcpcd address range
    try:
        ip = f'http://192.168.10.{i}'
        response = urlopen(ip, timeout=.1)
    except:
        pass
    else:
        soup = BeautifulSoup(response, 'html.parser')
        if soup.title.string == '{v}':
            url = f'{ip}/commit'
            print(url)
            urlopen(url)
            break

And voilà! Hopefully your mugic is now in client mode… to be sure, power cycle and see if it starts in client mode again. If anything goes wrong, there’s a reset button on the small side - just press it and start again.

When it works, switch it off… and do the same with the next mugic!

Note One of the problems I had was that my wifi SSID was rejected by the mugic. I’m not sure what the problem was, but it seems having an SSID that is at least 8 characters long and without special characters (whatever that means) helps.