General ArduinoBluno

Bluno ble + python communication?

userHead spinal.cord 2024-07-23 13:39:07 327 Views4 Replies

If I use the example code for arduino…

 

[code]


void setup() {
 Serial.begin(115200);
 //initial the Serial
}

void loop()
{
 if(Serial.available()){
   Serial.write(Serial.read());
 }

}
[/code]

 

and the following Python code on PC

 

[code]

async def send_data(client, data):
   # Write data to the characteristic in chunks
   for i in range(0, len(data), MAX_DATA_SIZE):
       chunk = data[i:i + MAX_DATA_SIZE]  # Get the chunk of data
       encoded_chunk = chunk.encode('utf-8')  # Encode the chunk as bytes
       await client.write_gatt_char(SERIAL_PORT_UUID, encoded_chunk)
       print(f"Sent data chunk: {chunk}")
[/code]

 

I can send text to the bluno and print it out on the serial monitor.

But how do I do it the other way around? I want to send data from the bluno TO the python code on the PC.

 

Thanks.

 

 

2024-08-02 08:11:37

I can see this isn't a super high traffic forum, but I guess

 

♫♪♭ We don't talk about Bluno..no..no..no, We don't talk about Bluno….. ♫♪♭

userHeadPic spinal.cord
2024-07-30 08:25:47

Anoth try, still nothing, can anyone tell my why this doesn't work?

 

 

import asyncio
import keyboard
from bleak import BleakScanner, BleakClient

target_device_name = "Bluno"
DEVICE_INFO_SERVICE_UUID = "0000dfb0-0000-1000-8000-00805f9b34fb"
SERIAL_UUID = "0000dfb1-0000-1000-8000-00805f9b34fb"

async def main():
   # Scan for all available devices
   devices = await BleakScanner.discover()

   for device in devices:
       if device.name == target_device_name:
           print(f"Found target device: {device.name}, {device.address}")

           async with BleakClient(device) as client:
               print(f"Connected: {client.is_connected}")

               services = await client.get_services()
               print("Reading device information...")

               for service in services:
                   if service.uuid == DEVICE_INFO_SERVICE_UUID:
                       print(f"Device Information Service: {service.uuid}")
                       
                       while not keyboard.is_pressed('q'):  # Check if 'q' key is pressed
                           for char in service.characteristics:
                               if char.uuid == SERIAL_UUID:
                                   got_text = await client.read_gatt_char(char)
                                   decoded_text = got_text.decode("utf-8")  # Decode the bytearray to UTF-8 string
                                   print(f"\tData: {decoded_text}")
                                   
asyncio.run(main())

userHeadPic spinal.cord
2024-07-27 02:29:30

Using

SERIAL_PORT_UUID = "0000dfb1-0000-1000-8000-00805f9b34fb"
I can send data to the bluno, but the code you gave does not work, I't claims that a device with the MAC address is not connected, I used the same MAC address that I was using to read from the bluno.

 

 

This code wil send, but does not read…

 

import asyncio
from bleak import BleakClient

# Define the MAC address and UUIDs.
MAC_ADDRESS = "A0:12:34:56:78:B9" # replace with read MAC
SERIAL_PORT_UUID = "0000dfb1-0000-1000-8000-00805f9b34fb"

# Define maximum data size for BLE characteristic write
MAX_DATA_SIZE = 20  # Maximum size in bytes

async def send_data(client, data):
   # Write data to the characteristic in chunks
   for i in range(0, len(data), MAX_DATA_SIZE):
       chunk = data[i:i + MAX_DATA_SIZE]  # Get the chunk of data
       encoded_chunk = chunk.encode('utf-8')  # Encode the chunk as bytes
       await client.write_gatt_char(SERIAL_PORT_UUID, encoded_chunk)
       print(f"Sent data chunk: {chunk}")

       # Read back data after sending
       await read_data(client)

async def read_data(client):
   # Read data from the characteristic
   data = await client.read_gatt_char(SERIAL_PORT_UUID)
   decoded_data = data.decode('utf-8')  # Decode bytes to string
   print(f"Received data: {decoded_data}")

async def main():
   async with BleakClient(MAC_ADDRESS) as client:
       print("Connected to the Bluno Beetle...")

       while True:
           await read_data(client)
           action = input("Type 'send' to send data, or 'exit' to quit: ")
           if action.lower() == 'exit':
               break
           elif action.lower() == 'send':
               data_to_send = input("Enter data to send: ")
               await send_data(client, data_to_send)
           else:
               print("Invalid action. Please type 'send' or 'exit'.")

if __name__ == "__main__":
   loop = asyncio.get_event_loop()
   loop.run_until_complete(main())
 

 

 

 

 

Where did you find RX_CHARACTERISTIC_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" ? I can't seem to find any of this information online at all.

 

 

Thanks.

userHeadPic spinal.cord
2024-07-26 13:21:31

Nobody?

userHeadPic spinal.cord