Exporting serial data from Arduino in real time

Hello,

I'm inquiring in regard to the feasibility of a project and the approach I should take. I'd like to send serial data via bluetooth to a Mac and parse the data within a Mac program in real time. On the Arduino end, what steps would I need to take to prepare and send the data to the computer? I am using an Arduino Portenta H7.

I'm not sure how bluetooth data is handled. Is it possible to parse the data into JSON on the Arduino end and send the data in that format to the computer for it to be read?

Any help would be greatly appreciated. Thanks.

Yes

Thanks for your response! Is it possible able to send packets of data at once (JSON objects), or does it have to line by line, or character by character?

Yes, if you can find suitable libraries at the sending and receiving sides. In practice, those libraries are, "behind the scenes"/"under the hood", sending the JSON packet as lines and characters.

Okay, thank you. I have one more question: Is data sent through bluetooth via Serial.print(), or are there libraries with functions for that process?

Not Serial.print(). That would send the characters to the Serial Monitor via USB cable, if connected.

I don't know much about the Portentious H7, but I imagine it would have some other Stream set up for the Bluetooth data, if connected as a serial device. If that stream is called XYZ, you would use XYZ.print().

Okay, thank you for your help.

Yes. This is achieved by connecting Bluetooth to hardware serial pins 0,1, and no libraries are required, hence the code is exactly the same as that used to send the data to the serial monitor via USB cable.

You don't say which Arduino you are using but note that the port on pin 0,1 is usually shared with the serial monitor. This means that, if you do this:
1 you can observe two way serial traffic on the monitor
2 you cannot send from the PC
3. Bluetooth must be disconnected while you are uploading your programme.

Look around in the Portenta library examples and tutorials.

Hint: a cleverly constructed internet search, perhaps using a search phrase something like "arduino portenta bluetooth data", turns up many possibly relevant hits.

1 Like

Thank you for your responses. I have a few of follow questions. In the documentation below, it has the lines defining a ledService and BLEByteCharacteristic. Can they be any string of letters and numbers? Does it need to be the same? Does it need to follow a certain format? Are these both for different types of connections (regular bluetooth and bluetooth low energy)?

Lastly, what function(s) in the BLE library transmit data?

https://docs.arduino.cc/tutorials/portenta-h7/ble-connectivity/

Hello,

I have a couple questions in regard to using the Arduino Portenta as a peripheral to transmit data via bluetooth. I was looking at the tutorial linked below and I'm unsure about a couple of things.

First, I need clarification on the following two lines (the code I'm referencing can be found here: https://docs.arduino.cc/tutorials/portenta-h7/ble-connectivity/):

BLEService ledService("19b10000-e8f2-537e-4f6c-d104768a1214");

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19b10000-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite);

Do the strings in these two declared variables need to be the same? Can they be any combination of letters and numbers in that format? Are they each for two different types of Bluetooth connections (Bluetooth and Bluetooth Low Energy)? Do they both need to be declared?

Second, I'm sending data in JSON format. What function in the ArduinoBLE library sends the data via Bluetooth?

Any information would be greatly appreciated. Thank you.

@mfusco ,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

My apologies. I reviewed the link.

1 Like

I have an additional question:

What sensor could I use to detect the distance that the sensor itself traveled (in all 3 axis) down to the millimeter?

Thank you.

have a look at Pololu TOF sensors and Pololu proximity-sensors-and-range-finder

Thank you for your response. I have follow up questions:

I have an accelerometer that outputs values between -1 and 1 for all three axis depending on the orientation of the sensor. When I looked up information about reading accelerometer data, I read that it senses and outputs acceleration data. I'm trying to calculate velocity and displacement with the following expressions:

vx = vx + ax * dt
x = vx * dt

I thought to plug in my x-axis reading into ax and 100ms to dt (my program reads and averages readings in groups of five; each reading is every 20ms).

The data and calculations I'm getting doesn't seem to be working out. The value I'm getting for vx (which is declared as xAvg in the code below) does not reflect the velocity of the sensor, and when the accelerometer is tilted on an axis, the value I'm using for ax increases or decreases depending on the orientation. When the sensor is parallel with the flat surface that it's on, the value from the accelerometer reads 0. The code that I have is below. Any help would be greatly appreciated. Thank you.

#include <Wire.h>
#include <SparkFun_KX13X.h> 

SparkFun_KX132 kxAccel;

outputData data;

float x[5];

float xAvg = 0;

float vx = 0;

float ax = 0;

float xPos = 0;

const float dt = 100;

int points = 0;
int i = 0;

void setup() {
  Wire.begin();

  Serial.begin(115200);
  Serial.println("Welcome.");

  while (!Serial)
    delay(50);

  if (!kxAccel.begin()) {
    Serial.println("Could not communicate with the the KX13X. Freezing.");
    while (1)
      ;
  }

  Serial.println("Ready.");

  if (kxAccel.softwareReset())
    Serial.println("Reset.");

  delay(5);

  kxAccel.enableAccel(false);

  kxAccel.setRange(SFE_KX132_RANGE16G); 

  kxAccel.enableDataEngine(); 

  kxAccel.enableAccel();
}

void loop() {
  if (kxAccel.dataReady()) {
    kxAccel.getAccelData(&data);

    if (i < 5) {
      x[i] = data.xData;
      i++;
    } else {
      for (int ind = 0; ind < 5; ind++) {
        xAvg += x[ind];
      }

      xAvg /= 5;

      if (xAvg > -0.02 && xAvg < 0.02) xAvg = 0.0;

      vx = vx + xAvg * dt; // vx = vx + ax * dt
      xPos = vx * dt // x = vx * dt

      Serial.print(xAvg);
      Serial.print(", ");
      Serial.print(vx);
      Serial.print(", ");
      Serial.println(xPos);
     
      clearValues();
    }
  }

    delay(20);
}

void clearValues() {
  for (int ind = 0; ind < 5; ind++) {
    x[ind] = 0;
  }

  xAvg = 0;

  i = 0;
}

Hello,

I'm seeking a solution instead of calculating displacement with an accelerometer, as I've heard from multiple sources is not feasible. Is there any kind of sensor that can detect movement/displacement/position in all three axis, within a certain range?

To reiterate, I'm looking for a sensor kind of like a gps that senses the xyz position of the sensor within a certain area.

Any insight would be greatly appreciated. Thank you.

Have a look at pozyx.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.