Skip to content

Instantly share code, notes, and snippets.

@gdunstone
Last active July 9, 2018 01:01
Show Gist options
  • Save gdunstone/321900574cb9155f7a596690336bb9f4 to your computer and use it in GitHub Desktop.
Save gdunstone/321900574cb9155f7a596690336bb9f4 to your computer and use it in GitHub Desktop.
bme280 msgpack program
// libraries
// https://github.com/bakercp/CRC32
// https://github.com/DFRobot/DFRobot_BME280
// https://github.com/HEADS-project/arduino_msgpack
#include <CRC32.h>
#include <DFRobot_BME280.h>
#include <msgpck.h>
#define SEA_LEVEL_PRESSURE 1013.25f
#define BME_CS 10
DFRobot_BME280 bme; //I2C
float temp, pa, hum, alt, es, ea, vpd, ah_kgm3, ah_gm3;
void setup() {
Serial.begin(115200);
// I2c default address is 0x76, if the need to change please modify bme.begin(Addr)
if (!bme.begin(0x77)) {
Serial.println("No sensor device found, check line or address!");
while (1);
}
}
void loop() {
temp = bme.temperatureValue();
hum = bme.humidityValue();
pa = bme.pressureValue();
alt = bme.altitudeValue(SEA_LEVEL_PRESSURE);
// saturated vapor pressure
es = 0.6108 * exp(17.27 * temp / (temp + 237.3));
// actual vapor pressure
ea = hum / 100.0 * es;
// this equation returns a negative value (in kPa), which while technically correct,
// is invalid in this case because we are talking about a deficit.
vpd = (ea - es) * -1;
// mixing ratio
//w = 621.97 * ea / ((pressure64/10) - ea);
// saturated mixing ratio
//ws = 621.97 * es / ((pressure64/10) - es);
// absolute humidity (in kg/m³)
ah_kgm3 = es / (461.5 * (temp + 273.15));
// report it as g/m³
ah_gm3 = ah_kgm3*1000;
CRC32 crc;
float values[] = { temp, hum, pa, alt, es, ea, vpd, ah_gm3 };
for ( byte i = 0; i < 8; i++){
crc.update(values[i]);
}
msgpck_write_map_header(&Serial, 9);
msgpck_write_string(&Serial, "temp_c");
msgpck_write_float(&Serial, temp);
msgpck_write_string(&Serial, "hum_rh");
msgpck_write_float(&Serial, hum);
msgpck_write_string(&Serial, "pa_p");
msgpck_write_float(&Serial, pa);
msgpck_write_string(&Serial, "alt_m");
msgpck_write_float(&Serial, alt);
msgpck_write_string(&Serial, "es_kPa");
msgpck_write_float(&Serial, es);
msgpck_write_string(&Serial, "ea_kPa");
msgpck_write_float(&Serial, ea);
msgpck_write_string(&Serial, "vpd_kPa");
msgpck_write_float(&Serial, vpd);
msgpck_write_string(&Serial, "ah_gm3");
msgpck_write_float(&Serial, ah_gm3);
msgpck_write_string(&Serial, "crc16");
msgpck_write_integer(&Serial, crc.finalize());
Serial.write('\n');
//
// Serial.print("Temp: ");
// Serial.print(temp);
// Serial.println("°C");
//
// Serial.print("Pa: ");
// Serial.print(pa);
// Serial.println("Pa");
//
// Serial.print("Hum: ");
// Serial.print(hum);
// Serial.println("%");
//
// Serial.print("Alt: ");
// Serial.print(alt);
// Serial.println("m");
// Serial.print("SVP: \t");
// Serial.print(es);
// Serial.println("kPa");
//
// Serial.print("AVP: \t");
// Serial.print(ea);
// Serial.println("kPa");
//
// Serial.print("VPD: \t");
// Serial.print(vpd);
// Serial.println("kPa");
//
// Serial.print("AH: \t");
// Serial.print(ah_gm3);
// Serial.println("g/m³");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment