Hi guys
Im trying to use the cheap RF links to send a variable from an Uno to a Leonardo. The variable is the temperature which my sensors reads. Im using the LM35DT sensor. I managed to read the correct temperature - in celsius. But I can't manage to receive the right values on the receiver side.
Transmitter code:
#include <VirtualWire.h>
int tempPin = 0;
long temperature = 0;
char msg[24];
void setup()
{
Serial.begin(9600);
analogReference(INTERNAL);
//VirtualWire setup
vw_set_tx_pin(12);
vw_setup(2000);
}
void loop() {
//Finding the average temperature after 20 readings
int aRead = 0;
for (int i = 0; i < 20; i++) {
aRead = aRead+analogRead(tempPin);
}
aRead = aRead / 20;
//Converting the temperature to celsius
temperature = ((100*1.1*aRead)/1024)*10;
//Sending the temperature value to the reciever
sendTemp(long(temperature));
delay(100);
}
void sendTemp(int value) {
// prints a value of 123 as 12.3
long hele = value / 10;
float deci = (value % 10)/10.0;
float calTemp = hele + deci;
sprintf(msg, "%f", calTemp);
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg));
}
Reciever code:
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600);
//VirtualWire Setup
vw_setup(2000);
vw_set_rx_pin(11);
vw_rx_start();
}
void loop()
{
uint8_t buflen = VW_MAX_MESSAGE_LEN; //Maximum length of the message
uint8_t buf[buflen]; // The buffer that holds the message
if (vw_get_message(buf, &buflen))
{
for (int i = 0; i < buflen; i++) //Checking what the buffer holds
{
Serial.println(buf[i]);
}
}
}
On the receiver side Im just getting the number 63 over and over again. It doesn't matter if I heat the sensor. The value won't change.
I read that to send variables I need to use the sprint() function. Am I right in assuming that this function in this case converts from a float to a string? I placed a Serial.println to check the output of this sprint() function but I am only getting a lot of question marks.
What is the deal here? I think error is the sprint function which I don't understand completely. Thanks a lot!