Sensor values are published as JSON in nonMQTT mode

This commit is contained in:
Ollo 2021-11-27 17:08:03 +01:00
parent 29beff5e82
commit 73f637d6ec
2 changed files with 104 additions and 19 deletions

View File

@ -3,8 +3,56 @@
<head> <head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>IKEA Home Sensor</title> <title>IKEA Home Sensor</title>
<script type="text/javascript">
function onBodyLoad(){
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("debug").innerHTML = this.responseText;
}
xhttp.open("GET", "/sensors", true);
xhttp.send();
}
</script>
</head> </head>
<body id="body"> <body id="body" onload="onBodyLoad()">
<h1>Room Sensor</h1> <h1>Room Sensor</h1>
<table>
<th>
<td>Sensor</td>
<td>Value</td>
<td></td>
</th>
<tr>
<td>Temperatur</td>
<td id="temp">temp</td>
<td>°C</td>
</tr>
<tr id="rowHumidity">
<td>Humidity</td>
<td id="humidity">humidity</td>
<td>%</td>
</tr>
<tr id="rowGas">
<td>Gas</td>
<td id="gas">gas</td>
<td> KOhms</td>
</tr>
<tr>
<td>Altitude</td>
<td id="altitude">altitude</td>
<td>m</td>
</tr>
<tr>
<td>Pressure</td>
<td id="pressure">pressure</td>
<td>hPa</td>
</tr>
<tr>
<td>Particle</td>
<td id="particle">particle</td>
<td>micro gram per quibik</td>
</tr>
</table>
<p id="debug">Nothing</p>
</body> </body>
</html> </html>

View File

