UnitV2 Jupyter Notebook

Driver Installation

Download the corresponding SR9900 driver according to the operating system used.

Windows10

Unzip the driver package to the desktop path -> Enter the device manager and select the currently unrecognized device (named USB 10/100 LAN or with the characters SR9900) -> Right-click and select custom update -> select The path where the compressed package is decompressed -> click OK and wait for the update to complete.

MacOS

Unzip the driver package -> double-click to open the SR9900_v1.x.pkg file -> follow the prompts and click Next to install. (The compressed package contains a detailed version of the driver installation tutorial pdf)

  • After the installation is complete, if the network card cannot be enabled normally, you can open the terminal and use the command below to re-enable the network card.
sudo ifconfig en10 down
sudo ifconfig en10 up

Connect device

After connecting USB for power supply, UnitV2 will start automatically. The power indicator will be red and white, and it will go out after the startup is complete. UnitV2's factory Linux image integrates a variety of basic peripherals and development tools. Users can use the built-in Jupyter Notebook for development or access the device through SSH to directly edit and run program files. Establish a network connection with UnitV2 through the following two connection methods.

  • Ethernet Mode Connection: UnitV2 has a built-in wired network card. When you connect to a PC through the TypeC interface, it will automatically establish a network connection with UnitV2.

  • AP mode connection: After UnitV2 is started, the AP hotspot (SSID: M5UV2_XXX: PWD:12345678) will be turned on by default, and the user can establish a network connection with UnitV2 through WiFi access.

Jupyter Notebook

UnitV2's factory Linux mirror integrates the Jupyter Notebook development and debugging tool. Jupyter Notebook is a web development tool. Users can write and run code directly on the web page. If you need to write a description document during the programming process, you can write it directly on the same page, which is convenient for timely description and explanation. It is very convenient for online operation and debugging of equipment. Visit Jupyter Notebook official website to learn more . In this tutorial, you will use Jupyter Notebook to edit and run a few simple case programs to show you how to control the basic peripheral hardware on UnitV2. UnitV2 and this Jupyter Notebook run Python 3.8, which supports all the advanced features and syntactic sugar of Python!

After establishing a connection between the PC and UnitV2, use the browser to access the domain name unitv2.py or IP: 10.254.239.1 to access the preview webpage through the recognition function -> click the settings button in the upper right corner -> switch to Jupyter Notebook, refresh according to the page prompts.

After entering the Jupyter Notebook page, we can see a 01-Introduction to M5Stack UnitV2.ipynb program file built into the file system. This file contains some case program introductions for driving basic peripheral hardware (including driving LED lights and taking photos through Camera and displaying them to Jupyter Notebook) -> click on the file to view the code details.

Select the program step by step according to the program comment, and then click the run button above to experience online debugging of the device.

Examples

LED


import time

def control_white_led(value):
    open('/sys/class/gpio/export', 'w').write('0') # Export the GPIO0
    open('/sys/class/gpio/gpio0/direction', 'w').write('out') # Set the direction of the GPIO
    open('/sys/class/gpio/gpio0/value', 'w').write(str(value)) # Set the calute, '1' or '0'
    
def control_red_led(value):
    open('/sys/class/gpio/export', 'w').write('1') # Export the GPIO0
    open('/sys/class/gpio/gpio1/direction', 'w').write('out') # Set the direction of the GPIO
    open('/sys/class/gpio/gpio1/value', 'w').write(str(value)) # Set the calute, '1' or '0'


for i in range(10):
    control_white_led(0)
    time.sleep(0.5)
    control_white_led(1)
    time.sleep(0.5)

Microphone


import subprocess
import audioop
import time

audio_cmd = ['arecord', '-D', 'plughw:0', '-f', 'S16_LE', '-c', '1', '-r', '48000', '-t', 'raw', '-q', '-']
audio_source = subprocess.Popen(audio_cmd, stdout=subprocess.PIPE)

while True:
    audio_source.stdout.flush()
    data = audio_source.stdout.read(512)
    rms = audioop.rms(data, 2)
    print(rms)
    time.sleep(0.1)

UART


import serial

# Open the serial port, and set the baudrate to 115200
uart_grove = serial.Serial('/dev/ttyS1', 115200, timeout=0.5)

# Send Something
uart_grove.write('hello'.encode('utf-8')) 
# encode is for converting the string to bytes, or you can directly send bytes
uart_grove.write(b'hello') 


# Receive soemthing
x = uart_grove.read()          # read one byte
s = uart_grove.read(10)        # read up to ten bytes (timeout)
line = uart_grove.readline()   # read a '/n' terminated line

Camera

The built-in camera of M5Stack UnitV2 is located at /dev/video0. You can try to capture still image frames


import cv2

camera = cv2.VideoCapture(0)
ret, frame = camera.read()

if ret:
    print('Capture Successful!')
else:
    print('OOps, we get some trouble!')

Use the matplotlib library to display it on the Jupyter notebook page


from matplotlib import pyplot as plt

# Magic to tell matplotlib display the image in jupyter notebook
%matplotlib inline 

# Let show the image! 
plt.imshow(frame)

Convert the BGR used by OpenCV to RGB, so that the color of the image displayed by matplotlib is normal.

# Convert from BGR to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

plt.imshow(frame_rgb)

Try to convert to grayscale

# Convert from BGR to RGB
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# We need to tell matplotlib it is a grey image. You can try to change to other cmap, refer here: https://matplotlib.org/stable/tutorials/colors/colormaps.html
plt.imshow(frame_gray, cmap='gray') 

Take and save the picture

import cv2
camera = cv2.VideoCapture(0)
ret, frame = camera.read()

if ret:
    print('Capture Successful!')
    cv2.imwrite('test2.png',frame)
else:
    print('OOps, we get some trouble!')
On This Page