Go Down

Topic: NRF24 trying to debug issues (Read 441 times) previous topic - next topic

notsolowki

Jun 24, 2019, 03:39 am Last Edit: Jun 24, 2019, 11:26 am by notsolowki
I'm using NRF24 cards to communicate by sending a packet the packet is read by another NRF24 card and if the payload of the packet match what im looking for then NRF24 card 2 send a response the respose is read and processed by the master arduino with the main NRF24 card. Then the master NRF24 card sends packet to card 3 and the same for card 4 all in the same sequence of send then receive. when im close APROX. 10 ft away it works perfect then i move to 30ft away and it works for a few seconds but then i dont get a repose from either card. however in the unlikely event that it does work from 30 ft or more away it only works for a short period and its never just 1 card thats having issues it seems like 2 of the 3 slaves quit around the same time. im not sure what the cause is. i use decoupling caps and rc fitlers on a 3.3 linear regulators all powered from a battery source. it dont matter if i set retries to max datarate to low, or gains to min,med,high it all has the same outcome. im my code i clear the buffers. Also my master main transmitting NRF24 card is an amplified version and the other cards are not. how can i debug this card to see what is actually happen with the packet im new to NRF24 technology and i have read the classes for the library most of it i understand but not how to read the registers or how to print any useful debugging info.

notsolowki

#1
Jun 24, 2019, 05:01 am Last Edit: Jun 24, 2019, 05:02 am by notsolowki
here i have simplified my programs sorry it took so longdoes anyone see why it crashes?  i think the problem MAY be in the master code?

MASTER TX
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <EEPROM.h>


unsigned long mark = 0;
unsigned long nrftimeout = 0;
unsigned long  doselinkWait = 0;

unsigned long paramsdelay = 0;

bool nrfEnabled = true;

bool sendFailed = false;

const int pinCE = 22;
const int pinCSN = 24;


float incomingArray[32];
float RX1Request = 12.34;
float RX2Request = 12.34;

int x, y;
int updateState = 1;
int state = 1;
int statecounter = 0;

float RX1Value1 = 11.11;
float RX2Value1 = 12.21;
float RX3ShowParams = 20.20;
float RX3ShowParams2 = 21.21;
float tUpdateCommand = 16.61;
float Value2 = 22.22;
float p2Value = 5.80;
float p1Value = 5.80;

float p1Update = 22.22;



unsigned long retryRefreshMarker = 0;


RF24 wirelessSPI(pinCE, pinCSN);

float IncomingFloatValue = 0.0;
const uint32_t keyword = 0xFADEDEAD;
const uint16_t keywordAddress = 0x00;
const uint16_t paramAddress = keywordAddress + sizeof(keyword);

struct __attribute__ ((packed)) _paramS {
  float p1Set = 5.90;
  float p2Set = 5.90;
} txParams;

const uint64_t pAddress = 0xB00B1E5000F1;
const uint64_t pAddress1 = 0xB00B1E5000F2;
const uint64_t pAddress2 = 0xB00B1E5000F0;

void setup() {
  getParam();
  Serial.begin(115200);
  wirelessSPI.begin();
  wirelessSPI.setDataRate(RF24_250KBPS);
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setCRCLength(8);
  wirelessSPI.setRetries(25, 25);
  wirelessSPI.setPALevel(RF24_PA_LOW);
  Serial.println("hello");

}

void saveParam() {
  EEPROM.put(keywordAddress, keyword);
  EEPROM.put(paramAddress, txParams);
}

void getParam() {
  uint32_t tmpKey;
  EEPROM.get(keywordAddress, tmpKey);
  if (tmpKey == keyword) {
    EEPROM.get(paramAddress, txParams);    // EEPROM was already initialized OK to read
  } else {

  }
}

