diff --git a/tests/driver_mhz19/Makefile b/tests/driver_mhz19/Makefile new file mode 100644 index 0000000000..0516e545b7 --- /dev/null +++ b/tests/driver_mhz19/Makefile @@ -0,0 +1,24 @@ +include ../Makefile.tests_common + +USEMODULE += xtimer + +# set default device parameters in case they are undefined +TEST_MODE ?= 1 + +TEST_UART ?= UART_DEV\(1\) +TEST_PWM ?= GPIO_PIN\(PC,8\) + +ifeq ($(TEST_MODE),1) + USEMODULE += mhz19_uart +endif + +ifeq ($(TEST_MODE),2) + USEMODULE += mhz19_pwm +endif + +# export parameters +CFLAGS += -DTEST_MODE=$(TEST_MODE) +CFLAGS += -DTEST_UART=$(TEST_UART) +CFLAGS += -DTEST_PWM=$(TEST_PWM) + +include $(RIOTBASE)/Makefile.include diff --git a/tests/driver_mhz19/README.md b/tests/driver_mhz19/README.md new file mode 100644 index 0000000000..82b1bebad1 --- /dev/null +++ b/tests/driver_mhz19/README.md @@ -0,0 +1,14 @@ +# MH-Z19/MH-Z19B Driver Test + +## Introduction +This test will test if the MH-Z19 C02 gas sensor is working. The MH-Z19B seems +to be newer design which also works with this driver. + +## Configuration +When testing the UART mode, set `TEST_MODE` to 1. To test the PWM mode, set +`TEST_MODE` to 2. In UART mode, make sure that `TEST_UART` points to the +interface to use. In PWM mode, make sure to define `TEST_PWM`. + +## Expected result +The sensor should continuously (every 1 sec) output the CO2 ppm level. When the +sensor is disconnected, it should display a failure message every second. diff --git a/tests/driver_mhz19/main.c b/tests/driver_mhz19/main.c new file mode 100644 index 0000000000..7a17717fde --- /dev/null +++ b/tests/driver_mhz19/main.c @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2018 Koen Zandberg + * Copyright (C) 2018 Beduino Master Projekt - University of Bremen + * Copyright (C) 2020 Bas Stottelaar + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for the MH-Z19 sensor driver + * + * @author Koen Zandberg + * @author Christian Manal + * @author Bas Stottelaar + * + * @} + */ + +#define TEST_MODE_UART 1 +#define TEST_MODE_PWM 2 + +#ifndef TEST_MODE +#error "TEST_MODE not defined" +#endif + +#include +#include "xtimer.h" +#include "mhz19.h" +#include "mhz19_params.h" + +int main(void) +{ + mhz19_t dev; + +#if TEST_MODE == TEST_MODE_UART + mhz19_params_t params = { .uart = TEST_UART }; +#endif +#if TEST_MODE == TEST_MODE_PWM + mhz19_params_t params = { .pin = TEST_PWM }; +#endif + + puts("MH-Z19 CO2 sensor test application\n"); + + /* initialize the sensor */ +#if TEST_MODE == TEST_MODE_UART + printf("Initializing sensor in UART mode..."); +#endif +#if TEST_MODE == TEST_MODE_PWM + printf("Initializing sensor in PWM mode..."); +#endif + + if (mhz19_init(&dev, ¶ms) == 0) { + puts("[OK]"); + } + else { + puts("[Failed]"); + return 1; + } + + /* read CO2 level every 1 seconds */ + int16_t ppm; + while (1) { + printf("Testing sensor communication..."); + int res = mhz19_get_ppm(&dev, &ppm); + if (res == 0) { + puts("[OK]"); + } + else { + printf("[Failed]: %d\n", res); + } + + /* display results */ + printf("CO2: %d ppm\n", ppm); + + /* sleep between measurements */ + xtimer_sleep(1); + } + + return 0; +}