@ -141,7 +141,7 @@ Adafruit_NeoPixel strip(PIXEL_COUNT, GPIO_WS2812, NEO_GRB + NEO_KHZ800);
// Variablen // Variablen
uint8_t serialRxBuf[80]; uint8_t serialRxBuf[80];
uint8_t rxBufIdx = 0; uint8_t rxBufIdx = 0;
int spm25 = 0; int mParticle_pM25 = 0;
int last = 0; int last = 0;
unsigned int mButtonPressed = 0; unsigned int mButtonPressed = 0;
bool mSomethingReceived = false; bool mSomethingReceived = false;
@ -235,15 +235,28 @@ void onHomieEvent(const HomieEvent &event)
} }
} }
void bmpPublishValues() { void updateLEDs() {
#ifdef BME680 #ifdef BME680
// Tell BME680 to begin measurement. // Tell BME680 to begin measurement.
unsigned long endTime = bmx.beginReading(); unsigned long endTime = bmx.beginReading();
if (endTime == 0) { if (endTime == 0) {
log(MQTT_LEVEL_ERROR, F("BME680 not accessible"), MQTT_LOG_I2READ); log(MQTT_LEVEL_ERROR, "BME680 not accessible", MQTT_LOG_I2READ);
return; return;
} }
#endif #endif
if ( (rgbTemp.get()) && (!mSomethingReceived) ) {
if (bmx.readTemperature() < TEMPBORDER) {
strip.setPixelColor(0, strip.Color(0,0,255));
} else {
strip.setPixelColor(0, strip.Color(255,0,0));
}
strip.show();
} else {
bmx.performReading();
}
}
void bmpPublishValues() {
// Publish the values // Publish the values
temperaturNode.setProperty(NODE_TEMPERATUR).send(String(bmx.readTemperature())); temperaturNode.setProperty(NODE_TEMPERATUR).send(String(bmx.readTemperature()));
pressureNode.setProperty(NODE_PRESSURE).send(String(bmx.readPressure() / 100.0F)); pressureNode.setProperty(NODE_PRESSURE).send(String(bmx.readPressure() / 100.0F));
@ -252,15 +265,29 @@ void bmpPublishValues() {
gasNode.setProperty(NODE_GAS).send(String((bmx.gas_resistance / 1000.0))); gasNode.setProperty(NODE_GAS).send(String((bmx.gas_resistance / 1000.0)));
humidityNode.setProperty(NODE_HUMIDITY).send(String(bmx.humidity)); humidityNode.setProperty(NODE_HUMIDITY).send(String(bmx.humidity));
#endif #endif
}
if ( (rgbTemp.get()) && (!mSomethingReceived) ) { /**
if (bmx.readTemperature() < TEMPBORDER) { * @brief Generate JSON with last measured values
strip.setPixelColor(0, strip.Color(0,0,255)); * For the update intervall, please check @see PM1006_MQTT_UPDATE
} else { * @return String with JSON
strip.setPixelColor(0, strip.Color(255,0,0)); */
} String sensorAsJSON(void) {
strip.show(); String buffer;
} StaticJsonDocument<500> doc;
doc["temp"] = String(bmx.temperature);
#ifdef BME680
doc["gas"] = String((bmx.gas_resistance / 1000.0));
doc["humidity"] = String(bmx.humidity);
#endif
float atmospheric = bmx.pressure / 100.0F;
float altitude = 44330.0 * (1.0 - pow(atmospheric / SEALEVELPRESSURE_HPA, 0.1903));
doc["altitude"] = String(altitude);
doc["pressure"] = String(bmx.pressure / 100.0F);
doc["particle"] = String(mParticle_pM25);
serializeJson(doc, buffer);
return buffer;
} }
/** /**
@ -278,13 +305,15 @@ void loopHandler()
{ {
static long lastRead = 0; static long lastRead = 0;
if ((millis() - lastRead) > PM1006_MQTT_UPDATE) { if ((millis() - lastRead) > PM1006_MQTT_UPDATE) {
int pM25 = getSensorData(); mParticle_pM25 = getSensorData();
if (pM25 >= 0) { if (mParticle_pM25 >= 0) {
particle.setProperty(NODE_PARTICLE).send(String(pM25)); if (!mConnectedNonMQTT) {
particle.setProperty(NODE_PARTICLE).send(String(mParticle_pM25));
}
if (!mSomethingReceived) { if (!mSomethingReceived) {
if (pM25 < 35) { if (mParticle_pM25 < 35) {
strip.fill(strip.Color(0, 255, 0)); /* green */ strip.fill(strip.Color(0, 255, 0)); /* green */
} else if (pM25 < 85) { } else if (mParticle_pM25 < 85) {
strip.fill(strip.Color(255, 127, 0)); /* orange */ strip.fill(strip.Color(255, 127, 0)); /* orange */
} else { } else {
strip.fill(strip.Color(255, 0, 0)); /* red */ strip.fill(strip.Color(255, 0, 0)); /* red */
@ -295,8 +324,11 @@ void loopHandler()
/* Read BOSCH sensor */ /* Read BOSCH sensor */
if (i2cEnable.get() && (!mFailedI2Cinitialization)) { if (i2cEnable.get() && (!mFailedI2Cinitialization)) {
updateLEDs();
if (!mConnectedNonMQTT) {
bmpPublishValues(); bmpPublishValues();
} }
}
lastRead = millis(); lastRead = millis();
} }
@ -415,6 +447,7 @@ void setup()
Adafruit_BMP280::FILTER_X16, /* Filtering. */ Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
#endif #endif
printf("Sensor found on I2C bus\r\n");
} else { } else {
printf("Failed to initialize I2C bus\r\n"); printf("Failed to initialize I2C bus\r\n");
} }
@ -436,6 +469,9 @@ void setup()
mHttp->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){ mHttp->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", String(ESP.getFreeHeap())); request->send(200, "text/plain", String(ESP.getFreeHeap()));
}); });
mHttp->on("/sensors", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", sensorAsJSON());
});
mHttp->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); mHttp->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
mHttp->begin(); mHttp->begin();
Homie.getLogger() << "Webserver started" << endl; Homie.getLogger() << "Webserver started" << endl;
@ -454,7 +490,8 @@ void loop()
if (!mConnectedNonMQTT) { if (!mConnectedNonMQTT) {
Homie.loop(); Homie.loop();
} else { } else {
/*FIXME handle here a webserver */ /* call the custom loop direclty, as Homie is deactivated */
loopHandler();
} }
/* use the pin, receiving the soft serial additionally as button */ /* use the pin, receiving the soft serial additionally as button */
if (digitalRead(GPIO_BUTTON) == LOW) { if (digitalRead(GPIO_BUTTON) == LOW) {