I am trying to use 2 RFM69HCWs to send sensor data from the HC-SR04 to each other. However, I realised that the data sent might not necessarily be the correct data. I have attached the codes for the receiver and the transmitter side :
For transmitter
#include <RFM69.h>
#include <SPI.h>
int tri = 9;
int echo = 8;
float Micro, sec;
int cm;
float cmps;
float mps = 342.5;
#define NETWORKID 0 // Must be the same for all nodes
#define MYNODEID 1 // My node ID
#define TONODEID 2 // Destination node ID
// RFM69 frequency, uncomment the frequency of your module:
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_915MHZ
// AES encryption (or not):
#define ENCRYPT true // Set to "true" to use encryption
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes
// Use ACKnowledge when sending messages (or not):
#define USEACK false // Request ACKs or not
// Create a library object for our RFM69HCW module:
RFM69 radio;
void setup()
{
// Open a serial port so we can send keystrokes to the module:
Serial.begin(9600);
pinMode(echo,INPUT);
pinMode(tri,OUTPUT);
// Initialize the RFM69HCW:
radio.initialize(FREQUENCY, MYNODEID, NETWORKID);
radio.setHighPower(); // Always use this for RFM69HCW
}
void loop()
{
digitalWrite(tri,HIGH);
delayMicroseconds(10);
digitalWrite(tri,LOW);
Micro = pulseIn(echo,HIGH);
sec = Micro*pow(10,-6);
cmps = mps*100;
cm = cmps*(sec/2);
Serial.println(cm);
delay(500);
static int sendlength = 2;
radio.send(TONODEID, cm, sendlength);
}
For receiver :
#include <RFM69.h>
#include <SPI.h>
#define NETWORKID 0 // Must be the same for all nodes
#define MYNODEID 2 // My node ID
#define TONODEID 1 // Destination node ID
// RFM69 frequency, uncomment the frequency of your module:
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_915MHZ
// AES encryption (or not):
#define ENCRYPT true // Set to "true" to use encryption
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes
// Use ACKnowledge when sending messages (or not):
#define USEACK false // Request ACKs or not
// Create a library object for our RFM69HCW module:
RFM69 radio;
void setup()
{
// Open a serial port so we can send keystrokes to the module:
Serial.begin(9600);
// Initialize the RFM69HCW:
radio.initialize(FREQUENCY, MYNODEID, NETWORKID);
radio.setHighPower(); // Always use this for RFM69HCW
}
void loop()
{
if (radio.receiveDone()) // Got one!
{
// The actual message is contained in the DATA array,
// and is DATALEN bytes in size:
for (byte i = 0; i < radio.DATALEN; i++)
Serial.print((int)radio.DATA[i]);
Serial.print("\n" );
}
}
In the attached picture, COM5 is the transmitting side (with the HC-SR04) and COM3 is the receiving side. As observed, there isn't same data being received. Can anyone help me on this?Thanks!
On the transmit side, change the send command to:
radio.send(TONODEID, &cm, sendlength); // NOTE: need to use address of 'cm'
On the receive side, I would change the code to:
if (radio.receiveDone()) // Got one!
{
// The actual message is contained in the DATA array,
// and is DATALEN bytes in size:
// NOTE: each element is an int (2 bytes), so need to recast the data as such...
int *pData = (int*)radio.DATA;
for (byte i = 0; i < radio.DATALEN; i+=2)
Serial.print(*pData++);
Serial.print("\n" );
}
Tom
Could you send an integer array instead of the cm variable to transmit more variables?