### Set CyberPi to AP Mode and Create Server Example Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Demonstrates configuring CyberPi in AP mode, setting its IP, waiting for a client connection, and creating a TCP/IP server to communicate with the connected client. ```Python import cyberpi from cyberpi import network import time import event import ujson # Set the number of the port to be used PORT= 5060 # Set CyberPi to the AP mode and set the IP address network.config_ap("chj_test", "12345678") network.set_ip("192.168.4.1") # Wait till a STA device connects to CyberPi you are setting while not network.is_connect(): time.sleep(0.1) # Set the LEDs to be lit up in green when a STA device is connected cyberpi.led.show_all("green") # Create a TCP/IP server at the port network.create_server(PORT) # Listen to client connection at the port. The subsequent APIs are not executed until a client is connected. client_ip = network.server_wait_connection(PORT) # Set the LEDs to be lit up in red when a client is connected. Make sure that the indication is different from that set for Wi-Fi connection. ``` -------------------------------- ### Implement TCP/IP Client on CyberPi Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api?language=en-us Example demonstrating how to configure a CyberPi in Station mode to connect to a remote TCP/IP server. ```python import cyberpi from cyberpi import network import time PORT = 5060 RMT_IP = "192.168.4.1" network.config_sta("chj_test", "12345678") while not network.is_connect(): time.sleep(0.1) cyberpi.led.show_all("green") network.create_client() network.connect_server(RMT_IP, PORT) ``` -------------------------------- ### Implement TCP/IP Server on CyberPi Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api?language=en-us Example demonstrating how to configure a CyberPi as an Access Point and host a TCP/IP server to accept client connections. ```python import cyberpi from cyberpi import network import time PORT = 5060 network.config_ap("chj_test", "12345678") network.set_ip("192.168.4.1") while not network.is_connect(): time.sleep(0.1) cyberpi.led.show_all("green") network.create_server(PORT) client_ip = network.server_wait_connection(PORT) ``` -------------------------------- ### Set CyberPi to STA Mode and Create Client Example Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Demonstrates configuring CyberPi in STA mode to connect to an AP, setting LEDs upon connection, and establishing a TCP/IP client connection to a remote server. ```Python import cyberpi from cyberpi import network import time import event import ujson # Set the number of the port to be used PORT= 5060 # Set the IP address of the server to be connected RMT_IP = "192.168.4.1" # Set CyberPi to the STA mode. so that it can connect to an AP device network.config_sta("chj_test", "12345678") # Wait till it connects to an AP device, that is, another CyberPi that is set to the AP mode while not network.is_connect(): time.sleep(0.1) # Set the LEDs to be lit up in green when it is connected to an AP device cyberpi.led.show_all("green") # Create a TCP/IP client network.create_client() # Connect to the server whose IP address is "RMT_IP" and port is "PORT". The subsequent APIs are not blocked when this API is executed. This API is executed until a server is connected or an exception occurs. ``` -------------------------------- ### Configure CyberPi as TCP Server for Walkie-Talkie Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/walkie-talkie This script initializes CyberPi 1 as an Access Point and starts a TCP server. It waits for a client connection and provides visual feedback using LEDs. ```Python import cyberpi from global_objects import mic_o,mp3_music_o import time import event import ujson PORT= 5060 # Set CyberPi 1 to the AP mode cyberpi.network.config_ap("chj_test", "12345678") # Wait for CyberPi 2 to connect to CyberPi 1 while not cyberpi.network.is_connect(): time.sleep(0.1) # Set the LEDs to be lit up in green to indicate that the Wi-Fi connection is successful cyberpi.led.show_all("green") # Set CyberPi 1 to work as a TCP/IP server cyberpi.network.create_server(PORT) # Block other APIs until a client is connected client_ip = cyberpi.network.server_wait_connection(PORT) # Set the LEDs to be lit up in red to indicate that a client is connected ``` -------------------------------- ### GET /network/server_wait_connection Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Waits for a client to connect to the TCP/IP server running on the specified port. ```APIDOC ## GET /network/server_wait_connection ### Description Waits for a client to connect to the server on the specified port. ### Method GET ### Endpoint `/network/server_wait_connection` ### Parameters #### Query Parameters - **port** (integer) - Required - The port number the server is listening on. ### Response #### Success Response (200) - **client_ip** (string) - The IP address of the connected client. ``` -------------------------------- ### CyberPi Walkie-Talkie Network Configuration Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/walkie-talkie?language=zh-cn This snippet covers the setup for both the server (CyberPi 1) and client (CyberPi 2) devices. It includes configuring Wi-Fi modes, establishing a TCP connection, and managing LED status indicators. ```Python import cyberpi from global_objects import mic_o, mp3_music_o import time import event import ujson PORT = 5060 # CyberPi 1: Server Setup cyberpi.network.config_ap("chj_test", "12345678") while not cyberpi.network.is_connect(): time.sleep(0.1) cyberpi.led.show_all("green") cyberpi.network.create_server(PORT) client_ip = cyberpi.network.server_wait_connection(PORT) cyberpi.led.show_all("red") ``` ```Python import cyberpi from global_objects import mic_o, mp3_music_o import time import event, ujson PORT = 5060 RMT_IP = "192.168.4.1" # CyberPi 2: Client Setup cyberpi.network.config_sta('chj_test', "12345678") while not cyberpi.network.is_connect(): time.sleep(0.1) cyberpi.led.show_all("green") cyberpi.network.create_client() cyberpi.network.client_connect_to(RMT_IP, PORT) cyberpi.led.show_all("red") ``` -------------------------------- ### GET /network/is_connect Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Checks if the CyberPi is currently connected to a Wi-Fi network (either as AP or STA). ```APIDOC ## GET /network/is_connect ### Description Checks if CyberPi is connected to a network. ### Method GET ### Endpoint `/network/is_connect` ### Response #### Success Response (200) - **connected** (boolean) - True if connected, False otherwise. ``` -------------------------------- ### POST /network/create_client Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Initializes the CyberPi as a TCP/IP client. ```APIDOC ## POST /network/create_client ### Description Initializes CyberPi as a TCP/IP client. ### Method POST ### Endpoint `/network/create_client` ### Response #### Success Response (200) - **status** (string) - Indicates success or failure of the operation. ``` -------------------------------- ### POST /network/create_server Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api?language=en-us Initializes a TCP/IP server on the CyberPi to listen for incoming client connections on a specified port. ```APIDOC ## POST /network/create_server ### Description Sets up the CyberPi as a TCP/IP server, enabling it to communicate with multiple clients. ### Method POST ### Endpoint network.create_server(port) ### Parameters #### Request Body - **port** (int) - Required - The port number to listen on (e.g., 5060). ### Response #### Success Response (200) - **status** (string) - Indicates the server has been successfully created. ``` -------------------------------- ### Create TCP/IP Server Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Initializes a TCP/IP server on the CyberPi, listening on a specified port. This allows the CyberPi to accept incoming client connections. ```Python network.create_server(PORT) ``` -------------------------------- ### POST /network/create_client Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api?language=en-us Initializes a TCP/IP client on the CyberPi to connect to a remote server. ```APIDOC ## POST /network/create_client ### Description Configures the CyberPi as a TCP/IP client to initiate a connection to a server at a specific IP address and port. ### Method POST ### Endpoint network.create_client() ### Parameters - None ### Response #### Success Response (200) - **status** (string) - Indicates the client has been initialized and is attempting to connect. ``` -------------------------------- ### Create TCP/IP Client Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Initializes the CyberPi to act as a TCP/IP client, enabling it to connect to a remote server. ```Python network.create_client() ``` -------------------------------- ### Import CyberPi Library Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Imports the necessary cyberpi library to access its functionalities, including network APIs. ```Python from cyberpi import ``` -------------------------------- ### Connect TCP/IP Client to Server Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Attempts to establish a TCP/IP connection from the CyberPi client to a specified server IP address and port. Execution continues after connection or if an exception occurs. ```Python network.connect_server(RMT_IP, PORT) ``` -------------------------------- ### POST /network/client_connect Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Connects the CyberPi client to a specified server IP address and port. ```APIDOC ## POST /network/client_connect ### Description Connects the CyberPi client to a server. ### Method POST ### Endpoint `/network/client_connect` ### Parameters #### Query Parameters - **ip_address** (string) - Required - The IP address of the server to connect to. - **port** (integer) - Required - The port number of the server. ### Request Example ```json { "ip_address": "192.168.4.1", "port": 5060 } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success or failure of the operation. ``` -------------------------------- ### POST /network/config_sta Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Configures the CyberPi to work in Station (STA) mode, allowing it to connect to an existing Wi-Fi network. ```APIDOC ## POST /network/config_sta ### Description Sets CyberPi to STA mode. Cannot be used simultaneously with `config_ap`. ### Method POST ### Endpoint `/network/config_sta` ### Parameters #### Query Parameters - **ssid** (string) - Required - The name of the Wi-Fi network to connect to. - **password** (string) - Required - The password for the Wi-Fi network. ### Request Example ```json { "ssid": "my_wifi_network", "password": "wifi_password" } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success or failure of the operation. ``` -------------------------------- ### Network API Constants Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Defines default network configuration constants for IP addresses, netmask, gateway, DNS, authentication mode, port, and buffer size. ```Python DEFAULT_AP_IP = "192.168.4.1" DEFAULT_STA_IP = "192.168.4.2" DEFAULT_NETMARK = "255.255.255.0" DEFAULT_GATEWAY = "192.168.1.1" DEFAULT_DNS = "172.16.50.20" DEFAULT_AUTHMODE = "AUTH_WPA2_PSK" DEFAULT_PORT = 5050 DEFAULT_READ_BUFFER_SIZE = 32 ``` -------------------------------- ### Network API Configuration Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api These constants define default network settings for CyberPi, including IP addresses, subnet masks, gateway, DNS, authentication mode, and port numbers. ```APIDOC ## Default Network Settings ### Description Constants for default network configurations. ### Parameters - **DEFAULT_AP_IP** (string) - Default IP address for AP mode. - **DEFAULT_STA_IP** (string) - Default IP address for STA mode. - **DEFAULT_NETMARK** (string) - Default network mask. - **DEFAULT_GATEWAY** (string) - Default gateway address. - **DEFAULT_DNS** (string) - Default DNS server address. - **DEFAULT_AUTHMODE** (string) - Default Wi-Fi authentication mode. - **DEFAULT_PORT** (integer) - Default port number for server/client communication. - **DEFAULT_READ_BUFFER_SIZE** (integer) - Default buffer size for reading data. ``` -------------------------------- ### CyberPi Audio Data Reception Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/walkie-talkie?language=zh-cn This snippet demonstrates the client-side logic for receiving audio data packets over the network and playing them back using the speaker module. ```Python @event.start def test(): while True: head_data = cyberpi.network.read_line(RMT_IP, port = PORT) try: data_len = ujson.loads(str(head_data, 'utf8'))['data_len'] cyberpi.network.set_read_buffer_size(data_len) data = cyberpi.network.read(RMT_IP, port = PORT) cyberpi.led.show_all("cyan") mp3_music_o.play_raw_data(data, 16000) cyberpi.led.show_all("red") time.sleep(0.1) except: print("error") ``` -------------------------------- ### Configure CyberPi as Station (STA) Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Sets the CyberPi to operate in Station (STA) mode, allowing it to connect to an existing Wi-Fi network. This function cannot be used concurrently with `config_ap`. ```Python def config_sta(ssid, password): pass ``` -------------------------------- ### Configure CyberPi as TCP Client for Walkie-Talkie Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/walkie-talkie This script configures CyberPi 2 to connect to the server, listen for incoming audio data packets, and play them through the speaker. ```Python import cyberpi from global_objects import mic_o,mp3_music_o import time import event, ujson PORT= 5060 RMT_IP = "192.168.4.1" cyberpi.network.config_sta('chj_test', "12345678") while not cyberpi.network.is_connect(): time.sleep(0.1) cyberpi.led.show_all("green") cyberpi.network.create_client() cyberpi.network.client_connect_to(RMT_IP, PORT) cyberpi.led.show_all("red") @event.start def test(): while True: # read head head_data = cyberpi.network.read_line(RMT_IP, port = PORT) try: data_len = ujson.loads(str(head_data, 'utf8'))['data_len'] cyberpi.network.set_read_buffer_size(data_len) data = cyberpi.network.read(RMT_IP, port = PORT) cyberpi.led.show_all("cyan") mp3_music_o.play_raw_data(data, 16000) cyberpi.led.show_all("red") time.sleep(0.1) except: print("error") ``` -------------------------------- ### Configure CyberPi Network Mode Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api?language=en-us Functions to set the Wi-Fi operating mode. Note that AP and STA modes are mutually exclusive and should not be configured simultaneously. ```python from cyberpi import network # Set CyberPi to AP mode network.config_ap("ssid_name", "password") # Set CyberPi to STA mode network.config_sta("ssid_name", "password") ``` -------------------------------- ### POST /network/set_ip Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Sets the IP address for the CyberPi. This is typically used after configuring AP or STA mode. ```APIDOC ## POST /network/set_ip ### Description Sets the IP address for the CyberPi. ### Method POST ### Endpoint `/network/set_ip` ### Parameters #### Query Parameters - **ip_address** (string) - Required - The IP address to set for the CyberPi. ### Request Example ```json { "ip_address": "192.168.4.1" } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success or failure of the operation. ``` -------------------------------- ### Wait for TCP/IP Client Connection Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Blocks execution until a client connects to the specified port on the CyberPi's TCP/IP server. Returns the IP address of the connected client. ```Python client_ip = network.server_wait_connection(PORT) ``` -------------------------------- ### POST /network/config_ap Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Configures the CyberPi to work in Access Point (AP) mode. This allows other devices to connect to the CyberPi. ```APIDOC ## POST /network/config_ap ### Description Sets CyberPi to AP mode. Cannot be used simultaneously with `config_sta`. ### Method POST ### Endpoint `/network/config_ap` ### Parameters #### Query Parameters - **ssid** (string) - Required - The name broadcast by the AP. - **password** (string) - Required - The password for functioning as the AP. ### Request Example ```json { "ssid": "my_ap_name", "password": "my_password" } ``` ### Response #### Success Response (200) - **status** (string) - Indicates success or failure of the operation. ``` -------------------------------- ### POST /network/config_ap Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api?language=en-us Configures the CyberPi Wi-Fi module to operate in Access Point (AP) mode. Note that AP and STA modes cannot be active simultaneously. ```APIDOC ## POST /network/config_ap ### Description Sets the CyberPi to AP mode, allowing it to broadcast a Wi-Fi network for other devices to connect to. ### Method POST ### Endpoint network.config_ap(ssid, password) ### Parameters #### Request Body - **ssid** (string) - Required - The name of the Wi-Fi network to broadcast. - **password** (string) - Required - The password for the AP network. ### Response #### Success Response (200) - **None** (null) - Returns no value upon successful configuration. ``` -------------------------------- ### Configure CyberPi as Access Point (AP) Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Sets the CyberPi to operate in Access Point (AP) mode, broadcasting a specified SSID and password. This function cannot be used concurrently with `config_sta`. ```Python def config_ap(ssid, password): pass ``` -------------------------------- ### Set CyberPi IP Address Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Assigns a static IP address to the CyberPi. This is typically used after configuring the AP or STA mode. ```Python network.set_ip("192.168.4.1") ``` -------------------------------- ### Check Wi-Fi Connection Status Source: https://www.yuque.com/makeblock-help-center-en/cyberpi-api/network-api Checks if the CyberPi is currently connected to a Wi-Fi network (either as an AP or STA). Returns `True` if connected, `False` otherwise. ```Python network.is_connect() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.