3
\$\begingroup\$

been using picaxe for a few years but now playign with arduino and trying to learn how it all works.

I have a couple of arduinos talking via the HOPERF library successfully and now want to send some meaningful data over the link.

I have the code for the DS18B20 running on the client aurduino and "printing" the temp value to the PC via USB but just not sure how to get it into the HOPERF commands to send over the RF. Here is a bit of the code. The 2nd line is where the data is sent over the HOPERF. What i need to do is replace the string "test" with the data returned by the (sensors.getTebyCByIndex(0)) command from the 4th line.

sensors.requestTemperatures();            // Send the command to get temperatures
client.sendto(11, (uint8_t*)"test\n", 6); // Includes the NUL at the end of the string
Serial.print("sending\n");
Serial.print(sensors.getTempCByIndex(0)); //prints the temp value over the USB

Any clues anyone?? I prefer to learn but doing stuff like this - and yes i did do the "BLINK" thing first! I find doing something i find useful helps me learn faster :-)

I am struggling a bit with working out the docs as there doesnt seem to be a nice "manual" liek the picaxes to follow :-(

\$\endgroup\$
  • \$\begingroup\$ libraries used are HOPERF & DallasTemperature \$\endgroup\$ – Stocky6409 Jan 27 '10 at 11:34
2
\$\begingroup\$

In C, just about any data type can be represented as an array of bytes. Without getting into a complete course on data types and pointers in C (which I'm not really qualified to do, anyway), the following should work on the transmitting end:

float tempC = sensors.getTempCByIndex(0);
client.sendto(11, (uint8_t *)&tempC, sizeof(tempC));

On the receiving end, you'd do something like this:

float tempC;
server.recv((uint8_t *)&tempC, sizeof(tempC));
\$\endgroup\$
  • \$\begingroup\$ Thanks - that looks like it should work. Its sort of what i expected it to be but as I'm still getting my head around the program structures (being ex BASIC) i just couldn't work it out. I'll give it bash this morning and see what results i get - thank you! :-) \$\endgroup\$ – Stocky6409 Jan 27 '10 at 20:38
  • \$\begingroup\$ YAY - many thanks! With a little bit of adjustment I got that to work properly. I now have my temp to 2 decimal places being sent over the RF and displayed in the serial monitor window. I'm also using SoftwareSerial to display data on an LCD. using LCD.print(tempC) gives an error even though serial.print(tempC) works fine. I had a read of the SoftwareSerial library page and changed the line to LCD.print(tempC, DEC) which works but only gives me the whole part of the temp whereas the serial.print gives me the whole plus decimal. I'll keep reading to see if I can sort it out :-) Many thanks! \$\endgroup\$ – Stocky6409 Jan 28 '10 at 1:45
  • \$\begingroup\$ Working! Codes not pretty but it works :-) Thanks again! \$\endgroup\$ – Stocky6409 Jan 28 '10 at 4:12
  • \$\begingroup\$ .....and after a tidy-up saved over 700bytes to boot :-) \$\endgroup\$ – Stocky6409 Jan 28 '10 at 5:55
  • \$\begingroup\$ Nice! Congrats! \$\endgroup\$ – blalor Jan 28 '10 at 13:23
1
\$\begingroup\$

You may want to spend some time learning C or C++ on a PC first, with a standard programming book/course, before you start adding more complex things like microcontrollers and RF links and USB and so on. Without a solid foundation, you will run into many problems like this where you'll get stuck on the most basic items. Of course, if your goal is just to quickly hack together this one item, and you don't want to spend time learning or plan to use these skills in the long term, then my suggestion might not be useful.

Here's a random free book on C++: Thinking in C++

\$\endgroup\$
  • \$\begingroup\$ I've been trying to learn on PC - brain too stuck in the world of Basic! I've actually learnt more about "C - like" programming in a week of Arduino than months trying to work it our on the PC. I'm just weird sorry :P Thanks for the link - I'll have a look anyway \$\endgroup\$ – Stocky6409 Jan 27 '10 at 20:35
  • \$\begingroup\$ A thoroughly good book, it starts off looking at C then moves onto C++. \$\endgroup\$ – Amos Jan 27 '10 at 20:57
0
\$\begingroup\$

What does sensors.getTempCByIndex(0) return? If it's a string you should be able to cast it in the same way as your test string.

If not is it a number (float?), does the second argument to client.sendto() have to be a uint8_t*? Can you cast from one to the other?

Do you need to cast it? I don't know whether the client.sendto() function and whatever is receiving it are in the library or written by you. If you've written them, you could modify them to accept the type of value returned by sensors.getTempCByIndex(0), then you wouldn't need a cast.

[EDIT (in response to the first two comments below)]I know that Arduino Sketches are a subset of C, but I'm not certain which bits of C work and which don't. There is a discussion here about casting a float (floating point number) to a string on the Arduino, but it appears to be saying that the method may be very memory intensive. A cast is basically when you change the type of a variable (eg float -> string), in C you typically do this by putting the new type in brackets immediately preceding the variable to change.

Looking at the code you've got:

client.sendto(11, (uint8_t*)"test\n", 6);

which appears to be casting the string "test\n" (typically called a string literal) to a uint8_t*, which appears to be a pointer to an 8 bit wide byte of data or possibly the first byte of an array of uint8_t bytes. This whole function call I'm guessing is converting the string into 6 bytes to transmit with 6 being the length of string.

Does the receiver automatically decode the message back to a string? The comments in the source code for your library appear to suggest that you can send any binary encoded data as the packet in your datagram/message, so rather than converting your existing float to a string you could convert the whole number part to an integer and send it, then take the first 2 or so digits of the decimal part and send them as an integer and then reconstruct the float at the far end.

At the bottom of this page there's mention of the standard C function itoa which is used to convert an integer (whole number) to an ascii string, if you can split your number into a whole number part and decimal part you can encode them using this as a string and concatenate them (computer programmer speak for join them together) into "wholenumberstring"+"."+"decimalstring" and then put the resulting string into client.sendto() making sure that the final argument to client.sendto() is the length of the newly formed string. It also tells you on that page what the overhead will be in terms of memory of using different implementations of itoa.

A further thought, how is your temperature data coming into the Arduino? Do you get a float, or do you actually get an analogue value (eg in the range 0 to 255 depending on the resolution) which is then converted to a decimal value. It might be easier to send the raw data as a string over the link and convert it at the other end.

\$\endgroup\$
  • \$\begingroup\$ sensors.getTempCByIndex returns a value of XX.xx in degreesC You lost me on the the rest - still trying to get my head around "cast" "Float" and all these other unfamiliar terms (i'm from a BASIC background - sorry!) \$\endgroup\$ – Stocky6409 Jan 27 '10 at 11:20
  • \$\begingroup\$ so if what i am getting returned is a "float" (floating point number????) is there some way I can convert it into a string?? I tried putting the (sensor.getTempCByIndex) command into the client.sendto() without any luck - i just got errors about "cast" that i simple dont understand yet :-( \$\endgroup\$ – Stocky6409 Jan 27 '10 at 11:22
  • \$\begingroup\$ Thank you for the extra description - some of this is starting to make sense now :-) \$\endgroup\$ – Stocky6409 Jan 27 '10 at 20:38
0
\$\begingroup\$

Thanks all who contributed. My code works and I have learnt about hardware serial, rf comms, software serial, cast, floats, serial LCD and dallas onewire all in 2 days :-)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.