1

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);
  }
}
4
  • I'm unable to do that is meaningless ..... clear description of observed results is meaningful
    – jsotola
    Dec 5, 2018 at 3:27
  • 1
    Hint: What is the payload max size? And what would be transmitted for radio.write(&Data, sizeof(Data)), the String object or its value? Dec 5, 2018 at 10:29
  • 1
    Have you tried sending command values instead of string values? For example, send 0x01 for Voltage, 0x02 for Current etc. That way you don't send Strings (which in embedded programming you really shouldn't be using!!) and will reduce the data transferred.
    – Tom
    Dec 5, 2018 at 15:10
  • Why not just send the voltage, and do all those calculations on the receiver, as there are just basic division/multiplication with set constants. Also, first try a more basic program to get the radio working. Were you able to run one of the example sketches, and get the radios working?
    – Gerben
    Dec 5, 2018 at 15:26

1 Answer 1

2

Simple float transmission:

For TX use

float payload = 0.12345;
radio.write(&payload, sizeof(payload));

For RX use

float payload = 0;
radio.read(&payload, sizeof(payload));
1
  • here he wants to transmit the complete string, not a single float value your answer will be applicable if it is a single float but here the question deals with how he can transmit a complete string.
    – Maaz Sk
    May 2, 2021 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.