This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:practical:hardware:sut:esp32:emb1b_1 [2024/03/06 15:31] – [Start] pczekalski | en:iot-open:practical:hardware:sut:esp32:emb1b_1 [2025/04/28 20:32] (current) – [Steps] pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== EMB1B: Reading environmental data with a Bosch integrated sensor ===== | ====== EMB1B: Reading environmental data with a Bosch integrated sensor ===== | ||
| - | In this scenario, we will read environmental data using a BME 280 sensor. It is one of the most popular sensors in weather stations. It integrates a digital thermometer, | + | We will read environmental data using a BME 280 sensor |
| - | The sensor communicates with the microcontroller using I2C. In our laboratory nodes, it uses the I2C bus on GPIOs 5 (SDA) and 4 (SCL) and is visible under the I2C address 0x76.\\ | + | The sensor communicates with the microcontroller using I2C. In all our laboratory nodes, it uses the I2C bus on GPIOs 5 (SDA) and 4 (SCL) and is visible under the I2C address 0x76.\\ |
| - | This scenario can be run stand-alone, | + | This scenario can be run stand-alone |
| ===== Prerequisites ===== | ===== Prerequisites ===== | ||
| Line 33: | Line 33: | ||
| ==== Start ==== | ==== Start ==== | ||
| - | For statical | + | For static |
| ==== Steps ==== | ==== Steps ==== | ||
| - | // Write some extra information if, i.e. some steps are optional; otherwise, cancel this paragraph | + | The steps below present only interaction with the sensor. Those steps should be supplied to present the data (or send it over the network) using other scenarios accordingly, |
| === Step 1 === | === Step 1 === | ||
| - | //Describe activities done in Step 1.// | + | Include a BME 280 control library: |
| + | <code c> | ||
| + | #include < | ||
| + | </code> | ||
| + | === Step 2 === | ||
| + | Declare BME's address, sensor controller class and variables to store readings: | ||
| + | <code c> | ||
| + | #define SCL 4 | ||
| + | #define SDA 5 | ||
| + | |||
| + | static const int BME280_addr = 0x76; //I2C address | ||
| + | |||
| + | static bool isBMEOk = false; | ||
| + | |||
| + | static float temperature; | ||
| + | static float pressure; | ||
| + | static float humidity; | ||
| + | |||
| + | static Adafruit_BME280 bme280; // | ||
| + | </ | ||
| + | |||
| + | === Step 3 === | ||
| + | Initialise the I2C bus and the controller class: | ||
| + | <code c> | ||
| + | Wire.begin(SDA, | ||
| + | delay(100); | ||
| ... | ... | ||
| + | isBMEOk = bme280.begin(BME280_addr); | ||
| + | </ | ||
| + | If '' | ||
| - | === Step n === | + | <note important> |
| - | //Describe activities done in Step n.// | + | === Step 4 === |
| + | Read environmental data (one at a time): | ||
| + | <code c> | ||
| + | temperature = bme280.readTemperature(); | ||
| + | pressure = bme280.readPressure() | ||
| + | humidity = bme280.readHumidity(); | ||
| + | </code> | ||
| + | The temperature is Celsius, air pressure is in Pascals (so we divide it by float 100 to obtain the hPa reading), and relative air humidity is in % (frequently referenced as %Rh). | ||
| + | Note that the controller class has an exciting function of trading Altitude based on the sea-level pressure (needed to have a correct reading: | ||
| + | <code c> | ||
| + | float altitude = bme280.readAltitude(1013.00F); | ||
| + | </ | ||
| + | |||
| + | You need to know the sea level pressure (a parameter, here, 1013hPa). It uses '' | ||
| + | |||
| + | The library also has a mathematical calculation function that returns the current sea level pressure if only altitude and local air pressure are known. It does not read the sensor itself, however: | ||
| + | <code c> | ||
| + | float seaLevelPressure = bme280.seaLevelForAltitude(230, | ||
| + | </ | ||
| + | In the example above, the first parameter is the altitude (230m). | ||
| ==== Result validation ==== | ==== Result validation ==== | ||
| - | Observable | + | The observable |
| ===== FAQ ===== | ===== FAQ ===== | ||
| - | This section | + | **I've got NaN (Not a Number) readings. What to do?**: Check if GPIO is OK (should |
| - | When using the printed version of this manual, please refer to the latest online version of this document to obtain | + | |
| - | // | + | |
| - | **Question? | + | |
| - | // | + | |
| + | <WRAP noprint> | ||
| ===== Project information ===== | ===== Project information ===== | ||
| {{: | {{: | ||
| Line 73: | Line 118: | ||
| {{: | {{: | ||
| </ | </ | ||
| - | + | </ | |
| - | + | ||