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
Спойлер

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 ?