-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apps: Add temperature and humidity sensor example
This patch includes an example of how to monitor ambient temperature and relative humidity using a DHT11 sensor. Signed-off-by: Luigi Luz <[email protected]>
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CONFIG_BT_DEVICE_NAME="KNoT DHT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* dht.c - KNoT Application Client */ | ||
|
||
/* | ||
* Copyright (c) 2019, CESAR. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* This example uses a DHT sensor to monitor ambient temperature and relative | ||
* humidity. | ||
*/ | ||
|
||
#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) | ||
{ | ||
} |