From 698d8dce90b081d0528ec8f65917516c6326c92a Mon Sep 17 00:00:00 2001 From: Harald Pichler Date: Wed, 26 Nov 2014 21:47:00 +0100 Subject: [PATCH] add coap18 --- examples/osd/climate/er-example-server.c | 184 ++---------------- examples/osd/climate/project-conf.h | 3 +- examples/osd/climate/resources/res-dht11hum.c | 101 ++++++++++ .../osd/climate/resources/res-dht11temp.c | 104 ++++++++++ examples/osd/climate/resources/res-ds1820.c | 6 +- examples/osd/climate/resources/res-info.c | 85 ++++++++ examples/osd/climate/resources/res-sht11.c | 96 --------- 7 files changed, 309 insertions(+), 270 deletions(-) create mode 100644 examples/osd/climate/resources/res-dht11hum.c create mode 100644 examples/osd/climate/resources/res-dht11temp.c create mode 100644 examples/osd/climate/resources/res-info.c delete mode 100644 examples/osd/climate/resources/res-sht11.c diff --git a/examples/osd/climate/er-example-server.c b/examples/osd/climate/er-example-server.c index d0ed5e2f9..fbf78cc63 100644 --- a/examples/osd/climate/er-example-server.c +++ b/examples/osd/climate/er-example-server.c @@ -44,27 +44,6 @@ #include "contiki-net.h" #include "rest-engine.h" - -#if PLATFORM_HAS_DHT11 -#include "dev/dht11.h" -uint16_t dht11_temp=0, dht11_hum=0; -#endif - -#if defined (PLATFORM_HAS_BUTTON) -#include "dev/button-sensor.h" -#endif - -#if defined (PLATFORM_HAS_TEMPERATURE) -#include "dev/temperature-sensor.h" -#endif -#if defined (PLATFORM_HAS_BATTERY) -#include "dev/battery-sensor.h" -#endif -#if defined (PLATFORM_HAS_SHT11) -#include "dev/sht11-sensor.h" -#endif - - #define DEBUG 0 #if DEBUG #define PRINTF(...) printf(__VA_ARGS__) @@ -80,6 +59,20 @@ uint16_t dht11_temp=0, dht11_hum=0; * Resources to be activated need to be imported through the extern keyword. * The build system automatically compiles the resources in the corresponding sub-directory. */ + +#if defined (PLATFORM_HAS_INFO) +extern resource_t res_info; +#endif + +#if PLATFORM_HAS_DHT11HUM +#include "dev/dht11.h" +extern resource_t res_dht11hum; +#endif + +#if PLATFORM_HAS_DHT11TEMP +#include "dev/dht11.h" +extern resource_t res_dht11temp; +#endif #if defined (PLATFORM_HAS_DS1820) #include "dev/ds1820.h" @@ -101,155 +94,6 @@ extern resource_t res_battery; extern resource_t res_radio; #endif - - -/******************************************************************************/ - -#if PLATFORM_HAS_INFO -/* - * Resources are defined by the RESOURCE macro. - * Signature: resource name, the RESTful methods it handles, and its URI path (omitting the leading slash). - */ -//RESOURCE(info, METHOD_GET, "info", "title=\"Info\";rt=\"text\""); -static void res_get_info_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); - -/* A simple getter example. Returns the reading from light sensor with a simple etag */ -RESOURCE(res_info, - "title=\"Info\";rt=\"text\"", - res_get_info_handler, - NULL, - NULL, - NULL); -/* - * A handler function named [resource name]_handler must be implemented for each RESOURCE. - * A buffer for the response payload is provided through the buffer pointer. Simple resources can ignore - * preferred_size and offset, but must respect the REST_MAX_CHUNK_SIZE limit for the buffer. - * If a smaller block size is requested for CoAP, the REST framework automatically splits the data. - */ -static void -res_get_info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) -{ - char message[100]; - int index = 0; - int length = 0; /* |<-------->| */ - - /* Some data that has the length up to REST_MAX_CHUNK_SIZE. For more, see the chunk resource. */ - // jSON Format - index += sprintf(message + index,"{\n \"version\" : \"V0.4.3\",\n"); - index += sprintf(message + index," \"name\" : \"6lowpan-climate\"\n"); - index += sprintf(message + index,"}\n"); - - length = strlen(message); - memcpy(buffer, message,length ); - - REST.set_header_content_type(response, REST.type.APPLICATION_JSON); - REST.set_response_payload(response, buffer, length); -} -#endif - - -#if PLATFORM_HAS_DHT11TEMP -/*A simple getter example. Returns the reading from dhtxx sensor*/ -//RESOURCE(dht11temp, METHOD_GET, "s/temp", "title=\"Temperatur DHTxx\";rt=\"temperature-c\""); - -static void res_get_dht11temp_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); - -/* A simple getter example. Returns the reading from light sensor with a simple etag */ -RESOURCE(res_dht11temp, - "title=\"Temperature DHTxx\";rt=\"temperature c\"", - res_get_dht11temp_handler, - NULL, - NULL, - NULL); - -static void -res_get_dht11temp_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) -{ - char message[100]; - int length = 0; /* |<-------->| */ - - const uint16_t *accept = NULL; - int num = REST.get_header_accept(request, &accept); - - if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN)) - { - REST.set_header_content_type(response, REST.type.TEXT_PLAIN); - snprintf(message, REST_MAX_CHUNK_SIZE, "%d.%02d",dht11_temp/100, dht11_temp % 100); - - length = strlen(message); - memcpy(buffer, message,length ); - - REST.set_response_payload(response, buffer, length); - } - else if (num && (accept[0]==REST.type.APPLICATION_JSON)) - { - REST.set_header_content_type(response, REST.type.APPLICATION_JSON); - snprintf(message, REST_MAX_CHUNK_SIZE, "{\"temp\":\"%d.%02d\"}",dht11_temp/100, dht11_temp % 100); - - length = strlen(message); - memcpy(buffer, message,length ); - - REST.set_response_payload(response, buffer, length); - } - else - { - REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); - REST.set_response_payload(response, (uint8_t *)"Supporting content-types text/plain and application/json", 56); - } -} -#endif //PLATFORM_HAS_DHT11TEMP - -#if PLATFORM_HAS_DHT11 -/*A simple getter example. Returns the reading from dhtxx sensor*/ -//RESOURCE(dht11, METHOD_GET, "s/hum", "title=\"Humidity DHTxx\";rt=\"humidity-%\""); - -static void res_get_dht11hum_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); - -/* A simple getter example. Returns the reading from light sensor with a simple etag */ -RESOURCE(res_dht11hum, - "title=\"Humidity DHTxx\";rt=\"humidity %\"", - res_get_dht11hum_handler, - NULL, - NULL, - NULL); -static void -res_get_dht11hum_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) -{ - char message[100]; - int length = 0; /* |<-------->| */ - - unsigned int accept = -1; - REST.get_header_accept(request, &accept); - - if(accept == -1 || accept == REST.type.TEXT_PLAIN) - { - REST.set_header_content_type(response, REST.type.TEXT_PLAIN); - snprintf(message, REST_MAX_CHUNK_SIZE, "%d.%02d",dht11_hum/100, dht11_hum % 100); - - length = strlen(message); - memcpy(buffer, message,length ); - - REST.set_response_payload(response, buffer, length); - } - else if (accept == REST.type.APPLICATION_JSON) - { - REST.set_header_content_type(response, REST.type.APPLICATION_JSON); - snprintf(message, REST_MAX_CHUNK_SIZE, "{\"hum\":\"%d.%02d\"}",dht11_hum/100, dht11_hum % 100); - - length = strlen(message); - memcpy(buffer, message,length ); - - REST.set_response_payload(response, buffer, length); - } - else - { - REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); - REST.set_response_payload(response, (uint8_t *)"Supporting content-types text/plain and application/json", 56); - } -} -#endif //PLATFORM_HAS_DHT11 - - void hw_init() { diff --git a/examples/osd/climate/project-conf.h b/examples/osd/climate/project-conf.h index 6ae7f7aa5..af40cac78 100644 --- a/examples/osd/climate/project-conf.h +++ b/examples/osd/climate/project-conf.h @@ -32,9 +32,10 @@ #ifndef PROJECT_ERBIUM_CONF_H_ #define PROJECT_ERBIUM_CONF_H_ +#define PLATFORM_HAS_INFO 1 #define PLATFORM_HAS_BATTERY 1 #define PLATFORM_HAS_DS1820 1 -#define PLATFORM_HAS_DHT11 1 +#define PLATFORM_HAS_DHT11HUM 1 //#define PLATFORM_HAS_DHT11TEMP 1 #define PLATFORM_HAS_LEDS 1 diff --git a/examples/osd/climate/resources/res-dht11hum.c b/examples/osd/climate/resources/res-dht11hum.c new file mode 100644 index 000000000..84da7d151 --- /dev/null +++ b/examples/osd/climate/resources/res-dht11hum.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2014, OSDomotics, Institute of Technology. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + */ + +/** + * \file + * dht11hum Sensor Resource + * + * This is a simple GET resource that returns the humidity in % rel. + * + * \author + * Harald Pichler + */ + +#include "contiki.h" + +#if PLATFORM_HAS_DHT11HUM + +#include +#include +#include +#include +#include +#include "rest-engine.h" +#include "dev/dht11.h" + +/*A simple getter example. Returns the reading from dhtxx sensor*/ +static void res_get_dht11hum_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); + +RESOURCE(res_dht11hum, + "title=\"Humidity DHTxx\";rt=\"humidity %\"", + res_get_dht11hum_handler, + NULL, + NULL, + NULL); + +uint16_t dht11_hum=0; + +static void +res_get_dht11hum_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) +{ + char message[100]; + int length = 0; /* |<-------->| */ + + unsigned int accept = -1; + REST.get_header_accept(request, &accept); + + if(accept == -1 || accept == REST.type.TEXT_PLAIN) + { + REST.set_header_content_type(response, REST.type.TEXT_PLAIN); + snprintf(message, REST_MAX_CHUNK_SIZE, "%d.%02d",dht11_hum/100, dht11_hum % 100); + + length = strlen(message); + memcpy(buffer, message,length ); + + REST.set_response_payload(response, buffer, length); + } + else if (accept == REST.type.APPLICATION_JSON) + { + REST.set_header_content_type(response, REST.type.APPLICATION_JSON); + snprintf(message, REST_MAX_CHUNK_SIZE, "{\"hum\":\"%d.%02d\"}",dht11_hum/100, dht11_hum % 100); + + length = strlen(message); + memcpy(buffer, message,length ); + + REST.set_response_payload(response, buffer, length); + } + else + { + REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); + REST.set_response_payload(response, (uint8_t *)"Supporting content-types text/plain and application/json", 56); + } +} +#endif /* PLATFORM_HAS_DS1820 */ diff --git a/examples/osd/climate/resources/res-dht11temp.c b/examples/osd/climate/resources/res-dht11temp.c new file mode 100644 index 000000000..7d6cf1a30 --- /dev/null +++ b/examples/osd/climate/resources/res-dht11temp.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2014, OSDomotics, Institute of Technology. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + */ + +/** + * \file + * DHT11temp Sensor Resource + * + * This is a simple GET resource that returns the temperature in Celsius + * + * \author + * Harald Pichler + */ + +#include "contiki.h" + +#if PLATFORM_HAS_DHT11TEMP + +#include +#include +#include +#include +#include +#include "rest-engine.h" +#include "dev/dht11.h" + +/*A simple getter example. Returns the reading from dhtxx sensor*/ +//RESOURCE(dht11temp, METHOD_GET, "s/temp", "title=\"Temperatur DHTxx\";rt=\"temperature-c\""); + +static void res_get_dht11temp_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); + +/* A simple getter example. Returns the reading from light sensor with a simple etag */ +RESOURCE(res_dht11temp, + "title=\"Temperature DHTxx\";rt=\"temperature c\"", + res_get_dht11temp_handler, + NULL, + NULL, + NULL); + +uint16_t dht11_temp=0; + +static void +res_get_dht11temp_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) +{ + char message[100]; + int length = 0; /* |<-------->| */ + + const uint16_t *accept = NULL; + int num = REST.get_header_accept(request, &accept); + + if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN)) + { + REST.set_header_content_type(response, REST.type.TEXT_PLAIN); + snprintf(message, REST_MAX_CHUNK_SIZE, "%d.%02d",dht11_temp/100, dht11_temp % 100); + + length = strlen(message); + memcpy(buffer, message,length ); + + REST.set_response_payload(response, buffer, length); + } + else if (num && (accept[0]==REST.type.APPLICATION_JSON)) + { + REST.set_header_content_type(response, REST.type.APPLICATION_JSON); + snprintf(message, REST_MAX_CHUNK_SIZE, "{\"temp\":\"%d.%02d\"}",dht11_temp/100, dht11_temp % 100); + + length = strlen(message); + memcpy(buffer, message,length ); + + REST.set_response_payload(response, buffer, length); + } + else + { + REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); + REST.set_response_payload(response, (uint8_t *)"Supporting content-types text/plain and application/json", 56); + } +} +#endif /* PLATFORM_HAS_DS1820 */ diff --git a/examples/osd/climate/resources/res-ds1820.c b/examples/osd/climate/resources/res-ds1820.c index 000ebd7c6..4d059bb5f 100644 --- a/examples/osd/climate/resources/res-ds1820.c +++ b/examples/osd/climate/resources/res-ds1820.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Nimbus Centre for Embedded Systems Research, Cork Institute of Technology. + * Copyright (c) 2014, OSDomotics, Institute of Technology. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -34,9 +34,9 @@ * DS1820 Sensor Resource * * This is a simple GET resource that returns the temperature in Celsius - * and the humidity reading from the SHT11. + * * \author - * Pablo Corbalan + * Harald Pichler */ #include "contiki.h" diff --git a/examples/osd/climate/resources/res-info.c b/examples/osd/climate/resources/res-info.c new file mode 100644 index 000000000..8d3232ead --- /dev/null +++ b/examples/osd/climate/resources/res-info.c @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014, OSDomotics, Institute of Technology. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + */ + +/** + * \file + * Info Resource + * + * This is a simple GET resource that returns Information + * + * \author + * Harald Pichler + */ + +#include "contiki.h" + +#if PLATFORM_HAS_INFO + +#include +#include +#include +#include "rest-engine.h" + +static void res_get_info_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); + +/* A simple getter example. Returns the reading from light sensor with a simple etag */ +RESOURCE(res_info, + "title=\"Info\";rt=\"text\"", + res_get_info_handler, + NULL, + NULL, + NULL); +/* + * A handler function named [resource name]_handler must be implemented for each RESOURCE. + * A buffer for the response payload is provided through the buffer pointer. Simple resources can ignore + * preferred_size and offset, but must respect the REST_MAX_CHUNK_SIZE limit for the buffer. + * If a smaller block size is requested for CoAP, the REST framework automatically splits the data. + */ +static void +res_get_info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) +{ + char message[100]; + int index = 0; + int length = 0; /* |<-------->| */ + + /* Some data that has the length up to REST_MAX_CHUNK_SIZE. For more, see the chunk resource. */ + // jSON Format + index += sprintf(message + index,"{\n \"version\" : \"V0.4.3\",\n"); + index += sprintf(message + index," \"name\" : \"6lowpan-climate\"\n"); + index += sprintf(message + index,"}\n"); + + length = strlen(message); + memcpy(buffer, message,length ); + + REST.set_header_content_type(response, REST.type.APPLICATION_JSON); + REST.set_response_payload(response, buffer, length); +} +#endif /* PLATFORM_HAS_INFO */ diff --git a/examples/osd/climate/resources/res-sht11.c b/examples/osd/climate/resources/res-sht11.c deleted file mode 100644 index 56ed86c01..000000000 --- a/examples/osd/climate/resources/res-sht11.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2014, Nimbus Centre for Embedded Systems Research, Cork Institute of Technology. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the Contiki operating system. - */ - -/** - * \file - * SHT11 Sensor Resource - * - * This is a simple GET resource that returns the temperature in Celsius - * and the humidity reading from the SHT11. - * \author - * Pablo Corbalan - */ - -#include "contiki.h" - -#if PLATFORM_HAS_SHT11 - -#include -#include "rest-engine.h" -#include "dev/sht11/sht11-sensor.h" - -static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); - -/* Get Method Example. Returns the reading from temperature and humidity sensors. */ -RESOURCE(res_sht11, - "title=\"Temperature and Humidity\";rt=\"Sht11\"", - res_get_handler, - NULL, - NULL, - NULL); - -static void -res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) -{ - /* Temperature in Celsius (t in 14 bits resolution at 3 Volts) - * T = -39.60 + 0.01*t - */ - uint16_t temperature = ((sht11_sensor.value(SHT11_SENSOR_TEMP) / 10) - 396) / 10; - /* Relative Humidity in percent (h in 12 bits resolution) - * RH = -4 + 0.0405*h - 2.8e-6*(h*h) - */ - uint16_t rh = sht11_sensor.value(SHT11_SENSOR_HUMIDITY); - - unsigned int accept = -1; - REST.get_header_accept(request, &accept); - - 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, "%u;%u", temperature, rh); - - REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer)); - } else if(accept == REST.type.APPLICATION_XML) { - REST.set_header_content_type(response, REST.type.APPLICATION_XML); - snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "", temperature, rh); - - 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, "{'Sht11':{'Temperature':%u,'Humidity':%u}}", temperature, rh); - - REST.set_response_payload(response, buffer, strlen((char *)buffer)); - } else { - REST.set_response_status(response, REST.status.NOT_ACCEPTABLE); - const char *msg = "Supporting content-types text/plain, application/xml, and application/json"; - REST.set_response_payload(response, msg, strlen(msg)); - } -} -#endif /* PLATFORM_HAS_SHT11 */