Put BME680 in separate task

This commit is contained in:
Admar Schoonen 2022-05-22 22:25:08 +02:00
parent 5bdf6f238f
commit 1d65b69a1a
1 changed files with 82 additions and 63 deletions

View File

@ -83,6 +83,7 @@ const char* root_ca = \
const float location[] = {51.445466493287434, 5.515445691496135}; // Telefoonstraat, Eindhoven
const String address = "Telefoonstraat 18, Eindhoven";
// const String address = "Bandeliersberg 157, Roosendaal";
//const float location[] = {51.51831326813842, 4.451744264773111}; // Bandeliersberg, Roosendaal
//const float location[] = {51.44083, 5.47778}; // Eindhoven
@ -119,6 +120,8 @@ RainpickerData rainpickerData;
StaticJsonDocument<6144> doc;
static bool initDone = false;
void parseJson(String * payload)
{
doc.clear();
@ -233,6 +236,82 @@ void leds_clear(void) {
}
}
void readSensors(void) {
static bool readingInProgress = false;
static unsigned long endTime = 0;
if (hw_variant == HW_PROTO_PAPER) {
return;
}
if (readingInProgress == false) {
Serial.println("Starting new measurement");
unsigned long endTime = bme.beginReading();
if (endTime == 0) {
Serial.println(F("Failed to begin reading :("));
return;
}
readingInProgress = true;
Serial.print(F("Reading started at "));
Serial.print(millis());
Serial.print(F(" and will finish at "));
Serial.println(endTime);
} else {
if (millis() - endTime > 0) {
// Obtain measurement results from BME680. Note that this operation isn't
// instantaneous even if milli() >= endTime due to I2C/SPI latency.
//
// This call takes about 162 ms
if (!bme.endReading()) {
Serial.println(F("Failed to complete reading :("));
return;
}
readingInProgress = false;
Serial.print(F("Reading completed at "));
Serial.println(millis());
Serial.print(F("Temperature = "));
Serial.print(bme.temperature);
Serial.println(F(" *C"));
Serial.print(F("Pressure = "));
Serial.print(bme.pressure / 100.0);
Serial.println(F(" hPa"));
Serial.print(F("Humidity = "));
Serial.print(bme.humidity);
Serial.println(F(" %"));
Serial.print(F("Gas = "));
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(F(" KOhms"));
Serial.print(F("Approx. Altitude = "));
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(F(" m"));
Serial.println();
}
}
}
void taskBME680( void * parameter )
{
while (initDone == false) {
delay(100);
}
while (true) {
readSensors();
delay(100);
}
//Serial.println("Ending task 1");
//vTaskDelete( NULL );
}
void setup() {
const char url[] = "https://apikey:cqprlgiafadnidsgeqozcpldkaeqimqw@despatch.luon.net/files/4/despatch.json";
unsigned long interval = 60; // By default check for updates every 60 seconds
@ -297,6 +376,9 @@ void setup() {
Serial.print("dESPatch.configure() returned with code ");
Serial.println(x);
#endif
initDone = true;
xTaskCreate(taskBME680, "taskBME680", 10000, NULL, 1, NULL);
}
// From https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/
@ -730,67 +812,6 @@ static void updateLeds(void) {
ledsSetPixelColor(IAQI_LED, r, g, b);
}
void readSensors(void) {
static bool readingInProgress = false;
static unsigned long endTime = 0;
if (hw_variant == HW_PROTO_PAPER) {
return;
}
if (readingInProgress == false) {
Serial.println("Starting new measurement");
unsigned long endTime = bme.beginReading();
if (endTime == 0) {
Serial.println(F("Failed to begin reading :("));
return;
}
readingInProgress = true;
Serial.print(F("Reading started at "));
Serial.print(millis());
Serial.print(F(" and will finish at "));
Serial.println(endTime);
} else {
if (millis() - endTime > 0) {
// Obtain measurement results from BME680. Note that this operation isn't
// instantaneous even if milli() >= endTime due to I2C/SPI latency.
//
// This call takes about 162 ms
if (!bme.endReading()) {
Serial.println(F("Failed to complete reading :("));
return;
}
readingInProgress = false;
Serial.print(F("Reading completed at "));
Serial.println(millis());
Serial.print(F("Temperature = "));
Serial.print(bme.temperature);
Serial.println(F(" *C"));
Serial.print(F("Pressure = "));
Serial.print(bme.pressure / 100.0);
Serial.println(F(" hPa"));
Serial.print(F("Humidity = "));
Serial.print(bme.humidity);
Serial.println(F(" %"));
Serial.print(F("Gas = "));
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(F(" KOhms"));
Serial.print(F("Approx. Altitude = "));
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(F(" m"));
Serial.println();
}
}
}
void readConnectButton(void) {
static int reset_wifi_timer = millis();
static bool reset_blocked = true;
@ -865,8 +886,6 @@ void loop(void) {
} else if (hw_variant == HW_PROTO_V1) {
leds_rgbw_proto_v1.show();
}
// readSensors();
readConnectButton();