I'm making a few sensor nodes that I want to send data back to a main unit via a simple serial (RF eventually) link.
I can get a test example to work with the following set-up:
Hardware:
Remote Node(s) - ATTiny85
Main Unit - Arduino Nano
Radio Unit - Not being used yet.
Currently connecting a physical wire between the ATTiny's Output pin and the Nano's Rx pin - General I/O pin 2, not the Uart.
(Ground is also commoned using a physical wire.)
Library:
This Library fits well onto the ATTiny and does everything I need it to for this application ... If I can figure out my issue - below.
Related Tx side Code:
#include <RFTransmitter.h>
const byte RfNodeID = 1;
const byte RadioOut_PIN = 1; // ATTiny85 - No Uart on GPIO 1
unsigned int RfPulseLength = 250; // Time of each pulse section in uSec - Bigger = more stable, but slower
RFTransmitter RfTx(RadioOut_PIN,RfNodeID,RfPulseLength); // Set up RF Tx
void setup() {}
void loop() {
char *msg = "Hello World!"; // From example code
RfTx.send((byte *)msg, strlen(msg) + 1); // From example code
delay(5000);
}
And for the Reciever:
#include <RFReceiver.h>
const byte RadioIn_PIN = 2; // Pin Assignment for Radio Input
unsigned int RfPulseLength = 250; // ** MUST match Tx ** Time of each pulse section in uSec Bigger = more stable, but slower
RFReceiver RfRx(RadioIn_PIN, RfPulseLength); // Setup with my custom pulse Length value
void setup() {
Serial.begin(9600);
RfRx.begin();
}
void loop() {
char msg[MAX_PACKAGE_SIZE];
byte senderId = 0;
byte nodeId = 0;
byte len = receiver.RfRx((byte *)msg, &nodeId, &packageId);
Serial.println("");
Serial.print("Package: ");
Serial.println(packageId);
Serial.print("RF Node: ");
Serial.println(nodeId);
Serial.print("Message: ");
Serial.println(msg);
}
I get everything as expected. The Package ID increments, but otherwise the message is constant.
I now want to send actual info and I can't get my head around how to set-up the "msg" variable properly.
I understanding that msg is a char array, so it's fundamentally what I want, more-or-less ....
I've set data into a byte array:
// Global Variable
byte RfData[3]; // Data Packet of 3 bytes: [Reading 1, Reading 2, Reading 3]
// Function to pack everything to send
void makeRfDataPacket(void){
RfData[0] = aSensorReading1;
RfData[1] = aSensorReading2;
RfData[2] = aSensorReading3;
}
For the sake of this problem, it really doesn't matter what the readings are - other than they fit within 1 byte each. (Currently, each byte is limited to 0 - 50)
This is where I get stumped ....
How do I tell the Transmitter to send this byte array. I've tried sending it like the character array:
makeRfDataPacket(); // Assemble packet to send
RfTx.send(*RfData, strlen(RfData) + 1); // Send Data Packet
But I got gibberish on the other end.
I've attempted to work out from the Library source code how everything works, but I can't seem to wrap my head around the pointer concept properly, despite many attempts.
A few possibilities occurred to me, but I'm not sure how to set up the send and receive side to check all of them. I BELIEVE my problem if how I'm trying to trasmit the data, rather than the receiver side.
My thinking at the moment is, to send using the example code do I:
Need to add on a '/n' character at the end of the array? RfData[3] = '/n'; ??
Need to shift data out of the ascii control character range?? RfData[0] = (aSensorReading1+32); ??
Need to change how/where I'm using * and & ??
How do I set-up the Receive end to put this data into a temporary array for processing?
(Let us call it RfDataRx[] for this discussion)
Essentially recreating the transmitters RfData[] array so I can parse out the info?
Is the:
char msg[MAX_PACKAGE_SIZE];
Doing exactly that?
In this example, is msg[0] == RfDataRx[0]
Should I declare msg as byte, rather than char?
(Given the valid values are 0-99, does it even matter??)
Any suggestions are appreciated.
I'll keep banging my head trying to figure this out on my own, and will update here if I figure it out.
I thought sending data like this would be fairly common and a search would come up with lots of suggestions. It did, but they weren't actually helpful ...
What I have found seems to be focused more on setting up serial protocols, 'what is an array', or How to put data into an array element, rather than how to send them within an existing protocol.
Hopefully, the (potentially easy/obvious) solution will be of help to others, and maybe even help clarify this whole pointer thing for me.
Thanks!
One reason I'm not just trying tons of variations to try to figure it out ...
As my Transmitter is on an ATTiny85, code changes are a hassle, as I need to physically move the IC from my testing Hardware to a DIY 'Arduino as ISP' programmer, then back again.
I did make a physical 'emulator' of sorts (from a nano to 8pin DIP) to test the hardware portion of my design - I know the Hardware all functions properly.
However, given the memory size of the ATTiny and different libraries, I had to start using the real thing for program testing. (Miss the Serial print for debugging and quick usb/bootloader programming)
On the Receiver end, eventually, I want RfData[] to be fed into a 2D array where the NodeID sets the array Dimension. (Not sure of the correct terminology here.)
Planning for this, I have set-up the following as a global variable on the Nano:
byte sensorData[RFNodes][4]; // (RFNodes = 4) Rows x 4 Column array for node data - 16 bytes memory
// Row = Node # Node 0 = Local Data (not being received by RF)
// Columns 0 - 3 are: aSensorReading1, aSensorReading2, aSensorReading3, packageId
(Currently, this isn't being used)
From the Receiving example, I know how to set the position to fill the array, but I'm not sure how to make sure I'm putting in the data I want. I believe this will become a non-issue once I figure out the first section, but wanted to explain why I am trying to send the info as an array of bytes in the first place.