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
#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
#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