Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read data via bluetooth
#1
Hi,
I have Teensy/Arduino and HC-05 bluetooth module. I want to send 30000 number values to PC/Python.

I have this code for Arduino/Teensy. It will send array of 500 (just for testing) numers via bluetooth.

#define BUFFER_SIZE     (500)
float buf[BUFFER_SIZE];
char inChar;
float num;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {   
  if (Serial1.available() > 0) {
    inChar = Serial1.read();// get incoming byte:
    for(int i=0; i<BUFFER_SIZE; i++) {
      buf[i] = i;
    }
    Serial1.write((char*)buf, sizeof(buf)); 
    
    Serial.println( sizeof(buf));
    Serial.println("Array sent.");
    for (int i = 0; i < BUFFER_SIZE; i++) Serial.println(buf[i]);
    delay(1000);
   }  
}
This is my Python code. I want to receive array from Teensy/Arduino - but I am no sure how to do it. I just see nonsense numbers.

import bluetooth

v_data = []
bd_addr = "FC:A8:9A:00:22:33" 
port = 1

sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print('Connected')
sock.send("r")
print('Sent char to start sending data from Teensy')

i = 0
while i < 500:
    val = sock.recv(1)
    v_data.append(ord(val))
    i += 1
print(v_data)
Is any way how to read all array correctly?
Reply
#2
you can find the pinouts for the HC-05 here: https://components101.com/wireless/hc-05...oth-module
Reply
#3
Pinout is clear. I have no idea how to read the array. I can read sinle Serial.print(1) but not Serial.write(array)
Reply
#4
It would be helpful to know what you are sending and what you received.
Reply
#5
(Jul-08-2022, 08:46 PM)deanhystad Wrote: It would be helpful to know what you are sending and what you received.

I collect data at Teensy 4.1 from ADXL and convert with 24bit ADC. Now I have 30000 float values in buffer and need to send it to my app in Python.
I have it now via usb cable and it works well but without cable it will be much better. But I am not sure if is possible send 30k float values via bluetooth to PC/Python in less than 1 second and how to read / decode it in Python.

If I send every value separatelly, it works but is is very slow, maybe 20 seconds. If I send all array from Teensy with Serial1.write(buffer, sizeof(buffer)) then I get bytes values but only for example 10 values instead 30000.
Reply
#6
Do you need bluetooth for your application?

Can you run a wire instead.
Teensy 4.1 has ethernet capabilities built, all you need is a small adapter that costs $3.90, see:
https://www.pjrc.com/store/ethernet_kit.html

Do you have the additional memory installed? https://www.pjrc.com/store/psram.html

Please tell us a bit about your application.

Are you planning to use a cell phone as controller and thus the need for bluetooth?

Bluetooth estimate (hope math is correct)
assuming 4 byte floats:

30,000 * 4 = 120K (bytes) or 960K (bits)

still under 1 sec, double for any latency so less than 2 seconds for 30k floats.
Reply
#7
I do not need bluetooth but I have problem with USB cable noise on some laptops. Second thing is that it is a bit more comfortable without cable.
I collect acceleration data on Teensy and send it to PC/laptop via USB cable now. In Python I made vibration analyzer for FFT, etc.
So I have small box with Teensy and 24bit adc and just collect data to buffer and send it to PC. Via USB cable I am able send 30k float values in 0.2 seocnd. But if exist any way how to send it via BT under 1 second it will be great.

On Teensy I am waiting for char and then send data:

void loop() {   
  if (Serial1.available() > 0) {
    inChar = Serial1.read();// get incoming byte:
    for(int i=0; i<BUFFER_SIZE; i++) {
      buf[i] = i; //fill buffer by any numbers, just for testing
    }
then I send buffer via BT:

Serial1.write((char*)buf, sizeof(buf));   //not sure if this is correct way
in Python I just make connection with BT module and the read data:

v_data = sock.recv(30000)
print(v_data)
I am pretty sure this is wrong solution but I have no clue how to read the buffer sent form Teensy and decode it.
Reply
#8
I am checking now my code for usb cable data transfer and try to modify for bluetooth.

Teensy code:

#define BUFFER_SIZE     (300)
float buf[BUFFER_SIZE];
char inChar;
float num;
float adc_volt;

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {   
  if (Serial1.available() > 0) {
    inChar = Serial1.read();// get incoming byte:
    for(int i=0; i<BUFFER_SIZE; i++) {
      adc_volt = random(100);
      byte *b = (byte *)&adc_volt;
      Serial1.write(b[0]);
      Serial1.write(b[1]);
      Serial1.write(b[2]);
      Serial1.write(b[3]);
    }
   }  
}
Python code:

import bluetooth
import struct

SAMPLES= 300
s = struct.Struct('<' + str(SAMPLES) + 'f')
bt_data = []

bd_addr = "FC:A8:9A:00:22:33" 
port = 1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print('Connected')
sock.send("r")
print('Sent char to start sending data from Teensy')

bt_data = sock.recv(SAMPLES*4)
unpacked_data = s.unpack(bt_data)

print(unpacked_data)
print(len(unpacked_data))
I have error message:
Error:
Traceback (most recent call last): File "c:\Users\frohr\bt.py", line 18, in <module> unpacked_data = s.unpack(bt_data) struct.error: unpack requires a buffer of 1200 bytes
Reply
#9
ethernet is not USB, it is a regular network protocol. See: https://en.wikipedia.org/wiki/Ethernet
However I am not finding much that supports teensy ethernet.

Is there a particular reason to use teensy?

There are a lot of other inexpensive processors that have both Wi-Fi and bluetooth which also have powerful MCU's
One example is the seeed XIAO RP2040: https://www.seeedstudio.com/xiao-rp2040-...-5026.html
available for only $5.40 (US). This supports arduino, MicroPython and Circuit Python.

ESP32 or ESP8266 are other low cost alternatives.
Reply
#10
The reason to use Teensy is that I am collectiong data from adxl via ADS1256 (24bit ADC). Timing is very important for me - I have exactly 30000 float values in 1 second. I am pretty sure that Arduino is too slow and low memory but I am not sure about Xiao and ESP - I have to check it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Correctly read a malformed CSV file data klllmmm 2 1,813 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Read nested data from JSON - Getting an error marlonbown 5 1,309 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Connect to HC-05 Bluetooth (NOT BLE) korenron 0 1,421 Jun-26-2022, 09:06 AM
Last Post: korenron
  Read buffer from bluetooth frohr 2 2,077 Jun-01-2022, 01:31 PM
Last Post: frohr
  Write and read back data Aggie64 6 1,812 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  Scan for Bluetooth device korenron 0 2,579 Jan-10-2022, 01:06 PM
Last Post: korenron
  How to read rainfall time series and insert missing data points MadsM 4 2,123 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Sending string commands from Python to a bluetooth device Rovelin 13 9,276 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  ModuleNotFoundError: No module named 'bluetooth' Error Rovelin 4 11,980 Aug-31-2021, 04:04 PM
Last Post: Rovelin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020