Two examples of creating a Bittle instance and communicating with it through Bluetooth and WiFi.
Connecting through Bluetooth
This example shows how to create a Bittle instance, how to connect to it through Bluetooth and how to send commands.
import pyBittleimport timegreet_command = pyBittle.Command.GREETING # 'khi' messagerest_command = pyBittle.Command.REST # 'd' messagebittle = pyBittle.Bittle()# Create Bittle instanceis_connected = bittle.connect_bluetooth()# Search for Bittle and connect to itif is_connected: bittle.send_command_bluetooth(greet_command)# Send 'khi' message received = bittle.receive_msg_bluetooth()# Get response from previously sent command decoded_msg = received.decode('utf-8')# received is byte type decoded_msg = decoded_msg.replace('\r\n', '')# Replace new linesprint(f"Received message: {decoded_msg}, expected: 'k'") time.sleep(5)# Give Bittle few seconds to finish the sent action bittle.send_command_bluetooth(rest_command)# Send 'd' message received = bittle.receive_msg_bluetooth() decoded_msg = received.decode('utf-8') decoded_msg = decoded_msg.replace('\r\n', '')print(f"Received message: {decoded_msg}, expected: 'd'") time.sleep(5) bittle.disconnect_bluetooth()# Close Bluetooth connectionelse:print("Bittle not found!")
Connecting through WiFi
This example shows how to create a Bittle instance, how to connect to it through WiFi and how to send commands.
import pyBittleimport timegreet_command = pyBittle.Command.GREETING # 'khi' messagerest_command = pyBittle.Command.REST # 'd' messagebittle = pyBittle.Bittle()# Create Bittle instancebittle.wifiManager.ip ='192.168.1.132'# Set Bittle IP addresshas_connection = bittle.has_wifi_connection()if has_connection: response = bittle.send_command_wifi(greet_command)print(f"Received message: {response}, expected '200'") time.sleep(5) response = bittle.send_command_wifi(rest_command)print(f"Received message: {response}, expected '200'") time.sleep(5)# No need to disconnect from WiFielse:print("Can't connect to Bittle!")
Connecting through Serial
This example shows how to create a Bittle instance, how to connect to it through Serial and how to send commands.
import pyBittleimport timesit_command = pyBittle.Command.SIT # 'ksit' messagerest_command = pyBittle.Command.REST # 'd' messagebittle = pyBittle.Bittle()# Create Bittle instanceis_connected = bittle.connect_serial()# Search for Bittle and connect to itif is_connected: bittle.send_command_serial(sit_command)# Send 'ksit' message received = bittle.receive_msg_serial()# Get response from sent command decoded_msg = received.decode('utf-8')# received is byte type decoded_msg = decoded_msg.replace('\r\n', '')# Replace new linesprint(f"Received message: {decoded_msg}, expected: 'k'") time.sleep(5)# Give Bittle few seconds to finish the sent action bittle.send_command_serial(rest_command)# Send 'd' message received = bittle.receive_msg_serial() decoded_msg = received.decode('utf-8') decoded_msg = decoded_msg.replace('\r\n', '')print(f"Received message: {decoded_msg}, expected: 'd'") time.sleep(5) bittle.disconnect_serial()# Close Serial connectionelse:print("Bittle not found!")