M5Stack Unit2 switch mode by sending command in serial port

  • Hi there,

    I'm trying to manage UnitV2 device by Serial port. Reading data works just fine, for example in the "Object detection" mode Unit sends data:

    {'num': 1, 'obj': [{'prob': 0.697403133, 'x': 44, 'y': 8, 'w': 555, 'h': 466, 'type': 'person'}], 'running': 'Object Recognition'}
    {'num': 1, 'obj': [{'prob': 0.684900761, 'x': 49, 'y': 1, 'w': 543, 'h': 468, 'type': 'person'}], 'running': 'Object Recognition'}
    {'num': 1, 'obj': [{'prob': 0.686426222, 'x': 23, 'y': 2, 'w': 577, 'h': 472, 'type': 'person'}], 'running': 'Object Recognition'}

    Unfortunately I can't manage to send commands and actually switch the mode. Here is my code for Raspberry PI(connected with UnitV2 by serial ports):

    import serial, json
    with serial.Serial() as s:
        s.port = '/dev/ttyAMA0'
        s.baudrate = 115200
        s.timeout = .5
        js = {
            'function': 'Camera Stream',
            'args': ''
        print("send", js)
        response = s.readline()
        print("receive", response)

    And the output:

    send {'function': 'Camera Stream', 'args': ''}
    receive b''

    Am I missing something?

  • Finally I managed to switch the mode from Raspberry PI using ReaderThread. Here is the code:

    import sys
    import traceback
    import argparse
    import json
    import serial
    from serial.threaded import LineReader, ReaderThread
    from time import sleep
    MODES = [
        'Audio FFT',
        'Code Detector',
        'Face Detector',
        'Lane Line Tracker',
        'Motion Tracker',
        'Shape Matching',
        'Camera Stream',
        'Online Classifier',
        'Color Tracker',
        'Face Recognition',
        'Target Tracker',
        'Shape Detector',
        'Object Recognition',
    ap = argparse.ArgumentParser()
    ap.add_argument('-m', '--mode', help = 'Switch to mode: {}'.format(MODES))
    ap.add_argument('-a', '--args', default = '', help = 'Some modes require extra args. For "Object Recognition" either "yolo_20class" or "nanodet_80class"')
    args = vars(ap.parse_args())
    if 'mode' not in args or args['mode'] not in MODES:
    if args['mode'] == 'Object Recognition' and args['args'] not in OBJECT_RECOGNITION_ARGS:
    class PrintLines(LineReader):
        def connection_made(self, transport):
            super(PrintLines, self).connection_made(transport)
            sys.stdout.write('port opened\n')
        def handle_line(self, data):
            sys.stdout.write('line received: {}\n'.format(repr(data)))
        def connection_lost(self, exc):
            if exc:
            sys.stdout.write('port closed\n')
    with serial.Serial() as s:
        s.port = '/dev/ttyAMA0'
        s.baudrate = 115200
        s.timeout = .5
        with ReaderThread(s, PrintLines) as protocol:
            js = {
                'function': args['mode'],
                'args': args['args']

    And the output:

    pi@rpi0:~/serial$ ./serial_mode.py --mode 'Object Recognition' --args 'yolo_20class'
    port opened
    line received: '{"msg":"function switched to object_recognition."}'
    line received: '{"msg":"Running Object Recognition, Copyright 2021 M5Stack Technology Co., Ltd. All rights reserved.","running":"Object Recognition"}'
    port closed

  • Hi Odemakov
    Any way you can just provide the actual string sent for any one of the functions?
    I am not using Python and have a hard time parsing what the code produces as a json string that is sent to V2.

  • I would like to send command to change unit v2 to code dectector by arduino when m5 Stack power on. Please give me sample code. Thank you.