Go Down

Topic: Sending sensor values with rfm69hcw from 1 Uno to another Uno (Read 1 time) previous topic - next topic

wahma92

Hallo peoples,
I have an huge problem to send sensor data from one arduino uno to another arduino uno. What i want to do is to read an simple temperature sensor and send it to another arduino. The sensor data will be stored as an integer and should than be send with the rfm69hcw to the other uno, which has also an rfm69hcw.
The communication should go both ways because i will attache another temperature sensor to the second arduino and do some stuff with that data.
I found this code in the hookup guide and got it working but when i modifie it, it is not working anymore. I tried to look into the internet but that did not help, could you please help me?

PaulS

Quote
I found this code in the hookup guide and got it working
Good.

Quote
but when i modifie it, it is not working anymore.
But, your not going to tell us what you changed, what now happens, what now doesn't happen, or what you expect to have happen.

My crystal ball got dropped, and is being repaired. It's due back on the 12th of never. If you need help before then, I'm sure you can figure something out.
The art of getting good answers lies in asking good questions.

ChrisTenone

...
My crystal ball got dropped, and is being repaired. It's due back on the 12th of never. If you need help before then, I'm sure you can figure something out.
Love it Paul! One of the best "don't make me guess" posts I've seen. The OP's writing style suggests that he is not an English speaker, and so may not understand sarcasm in English.

Wahma92, what he's saying is that you need to give a LOT more information before anyone can give you meaningful help. Read the threads at the top of the listing called Read this before posting a programming question and How to use this forum - please read to see what things you need to do before expecting answers here.
What, I need to say something else too?

wahma92

#3
Dec 11, 2016, 08:02 pm Last Edit: Apr 19, 2019, 12:07 am by surbyte Reason: Tags code added
Sorry you are right,i forgot to give you the detailed information.
:
Well for now i  am trying to send an integer, sensorvalue fixed on value 10, from sendermodified to receiver. I inserted an delay so that the data gets only send every 1s, so i can read it on the receiver side.
When i start the code, the arduinos are sending and receiving the acknolegements, which is good, but i think they are not actualy sending the integer, which is put into the variable input. And this is strange because the acknolegement works.
For now i only tried to modifie only the sender code of one arduino, to start simple and making small steps to the mark. The next step would be to get the sended value out of the message on the receiver side and put it back into an integer.

Right now my integer sensorvalue should be send from sendermodified to receiver and i should see it in the serial monitor, which does not work. Afterwards the sended value will be used, when i get it back into an integer on the receiver side, to do some stuff, but that code is not integrated right now because the sending does not work.

I hope i did not forgot again any information.

modified parts
new values put before the setup
int sensorvalue = 10; //This will become the sensor value, for test puposes it is now fixed to the value of 10. This is also the value that should be transmitted to the other arduino.
char input;


