Let's Make Robots!

Problems when sending float (temperatur) with VirtualWire Arduino

// User Gareth    http://letsmakerobots.com/user/2941
// Project Node   http://letsmakerobots.com/node/12394
// Remote Receiving Temparature and Humidity over RF Link to LCD Display
// how to use VirtualWire to receive messages and display floating point values to SerialMonitor
// rx pin 23 on mega

#include 
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
    Serial.begin(9600);	// Debugging only
    
        // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec
    vw_set_rx_pin(23);
    vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
  
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;

        digitalWrite(13, true); // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	
	for (i = 0; i < buflen; i++)
    {
        Serial.print(buf[i]);  // the received data is stored in buffer
        }
    Serial.println("");
     }
}


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the_droid's picture

Guys, I set the SerialMonitor to 9600.. and I got: 

ÿ23.0ßC þ35.0% defintly not what I expected ^^ Any idea how I can fix that? 

Dan M's picture

Set your monitor to 9600 baud input to go along with the:

void setup() {

    Serial.begin(9600);

// other setup instructions

    }

the_droid's picture

When I set the monitor to 9600 i get :ÿ23.0ßC þ35.0%

That sucks -.- 

NilsB's picture
Same results with these baudrates? 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200
http://arduino.cc/en/Serial/Begin
the_droid's picture
// User Gareth    http://letsmakerobots.com/user/2941
// Project Node   http://letsmakerobots.com/node/12394
// Remote transmitting Temparature and Humidity over RF Link to SerialMonitor
// DHT11 Sensor

#include  // library for RF RX/TX
#undef int
#undef abs
#undef double
#undef float
#undef round
#undef round

#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11   // DHT 11 
DHT dht(DHTPIN, DHTTYPE);

float temp_c;
float humidity;
int packet = 0;
char charnum[10];

void setup()
{
//  Debug Serial.begin(38400); 
// Serial.println("Starting up");

// Initialise the IO and ISR
  vw_set_tx_pin(3);
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);	 // Bits per sec
  dht.begin();
}

void loop()
{
byte error;
byte rh;
float humival_f;
float tempval_f;
float dew_point_f;
  
char str[15];
 
// Read values from the sensor
  float temp_c;
  float humidity;
  temp_c = dht.readTemperature();
  humidity = dht.readHumidity();

  char buff[30];
  sprintf(buff,"%c",255);
  const char *msg0 = buff;
  vw_send((uint8_t *)msg0, strlen(msg0)); // Send control character 
  vw_wait_tx(); // Wait until the whole message is gone

// Serial.print("Temperature = ");
  serialPrintFloat(temp_c);
  sprintf(buff,"%cC ",223); 
  const char *msg1 = buff;
  vw_send((uint8_t *)msg1, strlen(msg1));
  vw_wait_tx(); // Wait until the whole message is gone
// Serial.print("C");    
 
////////////////////////////////////////////////////////////////////////////////////////     
  sprintf(buff,"%c",254);
  const char *msg5 = buff;
  vw_send((uint8_t *)msg5, strlen(msg5));
  vw_wait_tx(); // Wait until the whole message is gone
 
// Serial.print(" Rel Humidity = ");
  serialPrintFloat(humidity);
// Serial.print("%");
  const char *msg2 = "% ";
  vw_send((uint8_t *)msg2, strlen(msg2));
  vw_wait_tx(); // Wait until the whole message is gone
 
  delay(1000);
}

void serialPrintFloat( float f){
  //Serial.print((int)f);
  itoa((int)f, charnum, 10);    
  vw_send((uint8_t *)charnum, strlen(charnum));
  vw_wait_tx(); // Wait until the whole message is gone
  // Serial.print(".");
  const char *msg = ".";
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx(); // Wait until the whole message is gone
 
  int temp = (f - (int)f) * 100;
  // Serial.print( abs(temp) );
  itoa(abs(temp), charnum, 10);    
  vw_send((uint8_t *)charnum, strlen(charnum));
  vw_wait_tx(); // Wait until the whole message is gone
}



birdmun's picture

The first hit I got suggested via a forum post that:

#include <stdlib.h>
dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);

NilsB's picture

dev/null

birdmun's picture

what version of the DHT library you are using. The one I grabbed does not differentiate two sensors the way the one yours does.

All of this means I can not replicate your issues until I know what DHT lib you are using.

NilsB's picture

...looks like it is this one here. Sample here.

NilsB's picture

You can try to send the humidity value as a tupel of two int values. So then your float to char* problem turns into a int to char* problem.

There are several techniques to do that. A simple one is to take the truncated integer part of your humidity value into int A and the scaled floating part into int B. So a number like 1234.5678 turns into A: "1234" and B: "5678" (Scaling-Factor: 1000).

This two integers can be transmitted as two char* now.

 

Here is a transmitter code snippet:

const int FOUR_DIGITS = 4;
void loop()
{
    int fourDigitValue = 1234;
    char valueAsChar[FOUR_DIGITS];
    itoa(fourDigitValue,valueAsChar,10);
    vw_send((uint8_t *)valueAsChar, strlen(valueAsChar));
    vw_wait_tx(); // Wait until the whole message is gone
}

Here is a receiver code:

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	char* message = (char*)buf;
        int value = atoi(message);
	Serial.println(value);
    }
}

...

 

This is my test setup.