Here is a bit of code using freeRTOS on a ESP32
#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;
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_15); // 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 )
{
log_i( "received topic: %s", topic);
//log_i( "incomming... topic %s payload %s", Topic, payload );
}
////
void connectToMQTT() {
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()
{
log_i( "Connecting to WiFi " );
WiFi.begin( SSID, PWD );
vTaskDelay( 750);
while ( WiFi.status() != WL_CONNECTED )
{
log_i(".");
vTaskDelay( 800 );
}
log_i( "WiFi Connected - ");
}
////
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 )
{
while (!bme.begin())
{
log_i("Could not find BME680 sensor!");
vTaskDelay( 10 );
}
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 SEALEVELPRESSURE_HPA = 1013.25;
float Temperature = 0.0f;
float Pressure = 0.0f;
float Humidity = 0.0f;
float gas_resistance = 0.0f; //in K ohms
String sTemprature = "/Temperature";
String sPressure = "/Pressure";
String sGas_Resistance = "/GasResistance";
xEventGroupSetBits( eg, evtSetupBME_Complete ); // trigger tasks
for ( ;; )
{
xEventGroupWaitBits (eg, evtDoBME, pdTRUE, pdTRUE, portMAX_DELAY ); //
Temperature = (bme.readTemperature() * 1.8f) + 32; // (Celsius x 1.8) + 32
Pressure = bme.readPressure() / 133.3223684; //converts to mmHg
Humidity = bme.readHumidity();
gas_resistance = bme.gas_resistance / 1000.0f;
log_i( "Temperature %f C, Pressure %f hPa, Humidity %f, Gas Resistance %f KOhms", Temperature, Pressure, Humidity, gas_resistance );
client.publish( "Home/oTemperature" , String(Temperature).c_str() );
vTaskDelay( 12 ); // gives the Raspberry Pi 4 time to receive the message and process
client.publish( "Home/oHumidity" , String(Humidity).c_str() );
vTaskDelay( 12 ); // no delay and RPi is still processing previous message
client.publish( "Home/oGas_Resistance" , String(gas_resistance).c_str() );
vTaskDelay( 12 );
xSemaphoreGive ( sema_ReadBME680 );
} // for loop
vTaskDelete ( NULL );
} // void fDoBMP ( void *pvParameters )