Skip to content

Commit

Permalink
apps: Add temperature and humidity sensor example
Browse files Browse the repository at this point in the history
This patch includes an example of how to monitor ambient
temperature and relative humidity using a DHT series sensor.

Signed-off-by: Luigi Luz <[email protected]>
  • Loading branch information
luigiluz committed Jan 22, 2020
1 parent 4ea351c commit 7974940
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apps/dht/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.8.2)

if (NOT DEFINED ENV{KNOT_BASE})
message(FATAL_ERROR "Source the KNoT shell initialize script!")
endif()

include($ENV{KNOT_BASE}/core/CMakeLists.txt)
project(KNoT_DHT)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

include($ENV{ZEPHYR_BASE}/samples/net/common/common.cmake)
20 changes: 20 additions & 0 deletions apps/dht/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# KNoT
CONFIG_KNOT_NAME="KNoT DHT"
CONFIG_KNOT_THING_DATA_MAX=2

# Logging
CONFIG_LOG=y
CONFIG_LOG_IMMEDIATE=n
CONFIG_LOG_PRINTK=y
CONFIG_KNOT_LOG=y
CONFIG_KNOT_LOG_LEVEL_DEBUG=y

# Sensor
CONFIG_SENSOR=y
CONFIG_DHT=y
CONFIG_DHT_GPIO_PIN_NUM=47
# If using DHT22, comment DHT11 lines and uncomment DHT22 ones
CONFIG_DHT_NAME="DHT11"
#CONFIG_DHT_NAME="DHT22"
CONFIG_DHT_CHIP_DHT11=y
#CONFIG_DHT_CHIP_DHT22=y
1 change: 1 addition & 0 deletions apps/dht/setup.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_BT_DEVICE_NAME="KNoT DHT"
80 changes: 80 additions & 0 deletions apps/dht/src/dht.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* dht.c - KNoT Application Client */

/*
* Copyright (c) 2019, CESAR. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

/*
* This example uses a DHT series sensor (DHT11/DHT22) to monitor ambient
* temperature and relative humidity. This example works for both DHT11 and
* DHT22 sensors, but it is configured by default for DHT11. If you want to
* use it with DHT22, change DHT_NAME and DHT_CHIP flags on prj.conf file.
*/

#include <zephyr.h>
#include <net/net_core.h>
#include <logging/log.h>
#include <device.h>
#include <sensor.h>

#include "knot.h"
#include <knot/knot_types.h>
#include <knot/knot_protocol.h>

LOG_MODULE_REGISTER(dht, LOG_LEVEL_DBG);

struct device *dev;
int temperature, relative_humidity;

/* Get temperature values from DHT sensor*/
int on_get_temperature(int id)
{
struct sensor_value temp;

sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
temperature = temp.val1;
LOG_INF("Temperature value: %d \n", temperature);

return KNOT_CALLBACK_SUCCESS;
}

/* Get relative humidity values from DHT sensor*/
int on_get_humidity(int id)
{
struct sensor_value humidity;

sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity);
relative_humidity = humidity.val1;
LOG_INF("Relative humidity value: %d \n", relative_humidity);

return KNOT_CALLBACK_SUCCESS;
}

void setup(void)
{
/* Peripherals control */
dev = device_get_binding(CONFIG_DHT_NAME);
if (dev == NULL) {
LOG_ERR("Could not get DHT device\n");
}
/* KNoT config */
knot_data_register(0, "TEMPERATURE", KNOT_TYPE_ID_TEMPERATURE,
KNOT_VALUE_TYPE_INT, KNOT_UNIT_TEMPERATURE_C,
&temperature, sizeof(temperature),
NULL, on_get_temperature);
knot_data_config(0, KNOT_EVT_FLAG_TIME, 10, NULL);
knot_data_register(1, "RELATIVE HUMIDITY",
KNOT_TYPE_ID_RELATIVE_HUMIDITY,
KNOT_VALUE_TYPE_INT,
KNOT_UNIT_RELATIVE_HUMIDITY,
&relative_humidity,
sizeof(relative_humidity),
NULL, on_get_humidity);
knot_data_config(1, KNOT_EVT_FLAG_TIME, 10, NULL);
}

void loop(void)
{
}

0 comments on commit 7974940

Please sign in to comment.