void loop() {

  if (state >= 0) {
    readBothResponses();
  }

  if (nrfEnabled) {
    if (updateState == 1) {
      requestRX1update();
      retryRefreshMarker = millis();
      updateState = 2;
    }

    if (millis() - retryRefreshMarker > 250) {
      if (updateState == 2) {
        wirelessSPI.flush_rx();
        requestRX2update();
        wirelessSPI.flush_tx();
        retryRefreshMarker = millis();
        updateState = 3;

      }
    }
    if (millis() - retryRefreshMarker > 250) {
      if (updateState == 3) {
        requestRX3update();
        updateState = 4;
        retryRefreshMarker = millis();
      }
    }
    if (millis() - retryRefreshMarker > 250) {
      if (updateState == 4) {
        requestRX3Paramsupdate();
        updateState = 5;
      }
    }
    if (millis() - retryRefreshMarker > 450) {
      if (updateState == 5) {
        updateState = 1;
      }
    }
  }
}

void requestRX1update() {
  float Array2[1] = {RX1Request};
  wirelessSPI.stopListening();
  wirelessSPI.openWritingPipe(pAddress2);
  if (!wirelessSPI.write( &Array2, sizeof(Array2))) {
    Serial.println("rx1 failed");

  }
  else {

    wirelessSPI.stopListening();
    wirelessSPI.openReadingPipe(1, pAddress2);
    wirelessSPI.startListening();
    Serial.println(" rx1 sent");
  }
}
void requestRX2update() {
  float Array[1] = {RX2Request};
  wirelessSPI.stopListening();
  wirelessSPI.openWritingPipe(pAddress);
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
  }
 
  else {
    wirelessSPI.stopListening();
    wirelessSPI.openReadingPipe(1, pAddress);
    wirelessSPI.startListening();
  }
}
void requestRX3Paramsupdate() {
  wirelessSPI.stopListening();
  wirelessSPI.openWritingPipe(pAddress1);
  float Array[3] = {RX3ShowParams};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    wirelessSPI.stopListening();
  }
  else {
    wirelessSPI.stopListening();
    wirelessSPI.openReadingPipe(1, pAddress1);
    wirelessSPI.startListening();
  }
}

void requestRX3update() {
  wirelessSPI.stopListening();
  wirelessSPI.openWritingPipe(pAddress1);
  float Array[3] = {tUpdateCommand};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
    wirelessSPI.stopListening();
  }
  else {
    wirelessSPI.stopListening();
    wirelessSPI.openReadingPipe(1, pAddress1);
    wirelessSPI.startListening();
  }
}

void readBothResponses() {
  if (statecounter >= 10) {
    state = 0;
    statecounter = 0;
  }
  statecounter ++;
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    float incomingResponse2 = float(incomingArray[0]);

    if (incomingResponse2 == RX3ShowParams) {
      Serial.println("rx3 params 0");
    }
    if (incomingResponse2 == RX3ShowParams2) {
      Serial.println("rx3 params 1");


    }
    if (incomingResponse2 == p1Update) {
      Serial.println("rx1 response");
    }
    if (incomingResponse2 == RX2Value1) {
      Serial.println("rx2 response");

    }
  }
}







notsolowki

SLAVE RX1
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const int pinCE = 9;
const int pinCSN = 10;
const int p1Pin = A3;
const int p2Pin = A4;
int sendState = 1;

bool recvState = true;

float p1Value = 5.89;
float p1Update = 22.22;
float incomingArray[32];
float p1Request = 12.34;
float p2Value = 5.89;
unsigned long retryRefreshMarker = 0;
unsigned long respondDelay = 0;
unsigned long pRefreshMarker = 0;
int buf[10], temp;
int buf2[10], temp2;
unsigned long int p1avgValue;
unsigned long int p2avgValue;
RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000F0;

