I’m trying to use NRF24L01+ to send data from my sensors to the spark core. They work fine and I can get data from them individually and they are also constantly updated. This is part of the code.
if (radio.available())
{
bool done = false;
while (!done)
{
done = radio.read(data, sizeof(uint16_t)+1);
}
if (data[0] != 'NULL'){
char transmitter = data[0];
uint16_t sensorValue = data[2] | data[1] << 8;
if (transmitter == 'A'){
moistureA = sensorValue;
} else if (transmitter == 'B') {
moistureB = sensorValue;
}
delay(1000);
} else {
Serial.println("No radio available...");
}
However, the moment I add in this line
sprintf(message, "{\"SmartGarden\":{\"moistureA\":\"%d\",\"moistureB\":\"%d\",\"temperature\":\"%d\",\"humidity\":\"%d\"}}",moistureA,moistureB,temperature,humidity);
to combine all my sensor values into 1 variable to output as a JSON, the whole thing seems to break and can only read the data once and it stops updating. My received variables are all being sent to a variable in the spark called sensorValue.
I’m not sure but after narrowing it down the problem could be in the fact that all values are being sent to the variable called sensorValue or it could be the problem with sprintf itself since it breaks when I add that. Any ideas?
[Mod Edit - Solution - @HarrisonHJones]
Character array message was a little too short. Changing char message[80]
to char message[150]
fixed the problem. The desired string now fits inside of the allocated memory and sprintf does not overflow the buffer.