Trouble sending bytes using RF22.

Hi All,
I am working on a project that involves sending information between two computers using the RFM22B HopeRF Modules. (in particular, two Sparkfun RFM22 Shields)

Essentially Computer A periodically sends out a bunch of bytes (between 10-16 bytes) to the Arduino which should be read and sent straight out through the RF22. The other RF22/Arduino should receive the bytes and send it to Computer B.

The issue I am having is somewhere in this setup there is a whole bunch of extra bytes are received.

Sending the uint8_t ‘101’ come out at the other end as ‘101 2 147 8 213 8 227 1 62 3 47 1 62 66 65 149 3 95 1 62 6 42’.

This is absolutely driving me up the wall and some help would be greatly appreciated.

Below are the simplified version of the Send/Receive Sketches. I have not included the Serial.read() portion of the program as it does not seem to be the issue. Happy to post the whole code if that helps.

Transmitter

#include <SPI.h>
#include <RF22.h>

// Singleton instance of the radio
RF22 rf22;

void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
}

void loop()
{
  while (1)
  {
    // Test Message - Beacon   
    uint8_t data[1];
    data[0] = 101;
    rf22.send(data, sizeof(data));
 
    rf22.waitPacketSent();

    delay(1000);
  }
}

Receiver

#include <SPI.h>
#include <RF22.h>

// Singleton instance of the radio
RF22 rf22;

void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
}

void loop()
{
  if (rf22.available())
  {
    // Should be a message for us now   
    uint8_t buf[RF22_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf22.recv(buf, &len))
    {
      Serial.print((char*)buf);
    }
  }
}

The weird thing is sometimes this code actually works as its supposed to but when I make changes, it goes back to spitting out rubbish. Sending strings also seems to work but unfortunately is not useful.

Any help with this would be greatly appreciated, I am lost for ideas at this point…

Kind regards,
Alex.

void loop()
{
  while (1)
  {

Running an infinite loop (while(1)) inside an infinite loop() is a sign of cluelessness.

    uint8_t data[1];
    data[0] = 101;
    rf22.send(data, sizeof(data));

A one element array is silly. Use a scalar variable if you only need to send one byte.

    uint8_t buf[RF22_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf22.recv(buf, &len))
    {
      Serial.print((char*)buf);
    }

You did NOT receive a string. Do NOT send what you got to a function that expects a string.

A string is a NULL terminated array of chars. There is not a NULL terminator being added to the buffer, so, the buffer is NOT a string. The print() method being used expects a string. It can not be faulted for assuming that you knew what you were doing.

Hi Paul,
Thank you for your response.

Below is the working code just in case anyone has a similar issue. Probably not programmatically correct but working.

#include <SPI.h>
#include <RF22.h>

// Singleton instance of the radio
RF22 rf22;

void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
  Serial.println("RF22 init failed");
  if (!rf22.setModemConfig(RF22::GFSK_Rb125Fd125))
   Serial.println("setModemConfig failed");
}

void loop()
{
  while (Serial.available()) {
    // Send a message to rf22_server
    uint8_t data = Serial.read();
    rf22.send(&data, 1);
   
    rf22.waitPacketSent();
  }
  
  while (rf22.available()) {  
    // Recieve message from rf22_server
    uint8_t buf;
    uint8_t len = 1;
    if (rf22.recv(&buf, &len))
    {
      Serial.print((char)buf);
    }
  }
}

Its good to see the Arduino community is so helpful to people who don’t know what they’re doing.

Alex.