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.
import pyBittle
import time
greet_command = pyBittle.Command.GREETING # 'khi' message
rest_command = pyBittle.Command.REST # 'd' message
bittle = pyBittle.Bittle() # Create Bittle instance
bittle.wifiManager.ip = '192.168.1.132' # Set Bittle IP address
has_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 WiFi
else:
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 pyBittle
import time
sit_command = pyBittle.Command.SIT # 'ksit' message
rest_command = pyBittle.Command.REST # 'd' message
bittle = pyBittle.Bittle() # Create Bittle instance
is_connected = bittle.connect_serial() # Search for Bittle and connect to it
if 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 lines
print(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 connection
else:
print("Bittle not found!")