Hi,
So I've been working on a project which involves sending sensor data from about 4 transmitters (Nano) to a single receiver (Mega); all of which are interfaced to a NRF24L01 module.
The challenge I have is how to efficiently configure the transmitters to send their individual data to the receiver at different time intervals and also allowing for each one to only send info when the receiver is free and no other transmitter is transferring its data!
This is a necessary feature because the receiver is supposed to log each transmitter data into different files.
Find my code so far below:
Transmitter...
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "RF24.h"
#include "printf.h"
#define ACS712 A0
unsigned long lastData = 0;
int dataSum = 0;
int Count = 0;
float vpc = 4.8828126; // approx. voltage per count
RF24 radio(9, 10); // CE, CSN
struct Sensor1Data {
float v1;
float c1;
float p1;
float k1;
} Sensor1;
unsigned char ADDRESS0[5] = {0xb1, 0x43, 0x88, 0x99, 0x45}; // Define a static TX address
//just change b1 to b2 or b3 to send to other pip on resciever
void setup()
{
radio.begin();
Serial.begin(115200);
printf_begin();
radio.setPALevel(RF24_PA_MIN);
radio.enableDynamicPayloads();
radio.setDataRate(RF24_250KBPS);
radio.setChannel(124);
radio.openWritingPipe(ADDRESS0);
radio.openReadingPipe(0, ADDRESS0);
radio.stopListening();
}
void loop()
{
if (millis() > lastData + 1) {
dataSum += sq(analogRead(ACS712) - 512);
Count++;
lastData = millis();
}
// If statement to make calculations.
if (Count == 1000) {
// To get the RMS of the data.
float mean = dataSum / Count;
float value = sqrt(mean);
// To get the Voltage (in volts) consumed.
float Voltage = value * vpc;
// To get the Current (in amps) consumed.
float Current = Voltage / 66;
// To get the Power (in watts) consumed.
float Power = Current * 240;
//To get the Consumption rating (in kilowatthour)
float Consumption = Power / 1000;
dataSum = 0;
Count = 0;
//Attach values to struct
Sensor1.v1 = Voltage;
Sensor1.c1 = Current;
Sensor1.p1 = Power;
Sensor1.k1 = Consumption;
Serial.println(Sensor1.v1);
Serial.println(Sensor1.c1);
Serial.println(Sensor1.p1);
Serial.println(Sensor1.k1);
//Transmit data and check if it goes succesfully!
bool ok = radio.write(&Sensor1, sizeof(Sensor1));
if (ok) {
Serial.println("Sent!");
} else {
Serial.println("Failed!");
}
delay(100);
}
}
MasterReceiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "RF24.h"
#include "printf.h"
RF24 radio(53, 48); // CE, CSN
#define PloadSize 32 // 32 unsigned chars TX payload
byte pipe;
byte CurrentPloadWidth;
byte newdata;
unsigned char rx_buf[PloadSize] = {0};
struct Sensor1Data {
float v1;
float c1;
float p1;
float k1;
} Sensor1;
struct Sensor2Data {
float v2;
float c2;
float p2;
float k2;
} Sensor2;
struct Sensor3Data {
float v3;
float c3;
float p3;
float k3;
} Sensor3;
unsigned char ADDRESS2[1] = {0xb2};
unsigned char ADDRESS3[1] = {0xb3};
unsigned char ADDRESS4[1] = {0xb4};
unsigned char ADDRESS5[1] = {0xb5};
unsigned char ADDRESS1[5] = {0xb1, 0x43, 0x88, 0x99, 0x45}; // Define a static TX address
unsigned char ADDRESS0[5] = {0xb0, 0x43, 0x88, 0x99, 0x45}; // Define a static TX address
void setup()
{
radio.begin();
printf_begin();
Serial.begin(115200);
radio.setDataRate(RF24_250KBPS);
radio.enableDynamicPayloads();
radio.setChannel(124);
radio.openWritingPipe(ADDRESS0);
radio.openReadingPipe(0, ADDRESS0);
radio.openReadingPipe(1, ADDRESS1);
radio.openReadingPipe(2, ADDRESS2);
radio.openReadingPipe(3, ADDRESS3);
radio.openReadingPipe(4, ADDRESS4);
radio.openReadingPipe(5, ADDRESS5);
radio.startListening();
delay(10);
}
void loop()
{
if (radio.available(&pipe)){
// Fetch the payload, and see if this was the last one.
CurrentPloadWidth = radio.getDynamicPayloadSize();
// If a corrupt dynamic payload is received, it will be flushed
if (!CurrentPloadWidth) {
}
else
{
radio.read(rx_buf, CurrentPloadWidth);
newdata = 1;
}
}
if (newdata == 1) {
newdata = 0;
if (pipe == 1 && CurrentPloadWidth == sizeof(Sensor1))
{
memcpy(&Sensor1, rx_buf, sizeof(Sensor1));
Serial.println("Data from Sensor 1: ");
Serial.println(Sensor1.v1);
Serial.println(Sensor1.c1);
Serial.println(Sensor1.p1);
Serial.println(Sensor1.k1);
}
if (pipe == 2 && CurrentPloadWidth == sizeof(Sensor2))
{
memcpy(&Sensor2, rx_buf, sizeof(Sensor2));
Serial.println("Data from Sensor 2: ");
Serial.println(Sensor2.v2);
Serial.println(Sensor2.c2);
Serial.println(Sensor2.p2);
Serial.println(Sensor2.k2);
}
if (pipe == 3 && CurrentPloadWidth == sizeof(Sensor3))
{
memcpy(&Sensor3, rx_buf, sizeof(Sensor3));
Serial.println("Data from Sensor 3");
Serial.println(Sensor3.v3);
Serial.println(Sensor3.c3);
Serial.println(Sensor3.p3);
Serial.println(Sensor3.k3);
}
Serial.println("");
}
delay(100);
}
Thanks for your contributions!