Send a struct to serial with Serial.write()

I do not know how to send my struct to serial with Serial.write( ) . Here my code:

#include "DHT.h"
#define DHTPIN 2     // pino do sensor
#define DHTTYPE DHT22   // modelo do sensor

struct dataSent
{
  float temperatur;
  float humidSend;
};

/////FUNCIAO SENSOR TEMPERATURA E HUMIDADE//////

DHT dht(DHTPIN, DHTTYPE);

void setup() {

  Serial.begin(115200);
  Serial.println("Inicializando...");
  dht.begin();
}

void loop() {

  //Aguardando leitura do sensor
  delay(2000);

  float humidade = dht.readHumidity();
  float temperatura = dht.readTemperature();

  // Teste de leitura do sensor
  if (isnan(humidade) || isnan(temperatura)) {
    Serial.println("Erro ao efetuar leitura do sensor!");
    return;
  }

  Serial.print("Humidade: ");
  Serial.print(humidade);
  Serial.print(" %\t");
  Serial.print("Temperatura: ");
  Serial.print(temperatura);
  Serial.println(" *C ");

  dataSend  data;
  data.tempSend = temperatura;
  data.humidSend = humidade;

  Serial.write(data.tempSend);

}

Make a union of your struct and an 8 byte char array.

This Python Binary Data demo uses a struct and a union.

...R

Delta_G: Make a union of your struct and an 8 byte char array.

Or cast to byte pointer; below will send the complete struct

Serial.write((byte*)&dataSend, sizeof(dataSend));

Why do you want to use Serial.write? Does the receiver expect binary data?

PS: Your struct is called dataSent, not dataSend. And it does not have a member tempSend. Please be accurate.

sterretje

Actually I need send my struct with data sensors using library softmodem.