Sending characteristic value by I2C

Hi all,

My system consists of an nRF52 BLE micro and an ESP32.
I receive a username to the nRF52 and I have to send it to the ESP32 the I store it there.
My issue is in the sender part, with the format of the packages.
I use the BLEperipheral.h library and the Wire.h library
An example: I write my username in an app "PABLO_123" and I want to store this value on the ESP32.

Sender Code:

void sendCredentials(){

  //use slave_receiver_esp32 in esp32.
  
  Whatever_format userName = My_characteristic.value();
  Wire.beginTransmission(55); // transmit to device 0x55
  Wire.write(userName);              // sends one byte
  Wire.endTransmission();  
}

Receiver code (just an example to print the value):

#include <Wire.h>

void setup() {
  Wire.begin(55);                // join I2C bus with address #55
  Wire.onReceive(receiveEvent);  // register event
  Serial.begin(9600);            // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {

  char c = Wire.read();  // receive byte as a character
  Serial.print(c);       // print the character
  Serial.print(" ");
  int x = Wire.read();  // receive byte as an integer
  Serial.println(x);    // print the integer
  Serial.println();
}

Thank you.

The username "PABLO_123" is 9 chars long and thus needs 10 bytes to store it. But you send only one byte:

What do you try to achieve?

And about the receiver - although you transmitted only one byte, you try to read two....

The comment "//sends one byte" is from a previous test sorry. I want to send all the bytes.
As for the receiver, it also changed, now I try to read a message depending on its size:

void receiveEvent(int howMany) {

  char msg[howMany];
  for (int i = 0; i < howMany; i++) {
      msg[i] = Wire.read();
      Serial.print(msg[i]);
    }

I'm trying this with the sender:

void sendCredentials(){
  
  Whatever_format username = test_char.value(); //Don't know what format can I use
  if (My_Characteristic.value()>0) {
  Wire.beginTransmission(55); // transmit to device 0x55
  Wire.write(username); //I should write an array and do it inside a for loop?
  Wire.endTransmission();  
  delay(500);
  }

}

With this code, if I write "PABLO_123" the receiver prints only P.

indeed
The line:

sends only one byte.

I know, that's the reason I ask here, I want to know how to do it.

You have to use multi-byte syntax of the write method:

Wire.write(username, username_len);

where username_len is length of the array.
The way to calculate the length depends on username variable type.

But how do I create an array with the value I wrote from the app?

I don't know, because you did not specified the fornat of username:

I think it has to be char. But if needed I can use another format.

You can't just assign char array with line like this:

Whatever_format username = test_char.value();

So you need to know exactly what the type
test_char.value() operator returns.

By the way, try this code:

void sendCredentials(){
  
  Whatever_format username = test_char.value(); //Don't know what format can I use
  if (My_Characteristic.value()>0) {
  Wire.beginTransmission(55); // transmit to device 0x55
  Wire.write(username,10); //I should write an array and do it inside a for loop?
  Wire.endTransmission();  
  delay(500);
  }

}

What it will receive?

In the library I find this:

unsigned const char* BLECharacteristic::value() const {
  return this->_value;
}

where _value is:

unsigned char*                        _value;

So the type is "pointer to unsigned char".
Please try the code from post #11

I don't know what format to use for username. if I use unsigned char it fails

What format do you used in the code that prints "P' as you said?

not the unsigned char, but pointer to it.
Do you understand the difference between

unsigned char a; // char

and

unsigned char* b; // pointer to char

I know more or less the difference but not much about how to apply it.

const unsigned char username= *test_char.value();
  if (test_char.value()>0) {
      Wire.beginTransmission(55); // transmit to device 0x55
      Wire.write(username); 
      Wire.endTransmission();  
      delay(500);
  }

This is the code that only writes the first byte.
if I write Wire.write(username,10); , I have this error:

src\main.cpp:320:23: error: no matching function for call to 'write(const unsigned char&, int)'
     Wire.write(username,10);

I didn't realize the pointer is in the wrong place.

const unsigned char* username= test_char.value();
  if (test_char.value()>0) {
    Wire.beginTransmission(55); 
    Wire.write(username,10); 
    Wire.endTransmission();  
  delay(500);
  }

This works! Thank you!

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