From 4d90cd2e3fef175cffef19dade534d5b0d5e0165 Mon Sep 17 00:00:00 2001 From: Harald Pichler Date: Thu, 19 Oct 2017 11:35:46 +0200 Subject: [PATCH] add dht sensor code --- examples/osd/arduino-dht/DHT.h | 7 +- examples/osd/arduino-dht/arduino-dht.geany | 65 +++++++++++++++++++ .../osd/arduino-dht/resources/res-htu21dhum.c | 6 +- .../arduino-dht/resources/res-htu21dtemp.c | 6 +- examples/osd/arduino-dht/sketch.pde | 55 +++++++++++----- 5 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 examples/osd/arduino-dht/arduino-dht.geany diff --git a/examples/osd/arduino-dht/DHT.h b/examples/osd/arduino-dht/DHT.h index d81f6dbc9..4d85657b5 100644 --- a/examples/osd/arduino-dht/DHT.h +++ b/examples/osd/arduino-dht/DHT.h @@ -6,12 +6,7 @@ written by Adafruit Industries #ifndef DHT_H #define DHT_H -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif - +#include "Arduino.h" // Uncomment to enable printing out nice debug messages. //#define DHT_DEBUG diff --git a/examples/osd/arduino-dht/arduino-dht.geany b/examples/osd/arduino-dht/arduino-dht.geany new file mode 100644 index 000000000..add8ab8ad --- /dev/null +++ b/examples/osd/arduino-dht/arduino-dht.geany @@ -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 diff --git a/examples/osd/arduino-dht/resources/res-htu21dhum.c b/examples/osd/arduino-dht/resources/res-htu21dhum.c index f66c71c74..b41618b5a 100644 --- a/examples/osd/arduino-dht/resources/res-htu21dhum.c +++ b/examples/osd/arduino-dht/resources/res-htu21dhum.c @@ -52,7 +52,7 @@ RESOURCE(res_htu21dhum, NULL, NULL); -extern char htu21d_hum_s[8]; +extern char dht_hum_s[8]; 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) { 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)); } else if(accept == 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)); } else { diff --git a/examples/osd/arduino-dht/resources/res-htu21dtemp.c b/examples/osd/arduino-dht/resources/res-htu21dtemp.c index cdc9a9a7d..cf6c4cb32 100644 --- a/examples/osd/arduino-dht/resources/res-htu21dtemp.c +++ b/examples/osd/arduino-dht/resources/res-htu21dtemp.c @@ -52,7 +52,7 @@ RESOURCE(res_htu21dtemp, NULL, NULL); -extern char htu21d_temp_s[8]; +extern char dht_temp_s[8]; static void 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) { 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)); } else if(accept == 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)); } else { diff --git a/examples/osd/arduino-dht/sketch.pde b/examples/osd/arduino-dht/sketch.pde index b8ce34ae6..72db39f5b 100644 --- a/examples/osd/arduino-dht/sketch.pde +++ b/examples/osd/arduino-dht/sketch.pde @@ -11,32 +11,43 @@ */ #include -#include "Adafruit_HTU21DF.h" +#include "DHT.h" extern "C" { #include "arduino-process.h" #include "rest-engine.h" -Adafruit_HTU21DF htu = Adafruit_HTU21DF(); - 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 + +// 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) { + printf("DHT Sensor\n"); // switch off the led pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH); - // htu21d sensor - if (!htu.begin()) { - printf("Couldn't find sensor!"); - } + // DHT sensor + dht.begin(); // init coap resourcen rest_init_engine (); #pragma GCC diagnostic ignored "-Wwrite-strings" @@ -51,12 +62,22 @@ void setup (void) // LOOP_INTERVAL (30 * CLOCK_SECOND) void loop (void) { - htu21d_temp = htu.readTemperature(); - htu21d_hum = htu.readHumidity(); - dtostrf(htu21d_temp , 0, 2, htu21d_temp_s ); - dtostrf(htu21d_hum , 0, 2, htu21d_hum_s ); + // Reading temperature or humidity takes about 250 milliseconds! + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + dht_hum = dht.readHumidity(); + // 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 -// printf("Temp: '%s'",htu21d_temp_s); -// printf("\t\tHum: '%s'\n",htu21d_hum_s); + printf("Temp: '%s'",dht_temp_s); + printf("\t\tHum: '%s'\n",dht_hum_s); }