Created
February 6, 2025 21:34
-
-
Save DamirAksenov/9253d732861161a5757df9adeaaaefa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <RF24Network.h> | |
#include <RF24.h> | |
#include <SPI.h> | |
RF24 radio(7, 8); // CE, CSN pins | |
RF24Network network(radio); | |
const uint16_t satellite = 00; | |
const uint16_t main_station = 01; | |
const uint16_t second_station = 02; | |
const uint16_t this_node = main_station; | |
const size_t MAX_FRAGMENT_SIZE = 31; // Максимальный размер фрагмента | |
void setup() { | |
Serial.begin(9600); | |
SPI.begin(); | |
radio.begin(); | |
radio.setChannel(90); | |
radio.setDataRate(RF24_250KBPS); | |
radio.setPALevel(RF24_PA_MAX); | |
network.begin(90, this_node); // Channel 90 | |
} | |
void loop() { | |
network.update(); | |
if (this_node == main_station){ | |
if(Serial.available()){ | |
char c = char(Serial.read()); | |
if (c == '1'){ | |
//Send to satellite | |
String payload = "Hello there!!!!"; | |
sendString2(payload, satellite); | |
} else if (c == '2'){ | |
//Send to second station | |
String payload = "TRANSFER TO SATELLITE"; | |
sendString2(payload, second_station); | |
} else if (c == '3'){ | |
String payload = "11111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222222"; | |
sendStringBlock(payload, satellite); | |
} | |
} | |
} | |
if (this_node == satellite){ | |
String data = receiveString2(); | |
} | |
if (this_node == second_station){ | |
String data = receiveString2(); | |
if (data == "TRANSFER TO SATELLITE"){ | |
String payload = "11111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999000000000011111111112222222222"; | |
sendStringBlock(payload, satellite); | |
} | |
} | |
//delay(20); | |
} | |
bool sendString2(const String& payload, uint16_t to_node) { | |
RF24NetworkHeader header(to_node); | |
bool ok = network.write(header, payload.c_str(), payload.length() + 1); | |
if (!ok) { | |
Serial.println("Failed to send to node " + String(to_node)); | |
} else { | |
Serial.println("Sended to node " + String(to_node)); | |
} | |
return ok; | |
} | |
bool sendStringBlock(const String& payload, uint16_t to_node) { | |
Serial.println("Length: " + String(payload.length())); | |
for (size_t i = 0; i < payload.length(); i += MAX_FRAGMENT_SIZE) { | |
String fragment = payload.substring(i, i + MAX_FRAGMENT_SIZE); | |
Serial.println("Fragment: " + fragment + " " + String(i)); | |
RF24NetworkHeader header(to_node, 'F'); // 'F' для фрагмента | |
bool ok = network.write(header, fragment.c_str(), fragment.length() + 1); | |
delay(50); | |
if(!ok){ | |
Serial.println("Failed to send fragment " + String(i) + " to node " + String(to_node)); | |
} | |
} | |
// Отправляем сигнал окончания сообщения | |
RF24NetworkHeader endHeader(to_node, 'E'); | |
bool ok = network.write(endHeader, nullptr, 0); | |
return ok; | |
} | |
String receiveString2() { | |
while (network.available()) { | |
RF24NetworkHeader header; | |
char payload[32]; | |
network.read(header, &payload, sizeof(payload)); | |
Serial.println("Received: " + String(payload) + " from node " + String(header.from_node)); | |
return String(payload); | |
} | |
} | |
String receiveStringBlock(){ | |
String receivedMessage = ""; | |
while (network.available()) { | |
RF24NetworkHeader header; | |
uint16_t message_size = network.peek(header); | |
if (header.type == 'F') { // Фрагмент | |
char* buffer = new char[message_size + 1]; | |
network.read(header, buffer, message_size); | |
buffer[message_size] = '\0'; | |
receivedMessage += String(buffer); | |
delete[] buffer; | |
} else if (header.type == 'E') { // Конец сообщения | |
network.read(header, nullptr, 0); | |
break; | |
} | |
} | |
return receivedMessage; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment