I have a distillation unit and I'd like to gather the measured temperatures wirelessly.
The temperature sensors are connected to an Arduino Mega and I am using a pair of NRF24l01 for the communication.
There is one NRF24l01 connected to the Mega and there is another one connected to an Uno. The Uno is connected to my computer.
My question is: What's the best practice to send the temperature values from the Mega to the Uno. Here is my idea:
Transmitter side:
I multiply the temperature values (floats) with 1000, then I cast them to integers after that I use itoa() to convert them to strings.
Now I have two strings. I will create one string from the two existing strings, and I will use some character (e.g. “-“) to separate the two. So, if temp_1 = 56.45 and temp_2 = 70.30, then I will have “56450-70300”
After that I can send the one string to the Uno (connected to my Computer) from the Mega (connected to the distillation unit).
Receiver side:
A short code will break down the one string into two different strings and it will remove the separator character.
I convert back the strings into integers (with aoit()), then I cast them to floats and finally I divide them with 1000.
I guess this is not the easiest way...and I’m afraid of the rounding issues… Is there any better way?
But they might be returned as floats by some library. Dallas temperature library for example. The OP would have to break into the library to get the raw values, not a very nice idea. If that's the case I would concur with gfvalvo, put the floats in a struct or array and send that as a binary object. But if the OP's code is turning the raw values into floats then I concur with AWOL. Send the raw values and perform the conversion to float on the Uno.
PaulRB:
But if the OP's code is turning the raw values into floats then I concur with AWOL. Send the raw values and perform the conversion to float on the Uno.
But, regardless of the type of data, I never like converting to ASCII and sending as text. That's where you might lose accuracy/precision. Always use a struct or array and send the native binary.
vegetarian_vulture:
Thank you for all the answers:)
1. Asume that the data are:
float myTemp1 = 24.93;
float myTemp2 = 24.12;
2. You want to transmit them using SPI oriented device -- the NRF24l01. SPI is a byte oriented communication protocol; so, you need to break your data into bytes which can be done using the following union structure.
union
{
float myTemp1;
byte temp1Array[4]; //holds binary32 formatted 32-bit data for myTemp1
}myData;
myData.myTemp1 = 24.93;
//myData.temp1Array[0] holds lower byte which is: 0xA4
//myData.temp1Array[1[ holds next higher byte which is: 0x70
//myData.tenp1Array[2] holds next higher byte which is: 0xC7
//myData.temp1Array[3] most significant byte which is: 0x41
3. How to send and receive 4-byte (32-bit) data of the array myData.temp1Array[4] using SPI Port, you may look for the trick in the pdf of this link.
GolamMostafa: 2. You want to transmit them using SPI oriented device -- the NRF24l01.
SPI is a byte oriented communication protocol;
so, you need to break your data into bytes which can be done using the following union structure.
No.
This translatation is handled transparently by the library,
there is no reason to feed an NRF24L01 bytes only on the user side.
@GolamMostafa the usual absolutely irrelevant enumerated stuff again?
This translatation is handled transparently by the library,
there is no reason to feed an NRF24L01 bytes only on the user side.
@GolamMostafa the usual absolutely irrelevant enumerated stuff again?
This is the signal signatures of NRF24L01 device. Is it not a SPI oriented device? People should have freedom to use their own codes to deliver data to the device rather than being dependent all the times on the Library Functions? This approach helps people to be self educated?
@GolamMostafa the usual absolutely irrelevant enumerated stuff again?
@GolamMostafa has spent about 770 days in the Forum and has made 241 posts meaningful out of 3710 posts; on the other hand, @Whandall has spent about 1460 days in the Forum and has made 1053 posts meaningful out of 8472 posts. One may calculate the relative quality of the posts using the following formula:
Last time, your condition was 'no good' as you had to spend most of your times looking around the posts of the Forum and running after people for good reasons?
Robin2:
What a ridiculous way to estimate the quality or relevance of a person's contributions.
@GolamMostafa, @Whandall, and @tallMan are not the 'subjective persons'; they are 'objective posters'. Don't be sick putting nose in the unnecessary issues. Let the involved parties resolve the case in the best amicable way, and it was done?
A poster has made 100 posts in 53 days and another poster has made the same number of posts in 35 days; the quality of their contributions in the Forum should be (in my view) proportional to the inverse of the days spent in the Forum. If you are not happy with the posted empirical formula you may add some adjustment factor or a new one. The @tallMan has liked it with a rating of: ; whereas, @GolamMostafa has this rating: .
Nothing is ridiculous -- continuous practice of a norm turns it into a culture which when dies becomes the part of history.