The following is a step by step guide for users with Windows. Mac users may use it as a reference.

Quick start for users who already have a Python environment and a third-party editor installed:

  • Skip to step 3, or install pyserial and cyberpi libraries with the following commands:
    pip install pyserial
    pip install cyberpi

    1. Set up a Python environment

  • Go to the Python.org to select and download Python for your operating system.


image.png

  • Click the selected version to install, remember to check Add Python 3.6 to PATH, and then click Install Now.
  • You can also click Customize installation to install Python environment to a specified directory.

image.png

2. Install and configure the third-party Python editor (Visual Studio Code as an example)

Note: The steps may vary depending on the editor you are using. It is also recommended that you configure the installed Python environment in the editor.

  • Download VS Code according to your operating system. Download VS Code
  • Click the downloaded installation package to install it. (Just follow the prompts to install)
  • Click the VS Code icon on the desktop to run the program.
  • On the left side of the software interface, find the icon in the red rectangle as shown below and click on it.

image.png

  • Search for Python extension and install it. Note that the publisher is Microsoft.

image.png

  • Configure the previously installed Python environment in VS Code editor.
  • Create a new folder and a .py file and you can write basic Python programs.

3. Preparation for programming CyberPi with the third party editor

  • In Windows OS, press Windows + R on the keyboard, type CMD and press Enter, then the CMD window will pop up.

