SPS example using Python library
Introduction
In this tutorial, we're going to set up two dongles using Python script to send data back and forth.
Connect a dongle each into two computers that has Python installed.
In short:
One dongle will take on the Central role and the other will take on the Peripheral role.
Then they will connect to each other.
The Central dongle will then start off sending a message; "Echo".
The Peripheral dongle will then receive the message and send it back to the Central dongle which in turn will receive it and send it back and so forth until the script is stopped.
First setup the peripheral dongle using follwing script.
from bleuio_lib.bleuio_funcs import BleuIo
from serial import SerialException
from time import sleep
# This script will receive data from a central dongle
# and echo it back to the central
my_dongle = None
connected_to_dongle = False
connected_to_central = False
message = ""
while not connected_to_dongle:
try:
# Specify the COM PORT connected to the dongle
my_dongle = BleuIo(port="COM6")
# Start the deamon (background process handler) for RX and TX data.
my_dongle.start_daemon()
connected_to_dongle = True
except SerialException:
print("Dongle not found. Please connect your dongle")
sleep(5)
print("Connected to dongle\n\n" "Welcome to the BleuIO SPS example!\n\n")
# Starts advertising so it can be detected
my_dongle.at_advstart()
try:
time_out_counter = 0
i = 0
while not connected_to_central:
# Get information from the dongle
# to see if it's connected to the central
status = my_dongle.at_gapstatus()
# Checks for information about connection
if "\\nConnected\\r" in str(status):
print("\nConnected to central")
while True:
# Listen for a received message
response = str(my_dongle.rx_buffer)
if "Received" in response:
print(response)
my_dongle.rx_buffer = b""
else:
print("Trying to connect to central")
sleep(2)
except KeyboardInterrupt:
# Disconnects and stops the dongle
my_dongle.at_gapdisconnect()
my_dongle.stop_daemon()
exit()
And now set up the central dongle using following script
from bleuio_lib.bleuio_funcs import BleuIo
from serial import SerialException
from time import sleep
my_dongle = None
connected_to_dongle = False
connected_to_peripheral = False
message = ""
target_device_address = ""
# This script will send data from a central dongle(my_dongle)
# to a peripheral device(target_device_address)
# which in turn will echo it back to the central
# Target device needs to be set in peripheral role and be advertising
while not connected_to_dongle:
try:
# Specify the COM PORT connected to the dongle
my_dongle = BleuIo(port="COM3")
# Start the deamon (background process handler) for RX and TX data.
my_dongle.start_daemon()
connected_to_dongle = True
except SerialException:
print("Dongle not found. Please connect your dongle")
sleep(5)
print(
"Connected to dongle\n\n" "Welcome to the BleuIO SPS example!\n\n"
) # //TODO byt namn på de andra
# Set the dongle in central role
my_dongle.at_central()
target_device_address = input(
"Enter the target address:\n" "For example: [0]40:48:FD:E5:2C:EF\n"
)
# Requests connection with target
my_dongle.at_gapconnect(target_device_address)
# Fetch information about the connection
response = my_dongle.rx_response
try:
time_out_counter = 0
while not connected_to_peripheral:
# Checks if a connection been made
if "CONNECTED." in str(response):
connected_to_peripheral = True
print("\nConnected to {}".format(target_device_address))
sleep(3)
# Tries to connect 5 times
elif time_out_counter < 5:
print("Trying to connect to target device")
sleep(3)
time_out_counter += 1
else:
print("Connection timed out")
break
if connected_to_peripheral:
while True:
message = input("\nEnter a message to send to the peripheral:\n")
# Sends provided message to the connected device
my_dongle.at_spssend(message)
except KeyboardInterrupt:
# Disconnects and stops the dongle
my_dongle.at_gapdisconnect()
my_dongle.stop_daemon()
exit()
The script is well commented for easy understanding. Don't forget to put correct COM port for both central and peripheral dongles.
Full source also available on GitHub.