void setup() {
  Serial.begin(57600);
  Serial.println("BOOTING");
  Serial.println("REV 1.0.6");
  wirelessSPI.begin();
   wirelessSPI.setDataRate(RF24_250KBPS);
   wirelessSPI.setPALevel(RF24_PA_LOW);
  wirelessSPI.setAutoAck(1);
   wirelessSPI.setCRCLength(8);
  wirelessSPI.enableAckPayload();
  // wirelessSPI.setChannel(35);
  wirelessSPI.setRetries(10, 10);
  wirelessSPI.openReadingPipe(1, pAddress);
//  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.startListening();
}

void loop() {
  if (recvState){
  readResponse();
  }

  if (sendState == 1) {
    recvState = false;
    if (millis() - respondDelay > 320) {
      sendData();
      sendState = 0;
      recvState = true;
      respondDelay = millis();
    }
  }
  if (millis() - pRefreshMarker > 800) {
            for (int i = 0; i < 10; i++)  {
              buf[i] = analogRead(p1Pin);
            }
            for (int i = 0; i < 10; i++)  {
              buf2[i] = analogRead(p2Pin);
            }

            p1avgValue = 0;
            for (int i = 2; i < 8; i++)
              p1avgValue += buf[i];
            float pVol = (float)p1avgValue * 5.0 / 1024 / 6;
            p1Value = -5.7 * pVol + 24.4;
            p2avgValue  = 0;
            for (int i = 2; i < 8; i++)
              p2avgValue  += buf2[i];
            float pVol2 = (float)p2avgValue  * 5.0 / 1024 / 6;
            p2Value = -5.7 * pVol2 + 24.4;

            pRefreshMarker = millis();
          }
}

void sendData() {
 // Serial.println("sending ata");
  wirelessSPI.stopListening();
  wirelessSPI.openWritingPipe(pAddress);
  float Array[3] = {p1Update, p1Value, p2Value};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
   // Serial.println("send failed");
  }
  else {
  //  Serial.println("Send okayY");


  }
}
void readResponse() {
  wirelessSPI.startListening();
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    //Serial.println("sending try");
    float incomingResponse = float(incomingArray[0]);
    if (incomingResponse == p1Request) {
      sendState = 1;
      wirelessSPI.flush_rx();
      wirelessSPI.flush_tx();
    }
  }
}


SLAVE RX2

Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <EEPROM.h>

int sendState = 0;

const int pinCE = 9;
const int pinCSN = 10;
const int Pump1 = 4;
const int Pump2 = 5;

float commandValue = 0.0;
float incomingCommand = 0.0;
float incomingArray[32];
float Request = 12.34;
float Value1 = 12.21;
float Value2 = 22.22;
float setUpTime1 = 2222.2222;
float setDownTime1 = 2222.3333;
float pIsHigh = 123.123;
float pIsLow = 321.321;
unsigned long OnMarker = 0;
unsigned long UpOnMarker = 0;

unsigned long respondDelay = 0;
unsigned long UpCounter = 0;
unsigned long DownCounter = 0;
unsigned long mark = 0;

int pState = 0;

RF24 wirelessSPI(pinCE, pinCSN);
const uint64_t pAddress = 0xB00B1E5000F1;

const uint32_t keyword = 0xFADEDEAD;
const uint16_t keywordAddress = 0x00;
const uint16_t paramAddress = keywordAddress + sizeof(keyword);

struct __attribute__ ((packed)) _paramS {
  unsigned long DownTime1 = 2500;
  unsigned long UpTime1 = 2500;
} dParams;


void setup() {
  getParam();
  Serial.begin(57600);
  wirelessSPI.begin();
  wirelessSPI.setDataRate(RF24_250KBPS);
  wirelessSPI.setAutoAck(1);
  wirelessSPI.enableAckPayload();
  wirelessSPI.setRetries(15, 15);
  wirelessSPI.setPALevel(RF24_PA_HIGH);
  wirelessSPI.openReadingPipe(1, pAddress);
  wirelessSPI.openWritingPipe(pAddress);
  wirelessSPI.startListening();
  Serial.println("hello");
  pinMode(Pump1, OUTPUT);
  pinMode(Pump2, OUTPUT);
}

