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();
}
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.
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);
}
}