image.png
image.png

  • Install pyserial library, which ensures that CyberPi is connected to the third-party editor via USB cable or Bluetooth dongle.

    1. Type `pip install pyserial` in the CMD window and press enter, wait for the library to be installed.<br />![](https://cdn.nlark.com/yuque/0/2021/png/22023205/1628045093986-8edb30c1-e2bd-435a-92a9-1999f97c3c34.png#from=url&id=JZTLs&margin=%5Bobject%20Object%5D&originHeight=511&originWidth=976&originalType=binary&ratio=1&status=done&style=none)
  • Install cyberpi library, which is a Python library used for online programming of CyberPi.

    1. Type ` pip install cyberpi` in the CMD window and press **Enter**, wait until the library is installed.
    2. You can refer to the [Python API Documentation for CyberPi](https://www.yuque.com/makeblock-help-center-en/mcode/cyberpi-api).<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22023205/1628048457375-99e2470c-cd7d-44cc-801c-7cb723058b25.png#clientId=u955dbee2-4678-4&from=paste&height=510&id=ub69f36f1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=510&originWidth=978&originalType=binary&ratio=1&size=18207&status=done&style=none&taskId=u7f82c3ae-8c20-46b5-94fc-15b16d91cf6&width=978)
  • After the installation is completed, you can write programs in the editor configured with the Python environment.

    4. Note

The third-party editor currently only supports online programming for CyberPi.

5. Sample programs

CyberPi mouse

Need to install pynput module. Refer to the above step pip install pynput

  1. """"
  2. Name: Cyber Pi mouse
  3. Introduction:
  4. Use the gyroscope module, buttons of Cyber Pi, and the mouse control function of pynput module, to convert Cyber Pi into a mouse.
  5. You can also use the Bluetooth module to convert Cyber Pi into a wireless mouse.
  6. """"
  7. from pynput.mouse import Button, Controller
  8. import cyberpi
  9. import time
  10. mouse = Controller()
  11. while True:
  12. if cyberpi.is_tiltback():
  13. mouse.move(-3, 0)
  14. print(mouse.position)
  15. if cyberpi.is_tiltforward():
  16. mouse.move(3, 0)
  17. if cyberpi.is_tiltleft():
  18. mouse.move(0, -3)
  19. if cyberpi.is_tiltright():
  20. mouse.move(0, 3)
  21. if cyberpi.controller.is_press("b"):
  22. mouse.press(Button.left)
  23. mouse.release(Button.left)
  24. mouse.press(Button.left)
  25. mouse.release(Button.left)
  26. if cyberpi.controller.is_press("a"):
  27. mouse.press(Button.right)
  28. mouse.release(Button.right)
  29. time.sleep(0.01)

CyberPi voice typer

  1. """"
  2. Name: 074 Cyber Pi voice typer
  3. Hardware: Cyber Pi
  4. Introduction:
  5. Use the voice recognition function of Cyber Pi, and the keyboard control function of pynput module.
  6. The result of voice recognition will be printed out in a file through pynput.
  7. This program currently only supports English.
  8. """"
  9. import cyberpi
  10. from pynput.keyboard import Key, Controller
  11. import time
  12. keyboard = Controller()
  13. cyberpi.console.clear()
  14. cyberpi.led.on(0, 0, 0)
  15. cyberpi.set_recognition_url()
  16. cyberpi.cloud.setkey("Enter the cloud service authorization code") # You can get it through your mBlock account
  17. cyberpi.wifi.connect("WIFI name", "WIFI password")
  18. while not cyberpi.wifi.is_connect():
  19. pass
  20. cyberpi.led.on(0,0,255)
  21. cyberpi.console.println("WIFI connected")
  22. cyberpi.console.println("--------------")
  23. cyberpi.console.println("Press the button A to start voice recognition")
  24. while True:
  25. if cyberpi.controller.is_press('a'):
  26. keyboard.press(Key.space)
  27. cyberpi.console.clear()
  28. cyberpi.led.on(100, 0, 0)
  29. cyberpi.console.println("Start voice recognition")
  30. cyberpi.audio.play("switch")
  31. cyberpi.console.println("--------------")
  32. cyberpi.cloud.listen("english", 2)
  33. cyberpi.led.on(0, 0, 0)
  34. say = cyberpi.cloud.listen_result()
  35. cyberpi.console.println(say)
  36. cyberpi.console.println("--------------")
  37. for i in say:
  38. if i == '':
  39. keyboard.press(' ')
  40. else:
  41. keyboard.press(str(i))
  42. time.sleep(0.03)

Matplotlib volume histogram

  1. import cyberpi
  2. import time
  3. from matplotlib import pyplot as plt
  4. plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
  5. plt.ion()
  6. while True:
  7. loud = cyberpi.get_loudness()
  8. plt.clf()
  9. plt.xlabel("Data name")
  10. plt.ylabel("Volume readings")
  11. y_locator = plt.MultipleLocator(5)
  12. ax=plt.gca()
  13. ax.yaxis.set_major_locator(y_locator)
  14. plt.ylim(0,100)
  15. plt.bar('Volume', loud, align='center',label=f'Temperature{loud}')
  16. plt.title('Cyber Pi sound sensor readings')
  17. plt.legend()
  18. plt.show()
  19. plt.pause(0.01)

Matplotlib multi-chart display

  1. from matplotlib import pyplot as plt
  2. import time
  3. import cyberpi
  4. loud_list = []
  5. bri_list = []
  6. plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
  7. plt.ion()
  8. plt.figure(figsize=(10,10))
  9. plt.figure(1)
  10. x_locator = plt.MultipleLocator(5)
  11. y_locator = plt.MultipleLocator(5)
  12. while True:
  13. loud = cyberpi.get_loudness()
  14. bri = cyberpi.get_bri()
  15. loud_list.append(loud)
  16. bri_list.append(bri)
  17. battery = cyberpi.get_battery()
  18. size = [battery, 100-battery]
  19. status = [f'Remaining power:{battery}%', f'Power used:{100-battery}%']
  20. ax1 = plt.subplot(221)
  21. plt.title('Light line chart')
  22. ax1.plot(bri_list)
  23. ax2 = plt.subplot(222)
  24. plt.title('Sound histogram')
  25. ax2.xaxis.set_major_locator(x_locator)
  26. ax2.yaxis.set_major_locator(y_locator)
  27. plt.ylim(0,100)
  28. ax2.bar('sound', loud)
  29. ax3 = plt.subplot(223)
  30. ax3.xaxis.set_major_locator(x_locator)
  31. ax3.yaxis.set_major_locator(y_locator)
  32. plt.xlim(0,100)
  33. plt.ylim(0,100)
  34. plt.title('Sound and volume scatter chart')
  35. ax3.scatter(loud_list,bri_list)
  36. ax4 = plt.subplot(224)
  37. ax4.pie(size, labels = status, radius=1,wedgeprops = {'width': 0.3, 'edgecolor': 'w'})
  38. plt.title('Cyber Pi power')
  39. plt.pause(0.2)
  40. plt.clf()
  41. if cyberpi.controller.is_press('a'):
  42. break
  43. if len(bri_list) > 500:
  44. bri_list = []
  45. loud_list = []