i am now working on a wireless project that calls for taking three integers, and combining them into one byte. This will be transmitted wirelessly to another arduino using rf22 radio modules, which then receives the byte, and turns that back into three integers for function control. I am basically wanting to control some lighting using relay and a wireless remote.
I have read up on pointers, and the uint8_t, and Char*. i kinda get what it is saying but it is still beyond my comprehension.
So here is what I would like to accomplish.
// this is on the Client arduino
int a = 1;
int b = 1;
int c = 1;
uint8_t data[] = "combine the three integers a,b,c here into a byte";
rf22.send(data, sizeof(data)); //<-- does this need to be modified?
// here is the code for the server side
// 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("got request: ");
Serial.println((char*)buf);
// Send a reply
uint8_t data[] = "And hello back to you";
rf22.send(data, sizeof(data));
rf22.waitPacketSent();
Serial.println("Sent a reply");
}
else
{
Serial.println("recv failed");
}
// the above is the code that came with the example hello world. I would like to take the three integers and extract them back into
// three separate bytes i can use for controlling the server functions.
lapoza:
taking three integers, and combining them into one byte
It's not obvious why you're limiting yourself to sending a message containing a single byte, since your RF22 code seems to imply that you can send multi-byte messages. What ranges of values will the three integers have?
The integers will be the status of a digital button, so either a 1, or a 0.
The reason I have came to this is this is what was recommended to me on the rf22 forum, but they do not help with coding issues. just issues with the radio itself.
I am open to solution, I would just like to be able to push a button on one arduino, and light an led on the receiving arduino. that is the goal at this stage. If I can get to that point I would be fine.
I'm still unclear why you've been advised to encode the values in a single byte, but it's perfectly feasible to do: Each value only requires a single bit so you can pack them all into a single byte. The Arduino runtime provides convenience functions bitRead() and bitWrite() to read and write individual bits within a byte:
byte data = 0;
bitWrite(data, 0, a);
bitWrite(data, 1, b);
bitWrite(data, 2, c);
// send the byte via rf22
// receive the byte via rf22
a = bitRead(data, 0);
b = bitRead(data, 1);
c = bitRead(data, 2);
void loop()
{
int a=0;
int b=0;
int c=0;
byte data;
bitWrite(data, 0, a);
bitWrite(data, 1, b);
bitWrite(data, 2, c);
rf22.send(data);
rf22.waitPacketSent();
I get this error:
rf22_client.ino: In function 'void loop()':
rf22_client:38: error: no matching function for call to 'RF22::send(byte&)'
C:\Program Files (x86)\Arduino\libraries\RF22/RF22.h:1021: note: candidates are: boolean RF22::send(const uint8_t*, uint8_t)
I am having a hard time grasping the pointers and uint8_t commands.
That worked for the sending client, I am receiving data on the serial monitor, but it is garbled and i don't know how to unscramble it. This is what I have:
// Should be a message for us now
uint8_t buf[RF22_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf22.recv(buf, &len))
{
a=bitRead(data,0);
b=bitRead(data,1);
c=bitRead(data,2);
Serial.println("got request: ");
Serial.print("a=");Serial.println(a);
Serial.print("b=");Serial.println(b);
Serial.print("c=");Serial.println(c);
Serial.println((char*)buf);
When i run this code it print A b and c as 0, regardless of what I put in the sending radio. There char* buf prints out =ld! or ?ld! and many others as i change the a b and c integers on the sending unit.
I tried this code to unscramble the byte back to integers.
// Should be a message for us now
uint8_t (data);
if (rf22.recv(data)
{
a=bitRead(data,0);
b=bitRead(data,1);
c=bitRead(data,2);
Serial.println("got request: ");
Serial.print("a=");Serial.println(a);
Serial.print("b=");Serial.println(b);
Serial.print("c=");Serial.println(c);
I get these errors:
rf22_server:35: error: no matching function for call to 'RF22::recv(uint8_t&)'
/Users/wolsno2000/Documents/Arduino/libraries/RF22/RF22.h:1013: note: candidates are: boolean RF22::recv(uint8_t*, uint8_t*)
How do I take this uint8_t data and convert it back to a "byte data" so I can extract my integers from it?
Much thanks on all the help so far.
I did read the link that someone posted but it just left me more confused than i already am
At the moment your problems all seem to be how to send a receive a single byte using RF22. I suggest you look at the documentation and examples to understand how to do that and get the basic send/receive working before you worry about how to encode and decode the messages. For test purposes you could just send a single ascii character such as 'X' and make sure you can receive it OK.