Hi guys,
I've found this tutorial(1) that basically simulates sensor data (generating a random number) and sends it over from one arduino to another using a couple nRF24L01 chips.
I've adapted the sketch to read real data from my DS18B20 sensor and it works but its kinda buggy.
First of all, it will kill the 2 decimals that come from the sensor readings (would be handy to actually have the whole reading, e.g. 18.04 instead of 18), and on the other hand, the receiver will only receive one single reading once I open the serial monitor on the transmitter.
So basically I'll be opening the Receiver serial monitor and this will appear:
**************V1 Receive Sensor Data***********
...Listening
After I open the transmitter serial monitor, the transmitter serial monitor will show:
**************V1 Send Sensor Data***********
...Sending
0 data[1]: 0 | data[2]: 25 | Temperature sent: 25
1 data[1]: 0 | data[2]: 25 | Temperature sent: 25
2 data[1]: 0 | data[2]: 25 | Temperature sent: 25
3 data[1]: 0 | data[2]: 25 | Temperature sent: 25
But the receiver will only read the first packet, showing the following:
**************V1 Receive Sensor Data***********
...Listening
0 | Temperature Received: 25
And it will stay like this, while the transmitter keeps sending data every second.
I've been playing around with both the transmitter and receiver sketches but so far no luck. I might have missed something in the loop of the receiver but I can't see what...
Here's my transmitter code:
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);
// ce,csn pins
RF24 radio(8,7);
// init data buffer to hold a sensor type byte and one uint16_t sensor data
unsigned char data[3] = {
0};
unsigned long count=0;
void setup(void)
{
sensors.begin();
Serial.begin(57600);
Serial.println("**************V1 Send Sensor Data***********");
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(0x4c);
// open pipe for writing
radio.openWritingPipe(0xF0F0F0F0E1LL);
radio.enableDynamicPayloads();
radio.setAutoAck(true);
radio.powerUp();
Serial.println("...Sending");
}
void loop(void)
{
//read sensor data
sensors.requestTemperatures();
float currentTemp;
currentTemp = sensors.getTempCByIndex(0);
uint16_t sensorValue = currentTemp;
//assign 'T' to represent a Temperature reading
data[0] = 'T';
//do the bit shift to put uint16 into uint8 array
data[1] = sensorValue >> 8;
data[2] = sensorValue;
Serial.print(count);
count++;
Serial.print(" data[1]: ");
Serial.print(data[1]);
Serial.print(" | data[2]: ");
Serial.print(data[2]);
// print and increment the counter
radio.write(data, sizeof(uint16_t)+1);
Serial.print(" | Temperature sent: ");
Serial.println(sensorValue);
// pause a second
delay(1000);
}
And the receiver sketch:
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
// ce,csn pins
RF24 radio(8,7);
// set up the data buffer to receive the sensor type byte and the uint16_t sensorValue
unsigned char data[3] = {
0};
unsigned long count=0;
void setup(void)
{
// init serial monitor and radio
Serial.begin(57600);
Serial.println("**************V1 Receive Sensor Data***********");
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(0x4c);
// open pipe for reading
radio.openReadingPipe(1,0xF0F0F0F0E1LL);
radio.enableDynamicPayloads();
radio.setAutoAck(true);
radio.powerUp();
radio.startListening();
Serial.println("...Listening");
}
void loop(void)
{
// if there is data ready
if (radio.available())
{
// dump the payloads until we've got everything
bool done = false;
while (!done)
{
// fetch the payload, and see if this was the last one
done = radio.read(data, sizeof(uint16_t)+1);
bool done = true;
}
// print the payload
if (data[0] == 'T') {
//put the uint16_t back together
uint16_t sensorValue = data[2] | data[1] << 8;
Serial.print(count);
count++;
Serial.print(" | Temperature Received: ");
Serial.println(sensorValue);
}
else
Serial.println("Did not receive a value temperature reading");
}
}
I'd appreciate any comments on what could be wrong!
Thanks,
Eric
1: Part 2: Transmitting Data from One nRF24L01 to the Other using One Mac | bitknitting