Nucleo Lora / Mbed 2 deprecated STM32_I2C_Sensiron_SCD41

Dependencies:   mbed

Committer:
Picard22
Date:
Mon Aug 16 13:00:07 2021 +0000
Revision:
0:c8ecd653066c
Sensirion_Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Picard22 0:c8ecd653066c 1
Picard22 0:c8ecd653066c 2
Picard22 0:c8ecd653066c 3 /*
Picard22 0:c8ecd653066c 4 * Copyright (c) 2021, Sensirion AG
Picard22 0:c8ecd653066c 5 * All rights reserved.
Picard22 0:c8ecd653066c 6 *
Picard22 0:c8ecd653066c 7 * Redistribution and use in source and binary forms, with or without
Picard22 0:c8ecd653066c 8 * modification, are permitted provided that the following conditions are met:
Picard22 0:c8ecd653066c 9 *
Picard22 0:c8ecd653066c 10 * * Redistributions of source code must retain the above copyright notice, this
Picard22 0:c8ecd653066c 11 * list of conditions and the following disclaimer.
Picard22 0:c8ecd653066c 12 *
Picard22 0:c8ecd653066c 13 * * Redistributions in binary form must reproduce the above copyright notice,
Picard22 0:c8ecd653066c 14 * this list of conditions and the following disclaimer in the documentation
Picard22 0:c8ecd653066c 15 * and/or other materials provided with the distribution.
Picard22 0:c8ecd653066c 16 *
Picard22 0:c8ecd653066c 17 * * Neither the name of Sensirion AG nor the names of its
Picard22 0:c8ecd653066c 18 * contributors may be used to endorse or promote products derived from
Picard22 0:c8ecd653066c 19 * this software without specific prior written permission.
Picard22 0:c8ecd653066c 20 *
Picard22 0:c8ecd653066c 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Picard22 0:c8ecd653066c 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Picard22 0:c8ecd653066c 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Picard22 0:c8ecd653066c 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
Picard22 0:c8ecd653066c 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Picard22 0:c8ecd653066c 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Picard22 0:c8ecd653066c 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Picard22 0:c8ecd653066c 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Picard22 0:c8ecd653066c 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Picard22 0:c8ecd653066c 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Picard22 0:c8ecd653066c 31 * POSSIBILITY OF SUCH DAMAGE.
Picard22 0:c8ecd653066c 32 */
Picard22 0:c8ecd653066c 33 #include "mbed.h"
Picard22 0:c8ecd653066c 34 #include <stdio.h> // printf
Picard22 0:c8ecd653066c 35
Picard22 0:c8ecd653066c 36 #include "scd4x_i2c.h"
Picard22 0:c8ecd653066c 37 #include "sensirion_common.h"
Picard22 0:c8ecd653066c 38 #include "sensirion_i2c_hal.h"
Picard22 0:c8ecd653066c 39
Picard22 0:c8ecd653066c 40 /**
Picard22 0:c8ecd653066c 41 * TO USE CONSOLE OUTPUT (PRINTF) IF NOT PRESENT ON YOUR PLATFORM
Picard22 0:c8ecd653066c 42 */
Picard22 0:c8ecd653066c 43 //#define printf(...)
Picard22 0:c8ecd653066c 44
Picard22 0:c8ecd653066c 45 int main(void) {
Picard22 0:c8ecd653066c 46 int16_t error = 0;
Picard22 0:c8ecd653066c 47
Picard22 0:c8ecd653066c 48 sensirion_i2c_hal_init();
Picard22 0:c8ecd653066c 49
Picard22 0:c8ecd653066c 50 // Clean up potential SCD40 states
Picard22 0:c8ecd653066c 51 scd4x_wake_up();
Picard22 0:c8ecd653066c 52 scd4x_stop_periodic_measurement();
Picard22 0:c8ecd653066c 53 scd4x_reinit();
Picard22 0:c8ecd653066c 54
Picard22 0:c8ecd653066c 55 uint16_t serial_0;
Picard22 0:c8ecd653066c 56 uint16_t serial_1;
Picard22 0:c8ecd653066c 57 uint16_t serial_2;
Picard22 0:c8ecd653066c 58 error = scd4x_get_serial_number(&serial_0, &serial_1, &serial_2);
Picard22 0:c8ecd653066c 59 if (error) {
Picard22 0:c8ecd653066c 60 printf("Error executing scd4x_get_serial_number(): %i\n", error);
Picard22 0:c8ecd653066c 61 } else {
Picard22 0:c8ecd653066c 62 printf("serial: 0x%04x%04x%04x\n", serial_0, serial_1, serial_2);
Picard22 0:c8ecd653066c 63 }
Picard22 0:c8ecd653066c 64
Picard22 0:c8ecd653066c 65 // Start Measurement
Picard22 0:c8ecd653066c 66
Picard22 0:c8ecd653066c 67 error = scd4x_start_periodic_measurement();
Picard22 0:c8ecd653066c 68 if (error) {
Picard22 0:c8ecd653066c 69 printf("Error executing scd4x_start_periodic_measurement(): %i\n",
Picard22 0:c8ecd653066c 70 error);
Picard22 0:c8ecd653066c 71 }
Picard22 0:c8ecd653066c 72
Picard22 0:c8ecd653066c 73 printf("Waiting for first measurement... (5 sec)\n");
Picard22 0:c8ecd653066c 74
Picard22 0:c8ecd653066c 75 for (;;) {
Picard22 0:c8ecd653066c 76 // Read Measurement
Picard22 0:c8ecd653066c 77 sensirion_i2c_hal_sleep_usec(5000000);
Picard22 0:c8ecd653066c 78
Picard22 0:c8ecd653066c 79 uint16_t co2;
Picard22 0:c8ecd653066c 80 int32_t temperature;
Picard22 0:c8ecd653066c 81 int32_t humidity;
Picard22 0:c8ecd653066c 82 error = scd4x_read_measurement(&co2, &temperature, &humidity);
Picard22 0:c8ecd653066c 83 if (error) {
Picard22 0:c8ecd653066c 84 printf("Error executing scd4x_read_measurement(): %i\n", error);
Picard22 0:c8ecd653066c 85 } else if (co2 == 0) {
Picard22 0:c8ecd653066c 86 printf("Invalid sample detected, skipping.\n");
Picard22 0:c8ecd653066c 87 } else {
Picard22 0:c8ecd653066c 88 printf("CO2: %u\n", co2);
Picard22 0:c8ecd653066c 89 printf("Temperature: %d m°C\n", temperature);
Picard22 0:c8ecd653066c 90 printf("Humidity: %d mRH\n", humidity);
Picard22 0:c8ecd653066c 91 }
Picard22 0:c8ecd653066c 92 }
Picard22 0:c8ecd653066c 93
Picard22 0:c8ecd653066c 94 return 0;
Picard22 0:c8ecd653066c 95 }