Can i take these 3 bunches of code below

Hello all,

so i'm trying to use a BME280 with a EPS8266 wifi board. i can get the mqtt stuff working as the guild says. but it makes for alot more "stuff" in my node red interface (which is trivial). i have a raspberry pi 2 running "Magic Mirror, Node Red, and MQTT" that setup sends a single burst of data over my network for the broker to decide what to do with.

i know alot of useless info. can i take these 3 bunches of code below and send it all as one burst instead of 3?

that is where i'm at a loss, and could use some direction.

void loop() {
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis - previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
// New BME280 sensor readings
temp = bme.readTemperature();
//temp = 1.8*bme.readTemperature() + 32;
hum = bme.readHumidity();
pres = bme.readPressure()/100.0F;

// Publish an MQTT message on topic esp/bme280/temperature
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
Serial.printf("Message: %.2f \n", temp);

// Publish an MQTT message on topic esp/bme280/humidity
uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_HUM, packetIdPub2);
Serial.printf("Message: %.2f \n", hum);

// Publish an MQTT message on topic esp/bme280/pressure
uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_PRES, 1, true, String(pres).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_PRES, packetIdPub3);
Serial.printf("Message: %.3f \n", pres);

Everything you're sending is in string format. Just combine all your data in one string. Of course you then will have to sort it out on the other end.

Also your thread title is highly misleading, you seem to want to combine data, not compress code.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Perhaps something like this:

void DoTheBME680Thing( void *pvParameters )
{
  SPI.begin(); // initialize the SPI library
  vTaskDelay( 10 );
  if (!bme.begin()) {
    log_i("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms
  //wait for a mqtt connection
  while ( !MQTTclient.connected() )
  {
    vTaskDelay( 250 );
  }
  xEventGroupSetBits( eg, evtWaitForBME );
  TickType_t xLastWakeTime    = xTaskGetTickCount();
  const TickType_t xFrequency = 1000 * 15; //delay for mS
  int sendLRDataTrigger       = 240; // 1 hour-ish = 240
  int sendLRdataCount         = sendLRDataTrigger - 1; //send linear regression data when count is reached
  String bmeInfo = "";
  bmeInfo.reserve( 100 );
  for (;;)
  {
    x_eData.Temperature  = bme.readTemperature();
    x_eData.Temperature  = ( x_eData.Temperature * 1.8f ) + 32.0f; // (Celsius x 1.8) + 32
    x_eData.Pressure     = bme.readPressure();
    x_eData.Pressure     = x_eData.Pressure / 133.3223684f; //converts to mmHg
    sendLRdataCount++;
    if ( sendLRdataCount >= sendLRDataTrigger )
    {
      xQueueOverwrite( xQ_lrData, (void *) &x_eData.Pressure ); // send to trends
      sendLRdataCount    = 0;
    }
    x_eData.Humidity     = bme.readHumidity();
    x_eData.IAQ          = fCalulate_IAQ_Index( bme.readGas(), x_eData.Humidity );
    //log_i( " temperature % f, Pressure % f, Humidity % f IAQ % f", x_eData.Temperature, x_eData.Pressure, x_eData.Humidity, x_eData.IAQ);
    bmeInfo.concat( String(x_eData.Temperature, 2) );
    bmeInfo.concat( "," );
    bmeInfo.concat( String(x_eData.Pressure, 2) );
    bmeInfo.concat( "," );
    bmeInfo.concat( String(x_eData.Humidity, 2) );
    bmeInfo.concat( "," );
    bmeInfo.concat( String(x_eData.IAQ, 2) );
    xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
    if ( MQTTclient.connected() )
    {
      MQTTclient.publish( topicInsideInfo, bmeInfo.c_str() );
    }
    xSemaphoreGive( sema_MQTT_KeepAlive );
    xSemaphoreGive( sema_PublishPM ); // release publish of dust density
    xSemaphoreTake( sema_mqttOK, portMAX_DELAY );
    mqttOK ++;
    xSemaphoreGive( sema_mqttOK );
    xQueueOverwrite( xQ_eData, (void *) &x_eData );// send data to display
    //
    bmeInfo = ""; // empty string
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    // log_i( "DoTheBME280Thing high watermark % d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete ( NULL );
}

how would you change it to a .json format like the pi is in node red. like node red i just draged and droped.

{"temperature_C":23.82,"humidity":50.085054652957076,"pressure_hPa":1003.5699281016917,"model":"BME280"}

that is what i get from my raspberry pi running node red. it's a json object, but influxbd should take the data sent and catagorize it in it's database from each input i select in node red

how can i take

Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_BME280, packetIdPub1);
Serial.printf("Message: %.2f \n", temp);
Serial.printf("Message: %.2f \n", hum);
Serial.printf("Message: %.3f \n", pres);

so i don't know what the %.2f /n means, can i combine these three things into a single line to output {"temp":.,"humi":.,"pre":***.}

cause right not it's sending new lines for each and not putting any usefull data in the database.

It means "print the specified number to 2 decimal places, followed by a new line"

Clue for you?

%.2f /n means means "print the specified number to 2 decimal places, followed by /n" whereas %.2f \n means "print the specified number to 2 decimal places, followed by a new line"

uint16_t packetIdPub1 = mqttClient.publish(MQTT_BME280, 1, true);
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_BME280, packetIdPub1);
Serial.printf("Tempature: %.2f ", temp,"Humidity: %.2f ", hum,"Pressure: %.3f \n", pres);

blargins this is what i tryed and it's only publising the tempature. i tryed modifying the publish line to add more and the program wouldn't compile. i'm still not seeing anything past the tempature.

I often can't see things I would never do. :slight_smile:

Do some reading on printf()

You need to put all the text and any formatting characters in one pair of quotes and follow that with a list of the matching variables to be printed, like this

Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_BME280, packetIdPub1);

and not mix them up like this

Serial.printf("Tempature: %.2f ", temp,"Humidity: %.2f ", hum,"Pressure: %.3f \n", pres);

so is that why the code starts with 3 seprate chunks and make it send 3 mqtt messages? i don't understand why they decided to make the code this way.

That's why documentation and comments are so important.

here is the site i'm using.

i mean i can get it to work, i just don't like the end result. (Sorry wrong tab in my browser :frowning:

Aha, well maybe because it's a demo program, not a finished application.

i get it, it's a learning tool. i'm trying to learn and modify the scrip for my needs. but i'm an idiot and as much as i read these things i don't understand what im missing to get a output string of the 3 items from the bme280. i'm out putting this all to a influxdb database for graphana to display nice and pretty.

how do i serial print the output of temp, humi and press in a single string with the titles temp, humi, and pre like i did above so influxdb can use it?

i wish i could pi zeros all over the place like i was. my problem would be a non isssue, but over the last 2 years testing in my house, the raspberry pi's are not as roubost as the esp8266 on repetly sending out a string of data.

reply #12

i see that, it's gobly gook to me. i don't even know how you cliped my comment above.

Then you have to learn and study. There is no other way, apart from hiring a programmer.