Send temperature using RadioHead

Hi all,

I’ve been able to get my 433 MHz transmitter and receiver working. I am able to send words such as “hello”, 'HI", etc… I am trying to send temperature data now, but it is erroring. What I’m ultimately after is sending the temperature, in Hg, and Mb as a transmission and receive it on another arduino. The 2nd arudino is receiving the words I send.

The line that errors is:

char msg = currentTemperature // does not work

The error message is:

TX_1.ino: In function 'void loop()':
TX_1.ino:59:18: error: initializer fails to determine size of 'msg'
TX_1.ino:59:18: error: array must be initialized with a brace-enclosed initializer
TX_1.ino:61:5: error: expected ',' or ';' before 'driver'
Error compiling.
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
#include <SFE_BMP180.h>
#include <Wire.h>

RH_ASK driver;

String msg;
double T,P,p0;
char status;

SFE_BMP180 pressure;

#define ALTITUDE 778 // Altitude of in Van Buren, Ohio


void setup()
{
    Serial.begin(9600);   // Debugging only
    if (!driver.init())
         Serial.println("init failed");
         
         
    /*
    Set up SFE_BMP180
    */
   Serial.println("REBOOT");    
   
     if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
}
void loop()
{
    //const char *msg = "hello";
    //driver.send((uint8_t *)msg, strlen(msg));
   // driver.waitPacketSent();
    
       
    //Serial.print(getTemperature());
    //Serial.print(getPressureMb());
    //Serial.print(getPressureInHg());
    // Serial.print(getPressureAltinHg());
      
     float currentTemperature = getTemperature();
     float currentPressureMb = getPressureMb();
     float currentPressureInHg = getPressureInHg();
     float currentPressureAltInHg = getPressureAltinHg();
     float currentPressureAltMb = getPressureAltMb();
     
     Serial.println(currentPressureAltMb);
    
    
   // char msg[] = "hello" // works
    char msg[] = currentTemperature // does not work
    
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    
    delay(2000);
}

/*
Get Temperature
*/

float getTemperature()
{
      status = pressure.startTemperature();
   
   if (status != 0)
    {
      delay(status);
      
       status = pressure.getTemperature(T);
       float myTemp = ((9.0/5.0)*T+32.0);
       return myTemp;
    }
}

/*
Get Pressure in mb
*/
  
float getPressureMb()
{
      status = pressure.startPressure(3);
   
   if (status != 0)
    {
      delay(status);
      
       status = pressure.getPressure(P,T);
       float myPressure = (P);
       return myPressure;
    }
}

/*
Get Pressure in InHg
*/

float getPressureInHg()
{
      status = pressure.startPressure(3);
   
   if (status != 0)
    {
      delay(status);
      
       status = pressure.getPressure(P,T);
       float myPressure = (P*0.0295333727);
       return myPressure;
    }
}


/*
Get Pressure based on my current alt inHg
*/

double getPressureAltinHg()
{
  
   status = pressure.getPressure(P,T);
        if (status != 0)
        {
         p0 = pressure.sealevel(P,ALTITUDE);
        
        }
       return (p0*0.0295333727);
}

/*
Get Pressure based on my current alt in mb
*/

double getPressureAltMb()
{
  
   status = pressure.getPressure(P,T);
        if (status != 0)
        {
         p0 = pressure.sealevel(P,ALTITUDE);
        
        }
       return (p0);
}

hello i think the problem is the way you are trying to convert from float to bytes. I had to do something similar with another transmitter so i just added it to radiohead instead it works perfect take a look

Transmitter:

// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

RH_ASK driver;


struct dataStruct{
  float press_norm ; 
  float press_hg;
  float temp;
  unsigned long counter;
   
}myData;

byte tx_buf[sizeof(myData)] = {0};

void setup()
{
    Serial.begin(9600);	  // Debugging only
    if (!driver.init())
         Serial.println("init failed");
         
   myData.press_norm=1000.11; 
myData.press_hg=0.59; 
myData.temp=22.394; 
         
}

void loop()
{
     

  memcpy(tx_buf, &myData, sizeof(myData) );
  byte zize=sizeof(myData);

driver.send((uint8_t *)tx_buf, zize);

   // driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    myData.counter++;
    delay(2000);
}

Receiver:

// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

RH_ASK driver;

struct dataStruct{
  float press_norm ; 
  float press_hg;
  float temp;
  unsigned long counter;
   
}myData;


void setup()
{
    Serial.begin(9600);	// Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

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

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
	int i;

	// Message with a good checksum received, dump it.
	driver.printBuffer("Got:", buf, buflen);
        memcpy(&myData, buf, sizeof(myData));
        Serial.println("");
        
                Serial.print("press_norm: ");
        Serial.print(myData.press_norm);
        
                      Serial.print("  press_hg: ");
        Serial.print(myData.press_hg);
        
                          Serial.print("  temp: ");
        Serial.print(myData.temp);
        
        Serial.print("  counter: ");
        Serial.println(myData.counter);
    }
}

If you want to send something more just add it in the datastruct, just make sure the datastruct on tx and rx are exactly the same or you will get strange readings.

I added a counter too its always fun to know how long the transmitter have been working.
Anyway happy coding man and good luck.

Thank you for the reply and information. I'll be testing this out later today after my Easter activities with the family. I'll let you know the results.

The above code worked perfectly. I'm now transmitting my data from a sensor outside to my arduino inside. I'm now going to look at making some more sensors and then interfacing with my raspberry pi. Thanks again for your assistance.

Great information here! Using a data structure is a fine way to pass floating point info. I'm using it to send GPS data.

Still being appreciate, thanks it really helped me... :slight_smile:

It's 2017 and this thread is still incredibly helpful. Thanks, swe-dude!

1 Like

I tried this code and it does not work for me. Im a little confused how the software know which pin I am using for data input and output.

Im a little confused how the software know which pin I am using for data input and output.

The software does not know, it assumes certain pins.

You must either use those pins, or tell it which pins to use instead, as described in the RadioHead documentation.

Thank You swe-dude !! I don't quite know how it does but your solution did the trick. Your code will soon be sitting inside my tiny greenhouse!

Just wanted to say, that this was immensely valuable in getting weather data from a BME280 across a 433MHz radio link after all these years. Most common examples utilize strings with which I was having loads of problems parsing correctly at the receiver side. Using the mentioned data structure solved all my problems! :slight_smile:

Here’s what I’m using for reference

// Define input data structure
struct dataStruct{
 float hum;                    // Stores humidity value in percent
 float temp;                   // Stores temperature value in Celcius
 float pres;                   // Stores pressure in hPa
 float alt;                    // Stores altitude in m
 unsigned long pkt_counter;    // Counter for number of packets sent
}bmeData;

byte rdata_buf[sizeof(bmeData)] = {0};
void getBMEdata() {                                 // Read BME280 sensor data
   
   bme.takeForcedMeasurement();                    // Forced mode for typical 1/60Hz (60s) measurement (must have)
   bmeData.hum = bme.readHumidity();                       // Get Humidity value
   bmeData.temp = (bme.readTemperature() - 1);             // Get Temperature value with 1C calibration offset
   bmeData.pres = (bme.readPressure() / 100);              // Get Pressure vlaue
   bmeData.alt = bme.readAltitude(SEALEVELPRESSURE_HPA);   // Calculate Altiture based on sea level pressure
 }