Wire.onReceive(hadler) terminated only one time

Hello everybody!
I am trying to send a temperature and an altitude from bmp280 from one arduino nano to another.
Unfortunately, I faced with a problem: the Hadler function called only one time.

MASTER CODE

Спойлер

#include <Adafruit_BMP280.h>
#include <Wire.h>
#define ADDRESS 5

Adafruit_BMP280 bmp;

void setup() {
Wire.begin();
Serial.begin(9600);

if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
while (1) delay(10);
}

bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. /
Adafruit_BMP280::SAMPLING_X2, /
Temp. oversampling /
Adafruit_BMP280::SAMPLING_X16, /
Pressure oversampling /
Adafruit_BMP280::FILTER_X16, /
Filtering. /
Adafruit_BMP280::STANDBY_MS_500); /
Standby time. */

}

void loop() {
delay(50);

String data = String(bmp.readTemperature()) + ";" + String(bmp.readAltitude(1013.25));
Wire.beginTransmission(5);
Wire.write(data.c_str());
Serial.println(Wire.endTransmission());
delay(50);

}

SLAVE CODE

Спойлер

#include <Wire.h>

#define ADDRESS 5

void setup() {
Wire.begin(ADDRESS);
Serial.begin(9600);
Wire.onReceive(handle_data);

}

void loop() {

delay(10);

}

void handle_data(){
String s ="";
while (Wire.available() > 1) {
char literal = Wire.read();
s += literal;
}
Serial.println(s);
}

OUTPUT on the SLAVE side

Спойлер

problem

I know that the problem here:
while (Wire.available() > 1) {
char literal = Wire.read();
s += literal;
}

If I change it to:
while (Wire.available() >= 1) {
char literal = Wire.read();
s += literal;
}
everything will be good, but I don't understand why.
Could you explain me the problem ?

This leaves 1 byte in the pipe. Why don't you want to read everything received?

I see it, but I can't understand a logic of the mistake (((

What is the problem in understanding that "x >= 1" will be true while x is above or equal to one, whereas "x > 1" will be true while x is above one?

This is not a problem, the problem is in the following:
Handler function terminates only one time if I use:
while (Wire.available() > 1)
Why this function does not call again in this case ?
Should I read all data from the Wire buffer before the next call of the Handler function ?
If yes - why ?

Because it noticed that you don't want to process further input?

Yes, why not?

If you earn 20 Ruble, why do you want to collect only 19?

If you do not empty incoming data the Wire library cannot detect new, incoming data and thus will not trigger a new onReceive event.

1 Like

Tnx a lot!

That can't be true. Is that documented or in the source code somewhere ?

So what is a truth ?

Never looked, but as long as an event has been triggered for incoming data, there should not be triggered a new event before the data the event was triggered for has been dealt with. OP's question would most likely not exist if I'm wrong.