Environmental
Note: The code snippets in this article are intended for Mita version 1a1ed7881e2220895ec60ce4f657972538c43195 and XDK-Workbench versions 3.4.0 and higher.
This article introduces the configuration and use of the environmental sensor in Mita. The article’s focus is on the software and API provided by the platform. For more specific information about the hardware itself, specifically the environmental sensor BME280 used by the XDK, please read the corresponding BME280 datasheet.
- Environmental API Overview
- Reading Environmental Data
- Configuring the Environmental Sensor
- Full Code Example
- Appendix
Environmental API Overview
Mita provides the possibility to use the environmental sensor resource without setting it up beforehand. If the configuration and implementation provided by the Mita implementation are insufficient for a certain purpose, it is recommended to make adapting changes in the generated C code.
The environmental sensor implementation in Mita is based on the Platform Sensor library, which is included in the XDK SDK. It contains modules for a range of sensors and virtual sensors. The image below illustrates the sensor stack.
Reading Environmental Data
Reading the environmental data is fairly simple, as it does not require any kind of initialization beforehand.
The following code will read the temperature, pressure and humidity of the environmental sensor and print the values to the console every 1 second.
every 1 second {
var humi = environment.humidity.read();
var pres = environment.pressure.read();
var temp = environment.temperature.read();
println(`Humidity: ${humi} Pressure: ${pres} Temperature: ${temp}`);
}
Configuring the Environmental Sensor
This section provides a brief overview of the configuration for the environmental sensor. For more information on specific settings and setting recommendations, please refer to the BME280 datasheet.
Power Mode
The BME280 offers two operation modes, called Power Modes. The first (and default) mode is Normal.
In Normal Power Mode, the BME280 will be in a state of perpetual cycling between measurement periods and inactive periods.
In Forced Power Mode, the BME280 will perform one measurement, store the results and then return to sleep mode.
The configuration-item power_mode
can be set to one of the following values:
- Normal (default)
- Forced
Standby Time
The Standby Time determines the duration of the inactive periods in Normal Power Mode.
The configuration-item standby_time
can be set to a value between 1 ms and 1000 ms. While the configuration_item allows for any value represented by an unsigned 32 bit integer, it will always result in one of the following standby times:
- 1
- 10
- 20
- 63
- 125
- 250
- 500
- 1000
If the inserted value is not matching one of these configurations it will be rounded to the closest configuration value.
Oversampling
For each of the individual sensors that are part of the environmental sensor, oversampling can be configured. Oversampling can be used to reduce noise in the resulting sensor value. Please note that a higher oversampling means that measurement time and power consumption increase.
The configuration-items temperature_oversampling
, pressure_oversampling
, humidity_oversampling
can be respectively set to one of the following values:
- OVERSAMPLE_1X
- OVERSAMPLE_2X
- OVERSAMPLE_4X
- OVERSAMPLE_8X
- OVERSAMPLE_16X
Full Code Example
package main;
import platforms.xdk110;
// -- correction factor for the XDKs heatoutput
var tempCorrection = 6.5975;
// -- optional setup of the BME280 environmental sensor
setup environment {
power_mode = Normal;
standby_time = 1000; // ms
temperature_oversampling = OVERSAMPLE_1X;
}
every 1 second
{
var humi = environment.humidity.read();
var pres : double = environment.pressure.read()/1000.0;
var temp : double = environment.temperature.read()/1000.0;
// -- this corrects for the heat output of a typical running XDK application
temp = temp - tempCorrection;
println(`${humi} %%rh ${pres} kPa ${temp} degC`);
}
Appendix
XDK Console Output example
The following console log is an example output of the full code example above.