freezkyd
Posts: 8
Joined: Wed Aug 19, 2015 5:18 am

network.available() in rPi always false

Wed Aug 19, 2015 5:20 am

I can make two arduinos work together but not a rPi and an Arduino:

base.cpp
```
#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <time.h>

using namespace std;

//////////////////////////////////////
// Create the structures for data
//////////////////////////////////////
struct data_received {
uint16_t node;
uint16_t temp;
uint16_t humi;
uint16_t light;
uint16_t door;
uint16_t pir;
uint32_t hkey;
};

struct data_to_send {
uint32_t hkey;
}confirm;

unsigned long finish = 0;
unsigned long elapsed = 0;

//////////////////////////////////////
// Setup nRF24L01+ module & Oled
//////////////////////////////////////
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);

RF24Network network(radio);
const uint64_t this_node = 00; // base

//////////////////////////////////////
// loop
//////////////////////////////////////

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

//////////////////////////////////////
// setup
//////////////////////////////////////

// Initialize interfaces
cout<<"Starting interfaces...";
radio.begin();
radio.setRetries(7,7);
delay(5);
cout<<"complete]\n";
network.begin(90, this_node);
radio.setDataRate(RF24_250KBPS);
radio.printDetails();

while(1) {

network.update();
data_received payload;

cout<<"**"<<network.available()<<'\n';

// listen for incoming clients
while ( network.available() )
{
RF24NetworkHeader header;
confirm.hkey = 0;

// read the next available header
network.peek(header);

// print NRF24 header data
cout<<"\n[header info => id: ";
cout<<header.id<<" type: ";
cout<<header.type<<" from_node: ";
cout<<header.from_node<<"]";

// read data
network.read( header, &payload, sizeof(payload) );

// Print data received
cout<<"[NODE: "<<payload.node<<" ]=> ";
cout<<"[temp: "<<payload.temp<<"*C] ";
cout<<"[humi: "<<((float)payload.humi)<<"%] ";
cout<<"[light: "<<((float)payload.light)<<"%] ";
cout<<"[door: "<<payload.door<<"] ";
cout<<"[pir: "<<payload.pir<<"] ";
cout<<"[hkey: "<<payload.hkey<<"] ";

// send back data to confirm integrity
confirm.hkey = payload.hkey;
cout<<"...";
RF24NetworkHeader header2(header.from_node);

// starts counting just before sending
elapsed = 0;
unsigned long start = millis();

bool sent_back = network.write( header2, &confirm, sizeof(confirm));

if (sent_back == true){
finish = millis();
elapsed = finish - start;
cout<<elapsed;
cout<<"ms";
cout<<" - Confirmed back \n";
}
else{
cout<<"Not confirmed";
}

}

int value = 0;
float vin = 0.0;
value = payload.light;
vin = 3.472 * value / 1024;
cout<<"VIn: "<<vin<<'\n';

cout<<"Listening for nodes.."<<'\n';
delay(1000);
}
return 0;
}
```

a node (arduino)
```

#include <dht11.h>
#include "RF24Network.h"
#include "RF24.h"
#include <SPI.h>
#include "printf.h"
#include <JeeLib.h>

//////////////////////////////////////
// Create the structures for data
//////////////////////////////////////
struct data_to_send {
uint16_t node;
uint16_t temp;
uint16_t humi;
uint16_t light;
uint16_t door;
uint16_t pir;
uint32_t hkey;
};

struct data_received {
uint32_t hkey = 0;
};

//////////////////////////////////////
// Setup nRF24L01+ module
//////////////////////////////////////
RF24 radio(8,7);
RF24Network rf24Net(radio);
const uint64_t this_node = 02; // node A
const uint64_t other_node = 00; // base

//////////////////////////////////////
// Setup the rest of it
//////////////////////////////////////

const int led_A_Pin = 6;
const int optoPin = A2;

unsigned long currentTime = 0;
unsigned long startTime = 0;
bool runOnce = true;

ISR(WDT_vect) { Sleepy::watchdogEvent(); } // Setup the watchdog

//////////////////////////////////////
// setup
//////////////////////////////////////
void setup(void) {

// Initialize interfaces
printf_begin();
Serial.begin(9600);
Serial.print(F("Starting interfaces..."));
SPI.begin();
radio.begin();
Serial.print(F("complete]\n"));
rf24Net.begin(90, this_node);
radio.setDataRate(RF24_250KBPS);
radio.printDetails();
analogReference(INTERNAL);

// initialize pins
pinMode(led_A_Pin, OUTPUT);

}

//////////////////////////////////////
// loop
//////////////////////////////////////
uint32_t txTimer = 0;

void loop(void)
{
digitalWrite(led_A_Pin, HIGH);
delay(200);
digitalWrite(led_A_Pin, LOW);
delay(200);
digitalWrite(led_A_Pin, HIGH);

currentTime = millis();

if (currentTime - startTime <= 1000) {
Serial.println(currentTime - startTime);
}
else {
digitalWrite(led_A_Pin, LOW);
}

rf24Net.update();

if (millis() - txTimer > 1000) {
data_to_send payload;
payload.node = 02;
payload.temp = 11;
payload.humi = 11;

payload.light = analogRead(A2);
payload.door = 0;
payload.pir = 0;
payload.hkey = random(10000000, 99999999);


// starts counting
unsigned long elapsed = 0;
unsigned long start = millis();
Serial.println(payload.hkey);

digitalWrite(led_A_Pin, HIGH);
delay(100);
digitalWrite(led_A_Pin, LOW);
delay(100);
digitalWrite(led_A_Pin, HIGH);
delay(100);
digitalWrite(led_A_Pin, LOW);
delay(100);

// sends packet
RF24NetworkHeader header(other_node);

Serial.println("Sending data to base every 30 sec.");
txTimer = millis();
bool ok = rf24Net.write( header, &payload, sizeof(payload) );
if (ok) {
unsigned long finish = millis();
elapsed = finish - start;
Serial.println("ok.");
Serial.print(elapsed);
Serial.print("ms\n");
rf24Net.update();
}
else {
Serial.println("failed.");
}
}

while ( rf24Net.available() )
{
Serial.println("Received packet");
RF24NetworkHeader header2;
data_received confirm;
rf24Net.read(header2, &confirm, sizeof(confirm));
Serial.println(confirm.hkey);
Serial.println("------------------------------");

Serial.println(F("Going to sleep..."));
delay(100);
radio.powerDown();
Sleepy::loseSomeTime(2000);
radio.powerUp();
Serial.println(F("Woke up!"));
}
}
```

Can you please help? Is it a bug or am I doing something wrong ?

freezkyd
Posts: 8
Joined: Wed Aug 19, 2015 5:18 am

Re: network.available() in rPi always false

Thu Aug 20, 2015 5:00 pm

https://github.com/TMRh20/RF24Network/issues/54

anyone with experience in RPi <=> nRF24s ?

Return to “Networking and servers”