Created
July 10, 2018 07:02
-
-
Save gdunstone/de220c08f0eb13ea0af76e4e7fce0b31 to your computer and use it in GitHub Desktop.
ethernet dht22
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DHT.h> | |
#include "ArduinoJson.h" | |
#define DHTTYPE DHT22 | |
#define DHTPIN 7 | |
//#include <Dhcp.h> | |
#include <Ethernet.h> | |
#include <EthernetClient.h> | |
#include <EthernetServer.h> | |
// #include <msgpck.h> | |
#include <CRC32.h> | |
#define SEA_LEVEL_PRESSURE 1013.25f | |
DHT dht(DHTPIN, DHTTYPE); | |
float temp, hum, 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..."); | |
dht.begin(); | |
// start the Ethernet connection and the server: | |
Ethernet.begin(mac, ip); | |
server.begin(); | |
Serial.println("server live"); | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop() { | |
// The sensor can only be read from every 1-2s, and requires a minimum | |
// 2s warm-up after power-on. | |
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(); | |
delay(2000); | |
temp = dht.readTemperature(); | |
hum = dht.readHumidity(); | |
// 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, es, ea, vpd, ah_gm3 }; | |
for ( byte i = 0; i < 6; i++) { | |
crc.update(values[i]); | |
} | |
DynamicJsonBuffer jsonBuffer(bufferSize); | |
JsonObject& root = jsonBuffer.createObject(); | |
root["temp_c"] = temp; | |
root["hum_rh"] = hum; | |
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); | |
break; | |
// 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