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?
Pinout is clear. I have no idea how to read the array. I can read sinle Serial.print(1) but not Serial.write(array)
It would be helpful to know what you are sending and what you received.
(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.
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.
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.
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
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.
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.