void loop() {

  readResponse();

  if (sendState == 1) {
    if (millis() - respondDelay > 100) {
      sendStatus();
      sendState = 0;
      respondDelay = millis();
    }
  }
  //////
  if (pState == 1) {
    digitalWrite(Pump2, HIGH);
    if (millis() - OnMarker > dParams.DownTime1) {
      digitalWrite(Pump2, LOW);

      pState = 0;
    }
  }
  if (pState == 2) {
    digitalWrite(Pump1, HIGH);
    if (millis() - UpOnMarker > dParams.UpTime1) {
      digitalWrite(Pump1, LOW);
      pState = 0;
    }
  }
}


void readResponse() {
  wirelessSPI.startListening();
  while (wirelessSPI.available()) {
    wirelessSPI.read( &incomingArray, sizeof(incomingArray) );
    incomingCommand = float(incomingArray[0]);
    if (incomingCommand == Request) {
      commandValue = float(incomingArray[1]);
      respondDelay = millis();
      sendState = 1;
    }
    if (incomingCommand == pIsHigh) {//////////////
      Serial.print("Got Status High ");
      OnMarker = millis();
      pState = 1;
      DownCounter ++;
    }
    if (incomingCommand == pIsLow) {
      Serial.print("Got Status Low ");
      UpOnMarker = millis();
      pState = 2;
     UpCounter ++;
    }
    if (incomingCommand == setDownTime1) {
      for (byte y = 1; y < 2; y++) {
        unsigned long commandValue = float(incomingArray[y]);
        dParams.DownTime1 = commandValue;
      }
    }
  }
 // wirelessSPI.flush_rx();
}

void sendStatus() {

  wirelessSPI.stopListening();
  wirelessSPI.openReadingPipe(1, pAddress);
  float Array[3] = {Value1, UpCounter, DownCounter};
  if (!wirelessSPI.write( &Array, sizeof(Array))) {
   // Serial.println("delivery failed");
  //   wirelessSPI.flush_tx();
  }
  
  else {
  //  Serial.println("Send successful.");
  //  wirelessSPI.flush_tx();
  }
}
void saveParam()
{
  EEPROM.put(keywordAddress, keyword);
  EEPROM.put(paramAddress, dParams);
}

void getParam()
{
  uint32_t tmpKey;
  EEPROM.get(keywordAddress, tmpKey);
  if (tmpKey == keyword) {
    EEPROM.get(paramAddress, dParams);    // EEPROM was already initialized OK to read
  } else {
    saveParam();
  }
}

notsolowki

#3
Jun 24, 2019, 09:22 am Last Edit: Jun 24, 2019, 11:22 am by notsolowki
the strangest part is when the comms start to fail if i manually call "requestRX1update()" from serial input i see the response and it works then fails again. whats the difference of my calling requestRX1update() manually or by it being called in the loop. im lost, heresa link to the library im using. it acts like its sending successfully  but on the receiving end i can confirm it did not get my message. then i manually call requestRX1update(); and it works every time and rx2 starts working again for a very short period. rx3 always works i dont seem to have many problems with it. why does me calling requestRX1update() have any effect like this when its "supposedly"  be called in the loop after updatState == 1 and the timers expire??

notsolowki

#4
Jun 24, 2019, 11:53 am Last Edit: Jun 24, 2019, 11:54 am by notsolowki
for a temporary fix i placed  requestRX1update(); to be called when i get a valid response from rx3. this shouldnt have to happen i cant think of a reason why it works the way it does. as long as rx3 is powered on and sending replies the program works and this is not what im looking for

notsolowki

nobody, really? this is not  even a bug in my code this is a a bug in ide

Whandall

this is not  even a bug in my code this is a a bug in ide
Very funny.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

notsolowki


notsolowki

#8
Jun 24, 2019, 09:43 pm Last Edit: Jun 24, 2019, 10:07 pm by notsolowki
go ahead smart guy, tell me why this works

