I'm trying to figure out a way to transmit four sensor values: Voltage, Current, Power and consumption but I'm unable to do that. Yet, my syntax is correct... what am I missing?
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <RF24.h>
#define ACS712 A0
int lastData = 0;
int dataSum = 0;
int Count = 0;
float Voltage;
float Current;
float Power;
float kWh;
float Consumption;
String Data;
float vpc = 4.882813; // approx. voltage per count
RF24 radio(9, 10); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00001
radio.openReadingPipe(1, addresses[0]); // 00002
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
radio.stopListening();
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;
float kWh = Power / 1000;
float Consumption = kWh;
dataSum = 0;
Count = 0;
Data += F("\nVoltage: ");
Data += String(Voltage, 3);
Data += F("\nCurrent: ");
Data += String(Current, 3);
Data += F("\nPower: ");
Data += String(Power, 3);
Data += F("\nConsumption: ");
Data += String(Consumption, 3);
//Serial.println(Data);
radio.write(&Data, sizeof(Data));
if (radio.write(&Data, sizeof(Data)) && true) {
Serial.println("Sent!");
} else {
Serial.println("Failed!");
};
delay(10);
}
}
I'm unable to do thatis meaningless ..... clear description of observed results is meaningfulradio.write(&Data, sizeof(Data)), the String object or its value?0x01for Voltage,0x02for Current etc. That way you don't sendStrings (which in embedded programming you really shouldn't be using!!) and will reduce the data transferred.