maniacbug / RF24

Arduino driver for nRF24L01
http://maniacbug.github.com/RF24
950 stars 770 forks source link

ACK fails one direction. #64

Open OOTTEE opened 9 years ago

OOTTEE commented 9 years ago

Hello, I'm trying to connect a raspberry and Arduino, When I try to make a delivery from raspberry to Arduino Arduino packets arrive at, but do not receive the ACK in the raspberry.

otherwise, when sending packages from the Arduino to the raspberry everything works correctly.

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include "./RF24.h"
#include <wiringPi.h>
#include <errno.h>

#define INTERRUPT 4

using namespace std; // CE Pin, CSN Pin, SPI Speed

// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);

// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xFFFFFFFFFFLL, 0x0000000002LL };

char receive_payload[33],output[32]; // +1 to allow room for a terminating NULL char

int count = 0;

bool tx,rx,fail;

float h,t,f;

void interrupt(){
    radio.whatHappened( tx,fail,rx);
    if(rx){
        printf("recibido: ");
        count ++;
        if(radio.available()){
            while(radio.available()){
                radio.read( receive_payload, 32 );
                memcpy(&h, (receive_payload)+10, 4);
                memcpy(&t, (receive_payload)+14, 4);
                memcpy(&f, (receive_payload)+18, 4);
                printf("H:%f C:%f F:%f\n", h,t,f);
            }
        }       
    }
    if(tx){
        printf("enviado\n");
        radio.startListening();
    }
    if(fail){
        printf("fallo\n");
        radio.startListening();
    }
}

int main(int argc, char** argv){

  radio.begin();
  radio.setRetries(5,15);
  radio.setPALevel(RF24_PA_HIGH);
  radio.setAutoAck(1);
  radio.setChannel(50);
  radio.setDataRate(RF24_1MBPS);

  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);

  radio.printDetails();

  radio.stopListening();

  if (wiringPiSetup () < 0) {
      fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
      return 1;
  }

  // set Pin 17/0 generate an interrupt on high-to-low transitions
  // and attach myInterrupt() to the interrupt
  if ( wiringPiISR (INTERRUPT, INT_EDGE_FALLING, &interrupt) < 0 ) {
      fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
      return 1;
  }

  radio.startListening();
  output[0] = 'G';
string txt; 
  while(1){
    cin>>txt;
    radio.stopListening();
    radio.startWrite( &output , 32, 0);
  }

}
#include <Timer.h>
#include <Event.h>
#include <SPI.h>
#include <EEPROM.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <TimerOne.h>
#include "DHT.h"
#include "printf.h"

#define DHTPIN 4
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

//MEMORY POSITIONS
#define CHANNEL_POSITION 10
#define POWER_POSITION   12

uint64_t address_server  = 0xFFFFFFFFFFLL;
uint64_t address_cliente = 0x0000000002LL;

uint8_t output[32];
String inputString = ""; 
String serialAction = "";
boolean stringComplete = false, readValue=false;

//ID identificador de tipo de dispositivo 
#define TYPE "0003" //INTERRUPTOR

// POSITION VALUES PAYLOAD PARAMS [ACTION]
#define ACTION_B 0

// ACTION VALUES
#define GET  'G'

#define OK 'O'
#define FAIL 'F'

// PIN RELES
#define LED 9
#define BUTTON 3

// Hardware configuration
RF24 radio(7,8);  
bool tx,fail,rx;
// PAQUETE RECIVIDO
uint8_t receive_payload[33];
uint8_t action;

/********************** Setup *********************/
void setup(){
  pinMode(BUTTON,INPUT_PULLUP);

  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  delay(500);

  Serial.begin(115200);
  inputString.reserve(200);

  radio.begin(); 
  digitalWrite(LED, LOW);
  printf_begin();
  radio.setPALevel(readData(POWER_POSITION));
  radio.setDataRate(RF24_1MBPS);
  radio.setAutoAck(true);
  radio.setRetries(5,15);
  radio.setChannel(readData(CHANNEL_POSITION));           

  readAddr(&address_cliente);                                             
  radio.openWritingPipe(address_server);
  radio.openReadingPipe(1,address_cliente);

  radio.printDetails();

  radio.whatHappened(tx,fail,rx);   
  radio.startListening();

  delay(50);
  attachInterrupt(0, check_radio, LOW);
  //attachInterrupt(1, push_interrupt,RISING);

  delay(50);

  dht.begin();
  readTemp();

  Serial.println("INICIADO");
}

/********************** Main Loop *********************/
int boton = HIGH;
int pulsacion = 0;

void loop() {   
  boton = digitalRead(BUTTON);
  if( boton == LOW && pulsacion == 0){
      output[9] = GET;
      radio.stopListening();
      radio.startWrite( &output , 32, 0);
      pulsacion = 1;
  }
  if( boton == HIGH && pulsacion == 1){
      pulsacion = 0;
      digitalWrite(LED,!digitalRead(LED));
      delay(100);
  }
}

float h,t,f;
void readTemp(){
  int i;
  h = dht.readHumidity(); 
  t = dht.readTemperature(); //Celsius
  f = dht.readTemperature(true); //Fahrenheit
  for(i=0;i<4;i++){
     output[10+i]= ((uint8_t*)(&h))[i];
     output[14+i]= ((uint8_t*)(&t))[i];
     output[18+i]= ((uint8_t*)(&f))[i];
  }
}

/********************** Interrupt RADIO *********************/
void check_radio(void)                               
{
  radio.whatHappened(tx,fail,rx);                    

  if ( tx ) { //transmisin completada
      radio.startListening();
      printf("Ack Payload:Send\n\r");
  }

  if ( fail ) {
      delay(100);
      radio.stopListening();
      radio.startWrite( &output , 32, 0);
      printf("Ack Payload:Failed\n\r");
  }

  if ( rx ){
      radio.read( receive_payload, 32 );
      Serial.println(radio.isAckPayloadAvailable());
      Serial.println(radio.testRPD());
      action = receive_payload[ACTION_B];
      if(action == GET){
        output[9] = GET;
      }else{ 
        output[9] = FAIL;
      } 
      //radio.stopListening();
      //delay(200);
      //radio.startWrite( &output , 32, 0);
  }
}

void readAddr(uint64_t* addr){
  short pos;
  for (pos = 0; pos < 8; pos++){
    ((uint8_t*)addr)[pos] = EEPROM.read(pos);
     output[pos] = ((uint8_t*)addr)[pos];
  }
}

void writeAddr(uint64_t* addr){
  short pos;
  for (pos = 0; pos < 8; pos++){
     EEPROM.write(pos, ((uint8_t*)addr)[pos]);
     output[pos] = ((uint8_t*)addr)[pos];
  }
}

void saveData(int pos, uint8_t ch){
  EEPROM.write(pos, ch);
}

uint8_t readData(int pos){
  return  EEPROM.read(pos);
}  
}

Thank you very much.

bonnefoi commented 8 years ago

Hello,

If you use the function "wiringPiSetup" the numbering of the pins is made according to the WiringPi Library : the pin number is 0 and not 4 (you can find the correspondence between each pin in the WiringPi library documentation).

Regards, Pierre-François.