Mein Ziel ist es mit einem Empfänger meherer Sender abarbeiten zu können. Die Attinys sind jeweils die Sender und der Arduino ist der Empfänger.
Der Sender Code schaut so aus:
#include <RF24Network.h>
#include <RF24.h>
#define CE PA2
#define CSN PA3
const int ledPin = PA7; // Pin für die LED (PA7)
RF24 radio(CE, CSN); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 01; // Address of our node in Octal format (04,031, etc)
const uint16_t base_node = 00; // Address of the other node in Octal format
uint8_t deviceNumber_value = 3;
char ID[6] = "00001";
struct PayloadStruct {
char message[6]; // only using 6 characters for TX & RX payloads
uint8_t Dev;
uint8_t counter;
};
PayloadStruct payload;
void setup() {
pinMode(ledPin, OUTPUT);
payload.message[5] = 0;
radio.begin();
network.begin(90, this_node); // (channel, node address)
radio.setDataRate(RF24_250KBPS);
memcpy(payload.message, ID, 5); // set the outgoing message
payload.Dev = deviceNumber_value;
payload.counter = 0;
}
void loop() {
network.update();
RF24NetworkHeader header1(base_node); // (Address where the data is going)
network.write(header1, &payload, sizeof(payload)); // Send the data
unsigned long start_timeout = millis(); // timer to detect no response
while (!radio.available()) { // wait for response or timeout
if (millis() - start_timeout > 300) // only wait 300 ms
break;
}
PayloadStruct received;
if (radio.available()) { // Check if there is a response
radio.read(&received, sizeof(received)); // Read the response
// Check if the received message is "True"
if (strcmp(received.message, "True") == 0) {
digitalWrite(ledPin, HIGH); // Turn LED on if message is "True"
} else {
digitalWrite(ledPin, LOW); // Turn LED off otherwise
}
}
// Blink LED while waiting for response
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
und der Empfänger Code so:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#define CE_PIN 6
#define CSN_PIN 5
#define LED 7
RF24 radio(CE_PIN, CSN_PIN); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 01; // Address of the other node in Octal format
const uint16_t node02 = 02;
char *ID[] = {"00001", "00002", "00003", "00004", "00005", "00006", "00007", "00008"};
uint8_t deviceNumber_value = 3;
struct PayloadStruct {
char message[6]; // only using 6 characters for TX & RX payloads
uint8_t Dev;
uint8_t counter;
};
PayloadStruct payload;
void setup() {
Serial.begin(9600);
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_250KBPS);
pinMode(LED, OUTPUT);
}
void loop() {
network.update();
if (network.available()) { // Is there any incoming data?
PayloadStruct received;
RF24NetworkHeader header;
network.read(header, &received, sizeof(received)); // Read the incoming data
Serial.print("Received Message: ");
Serial.print(received.message);
Serial.print(" Device: ");
Serial.print(received.Dev);
Serial.print(" Counter: ");
Serial.println(received.counter);
// Prepare response
PayloadStruct response;
response.Dev = deviceNumber_value;
response.counter = received.counter + 1;
// Check if the received message matches any ID
bool idMatch = false;
for (int id_index = 0; id_index < 8; id_index++) {
if (strcmp(received.message, ID[id_index]) == 0) {
idMatch = true;
break;
}
}
// Set response message based on ID match
if (idMatch) {
strcpy(response.message, "True");
} else {
strcpy(response.message, "Wrong");
}
// Send response
RF24NetworkHeader responseHeader(node01); // (Address where the data is going)
bool success = network.write(responseHeader, &response, sizeof(response)); // Send the data
if (success) {
Serial.println("Response sent successfully");
} else {
Serial.println("Failed to send response");
}
}
}
jetzt sendet der Attiny korrekt seine ID an den Arduino aber es soll eine Antwort zurückgegeben werden ob die ID korrekt war. Und diese Nachricht wird nicht abgeschickt.
Kann mir hier wer helfen?