Connecting to Bittle

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 pyBittle
import time


greet_command = pyBittle.Command.GREETING  # 'khi' message
rest_command = pyBittle.Command.REST  # 'd' message

bittle = pyBittle.Bittle()  # Create Bittle instance

is_connected = bittle.connect_bluetooth()  # Search for Bittle and connect to it
if 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 lines
    print(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 connection
else:
    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.

Connecting through Serial

This example shows how to create a Bittle instance, how to connect to it through Serial and how to send commands.

Last updated

Was this helpful?