Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: RoboDocFezy on Apr 18, 2019, 12:32 pm

Title: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: RoboDocFezy on Apr 18, 2019, 12:32 pm
hey guys ,
M making a simple temperature transmitter receiver system using NRF24l01 modules , the goal is to install a remote module that gathers temperature data via ds18b20 and transmits via nrf to a base station that datalogs the temp data on serial . since the remote temperature module needs to be powered by a battery i used a lowpower library on Arduino pro mini 3.3v to transmit data only once every 8secs, and on the base its just a arduino mega with NRF attached .

the problem m facing is that every 10 to 20 packets i lose one or 2 packets , how can i make sure it doesnt happen , for initial testing the modules are kept just a few feet apart , code is given below

transmitter code Arduino pro mini 3.3v

Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "LowPower.h"
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

RF24 radio(9, 10); // CE, CSN

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const byte address[6] = "10001";
int count = 0 ;
byte sensorid = 0x01;

struct Tempdata {
  byte s_id;
  float celc;
  int count;
};
Tempdata packet;


void setup() {
 
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate( RF24_250KBPS );    //RF24_250KBPS //RF24_1MBPS //RF24_2MBPS
  radio.setRetries(15, 15);             // delay, count
  radio.setAutoAck(false);
  radio.stopListening();

}


void loop() {
  radio.powerUp(); // new bit for LP
  delay(5);  // new bit for LP

  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperature readings
  Serial.println("DONE");

  packet.celc = sensors.getTempCByIndex(0);
  packet.count = count;
  packet.s_id = sensorid;
  radio.write(&packet, sizeof(packet));

  count++;

   delay(100);
  radio.flush_tx();

// new bit added for LP
  radio.powerDown();
  MCUCR |= (3 << 5); //set both BODS and BODSE at the same time
  MCUCR = (MCUCR & ~(1 << 5)) | (1 << 6); //then set the BODS bit and clear the BODSE bit at the same time
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); // sleep for 4 seconds with all IOs OFF

}


Recieving end code Arduino Mega
Code: [Select]

#include <SPI.h>

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



byte node[6] = "10001";

RF24 radio(7, 8); // CE, CSN

struct Tempdata {
  byte s_id;
  float celc;
  int count;

};
Tempdata temp;
Tempdata packet[4];
int counter;



void setup() {
  Serial.begin(9600);

  radio.begin();
  radio.setPALevel(RF24_PA_MAX); //set data rate 256kpbs radio , set auto ack off
  radio.setDataRate( RF24_250KBPS );    //RF24_250KBPS //RF24_1MBPS //RF24_2MBPS
  radio.setRetries(15, 15);             // delay, count
  radio.setAutoAck(false);
  radio.startListening();
}

void loop() {

    radio.openReadingPipe(0, node);
    radio.startListening();
    Nrf_read(0);
    delay(50);
    radio.stopListening();
}


void Nrf_read(int x) {
  int error;
  if (radio.available()) {
    radio.read(&temp, sizeof(temp));
    packet[x] = temp;
    Serial.print("Sensor ID: ");
    Serial.print(packet[x].s_id);
    Serial.print("  ;  ");
    Serial.print("Temperature is: ");
    Serial.print(packet[x].celc);
    Serial.print("  ;  ");
    Serial.print("Count : ");
    Serial.print(packet[x].count);
    Serial.println("  ;  ");
  }
 
  else if (!radio.available())
   error++;
 }



m using a array because in next step i want recieve temprature from multiple modules
Title: Re: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: Robin2 on Apr 18, 2019, 03:33 pm
The Exhibition section is for working demo programs, not problems. I have suggested to the Moderator to move your question to the Networking section.

Have a look at this Simple nRF24L01+ Tutorial (http://forum.arduino.cc/index.php?topic=421081).

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work



How are you powering your nRF24s? Inadequate power is a common cause of problems. Try powering them with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R
Title: Re: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: Whandall on Apr 18, 2019, 07:04 pm
Code: [Select]
  radio.setRetries(15, 15);             // delay, count
  radio.setAutoAck(false);

This configuration is just silly.
There will be no retries or delays if autoAck is switched off.
Title: Re: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: Robin2 on Apr 18, 2019, 09:24 pm
There will be no retries or delays if autoAck is switched off.
And if autoAck is disabled it quite likely explains the lost packets.

...R
Title: Re: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: Whandall on Apr 18, 2019, 09:28 pm
And if autoAck is disabled it quite likely explains the lost packets.
At least nobody will retry and collisions happen on a shared medium.  ;)
Title: Re: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: RoboDocFezy on Apr 19, 2019, 06:38 am
How are you powering your nRF24s? Inadequate power is a common cause of problems. Try powering them with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R
I'm powering the transmitter via usb outlet .

also just ran it for 10 hours straight and got just 206 packet losses out of 4908 thats approx 4%packet loss
Title: Re: Simple NRF24L01 based temperature sensor transmitter/receiver
Post by: Robin2 on Apr 19, 2019, 08:33 am
I'm powering the transmitter via usb outlet .
You can't connect an nRF24 module to a USB outlet - so if you want help please provide a detailed description.

...R