mBot2_API_cyberpi
mBot2_API_cyberpi
com/makeblock-help-center-en/mcode/cyberpi-api
CyberPi provides abundant input and output functions, allowing you to interact with
your code and view the output of your code.
CyberPi is equipped with built-in Bluetooth and Wi-Fi, which can be used to set up a
local area network (LAN) or connect to the Internet. You can create your own projects
with CyberPi. It can help you understand and master the knowledge and skills related to
various fields, such as Internet of things (IoT), artificial intelligence (AI), computing,
and network.
In addition, CyberPi supports more than 30 electronic modules and multiple extension
shields, allowing you to create various projects such as smart farm, smart home, self-
You can use the library to implement the functions described in the
preceding.
The library described in this document refers to two separate libraries with
The CyberPi team develop these two libraries in this way on purpose, so that the
MicroPython code you compile in Upload mode (to run on CyberPi) can be executed as
Python code in Live mode (to run on a computer). We think this would be a good way to
help learners make the transition from MicroPython learning to Python learning.
To help you use the APIs more efficiently, the application scenarios of the APIs are
labelled as follows:
: indicates that an API supports Python 3 programming and can be used in Live
and can be used in both Live and Upload modes on the mBlock Python editor.
The non-omitting coding way is preferable for Python coding (Live mode), which
prevents errors of the code compiled for CyberPi when other Python libraries are
imported.
The omitting coding way is preferable for MicroPython coding (Upload mode), which
allows you to write fewer words.
mBlock 5 uses the non-omitting way for transcoding in most of the cases to prevent
errors.
The library provides multiple APIs for controlling hardware. This page
describes the APIs for CyberPi. For the APIs provided for function extension and
/cyberpi-api-function-extension>
/cyberpi-api-mbuild>
These APIs can be used to control CyberPi, allowing CyberPi to perform its functions.
Audio
CyberPi is equipped with a speaker and microphone. You can use the following APIs to
record and play audio files and set the volume and playing speed.
Parameter:
● music : name of the audio file to be played. The table "Setting range and
sound effects" describes the setting range and sound effects of this parameter.
Parameter:
● music : name of the audio file to be played. The table "Setting range and
sound effects" describes the setting range and sound effects of this parameter.
hi SPEAKER.hi hi
Instrument simulating
Plays the specified note of the specified instrument for the specified beats
This API blocks the thread until the playing ends. You can use it to compile and play
music.
Parameters:
● type : type of the instrument to be simulated. The value is set to .
● note : frequency of the sound to be played. Setting range: . The table
"Values and corresponding notes" describes the values and their corresponding
notes.
● beat : duration a note is to be played. Setting range: beat > 0. At general
playing speed, one beat equals one second.
0 C-1 43 G2 86 D6
2 D-1 45 A2 88 E6
3 D#-1 46 A#2 89 F6
4 E-1 47 B2 90 F#6
5 F-1 48 C3 91 G6
7 G-1 50 D3 93 A6
9 A-1 52 E3 95 B6
10 A#-1 53 F3 96 C7
12 C0 55 G3 98 D7
14 D0 57 A3 100 E7
16 E0 59 B3 102 F#7
17 F0 60 C4 103 G7
19 G0 62 D4 105 A7
21 A0 64 E4 107 B7
22 A#0 65 F4 108 C8
24 C1 67 G4 110 D8
26 D1 69 A4 112 E8
28 E1 71 B4 114 F#8
29 F1 72 C5 115 G8
31 G1 74 D5 117 A8
33 A1 76 E5 119 B8
34 A#1 77 F5 120 C9
36 C2 79 G5 122 D9
38 D2 81 A5 124 E9
40 E2 83 B5 126 F#9
41 F2 84 C6 127 G9
42 F#2 85 C#6
Example program
: closed hi-hat
: tambourine
: handclap
: claves
● beat : duration a note to be played. The value must be greater than 0. At
general playing speed, one beat equals one second.
Example program
Example program
Example program
Parameter:
● pct: , percentage by which the playing volume is changed; setting range:
Parameter:
● pct: , percentage of the playing volume; setting range: %
LED
CyberPi is equipped with five programmable LEDs. You can use the following APIs to
compile programs for controlling the LEDs.
Parameters:
● r: or
r: , intensity of the red color; setting range:
r: , full name or abbreviation of a color; the following describes colors
and their abbreviations:
1 red r
2 orange o
3 yellow y
4 green g
5 cyan c
6 blue b
7 purple p
8 white w
9 black k
Example program
1 import cyberpi
2 cyberpi.led.on(255,0,0) #Lights up all the LEDs
1 import cyberpi
2 cyberpi.led.on('red', id = 1) #Lights up LED 1 in red
1 import cyberpi
2 cyberpi.led.on('r', id = 1) #Lights up LED 1 in red
Parameter:
● color: , color(s) of the five LEDs, set in the
mode, with one space between any two colors. If you set more than five
colors, only the first five colors are used. You can set this parameter to the full name
or abbreviation of the colors. The options include the following:
,
,
,
,
,
,
,
,
,
Makes the colors of the LEDs roll from left to right by the specified number of positions
Parameter:
● step: , number of positions by which the colors of the LEDs roll; setting range:
; default value:
For example, you set the colors of the five LEDs to r,o,y,g,c and then use this
API. When you set step to 0, the colors of the LEDs are shown in Figure A; and when
you set step to 1, the colors roll from left to right by one position, and the color of
the first LED is that of the original last LED, as shown in Figure B
Example program
Display
CyberPi is equipped with a 1.89-inch full-color display. With the following APIs, you can
use CyberPi to display texts and images, draw charts, design games, and create apps.
Example program
1 import cyberpi
2 cyberpi.console.print('hello')
3 cyberpi.console.print('world')
Example program
1 import cyberpi
2 cyberpi.console.println('hello world')
3 cyberpi.console.print('hello'+' '+'world')
Positions of texts
Parameter:
● message: , text to be displayed, such as
● size: , font size of the text to be displayed; setting range: , , and
● x: or , position where the text is to be displayed
When x is , the options are as follows:
: in the upper center
: in the center
: in the middle left
Example program
Charts
and thus can draw line charts in different colors to present different data sets.
Parameter:
● data: , ranging from to
If a value to be input exceeds the setting range, rescale all the original data to be
within the setting range.
For example, you can rescale the values 200, 300, and 400 to 20, 30, and 40,
respectively.
You can use the API to set the color of a bar, and thus can draw
a bar chart with different colors of bars to present different data sets. The width of a bar
Parameter:
● data: , ranging from to
the cell.
You can use the API to set the color of the content.
The content can be texts and predefined icons.
Parameter:
● row: , number of rows to be created
● column: , number of columns to be created
● data: , content to be input into a cell of the table
Example program
● r: or
r: , intensity of the red color; setting range:
r: , full name or abbreviation of a color; the following describes colors
and their abbreviations:
1 red r
2 orange o
3 yellow y
4 green g
5 cyan c
6 blue b
7 purple p
8 white w
9 black k
Display directions
Sensing
With the following APIs, you can obtain output values of the sensors on your CyberPi or
Determines whether the specified button is pressed or the joystick is moved to the
specified direction
Parameter:
● name: , name of the button or joystick direction
Setting range:
: button A (in the square shape)
: button B (in the rectangle shape)
: joystick moved up
: button A or B
A value is returned.
Obtains the number of times the specified button is pressed or the joystick is moved to
: joystick moved up
An value is returned.
Resets the number of times the specified button is pressed or the joystick is moved to
Parameter:
● name: , name of the button or joystick direction
Setting range:
: button A (in the square shape)
: joystick moved up
: button A or B
Sensor outputs
Obtains the ambient light brightness detected by the light sensor on CyberPi
Parameter:
● mode: , type of loudness you want to obtain
Setting range
: average loudness in a period
: maximum loudness in a period; obviously, this mode is preferable for
sound-sensitive projects.
Timer
Other outputs
Obtains the media access control address (MAC address) of the Wi-Fi
Note that the address obtained may be wrong if Wi-Fi is not connected.
Obtains the battery level (in %) of the pocket shield connected to CyberPi
An value ranging from to is returned. When the value 100 is returned,
the battery is at its highest level; and when 0 is returned, no pocket shield is connected
to CyberPi or the battery of the connected pocket shield runs out of battery.
API .
CyberPi. By setting device names, users can find their CyberPis or identify one another
on a LAN or IoT.
Parameter:
● name: , name you set for your CyberPi, for example, ,
Motion Sensing
With the gyroscope, accelerometer, and built-in motion detection algorithm on CyberPi,
you can implement motion-based control. You can use the following APIs to obtain the
postures of CyberPi.
CyberPi's postures
A value is returned.
A value is returned.
A value is returned.
A value is returned.
A value is returned.
A value is returned.
Determines whether CyberPi is placed perpendicular to the ground, with the LED strip
on the bottom, as shown in the following figure:
A value is returned.
Determines whether CyberPi is placed perpendicular to the ground, with the LED strip
返回 。
Continuous motions
A value is returned.
A value is returned.
A value is returned.
A value is returned.
A large error may be caused in the value obtained due to the integral error.
An value ranging from to is returned.
The pitch angle refers to the angle between the y-axis and horizontal plane.
The yaw angle refers to the angle CyberPi rotates around the z-axis.
An value ranging from to is returned, in degrees.
Obtains the output (in m/s²) of the accelerometer on the specified axis
Parameter:
● axis: , axis defined for CyberPi; setting range: , , and
A value is returned. Note that the accelerometer measures Earth's gravity.
When CyberPi is placed on a desk, the output of the accelerometer on the z-axis is
–9.8m/s² (1g = 9.8m/s²).
Obtains the angle CyberPi rotates around the specified axis, with the counterclockwise
direction as the positive direction
Parameter:
● axis: , axis defined for CyberPi; setting range: , , and
A value is returned, in degrees.
Resets the angle (s) CyberPi rotates around the specified axis(axes) to zero
After this API is executed, the value obtained by is counted
from zero.
Parameter:
● axis: , axis or all the axes defined for CyberPi; setting range: , , , and
Wi-Fi
CyberPi is equipped with a Wi-Fi module. You can use the following APIs to connect
CyberPi to the Internet or perform LAN broadcast.
This API doesn't block the thread, and therefore CyberPi is not necessarily connected to
the Internet when this API stops. You need to use to determine
A value is returned.
Cloud Services
After connecting CyberPi to the Internet, you can use various cloud services provided by
can use the cloud services provided by Makeblock. Note that you can use this API only
IoT
Parameters:
● option: , weather data option
Setting range:
: highest temperature
: lowest temperature
: temperature
: humidity
● woe_id: , code of the city for which weather data is to be obtained
You can visit Yahoo Weather <https://www.yahoo.com/news/weather> to obtain
the codes of cities.
Change the location to the one of which the weather information is to be searched
for, and then view the code on the address bar.
Alternatively, you can view the code of a city in the corresponding block on mBlock
1 0 tornado
2 1 tropical storm
3 2 hurricane
4 3 severe thunderstorms
5 4 thunderstorms
6 5 mixed rain and snow
7 6 mixed rain and sleet
8 7 mixed snow and sleet
9 8 freezing drizzle
10 9 drizzle
11 10 freezing rain
12 11 showers
13 12 rain
14 13 snow flurries
15 14 light snow showers
16 15 blowing snow
17 16 snow
18 17 hail
19 18 sleet
20 19 dust
21 20 foggy
22 21 haze
23 22 smoky
24 23 blustery
25 24 windy
26 25 cold
27 26 cloudy
28 27 mostly cloudy (night)
29 28 mostly cloudy (day)
30 29 partly cloudy (night)
31 30 partly cloudy (day)
32 31 clear (night)
33 32 sunny
34 33 fair (night)
35 34 fair (day)
36 35 mixed rain and hail
37 36 hot
38 37 isolated thunderstorms
39 38 scattered thunderstorms
40 39 scattered showers (day)
41 40 heavy rain
42 41 scattered snow showers (day)
43 42 heavy snow
44 43 blizzard
45 44 not available
46 45 scattered showers (night)
47 46 scattered snow showers (night)
48 47 scattered thundershowers
AI
CyberPi provides some AI capabilities. After being connected to the Internet, CyberPi
can recognize speeches, read texts out loud, and translate some texts through the
corresponding AI interfaces.
A value is returned.
: Chinese
: Taiwan Mandarin
: Cantonese
: Japanese
: English
: French
: German
: Spanish
: Portuguese
: Russian
: Korean
: Italian
: Dutch
● message: , text to be read out loud
: Dutch
● message: , text to be translated
Broadcast
CyberPi supports multiple types of broadcasts. With these broadcasts, CyberPi can
implement communication between threads and on LANs and the Internet.
Basic broadcast
The message can be received by all threads on CyberPi or by mBlock sprites in Live
mode.
Parameter:
● message: , name of the broadcast message
LAN broadcast
Parameters:
● message: , name of the LAN broadcast message
● val: , value transmitted by the LAN broadcast message
1 import cyberpi
2 import time
3
4 while True:
5 cyberpi.led.on(0,0,255,1)
6 time.sleep(1)
7 cyberpi.wifi_broadcast.get(message)
mode broadcast messages are received by mBlock 5 sprites. This API is preferable for
projects in which real-time interaction between mBlock 5 sprites and hardware devices
is required.
Parameters:
● message: , name of the upload mode broadcast message
● val: , value transmitted by the upload mode broadcast message
Obtains the value transmitted by the specified upload mode broadcast message
Parameter
● message: , name of the upload mode broadcast message
A value is returned.
1 import cyberpi
2 import time
3
4 while True:
5 cyberpi.led.on(0,0,255,1)
6 time.sleep(1)
7 cyberpi.upload_broadcast.get(message)
User cloud messages can be used to share data across devices and projects. For
example, after signing in to mBlock 5 on multiple PCs, you can send a user cloud
broadcast message on one PC to all the other online hardware devices and mBlock 5
Internet. In addition, you can use mBlock 5 or mBlock-Python Editor to design visual
interaction UIs.
Parameters:
● message: , name of the user cloud broadcast message
● val: , value transmitted by the user cloud broadcast message
Obtains the value transmitted by the specified user cloud broadcast message
Parameter:
● message: , name of the user cloud broadcast message
A value is returned.
1 import cyberpi
2 import time
3
4 while True:
5 cyberpi.led.on(0,0,255,1)
6 time.sleep(1)
7 cyberpi.cloud_broadcast.get(message)
Script control
Restarts CyberPi
Events
CyberPi supports the execution of multiple threads. You can add a thread for CyberPi by
adding the corresponding event header, and the thread listens to the specified event.
When the event happens, the subsequent code in the thread is executed.
Event compiling
Event headers are compiled by using decorators, as shown in the following example:
1 import cyberpi
2
3 @cyberpi.event.start # Event to be listened to
4 def callback():
5 cyberpi.led.on("green") # Code to be executed after the event happens
6
7 @cyberpi.event.received('hello') # Event (with a parameter) to be listened to
8 def callback():
9 cyberpi.led.on("blue") # Code to be executed after the event happens
In this coding way, when the event defined in the event header happens, the
subsequent code is executed. In the preceding example, the function name
is used. As a matter of fact, however, any function name that meets the naming
convention can be used. In addition, it's OK to use function names repeatedly because
functions are defined after events.
As you can see in the preceding example, the function is executed when
you start CyberPi and the other function is executed when CyberPi
receives the broadcast message "hello." No conflict occurs because they are defined
after different events.
Listens to the start of CyberPi and executes the subsequent code when CyberPi starts
Listens to the state of the specified button or joystick moving direction and executes the
subsequent code when the specified button is pressed or the joystick is moved to the
specified direction
Parameter:
● name: , name of the button or joystick direction
Setting range:
: joystick moved up
: button A or B
Listens to the posture of CyberPi and executes the subsequent code when CyberPi tilts
forward
Listens to the posture of CyberPi and executes the subsequent code when CyberPi tilts
backward
Listens to the posture of CyberPi and executes the subsequent code when CyberPi tilts
to the left
Listens to the posture of CyberPi and executes the subsequent code when CyberPi tilts
to the right
Listens to the posture of CyberPi and executes the subsequent code when CyberPi's
screen faces up
Listens to the posture of CyberPi and executes the subsequent code when CyberPi's
Listens to the motion of CyberPi and executes the subsequent code when CyberPi is
shaken
Listens to the motion of CyberPi and executes the subsequent code when CyberPi is
waved up
Listens to the motion of CyberPi and executes the subsequent code when CyberPi is
waved down
Listens to the motion of CyberPi and executes the subsequent code when CyberPi is
Listens to the motion of CyberPi and executes the subsequent code when CyberPi is
waved to the right
Listens to the motion of CyberPi and executes the subsequent code when CyberPi falls
down
Listens to the motion of CyberPi and executes the subsequent code when CyberPi
Listens to the motion of CyberPi and executes the subsequent code when CyberPi
Listens to the comparison result and executes the subsequent code when the value is
: timer
Listens to the comparison result and executes the subsequent code when the value is
smaller than the threshold
Parameters:
● threshold: , threshold; setting range:
● type: , type of the data source
Setting range:
: microphone
: light sensor
: shaking strength
: timer
Listens to the specified broadcast message and executes the subsequent code when the
broadcast message is received
Parameter
● message: , name of the broadcast message listened to
Listens to the specified upload mode broadcast message and executes the subsequent
Listens to the specified user cloud broadcast message and executes the subsequent
Listens to the specified LAN broadcast message and executes the subsequent code
Change history
Date Description
Added the "APIs for mBot2 Shield" and "APIs for mBuild modules" sections
Separated the APIs for function extension, extension boards, and mBuild
2020/11/12 modules from this page and created new pages for them. Currently, this page
describes only the APIs for CyberPi.
2021/6/25 Added the labels for indicating whether the APIs support Python 3
programming, MicroPython programming, or both
<https://service.weibo.com/share/share.php?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.yuque.com%2Fmakeblock-
pic=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fcdn.nlark.com%2Fyuque%2F0%2F2020%2Fpng%2F757912%2F1
e27c18c11826.png&title=Python%20API%20
%20Welcome%20to%20use%20CyberPi%20for%20Python%20learning!CyberPi%20provides%20abundan
%2C%20allowing%20you%20to%20interact%20with