Nucleo Lora / Mbed 2 deprecated STM32_I2C_Sensiron_SCD41

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 
00003 /*
00004  * Copyright (c) 2021, Sensirion AG
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  * * Redistributions of source code must retain the above copyright notice, this
00011  *   list of conditions and the following disclaimer.
00012  *
00013  * * Redistributions in binary form must reproduce the above copyright notice,
00014  *   this list of conditions and the following disclaimer in the documentation
00015  *   and/or other materials provided with the distribution.
00016  *
00017  * * Neither the name of Sensirion AG nor the names of its
00018  *   contributors may be used to endorse or promote products derived from
00019  *   this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 #include "mbed.h"
00034 #include <stdio.h>  // printf
00035 
00036 #include "scd4x_i2c.h"
00037 #include "sensirion_common.h"
00038 #include "sensirion_i2c_hal.h"
00039 
00040 /**
00041  * TO USE CONSOLE OUTPUT (PRINTF) IF NOT PRESENT ON YOUR PLATFORM
00042  */
00043 //#define printf(...)
00044 
00045 int main(void) {
00046     int16_t error = 0;
00047 
00048     sensirion_i2c_hal_init();
00049 
00050     // Clean up potential SCD40 states
00051     scd4x_wake_up();
00052     scd4x_stop_periodic_measurement();
00053     scd4x_reinit();
00054 
00055     uint16_t serial_0;
00056     uint16_t serial_1;
00057     uint16_t serial_2;
00058     error = scd4x_get_serial_number(&serial_0, &serial_1, &serial_2);
00059     if (error) {
00060         printf("Error executing scd4x_get_serial_number(): %i\n", error);
00061     } else {
00062         printf("serial: 0x%04x%04x%04x\n", serial_0, serial_1, serial_2);
00063     }
00064 
00065     // Start Measurement
00066 
00067     error = scd4x_start_periodic_measurement();
00068     if (error) {
00069         printf("Error executing scd4x_start_periodic_measurement(): %i\n",
00070                error);
00071     }
00072 
00073     printf("Waiting for first measurement... (5 sec)\n");
00074 
00075     for (;;) {
00076         // Read Measurement
00077         sensirion_i2c_hal_sleep_usec(5000000);
00078 
00079         uint16_t co2;
00080         int32_t temperature;
00081         int32_t humidity;
00082         error = scd4x_read_measurement(&co2, &temperature, &humidity);
00083         if (error) {
00084             printf("Error executing scd4x_read_measurement(): %i\n", error);
00085         } else if (co2 == 0) {
00086             printf("Invalid sample detected, skipping.\n");
00087         } else {
00088             printf("CO2: %u\n", co2);
00089             printf("Temperature: %d m°C\n", temperature);
00090             printf("Humidity: %d mRH\n", humidity);
00091         }
00092     }
00093 
00094     return 0;
00095 }