Skip to content

Instantly share code, notes, and snippets.

@gdunstone
Last active July 10, 2018 07:01
Show Gist options
  • Save gdunstone/25892d4605b40691e89cee96db536331 to your computer and use it in GitHub Desktop.
Save gdunstone/25892d4605b40691e89cee96db536331 to your computer and use it in GitHub Desktop.
ethernet bme280
#include "ArduinoJson.h"
//#include <Dhcp.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
//#include <msgpck.h>
#include <CRC32.h>
#include <DFRobot_BME280.h>
#define SEA_LEVEL_PRESSURE 1013.25f
DFRobot_BME280 bme; //I2C
float temp, pa, hum, alt, es, ea, vpd, ah_kgm3, ah_gm3;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 100);
EthernetServer server(80);
const size_t bufferSize = JSON_OBJECT_SIZE(10);
void setup() {
Serial.begin(9600);
Serial.println("Startup...");
// I2c default address is 0x76, if the need to change please modify bme.begin(Addr)
while (!bme.begin(0x77)) {
Serial.println("No sensor device found, check line or address!");
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.println("server live");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
Serial.println("new client");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println();
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]);
}
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.createObject();
root["temp_c"] = temp;
root["hum_rh"] = hum;
root["pa_p"] = 95944.796875;
root["alt_m"] = alt;
root["es_kPa"] = es;
root["ea_kPa"] = ea;
root["vpd_kPa"] = vpd;
root["ah_gm3"] = ah_gm3;
root["crc32"] = int(crc.finalize());
root["host"] = "sensor01";
root.prettyPrintTo(client);
// msgpck_write_map_header(&client, 9);
// msgpck_write_string(&client, "temp_c");
// msgpck_write_float(&client, temp);
// msgpck_write_string(&client, "hum_rh");
// msgpck_write_float(&client, hum);
// msgpck_write_string(&client, "pa_p");
// msgpck_write_float(&client, pa);
// msgpck_write_string(&client, "alt_m");
// msgpck_write_float(&client, alt);
//
// msgpck_write_string(&client, "es_kPa");
// msgpck_write_float(&client, es);
// msgpck_write_string(&client, "ea_kPa");
// msgpck_write_float(&client, ea);
// msgpck_write_string(&client, "vpd_kPa");
// msgpck_write_float(&client, vpd);
// msgpck_write_string(&client, "ah_gm3");
// msgpck_write_float(&client, ah_gm3);
//
// msgpck_write_string(&client, "crc32");
// msgpck_write_integer(&client, crc.finalize());
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment