Transmitter-->Receiver : Type of received data

Hello!
As I’m a newbie in Programming and Arduino, I have a little problem on the type of my variables.
My project consists on a Remote Temperature Sensor that sends measurements through an RF link to a base station that collects it all and treats data as needed.
There are two modules, a measuring-sender unit and a data-collecting-base-station unit. The receiver unit is a 433MHz RX unit mounted on a Arduino UNO, it collects packets of data from the air. I’m using the VirtualWire library for that . The sensor unit is a DS18B20 and a 433MHz TX module,also mounted on another arduino board.
I found the same project on a website.
Here is my transmitter’s program:

#include <VirtualWire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
 
OneWire oneWire(7);
DallasTemperature sensors(&oneWire);
char msg[6];
 
void setup() {
    sensors.begin();
    vw_setup(2000);
    vw_set_tx_pin(3);
}
 
void loop() {
    sensors.requestTemperatures();
    float temperatura = sensors.getTempCByIndex(0);
    dtostrf(temperatura, 6, 2, msg);
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx();
    delay(200);
}

And the receiver’s code:

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);	// Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec

    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.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++)
	{
            Serial.println(buf[i]);
            Serial.print(" ");
	    Serial.print(buf[i], HEX);
	    Serial.print(" ");
            Serial.print(buf[i], DEC);
	    Serial.print(" ");
	}
	Serial.println("");
        digitalWrite(13, false);
    }
}

But I’m worrying about my results, I want to know how I can change the type that I receive to decimal. I everytime have the same thing for the first value printed and the Decimal one.
Can someone tell me please where my clumsiness is ? :blush:

But I'm worrying about my results, I want to know how I can change the type that I receive to decimal.

Decimal is not a type.

You are converting one type (float) to another type (char array), and then sending the char array.

On the receiving end, you need to reverse that process, converting the char array received to a float. The atof() function seems ideally suited.

Thank you for your answer !

I meant integer (int). I'm trying the function atof().

I meant integer (int). I'm trying the function atof().

If you are sending a float, why do you then want that as an int? If you really do, use atoi(), instead.

I have used the function atoi().

  • x=atoi(buf*);*
  • Serial.print(x);[/li]*
  • -*
  • [/list]*
    I also tried the function “strtod()”, but I always have this error: invalid conversion to ‘uint8_t’ to ‘const char*’.

I have used the function atoi().

x=atoi(buf); Serial.print(x);

I also tried the function "strtod()", but I always have this error: invalid conversion to 'uint8_t' to 'const char*'.

Post all of your code, not some hand-waving.

The string you are trying to convert is in the array buf, not an element of buf.

Here’s the code:

#include <VirtualWire.h>

int x; // New variable, created to contain the result of atoi()

void setup()
{
    Serial.begin(9600);	// Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec

    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.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++)
	{
	    Serial.println(buf[i], HEX);
	    Serial.print(" ");
            x=atoi(buf[i]);
            Serial.print(x);
            Serial.print(" ");
	}
	Serial.println("");
        digitalWrite(13, false);
    }
}
            x=atoi(buf[i]);

See the last line of Reply #5.