before
Code: [Select]
// If there is any serial input, add it to the buffer:

 if (Serial.available() > 0)
 {
   char input = Serial.read();

   if (input != '\r') // not a carriage return
   {
     sendbuffer[sendlength] = input;
     sendlength++;
   }

   // If the input is a carriage return, or the buffer is full:

   if ((input == '\r') || (sendlength == 61)) // CR or buffer full
   {
  // Send the packet!



after
Code: [Select]
// If there is any serial input, add it to the buffer:

sprintf(input,"%f",sensorvalue);

   if (input != '\r') // not a carriage return
   {
     sendbuffer[sendlength] = input;
     sendlength++;
   }

   // If the input is a carriage return, or the buffer is full:

   if ((input == '\r') || (sendlength == 61)) // CR or buffer full
   {
     // Send the packet!

PaulS

Code: [Select]
    sprintf(input,"%f",sensorvalue);
The %f format specifier is not supported. It is NOT the correct format specifier for an int, anyway.

You can NOT pass a char as the first argument. You MUST use an array.
The art of getting good answers lies in asking good questions.

wahma92

#5
Dec 17, 2016, 06:39 pm Last Edit: Apr 19, 2019, 12:08 am by surbyte Reason: Tag link and code added
Oh right, i have to use the %d for integer, the %f is for float values.

Now i can see some message being send  and received, but the message looks strange. If i send 2 values, example 10 and 15, in an array, should the message not look something like( 10|15 or 10,15. and not like in my screenshot?

I am probably still doing something wrong.

This is new in the sender code

Code: [Select]
// Create a library object for our RFM69HCW module:

RFM69 radio;

int sensorvalue1 = 10; //This will become the first sensor value, for test puposes it is now fixed to the value of 10. This is also the value that should be transmitted to the other arduino.
int sensorvalue2 = 15; //This will become the second sensor value, for test puposes it is now fixed to the value of 15. This is also the value that should be transmitted to the other arduino.
char input [10]; // This will become the array with 2 values
String sensorvalue1str; //sensorvalu1 as string
String sensorvalue2str; //sensorvalue2 as string
char sensorvalue1char; //sensorvalue1 as char
char sensorvalue2char; //sensorvalue2 as char

void setup()
{
 // Open a serial port so we can send keystrokes to the module:



// If there is any serial input, add it to the buffer:

   String sensorvalue1str(sensorvalue1, DEC);
   String sensorvalue2str(sensorvalue2, DEC);
   sensorvalue1str.toCharArray(sensorvalue1char,(sensorvalue1str.length()+1));
   sensorvalue2str.toCharArray(sensorvalue2char,(sensorvalue2str.length()+1));

   char input [] = { sensorvalue1char, sensorvalue2char };
   
   if (input != '\r') // not a carriage return
   {



PaulS

QUIT USING Strings. I do not want to have to tell you that again. There is no f**king reason to use them.

Code: [Select]
int sensorvalue1 = 10; //This will become the first sensor value, for test puposes it is now fixed to the value of 10. This is also the value that should be transmitted to the other arduino.
int sensorvalue2 = 15; //This will become the second sensor value, for test puposes it is now fixed to the value of 15. This is also the value that should be transmitted to the other arduino.
char input [10]; // This will become the array with 2 values

sprintf(input, "%d,%d", sensorvalue1, sensorvalue2); // some useless assed comment goes here, because I get paid by the letter. I'll make this a really long useless comment just to illustrate that, like yours, it adds no value whatsoever to the code.

// Send input to the radio
The art of getting good answers lies in asking good questions.

ChrisTenone

I remember the fantastic feeling of liberation and joy I felt the first time I was able to take all the Strings out of a text-heavy sketch! I got an immediate reward of a Kbyte back, and was able to find an additional 500 or so bytes using optimizations on the c-style strings, like the F macro, formatted printing, etc.
What, I need to say something else too?

wahma92

#8
Jan 05, 2017, 08:29 pm Last Edit: Apr 19, 2019, 12:09 am by surbyte Reason: Tag code added
Hallo and happy new year,

Now i understand what you meant the first time you replied and i can finaly send and receive the integer. Sender code works now.

The next step would be to get the received messge back into integer on the second arduino. (received message)

The message is splitt up into bytes and when i put it into an array i can print them out, just only an single charakter every time (msg). i tried to use the reverse function of sprintf, sscanf. I google this and found thid site: http://controlrobotics.rodrigomompo.com/2013/09/tutorial-arduino-kit-rf-433mhz-part4.html

The sprintf and sscanf are good explained here, but is used for an VW_send code. So i tried to make my code similar to it, the for function and sscanf, but without luck. I got 0 as sensor values (scanf)

I could solve the problem with simple math now, but only because i know that my send values have only 2 charakters. but what if the next time it would be something like this 8 value 1 = 1000 and value 2 = 3 or value 1 = 146 and value 2 =17890 and value 3 =56. It would be alright to store one whole sent message into one page of the array, value 1 msg[0] value 2 msg[1] and so on, but how do i do that?

this is old
Code: [Select]
for (byte i = 0; i < radio.DATALEN; i++)
     Serial.print((char)radio.DATA[i]);

   // RSSI is the "Receive Signal Strength Indicator",
   // smaller numbers mean higher power.

   Serial.print("], RSSI ");
   Serial.println(radio.RSSI);

   // Send an ACK if requested.
   // (You don't need this code if you're not using ACKs.)


this is new
Code: [Select]
RFM69 radio;
int sensorvalue1received;
int sensorvalue2received;
char msg [63]; // The array in which the message is stored

// The actual message is contained in the DATA array,
   // and is DATALEN bytes in size:

   for ( byte i = 0; i < radio.DATALEN; i++)
     msg[i] = char(radio.DATA[i]);
     //Serial.print((char)radio.DATA[i]);

   // RSSI is the "Receive Signal Strength Indicator",
   // smaller numbers mean higher power.

   Serial.print("], RSSI ");
   Serial.println(radio.RSSI);
   sscanf(msg, "%d, %d, %d,", sensorvalue1received, sensorvalue2received);
   Serial.print("msg[0] is ");
   Serial.println(msg[0]);
   Serial.print("msg[1] is ");
   Serial.println(msg[1]);
   Serial.print("msg[2] is ");
   Serial.println(msg[2]);
   Serial.print("msg[3] is ");
   Serial.println(msg[3]);
   Serial.print("msg[4] is ");
   Serial.println(msg[4]);
   Serial.print("msg[5] is ");
   Serial.println(msg[5]);
   Serial.print("msg[6] is ");
   Serial.println(msg[6]);
   Serial.print("sensorvalue1 is ");
   Serial.println(sensorvalue1received);
   Serial.print("sensorvalue2 is ");
   Serial.println(sensorvalue2received);
   // Send an ACK if requested.
   // (You don't need this code if you're not using ACKs.)



PaulS

Code: [Select]
sscanf(msg, "%d, %d, %d,", sensorvalue1received, sensorvalue2received);
The number of variables needs to match the number of format specifiers. The 3rd and subsequent arguments are supposed to be addresses where the function can write data.

Code: [Select]
sscanf(msg, "%d, %d", &sensorvalue1received, &sensorvalue2received);
The art of getting good answers lies in asking good questions.

wahma92

Thanks, now i can send 3 integer values from 1 arduino to an second one, turn them back into integer and do some stuff with it.
here are the 2 codes for those who might need an similar code

Go Up