*Solved* Assign buffer values to string

I have two 32u4 LoRa radios that I am using to send the state of a switch from the transmitter in order to move a motor on the receiver. I have used strings to act as a kind of checksum to hopefully prevent unwanted noise from activating the motor. So far I have been unable to assign the contents of the receiver buffer to a string in order to compare to the assigned prowords.

I have declared a string that will be used to accept the buffer contents but they are received as hex and then serial print with a char pointer. I have been trying to accomplish this with iterators and Serial functions but it has not been working for me. Any suggestions would be appreciated. Below is my code;

#include <SPI.h>
#include <RH_RF95.h>

/* for feather32u4 */
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7

// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

// Blinky on receipt
#define LED 13

//Declaring comparator strings (Open & Shut) and the received Tx string
char strOpen[5] = "Open";
char strShut[5] = "Shut";
char strReceived[5];

void setup() 
  //Identifying pin modes
  pinMode(LED, OUTPUT);     
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  // Manually RESET radio
  digitalWrite(RFM95_RST, LOW);
  digitalWrite(RFM95_RST, HIGH);

// Initialize Radio
while (!rf95.init()) {
    Serial.println("LoRa radio init failed");
    while (1);
  Serial.println("LoRa radio init OK!");
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println("setFrequency failed");
    while (1);
  Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
  // you can set transmitter powers from 5 to 23 dBm:
  rf95.setTxPower(23, false);

void loop()
  if (rf95.available())
    // Identifies size of buffer and assigns var to it  
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
      RH_RF95::printBuffer("Received: ", buf, len);
      Serial.print("Got: ");
      Serial.println((char*)buf);    // <------ This is where I want to store the received string to compare

      //If Tx switch is ON, then indicate claw is opened and send return to Tx
      if (strReceived == strOpen)
        uint8_t data[] = "Motor has been opened";
        rf95.send(data, sizeof(data));
        Serial.println("Sent a reply");
        digitalWrite(LED, HIGH);

      //If Tx switch is OFF, then indicate claw is closed and send return to Tx
      else if (strReceived == strShut)
         uint8_t data[] = "The motor has been closed";
         rf95.send(data, sizeof(data));
         Serial.println("Sent a reply");
         digitalWrite(LED, LOW);
      // Send a reply
      delay(200); // may or may not be needed
    Serial.println("Receive failed");

You read up on c-strings (arrays of char) and the available functions for manipulating them. Here are a few references:


Thank you for the reply. I have been reading and searching for the various functions but I guess my problem is I don't understand how the string is coming from the buffer. Is char* pointing to each character in the buffer for the length of the buffer (so it's technically reading one at a time and printing them to serial window)? Or is char* pointing to the whole string inside the buffer and then prints it? If it's option one, I assume I have to move through the buffer and store each character in an array. But I'm hoping it's option two where I can just grab the whole string at once. Or am I completely misunderstanding this?

Maybe this reference will help clear it up:

The char* is pointing to the first character. If you use array syntax on it you can access each character one by one. If you pass it to print, it will print that character and then march through memory printing each character in your string until you hit the null that marks the end of the string.

Thanks again for quick reply. Honestly I read that page about 10 times and it didn't make sense to me until I just got it to work using the memcpy function after I read through the library file more and saw it in the recv function. Here is the line I added;


Hopefully I understand this correctly, I am using the memcpy function to assign the destination array 'strReceived' and pointing to the char array in the buffer that is of size len which is determined by the sizeof function a few lines before. The syntax of ((char*)buf) was confusing me but I think this cleared it up for anyone else who is interested.