bilderberg2020:
Okay, I understand the drill, thank you. But I don't know what that code looks like. Do you have a sample code?
Sure thing:
#include "time.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include "certs.h"
#include "sdkconfig.h"
#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include "esp_sleep.h"
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
////
EventGroupHandle_t eg;
#define evtDoBME ( 1 << 2 ) //100
#define evtSetupBME_Complete ( 1 << 3 )
SemaphoreHandle_t sema_ReadBME680;
////
WiFiClientSecure secureClient = WiFiClientSecure();
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker
////
Adafruit_BME680 bme( GPIO_NUM_5); // hardware SPI
///
////
void setup()
{
eg = xEventGroupCreate();
SPI.begin();
sema_ReadBME680 = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore( fDoBME, "fDoBME", 20000, NULL, 3, NULL, 1 ); // assigned to core
//start this task last
xTaskCreatePinnedToCore( fDoTheThing, "fDoTheThing", 40000, NULL, 5, NULL, 1 ); // assigned to core
} // end setup()
////
void mqttCallBack( char* topic, byte* payload, unsigned int length )
{
if ( client.connected() )
{
String s;
for ( int i = 0; i < length; i++ )
{
s += (char)payload[i];
}
log_i( "incomming... topic %s payload %s", topic, s);
}
}
////
void connectToMQTT()
{
client.setCallback( mqttCallBack );
if ( client.connect(clientID, mqtt_username, mqtt_password) )
{
log_i("Connected to MQTT Broker!");
} else {
log_i("Connection to MQTT Broker failed...");
}
log_i("MQTT Connected");
}
//
void connectToWiFi()
{
struct tm timeinfo;
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -25200;
const int daylightOffset_sec = 3600;
log_i( "Connecting to WiFi " );
WiFi.begin( SSID, PWD );
vTaskDelay( 750);
while ( WiFi.status() != WL_CONNECTED )
{
log_i(".");
vTaskDelay( 800 );
}
log_i( "WiFi Connected - ");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
////
void fDoTheThing( void * pvParameters )
{
xEventGroupWaitBits (eg, evtSetupBME_Complete, pdTRUE, pdTRUE, portMAX_DELAY ); //
connectToWiFi();
connectToMQTT();
while (1)
{
xEventGroupSetBits( eg, evtDoBME ); // trigger tasks
xSemaphoreTake( sema_ReadBME680, portMAX_DELAY ); // wait for task to be done
log_i( "entering deep sleep" );
client.disconnect();
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
esp_sleep_enable_timer_wakeup( (60000000 * 6) ); // set timer to wake up every 60000000uS (1 minute) * 6
//esp_sleep_enable_timer_wakeup( (60000000) ); // set timer to wake up every 60000000uS (1 minute)
esp_deep_sleep_start();
} //while(1)
vTaskDelete ( NULL );
} // void fDoTheThing( void * pvParameters )
////
void fDoBME ( void *pvParameters )
{
float hum_score, gas_score;
while (!bme.begin())
{
log_i("Could not find BME680 sensor!");
vTaskDelay( 10 );
}
log_i("Found BME680 sensor!");
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
float Temperature = 0.0f;
float Pressure = 0.0f;
float Humidity = 0.0f;
float gas_reference = 0.0;
float hum_reference = 40.0f;
int getgasreference_count = 0;
float gas_lower_limit = 5000.0f; // Bad air quality limit
float gas_upper_limit = 50000.0f; // Good air quality limit
float air_quality_score = 0.0f;
xEventGroupSetBits( eg, evtSetupBME_Complete ); // trigger task to begin
for ( ;; )
{
xEventGroupWaitBits (eg, evtDoBME, pdTRUE, pdTRUE, portMAX_DELAY ); //
Temperature = (bme.readTemperature() * 1.8f) + 32.0f; // (Celsius x 1.8) + 32
Pressure = bme.readPressure() / 133.3223684f; //converts to mmHg
Humidity = bme.readHumidity();
//Calculate humidity contribution to IAQ index
gas_reference = GetGasReference(); // get 10 gas readings and average.
if ( Humidity >= 38.0f && Humidity <= 42.0f )
{
hum_score = 0.25f * 100.0f; // Humidity +/-5% around optimum
} else { //sub-optimal
if (Humidity < 38)
{
hum_score = 0.25f / hum_reference * Humidity * 100.0f;
} else {
hum_score = ((-0.25f / (100.0f - hum_reference) * Humidity) + 0.416666f) * 100.0f;
}
}
gas_score = (0.75 / (gas_upper_limit - gas_lower_limit) * gas_reference - (gas_lower_limit * (0.75 / (gas_upper_limit - gas_lower_limit)))) * 100.0f;
//Combine results for the final IAQ index value (0-100% where 100% is good quality air)
air_quality_score = hum_score + gas_score;
log_i( "Temperature %f C, Pressure %f hPa, Humidity %f, IAQ %f", Temperature, Pressure, Humidity, air_quality_score );
client.publish( "Home/oTemperature", String(Temperature).c_str() );
client.publish( "Home/oHumidity", String(Humidity).c_str() );
client.publish( "Home/oGas_Resistance", String(air_quality_score).c_str() );
client.publish( "Home/oPressure", String(Pressure).c_str() );
xSemaphoreGive ( sema_ReadBME680 );
} // for loop
vTaskDelete ( NULL );
} // void fDoBMP ( void *pvParameters )
////
float GetGasReference(){
// Now run the sensor for a burn-in period, then use combination of relative humidity and gas resistance to estimate indoor air quality as a percentage.
float gas_reference = 0.0f;
int readings = 10;
for (int i = 1; i <= readings; i++){ // read gas for 10 x 0.150mS = 1.5secs
gas_reference += bme.readGas();
}
gas_reference = gas_reference / readings;
return gas_reference;
}
////
void loop() {}
////