Tapatalk

Single Byte Transmission

Single Byte Transmission

9

PostJun 20, 2017#1

Could anyone point me in the direction of source codes/libraries/resources that describe how to transmit and receive small packets of data instead of long continuous? Something along the lines of the built-in nrf. Thank you for the help!

1885
1885

PostJun 20, 2017#2

Hi aholwi00,

You should be able to use the Radiohead library directly on the Whisper Node. Have a look on the examples provided by the RF69. The "rf69_client" and "rf69_server" should be a good start.

You can find the RF69 driver documentation here: http://www.airspayce.com/mikem/arduino/ ... _RF69.html

Here a sample code which should work.

Node A Code - Sender
This node will read the value from the Analog PIN A1 and transmit it to the air. Node that it's a very crude message format, the only data transmitted is the Analog PIN reading. You could include additional bytes as part of message to identify the Sending node, or type of message, etc. It'll depend on your project.

Code: Select all

#include <SPI.h>
#include <RH_RF69.h>

#define RF_FREQ 433.0 // Configure the correct frequency, need to be the same on both nodes
#define ANALOG_PIN A1 // Define from which analog PIN you'll be reading the data from
RH_RF69 myRadio;

void setup() 
{
  Serial.begin(115200);

  Serial.println(F("Initializing Radio"));
  myRadio.init();
  myRadio.setFrequency(RF_FREQ);
  myRadio.setTxPower(13); // You can use something bigger than 14 if you have the High-power version. See RH documentation.
  myRadio.setModemConfig(RH_RF69::FSK_Rb125Fd125); // You can change the radio modulation, bandwidth and transmission rate. See RH documentation.

  // The encryption key has to be the same as the one in the client
  uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  myRadio.setEncryptionKey(key);
}

void loop()
{
  // Read the value from the Analog PIN. I'm assuming this is the variable part.
  // We're using a 16 bits variable because the analogRead can return something between 0 and 1023, so
  // a single byte is not enough.
  uint16_t reading = analogRead(ANALOG_PIN);
  Serial.print(F("Sending analog reading: ")); Serial.println(reading);

  // Here we need to create a bytearray, to use as a buffer which will be sent via the Radio.
  // We need to shift the data, so we slit the 16 bits value (2 bytes) into two units of 8 bits (1 byte).
  uint8_t data[2] = { 0 };
  data[0] = reading >> 8;
  data[1] = reading;

  // Now we can send it to the "air"
  myRadio.send(data, sizeof(data));

  // Wait some time before sending the next reading
  // You can implement it by other ways in your code, ideally using a non-blocking method
  delay(1000);
}
Node B Code - Receiver
This node will simply listen to the frequency and print any message in the screen. Note it won't filter the messages. If you need, you can add additional bytes to the original message and use it as filter, similar to the Talk2 Voltage example where a few "headers" are defined. The complexity of your message format it'll depend on your project needs.

Code: Select all

#include <SPI.h>
#include <RH_RF69.h>

#define RF_FREQ 433.0 // Configure the correct frequency, need to be the same on both nodes
#define LED_PIN 6 // Define a LED to blink, for the Whisper Node this is the blue LED.
RH_RF69 myRadio;

void setup() 
{
  Serial.begin(115200);

  Serial.println(F("Initializing Radio"));
  myRadio.init();
  myRadio.setFrequency(RF_FREQ);
  myRadio.setTxPower(13); // You can use something bigger than 14 if you have the High-power version. See RH documentation.
  myRadio.setModemConfig(RH_RF69::FSK_Rb125Fd125); // You can change the radio modulation, bandwidth and transmission rate. See RH documentation.

  // The encryption key has to be the same as the one in the client
  uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  myRadio.setEncryptionKey(key);
  
  Serial.println(F("Initializing LED, it should blink once."));
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);
  delay(50);
  digitalWrite(LED_PIN, LOW);
}

void loop()
{
  // Check if the radio has a valid message in the buffer.
  if (myRadio.available())
  {
    // Let's turn ON the LED during this step, so we know a message arrived
    digitalWrite(LED_PIN, HIGH);
    
    // Create a variable to receive the data. The buffer can be any size which the message will fit.
    // We're using 2 because we know it can't be any bigger.
    uint8_t data[2] = { 0 };
    uint8_t len = sizeof(data);
    
    // Now we copy the message from the Radio library buffer to the program memory
    if (myRadio.recv(data, &len))
    {
      RH_RF69::printBuffer("RAW Message: ", data, len);
      
      // Create a 16 bits variable to store the Analog reading sent by Node A
      // We also receive byte by byte into the variable, shifting it to the left accordingly before concatenate. See bitwise and bitmath operations.
      uint16_t reading = 0;
      reading = (uint16_t)(data[1] << 8) | data[0];
      
      Serial.print(F("Analog reading received: ")); Serial.println(reading);
    }
  
    // Let's give some additional time and turn OFF the LED.
    // The additional 50ms delay is just because the steps above might happen so fast we won't see the LED blinking.
    delay(50);
    digitalWrite(LED_PIN, HIGH);
  }
}
I know the code above will compile, but I haven't actually tested the behavior, but this should get you going. One I have the change to upload and test the code I'll update this post as well.