🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    M5Stack Unit2 switch mode by sending command in serial port

    SOFTWARE
    4
    5
    5.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • O
      odemakov
      last edited by

      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
          s.open()
      
          js = {
              'function': 'Camera Stream',
              'args': ''
          }
          print("send", js)
          s.write(json.dumps(js).encode('utf-8'))
      
          response = s.readline()
          print("receive", response)
      

      And the output:

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

      Am I missing something?

      1 Reply Last reply Reply Quote 0
      • O
        odemakov
        last edited by odemakov

        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:
            ap.print_help()
            exit(1)
        
        OBJECT_RECOGNITION_ARGS = [
            'yolo_20class',
            'nanodet_80class'
        ]
        if args['mode'] == 'Object Recognition' and args['args'] not in OBJECT_RECOGNITION_ARGS:
            ap.print_help()
            exit(1)
        
        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:
                    traceback.print_exc(exc)
                sys.stdout.write('port closed\n')
        
        with serial.Serial() as s:
            s.port = '/dev/ttyAMA0'
            s.baudrate = 115200
            s.timeout = .5
            s.open()
        
            with ReaderThread(s, PrintLines) as protocol:
                js = {
                    'function': args['mode'],
                    'args': args['args']
                }
                protocol.write_line(json.dumps(js))
                sleep(2)
        

        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
        
        1 Reply Last reply Reply Quote 0
        • T
          tguneysu
          last edited by tguneysu

          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.
          Thanks.

          1 Reply Last reply Reply Quote 0
          • S
            satit
            last edited by

            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.
            .

            1 Reply Last reply Reply Quote 0
            • C
              class14
              last edited by class14

              Getting closer... This is the string to change modes.

              Function switch
              Switch between different recognition functions by clicking the navigation bar of the function page or sending JSON commands through Serial Port communication. Note: Except at the end of the sent command string, no line breaks are allowed in other positions.

              {
              "function": "Camera Stream",
              "args": ""
              }

              BUT this is what I get in return

              {"error":"invalid json format."}
              {"error":"invalid json format."}
              {"error":"invalid json format."}
              {"error":"invalid json format."}
              {"error":"Command is not supported"}
              {"error":"invalid json format."}
              {"error":"invalid json format."}

              Anyone have any ideas?
              I'm sending the string through a serial termanal. CR,LF

              1 Reply Last reply Reply Quote 0
              • First post
                Last post