add dht sensor code

This commit is contained in:
Harald Pichler 2017-10-19 11:35:46 +02:00
parent da8cd47dea
commit 4d90cd2e3f
5 changed files with 110 additions and 29 deletions

View file

@ -6,12 +6,7 @@ written by Adafruit Industries
#ifndef DHT_H #ifndef DHT_H
#define DHT_H #define DHT_H
#if ARDUINO >= 100
#include "Arduino.h" #include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Uncomment to enable printing out nice debug messages. // Uncomment to enable printing out nice debug messages.
//#define DHT_DEBUG //#define DHT_DEBUG

View file

@ -0,0 +1,65 @@
[file_prefs]
final_new_line=true
ensure_convert_new_lines=false
strip_trailing_spaces=false
replace_tabs=false
[indentation]
indent_width=4
indent_type=1
indent_hard_tab_width=8
detect_indent=false
detect_indent_width=false
indent_mode=2
[project]
name=arduino-dht
base_path=/home/harald/install/osd-contiki/examples/osd/arduino-dht/
description=
file_patterns=
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=0
FILE_NAME_0=530;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2Fsketch.pde;0;4
FILE_NAME_1=1698;None;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2FDHT-sensor-library-master%2Fexamples%2FDHTtester%2FDHTtester.ino;0;4
FILE_NAME_2=801;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-htu21%2Fsketch.pde;0;4
FILE_NAME_3=562;Make;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2FMakefile;0;4
FILE_NAME_4=111;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2FDHT.h;0;4
FILE_NAME_5=327;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2FDHT.cpp;0;4
FILE_NAME_6=1251;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fcpu%2Favr%2Fdev%2Farduino%2FArduino.h;0;4
FILE_NAME_7=0;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fplatform%2Fosd-merkur-256%2Fdev%2Fpins_arduino.h;0;4
FILE_NAME_8=2017;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fplatform%2Fosd-merkur-256%2Fcontiki-conf.h;0;4
FILE_NAME_9=2221;C;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2Fresources%2Fres-htu21dtemp.c;0;4
FILE_NAME_10=5464;Make;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fplatform%2Fosd-merkur-256%2FMakefile.osd-merkur-256;0;4
FILE_NAME_11=2883;C;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-dht%2Fresources%2Fres-htu21dhum.c;0;4
FILE_NAME_12=1581;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-htu21%2FAdafruit_HTU21DF.cpp;0;4
[VTE]
last_dir=/home/harald
[build-menu]
NF_00_LB=_Make
NF_00_CM=make TARGET=osd-merkur-256
NF_00_WD=
NF_01_LB=Make flash
NF_01_CM=make TARGET=osd-merkur-256 flash
NF_01_WD=
NF_03_LB=Make Clean
NF_03_CM=make clean TARGET=osd-merkur-256
NF_03_WD=
C++FT_00_LB=_Kompilieren
C++FT_00_CM=make TARGET=osd-merkur-256
C++FT_00_WD=
C++FT_01_LB=_Erstellen
C++FT_01_CM=make TARGET=osd-merkur-256 flash
C++FT_01_WD=
filetypes=C++;
[editor]
line_wrapping=false
line_break_column=72
auto_continue_multiline=true

View file

@ -52,7 +52,7 @@ RESOURCE(res_htu21dhum,
NULL, NULL,
NULL); NULL);
extern char htu21d_hum_s[8]; extern char dht_hum_s[8];
static void static void
@ -64,12 +64,12 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
if(accept == -1 || accept == REST.type.TEXT_PLAIN) { if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN); REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", htu21d_hum_s); snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", dht_hum_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer)); REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) { } else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON); REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'moisture':%s}", htu21d_hum_s); snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'moisture':%s}", dht_hum_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer)); REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else { } else {

View file

@ -52,7 +52,7 @@ RESOURCE(res_htu21dtemp,
NULL, NULL,
NULL); NULL);
extern char htu21d_temp_s[8]; extern char dht_temp_s[8];
static void static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
@ -63,12 +63,12 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
if(accept == -1 || accept == REST.type.TEXT_PLAIN) { if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN); REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", htu21d_temp_s); snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", dht_temp_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer)); REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) { } else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON); REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'temperature':%s}", htu21d_temp_s); snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'temperature':%s}", dht_temp_s);
REST.set_response_payload(response, buffer, strlen((char *)buffer)); REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else { } else {

View file

@ -11,32 +11,43 @@
*/ */
#include <Wire.h> #include <Wire.h>
#include "Adafruit_HTU21DF.h" #include "DHT.h"
extern "C" { extern "C" {
#include "arduino-process.h" #include "arduino-process.h"
#include "rest-engine.h" #include "rest-engine.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
extern resource_t res_htu21dtemp, res_htu21dhum, res_battery; extern resource_t res_htu21dtemp, res_htu21dhum, res_battery;
float htu21d_hum;
float htu21d_temp;
char htu21d_hum_s[8];
char htu21d_temp_s[8];
#define LED_PIN 4 #define LED_PIN 4
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +3.3V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) o
#define DHTPIN 9 // what digital pin we're connected to
DHT dht(DHTPIN, DHTTYPE);
float dht_hum;
float dht_temp;
char dht_hum_s[8];
char dht_temp_s[8];
} }
void setup (void) void setup (void)
{ {
printf("DHT Sensor\n");
// switch off the led // switch off the led
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); digitalWrite(LED_PIN, HIGH);
// htu21d sensor // DHT sensor
if (!htu.begin()) { dht.begin();
printf("Couldn't find sensor!");
}
// init coap resourcen // init coap resourcen
rest_init_engine (); rest_init_engine ();
#pragma GCC diagnostic ignored "-Wwrite-strings" #pragma GCC diagnostic ignored "-Wwrite-strings"
@ -51,12 +62,22 @@ void setup (void)
// LOOP_INTERVAL (30 * CLOCK_SECOND) // LOOP_INTERVAL (30 * CLOCK_SECOND)
void loop (void) void loop (void)
{ {
htu21d_temp = htu.readTemperature(); // Reading temperature or humidity takes about 250 milliseconds!
htu21d_hum = htu.readHumidity(); // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
dtostrf(htu21d_temp , 0, 2, htu21d_temp_s ); dht_hum = dht.readHumidity();
dtostrf(htu21d_hum , 0, 2, htu21d_hum_s ); // Read temperature as Celsius (the default)
dht_temp = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(dht_hum) || isnan(dht_temp)) {
printf("Failed to read from DHT sensor!\n");
return;
}
dtostrf(dht_temp , 0, 2, dht_temp_s );
dtostrf(dht_hum , 0, 2, dht_hum_s );
// debug only // debug only
// printf("Temp: '%s'",htu21d_temp_s); printf("Temp: '%s'",dht_temp_s);
// printf("\t\tHum: '%s'\n",htu21d_hum_s); printf("\t\tHum: '%s'\n",dht_hum_s);
} }