Циклические сбои в работе nrf24l01+
Столкнулся с не совсем корректной работой с модулями nrf24l01+. У меня проект умного дома. Строю свой протокол дабы обойти ограничение в 6 устройств. Проблема в том что модули циклически именно с какой-то системой (как по мне) перестают отвечать на запросы. Если модуль будет один то все будет ОК!(за исключением редких ошибок). База NodeMCU 1.0 (esp8266 12-e). Сенсоры Arduino Nano (Код для разных сенсоров был изменен, тобиж были измены труби прослушивания и переменная id_sensor) Прикол в том, что при одном сенсоре все работает правильно и стабильно, но при подключении второго, идут циклические ошибки, тоесть один из сенсоров попросту не отвечает на запрос! Код базы
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(D4, D8);
struct message {
uint8_t id;
uint8_t type;
uint8_t data;
};
struct info_ {
uint8_t id_sensor;
uint8_t type_sensor;
int16_t v_bat;
uint8_t status_sensor;
uint8_t signal_lvl;
uint8_t person_pipe[1][6];
};
message rx_data;
info_ reg_pack;
uint8_t address[20][6] = {"0Node", "1Node", "2Node", "3Node", "5Node", "9Node"}; //0-Заглушка;1-Alarms;2-Reg; 3-Get_data; Another sesors!
boolean get_data_all() {
radio.stopListening();
radio.powerUp();
radio.openWritingPipe(address[4]);
uint8_t data = 101;
boolean tx = radio.write(&data, sizeof(data));
Serial.print("TX status:");
Serial.println(tx);
radio.startListening();
uint8_t i = 0;
while (i < 15) {
if (radio.available()) {
break;
}
i++;
delay(150);
}
if (i == 15) {
Serial.println("1 sensor ban =(");
}
else {
message rx_firsth;
Serial.print("Data recived! ");
radio.read(&rx_firsth, sizeof(rx_firsth));
Serial.print("Id:");
Serial.print(rx_firsth.id);
Serial.print(" Data:");
Serial.print(rx_firsth.data);
}
delay(150);
radio.stopListening();
radio.powerUp();
radio.openWritingPipe(address[5]);
uint8_t data2 = 101;
boolean tx2 = radio.write(&data2, sizeof(data2));
Serial.print("TX2 status:");
Serial.println(tx2);
radio.startListening();
uint8_t i2 = 0;
while (i2 < 15) {
if (radio.available()) {
break;
}
i2++;
delay(150);
}
if (i2 == 15) {
Serial.println("2 sensor ban =(");
}
else {
message rx_two;
Serial.print("Data recived! ");
radio.read(&rx_two, sizeof(rx_two));
Serial.print("Id:");
Serial.print(rx_two.id);
Serial.print(" Data:");
Serial.print(rx_two.data);
}
return true;
}
void setup() {
Serial.begin(115200);
radio.begin();
radio.setAutoAck(true);
radio.setChannel(120);
radio.openReadingPipe(1, address[1]);
radio.setAutoAck(1, true);
radio.openReadingPipe(2, address[2]);//Reg pipe
radio.setAutoAck(2, true);
radio.openReadingPipe(3, address[3]);//Data-info pipe
radio.setAutoAck(3, true);
radio.openWritingPipe(address[0]);//Trash pipe
radio.setRetries(3, 15);
radio.setPALevel (RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.startListening();
radio.powerUp();
}
void loop() {
if (Serial.available() > 0) {
String buffer_ = Serial.readString();
if (buffer_ == "tx") {
Serial.println("Start sending...");
get_data_all();
}
}
}
Код сенсора
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(9, 10);
uint8_t id_sensor = 19;
uint8_t type_sensor = 1;
struct message {
uint8_t id;
uint8_t type;
uint8_t data;
};
struct info_ {
uint8_t id_sensor;
uint8_t type_sensor;
int16_t v_bat;
uint8_t status_sensor;
uint8_t signal_lvl;
uint8_t person_pipe[1][6];
};
message rx_data;
info_ reg_pack;
uint8_t address[20][6] = {"0Node", "1Node", "2Node", "3Node", "5Node", "9Node"}; //0-Заглушка;1-Alarms;2-Reg; 3-Get_data; Another sesors!
void setup() {
Serial.begin(115200);
radio.begin();
radio.setAutoAck(true);
radio.setChannel(120);
radio.openReadingPipe(1, address[5]);
radio.setAutoAck(1, true);
radio.openWritingPipe(address[3]);
radio.setRetries(3, 15);
radio.setPALevel (RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.startListening();
radio.powerUp();
}
void loop() {
if (radio.available()) {
uint8_t lol = 0;
radio.read(&lol, sizeof(lol));
Serial.println("Radio available!");
radio.stopListening();
radio.powerUp();
radio.openWritingPipe(address[3]);
message mess;
mess.id = id_sensor;
mess.type = type_sensor;
mess.data = (uint8_t)random(1,20);
boolean tx = radio.write(&mess, sizeof(mess));
Serial.print("Send status:");
Serial.println(tx);
radio.startListening();
}
}
Ответы (1 шт):
Вообщем проблема решена! Надо было отключить autoAck! Проблема скорее всего была из-за того что я постоянно менял трубы и nrf не мог ловить пакет об успешной передачи, и собственно датчик также не всегда отправлял данные База
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(D4, D8);
struct message {
uint8_t id;
uint8_t type;
uint8_t data;
};
struct info_ {
uint8_t id_sensor;
uint8_t type_sensor;
int16_t v_bat;
uint8_t status_sensor;
uint8_t signal_lvl;
uint8_t person_pipe[1][6];
};
message rx_data;
info_ reg_pack;
uint8_t address[20][6] = {"0Node", "1Node", "2Node", "3Node", "5Node", "9Node"}; //0-Заглушка;1-Alarms;2-Reg; 3-Get_data; Another sesors!
void radio_init() {
radio.begin();
radio.setAutoAck(false);
radio.setChannel(120);
radio.openReadingPipe(1, address[1]);
radio.setAutoAck(1, false);
radio.openReadingPipe(2, address[2]);//Reg pipe
radio.setAutoAck(2, false);
radio.openReadingPipe(3, address[3]);//Data-info pipe
radio.setAutoAck(3, false);
radio.openWritingPipe(address[0]);//Trash pipe
radio.setRetries(3, 15);
radio.setPALevel (RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
radio.powerUp();
}
boolean get_data_all() {
radio_init();
radio.stopListening();
radio.powerUp();
radio.openWritingPipe(address[4]);
uint8_t data = 101;
boolean tx = radio.write(&data, sizeof(data));
Serial.print("TX status:");
Serial.println(tx);
radio.startListening();
uint8_t i = 0;
while (i < 15) {
if (radio.available()) {
break;
}
i++;
delay(150);
}
if (i == 15) {
Serial.println("1 sensor ban =(");
}
else {
message rx_firsth;
Serial.print("Data recived! ");
radio.read(&rx_firsth, sizeof(rx_firsth));
Serial.print("Id:");
Serial.print(rx_firsth.id);
Serial.print(" Data:");
Serial.print(rx_firsth.data);
}
delay(150);
radio.stopListening();
radio.powerUp();
radio.openWritingPipe(address[5]);
uint8_t data2 = 101;
boolean tx2 = radio.write(&data2, sizeof(data2));
Serial.print("TX2 status:");
Serial.println(tx2);
radio.startListening();
uint8_t i2 = 0;
while (i2 < 15) {
if (radio.available()) {
break;
}
i2++;
delay(150);
}
if (i2 == 15) {
Serial.println("2 sensor ban =(");
}
else {
message rx_two;
Serial.print("Data recived! ");
radio.read(&rx_two, sizeof(rx_two));
Serial.print("Id:");
Serial.print(rx_two.id);
Serial.print(" Data:");
Serial.print(rx_two.data);
}
return true;
}
void setup() {
Serial.begin(115200);
radio_init();
}
void loop() {
if (Serial.available() > 0) {
String buffer_ = Serial.readString();
if (buffer_ == "tx") {
Serial.println("Start sending...");
get_data_all();
}
}
}
Сенсор
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(9, 10);
uint8_t id_sensor = 19;
uint8_t type_sensor = 1;
struct message {
uint8_t id;
uint8_t type;
uint8_t data;
};
struct info_ {
uint8_t id_sensor;
uint8_t type_sensor;
int16_t v_bat;
uint8_t status_sensor;
uint8_t signal_lvl;
uint8_t person_pipe[1][6];
};
message rx_data;
info_ reg_pack;
uint8_t address[20][6] = {"0Node", "1Node", "2Node", "3Node", "5Node", "9Node"}; //0-Заглушка;1-Alarms;2-Reg; 3-Get_data; Another sesors!
void radio_init() {
radio.begin();
radio.setAutoAck(false);
radio.setChannel(120);
radio.openReadingPipe(1, address[5]);
radio.setAutoAck(1, false);
radio.openWritingPipe(address[3]);
radio.setRetries(3, 15);
radio.setPALevel (RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
radio.powerUp();
}
void setup() {
Serial.begin(115200);
//radio_init();
radio.begin();
radio.setAutoAck(false);
radio.setChannel(120);
radio.openReadingPipe(1, address[5]);
radio.setAutoAck(1, false);
radio.openWritingPipe(address[3]);
radio.setRetries(3, 15);
radio.setPALevel (RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
radio.powerUp();
}
void loop() {
if (radio.available()) {
uint8_t lol = 0;
//radio_init();
delay(10);
radio.read(&lol, sizeof(lol));
Serial.println("Radio available!");
radio.stopListening();
radio.powerUp();
radio.openWritingPipe(address[3]);
message mess;
mess.id = id_sensor;
mess.type = type_sensor;
mess.data = (uint8_t)random(1, 20);
boolean tx = radio.write(&mess, sizeof(mess));
Serial.print("Send status:");
Serial.println(tx);
radio.startListening();
}
}
