Browse Source

don't crash, pressure compensation, remove battery

main
Hendrik Langer 5 years ago
parent
commit
c524bef8f5
  1. 39
      src/main.cpp
  2. 2
      src/main.h

39
src/main.cpp

@ -41,7 +41,7 @@ constexpr unsigned int dhcp_interval = 60*60*1000;
uint32_t loop_count = 0;
#define EXTERNAL_POWER 1
//#define EXTERNAL_POWER 1
//senseBox ID
#define SENSEBOX_ID "5a9e9e38f55bff001a494877"
@ -80,6 +80,8 @@ static constexpr uint8_t GEIGER_PIN = D6;
static constexpr float CONV_FACTOR = 0.008120;
static constexpr float OWN_BACKGROUND_CPS = 0.2; // documentation says 0.2 (make sure value doesn't get negative if subtracting!)
static constexpr float HEIGHT = 142;
#ifndef EXTERNAL_POWER
ADC_MODE(ADC_VCC);
#endif
@ -148,6 +150,11 @@ void inline lightsleep() {
void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int format) {
StaticJsonDocument<MQTT_MAX_PACKET_SIZE> jsonDoc; // ToDo: buf_len
// https://de.wikipedia.org/wiki/Barometrische_H%C3%B6henformel#Reduktion_auf_Meeresh%C3%B6he
// https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/
float absolute_humidity = (6.112*exp((17.67*sd.temperature)/(sd.temperature+243.5))*(sd.humidity/100)*18.02)/((273.15+sd.temperature)*1000*0.08314);
float pressure_compensation_factor = exp((9.80665/(287.05*(sd.temperature+273.15+0.12*(((absolute_humidity*461.5*(sd.temperature+273.15))/100)/100)+0.0065*(HEIGHT/2))))*HEIGHT);
if (format == 1) {
JsonArray array = jsonDoc.to<JsonArray>();
@ -162,7 +169,7 @@ void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int for
pressureObject["value"] = sd.pressure;
JsonObject pressureObject2 = array.createNestedObject();
pressureObject2["sensor"] = SENSOR3_ID;
pressureObject2["value"] = sd.pressure+16;
pressureObject2["value"] = sd.pressure*pressure_compensation_factor;
if (sd.sds_updated) {
JsonObject pm10Object = array.createNestedObject();
pm10Object["sensor"] = SENSOR4_ID;
@ -192,7 +199,7 @@ void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int for
jsonDoc["temperature"] = sd.temperature;
jsonDoc["humidity"] = sd.humidity;
jsonDoc["pressure"] = sd.pressure;
jsonDoc["pressure"] = sd.pressure*pressure_compensation_factor;
if (sd.sds_updated) {
jsonDoc["pm10"] = sd.p10;
jsonDoc["pm2.5"] = sd.p25;
@ -290,7 +297,7 @@ void ICACHE_FLASH_ATTR sendValues() {
}
DEBUG_MSG(" [CONNECTED, static]\n");
DEBUG_MSG("IP address: ");
DEBUG_MSG("%s\n", String(WiFi.localIP()).c_str());
DEBUG_MSG("%s\n", WiFi.localIP().toString().c_str());
} else {
DEBUG_MSG("dhcp\n");
@ -309,7 +316,7 @@ void ICACHE_FLASH_ATTR sendValues() {
}
DEBUG_MSG(" [CONNECTED, dhcp]\n");
DEBUG_MSG("IP address: ");
DEBUG_MSG("%s\n", String(WiFi.localIP()).c_str());
DEBUG_MSG("%s\n", WiFi.localIP().toString().c_str());
ip = WiFi.localIP();
dns = WiFi.dnsIP();
@ -355,6 +362,7 @@ void ICACHE_FLASH_ATTR sendValues() {
net.setInsecure();
mqttclient.begin(mqttserver, 8883, net);
int tries = 0;
bool mqtt_error = false;
constexpr unsigned int retry_delay = 500;
constexpr unsigned int max_retry_delay = 5000;
while (!mqttclient.connect(mqttusername, mqttusername, mqttpassword)) {
@ -364,19 +372,24 @@ void ICACHE_FLASH_ATTR sendValues() {
DEBUG_MSG(" [ERROR]\n");
//DEBUG_MSG("Rebooting..\n");
//ESP.restart();
mqtt_error = true;
}
delay(retry_delay);
}
mqttclient.loop();
getValuesJSON(buffer, MQTT_MAX_PACKET_SIZE, 2);
if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) {
DEBUG_MSG("mqtt done\n");
} else {
DEBUG_MSG("mqtt failed\n");
if (!mqtt_error) {
mqttclient.loop();
getValuesJSON(buffer, MQTT_MAX_PACKET_SIZE, 2);
if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) {
DEBUG_MSG("mqtt done\n");
} else {
DEBUG_MSG("mqtt failed\n");
}
mqttclient.loop();
mqttclient.disconnect();
}
mqttclient.loop();
mqttclient.disconnect();
if (sd.rssi == 0) { // re-read rssi if zero
sd.rssi = WiFi.RSSI();

2
src/main.h

@ -7,6 +7,6 @@
#define DEBUG_MSG(...)
#endif
#define FW_VERSION 30
#define FW_VERSION 34
#endif /* _MAIN_H */

Loading…
Cancel
Save