#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#define v_kef 100
uint8_t id_sensor = 0;
uint8_t type_sensor = 1;
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 v_kef = 100;
uint8_t pipe = 0;
//uint8_t address[][6] = {0xAABBCCDD11LL, 0xAABBCCDD22LL, 0xAABBCCDD33LL, 0xAABBCCDD44LL, 0xAABBCCDD55LL};
uint8_t address[20][6] = {"0Node", "1Node", "2Node", "3Node"}; //0-Заглушка;1-Alarms;2-Reg; 3-Get_data; Another sesors!
uint8_t pipes_elems() {
uint8_t memory = sizeof(address) / sizeof(address[0]);
uint8_t elems = 0;
if (memory <= 254) {
for (uint8_t i = 0; i < memory; i++) {
if (address[i][0] != 0) {
elems++;
}
}
}
else {
Serial.println("ERROR! Size of array more 255 elements!");
}
return elems;
}
boolean get_info(uint8_t elem) {
}
boolean get_data(uint8_t elem) {
Serial.print("Elem:");
Serial.print(elem);
radio.stopListening();
radio.openWritingPipe(address[elem]);
boolean tx = false;
uint8_t data = 101;
for (uint8_t i = 0; i < 20; i++) {
tx = radio.write(&data, sizeof(data));
if (tx) {
break;
}
delay(200);
}
radio.startListening();
if (!tx) {
Serial.println(" Sensor not talk with me!");
return false;
}
Serial.print(" Sended! ");
uint8_t i = 0;
uint8_t pipe = 0;
while (i < 20) {
if (radio.available(&pipe)) {
Serial.print(pipe);
if (pipe == 3) {
break;
}
}
i++;
delay(150);
}
if (i < 15) {
radio.read(&rx_data, sizeof(rx_data));
Serial.print(" Id: ");
Serial.print(rx_data.id);
Serial.print(" Data:");
Serial.println(rx_data.data);
return true;
}
else {
Serial.println("Responce not recived!");
return false;
}
}
void get_data_all() {
Serial.println("Start polling sensors!");
if (pipes_elems() > 4) {
for (uint8_t i = 4; i <= pipes_elems() - 1; i++) {
get_data(i);
delay(20);
}
}
else {
Serial.println("Not found sensors!");
}
}
boolean register_() {
radio.stopListening();
Serial.print("Scaning:");
radio.openWritingPipe(address[2]);
boolean tx = false;
uint8_t status_ = 200; //200 - granted!
for (uint8_t i = 0; i < 15; i++) {
tx = radio.write(&status_, sizeof(status_));
if (tx) {
break;
}
}
radio.startListening();
if (!tx) {
Serial.println("not found!");
return false;
}
Serial.println("found!");
uint8_t pipe_r = 0;
for (uint8_t i = 0; i < 20; i++) {
if (radio.available(&pipe_r)) {
if (pipe_r == 2) {
break;
}
}
delay(200);
}
if (pipe_r != 2) {
Serial.println("REG cansel, respocne not recived!");
return false;
}
radio.read(®_pack, sizeof(reg_pack));
if (reg_pack.id_sensor != 0 or reg_pack.signal_lvl < 100) {//Verifying..
Serial.println("Reg cansel, sensor not verifyed!");
return false;
}
Serial.println("Sensor OK! Continue reg him!");
reg_pack.id_sensor = random(1, 230);
Serial.print("Him new id is:");
Serial.println(reg_pack.id_sensor);
radio.stopListening();
boolean tx_second = false;
for (uint8_t i = 0; i < 20; i++) {
tx_second = radio.write(®_pack, sizeof(reg_pack));
if (tx_second) {
break;
}
delay(10);
}
if (!tx_second) {
Serial.println("Reg error, packet not recived!");
radio.startListening();
return false;
}
Serial.println("Reg OK!");
radio.startListening();
memcpy(address[pipes_elems()], reg_pack.person_pipe[0], sizeof(address[0]));
return true;
}
void setup() {
Serial.begin(115200);
radio.begin();
radio.setAutoAck(true);
radio.setChannel(120);
radio.openReadingPipe(1, address[1]);//Alarm pipe
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.setPALevel (RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.startListening();
radio.powerUp();
delay(100);
Serial.println();
Serial.print("Pipes:");
Serial.println(pipes_elems());
}
void loop() {
if (Serial.available() > 0) {
String buffer_ = Serial.readString();
if (buffer_ == "tx") {
Serial.println("Start sending...");
get_data_all();
}
else if (buffer_ == "reg") {
Serial.println("Start registering...");
register_();
}
}
}