Hey guys,
First of all, I'm a beginner in arduino programming, so if I ask a stupid question, please be understanding.
I'm trying to make a monitoring system for a wood-fired water heating unit. Because of the distance (around 25-30m) I thought of using 2 arduino nano that communicate through 2 RS-485 adapters, one reads the temperature through a DS18B20 digital sensor and the other displays it on a 2x16 display. I just found out that serial communication doesn't like floating point numbers that's why I'm asking you what would be the best method to send a DS18B20 temperature value over serial communication.
Welcome to the forum
Serial communications transfers bytes from one place to another. It is up to you how you interpret the bytes
For instance, if you do this
float aFloat = 123.45;
Serial.print(aFloat);
Then you will receive a series of bytes representing the characters that were sent. One way to interpret these byte would be to use the Serial.parseFloat() function
Please provide the proof That is, your sender code and your receiver code; don't forget to use code tags when posting code as described in How to get the best out of this forum.
If you like, just send an integer representing 1/10’s of a degree and divide back at the receiver end .
Ie instead of 21.3 send 213
Yes. Also, if you look at most thermal sensors, they actually report temperatures in scaled integer form. So you can bypass whatever library you are using, that converts the value to float, and send the raw data as a scaled integer (native sensor format) and then perform the conversion at the receiving end.
The code to do the scaled integer to float conversion, already exists in the sensor library and it's just a matter of adapting it for your purpose.
For help with that, please post your code as requested both in reply #3, and in the forum introductory texts that you didn't read or maybe didn't understand.
At least, provide a link to the DS18B20 library that you are using.
Thanks for the replies.
@UKHeliBob I made some sketches without the temp sensor just to test Serial.parseFloat() function. The correct number 123.45 is displayed just one time at startup then it switches between 0.45 or 0.12.
I attach the code:
Transmitter
int enablePin = 8;
float aFloat = 123.45;
void setup() {
Serial.begin(9600);
pinMode(enablePin, OUTPUT);
delay(10);
digitalWrite(enablePin, HIGH);
}
void loop() {
Serial.print(aFloat);
delay(100);
}
Rceiver:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
int enablePin = 8;
float aFloat;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" RS485 ");
Serial.begin(9600);
pinMode(enablePin, OUTPUT);
delay(1000);
digitalWrite(enablePin, LOW);
}
void loop() {
while (Serial.available())
{
lcd.clear();
aFloat = Serial.parseFloat();
lcd.setCursor(0, 0);
lcd.print(aFloat);
delay(1000);
}
}
@sterretje my old testing code it's a mess. Now with your help i want to start from scratch.
Thank you for your time.
@hammy I tried that it works well for about a minute then it displays random values.
@anon57585045 I have so many versions of code that i tested i don't know witch to post and all are really messy. I will post one.
The temp sensor library is https://github.com/milesburton/Arduino-Temperature-Control-Library
Thank you for your time.
Nah, we stopped doing that around the 18th century.
On the Tx side change
Serial.print(aFloat);
to
Serial.println(aFloat);
You are in luck. You can easily get the raw value:
// returns temperature raw value (12 bit integer of 1/128 degrees C)
int32_t getTemp(const uint8_t*);
I'm sorry but English is not my native language. I learned it by myself, but the lack of practice, especially the writing, still gives me problems.
Never mind. Lots of English speakers do things like that. Anyway, once you transmit the raw value you can convert it on the other end with
// convert from raw to Celsius
float rawToCelsius(int32_t raw) {
// C = RAW/128
return (float) raw * 0.0078125f;
}
just stolen from the library...
It works now but still every few second displays random values.
As soon as i test your solution i will get back. I have to figure out how use it in a new sketch.
I expect, your main task will be to transmit and receive a 16 bit integer.
Never mind it was a timing error. As soon as i matched the delay it works well.
You shouldn't have any delay on the receive side.
Too bad you're giving up and using float. You asked for the "best way".
I have to educate myself about that.
As I said, I am an absolute beginner in programming.
Well, consider it for a future enhancement. But meanwhile, it is a mistake to have a time delay on the receive side. The transmitter should set the timing.
Not giving up. But i like to learn other ways too. I really want to learn Arduino programming.
I just removed the delay and i have no value on display. I put the delay back and works like a charm.
Now i am confused.