microrobotics icon

NRF24L01 RF Serial Converter

microrobotics | PRO | 05/30/23 11:28:30 AM UTC | 0 ⭐ | 2740 👁️ | Never ⏰ | []
Arduino |

1.24 KB

|

None

|

0 👍

/

0 👎

#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(10, 11); // RX, TX
 
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
 
  // Set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}
 
void loop() {
  // Check if the Serial Converter has sent any data
  if (mySerial.available()) {
    byte len = mySerial.read(); // Read the first byte - the length of the actual data
    char data[32]; // Buffer to hold received data
    for (int i = 0; i < len; i++) {
      while (!mySerial.available()); // Wait for the next data byte
      data[i] = mySerial.read();
    }
    data[len] = '\0'; // Null terminate the string
 
    Serial.println("Received: " + String(data));
  }
 
  // To send data
  String dataToSend = "Hello, world!";
  byte len = dataToSend.length();
  char sendBuffer[32];
  sendBuffer[0] = len; // Set the length of the data in the first byte
 
  for (int i = 0; i < len; i++) {
    sendBuffer[i + 1] = dataToSend[i]; // Store the actual data starting from the second byte
  }
 
  mySerial.write(sendBuffer, len + 1); // Send the data, note the '+1' as we also need to send the length byte
 
  delay(1000); // Wait for a second before next loop iteration
}
 

Comments