Skip to content

Commit

Permalink
tests/driver_dht: adapted to driver changes
Browse files Browse the repository at this point in the history
  • Loading branch information
haukepetersen committed Feb 7, 2017
1 parent 3afc3eb commit c6aa413
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions tests/driver_dht/Makefile
Expand Up @@ -2,6 +2,7 @@ APPLICATION = driver_dht
include ../Makefile.tests_common

USEMODULE += dht
USEMODULE += fmt
USEMODULE += xtimer

include $(RIOTBASE)/Makefile.include
53 changes: 30 additions & 23 deletions tests/driver_dht/main.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2015 Ludwig Knüpfer, Christian Mehlis
* 2016 Freie Universität Berlin
* 2016-2017 Freie Universität Berlin
*
* 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
Expand All @@ -24,41 +24,48 @@
#include <stdio.h>

#include "xtimer.h"
#include "timex.h"
#include "fmt.h"
#include "dht.h"
#include "dht_params.h"

extern dht_t dht_devs[];
#define DELAY (2 * US_PER_SEC)

int main(void)
{
dht_t dev;
int16_t temp, hum;
char temp_s[10];
char hum_s[10];

puts("DHT temperature and humidity sensor test application\n");

/* initialize first configured sensor */
printf("Initializing DHT sensor...\t");
if (dht_init(&dev, &dht_params[0]) == DHT_OK) {
puts("[OK]\n");
}
else {
puts("[Failed]");
return 1;
}

/* periodically read temp and humidity values */
while (1) {
for (unsigned i = 0; i < DHT_NUMOF; i++) {
dht_t *dev = &dht_devs[i];
int16_t temp, hum;
int16_t dec_temp, dec_hum, int_temp, int_hum;

if (dht_read(dev, &temp, &hum) == -1) {
puts("error reading data");
continue;
}
if (dht_read(&dev, &temp, &hum) != DHT_OK) {
puts("Error reading values");
continue;
}

/* split up values into integral and fractional parts for nicer
* printing
* TODO: this should be done in some kind of library... */
int_temp = temp / 10;
dec_temp = temp - (int_temp * 10);
int_hum = hum / 10;
dec_hum = hum - (int_hum * 10);
size_t n = fmt_s16_dfp(temp_s, temp, 1);
temp_s[n] = '\0';
n = fmt_s16_dfp(hum_s, hum, 1);
hum_s[n] = '\0';

printf("DHT device #%u - ", i);
printf("temp: %i.%i°C, ", int_temp, dec_temp);
printf("relative humidity: %i.%i%%\n", int_hum, dec_hum);
printf("DHT values - temp: %s°C - relative humidity: %s%%\n",
temp_s, hum_s);

xtimer_usleep(2000 * US_PER_MS);
}
xtimer_usleep(DELAY);
}

return 0;
Expand Down

0 comments on commit c6aa413

Please sign in to comment.