Code: [Select]
  if (millis() - retryRefreshMarker2 > 40) {
    requestRX1update();
    retryRefreshMarker2 = millis();
  }

  if (nrfEnabled) {
    if (updateState == 1) {
      //Serial.println("requestrx1");

      //requestRX1update();
    //  retryRefreshMarker = millis();
      updateState = 2;
    }

    if (millis() - retryRefreshMarker > 15) {
      if (updateState == 2) {
        wirelessSPI.flush_rx();
        requestRX2update();
        wirelessSPI.flush_tx();
        retryRefreshMarker = millis();
        updateState = 3;

      }
    }
    if (millis() - retryRefreshMarker > 15) {
      if (updateState == 3) {
        requestRX3update();
        updateState = 4;
        retryRefreshMarker = millis();
      }
    }
    if (millis() - retryRefreshMarker > 15) {
      if (updateState == 4) {
        requestRX3Paramsupdate();
        updateState = 5;
        retryRefreshMarker = millis();
      }
    }
    if (millis() - retryRefreshMarker > 20) {
      if (updateState == 5) {
        updateState = 1;
      }
    }
  }


but this dont 
Code: [Select]
  if (nrfEnabled) {
    if (updateState == 1) {
      requestRX1update();
      retryRefreshMarker = millis();
      updateState = 2;
    }

    if (millis() - retryRefreshMarker > 250) {
      if (updateState == 2) {
        wirelessSPI.flush_rx();
        requestRX2update();
        wirelessSPI.flush_tx();
        retryRefreshMarker = millis();
        updateState = 3;

      }
    }
    if (millis() - retryRefreshMarker > 250) {
      if (updateState == 3) {
        requestRX3update();
        updateState = 4;
        retryRefreshMarker = millis();
      }
    }
    if (millis() - retryRefreshMarker > 250) {
      if (updateState == 4) {
        requestRX3Paramsupdate();
        updateState = 5;
      }
    }
    if (millis() - retryRefreshMarker > 450) {
      if (updateState == 5) {
        updateState = 1;
      }
    }
  }
}


this is obviously the compilers fault. i should mention that i did put a serial.print right underneath     requestRX1update(); and its SUPPOSEDLY called in either case but only in the top half with     requestRX1update(); outside of updateState does it actually send any data to rx1

Whandall

I was about to give you some comments regarding your code, but I decided I don't like your manners.

Good luck with your project.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

notsolowki

I was about to give you some comments regarding your code, but I decided I don't like your manners.

Good luck with your project.
yea please leave with you dignity and your useless information. if you dont know what your talking about then dont comment

Whandall

if you dont know what your talking about then dont comment
You are really funny. :D
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

notsolowki

#12
Jun 24, 2019, 09:59 pm Last Edit: Jun 24, 2019, 10:02 pm by notsolowki
You are really funny. :D
look i've been struggling with this same problem for a month now. something doesn't make sense. i dont have a doctorate in c++ but i cant see why the top works but the bottom half doesn't.

Romonaga

RF24 is a fantastic bit of gear.  They can be temperamental at times. 

I suspect the amplified one is overpowering the other devices.  So let's see if that's the case.  Do you have another one that is not amplified.  If so hook that one up and retest.
The universe exists only because we are aware of it.
We want a few mad people now. See where the sane ones have landed us!

notsolowki

RF24 is a fantastic bit of gear.  They can be temperamental at times. 

I suspect the amplified one is overpowering the other devices.  So let's see if that's the case.  Do you have another one that is not amplified.  If so hook that one up and retest.
sorry i didnt mention im using 1 amplified as the master that calls rx1,rx2 etc. the slave modules are all non + units non amplifier on them. the problem persisted even with just one module powered on. the loop acts as if it just skips  requestRX1update();; after a few seconds. but if i manually call it from serial or put it somewhere else in the loop it runs fine

Go Up