diff --git a/examples/osd/er-rest-example-dht11/er-example-server.c b/examples/osd/er-rest-example-dht11/er-example-server.c index b6c1a84de..eec3452b6 100644 --- a/examples/osd/er-rest-example-dht11/er-example-server.c +++ b/examples/osd/er-rest-example-dht11/er-example-server.c @@ -45,8 +45,9 @@ /* Define which resources to include to meet memory constraints. */ #define REST_RES_INFO 1 -#define REST_RES_DS1820 0 +#define REST_RES_DS1820 1 #define REST_RES_DHT11 1 +#define REST_RES_TEMPERATURE 1 #define REST_RES_HELLO 0 #define REST_RES_MIRROR 0 /* causes largest code size */ #define REST_RES_CHUNKS 0 @@ -89,6 +90,9 @@ uint8_t out_temp=0, humidity=0; #if defined (PLATFORM_HAS_LIGHT) #include "dev/light-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 @@ -314,7 +318,7 @@ ds1820_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre 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.%d °C\"}",grad,kgrad); + snprintf(message, REST_MAX_CHUNK_SIZE, "{\"temp\":\"%d.%d\"}",grad,kgrad); length = strlen(message); memcpy(buffer, message,length ); @@ -936,6 +940,41 @@ light_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred } #endif /* PLATFORM_HAS_LIGHT */ +/******************************************************************************/ +#if REST_RES_TEMPERATURE && defined (PLATFORM_HAS_TEMPERATURE) +/* A simple getter example. Returns the reading from light sensor with a simple etag */ +RESOURCE(temperature, METHOD_GET, "sensors/temperature", "title=\"Temperature status\";rt=\"Temperature\""); +void +temperature_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) +{ + int temperature = temperature_sensor.value(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((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", temperature); + + REST.set_response_payload(response, (uint8_t *)buffer, strlen((char *)buffer)); + } + else if (num && (accept[0]==REST.type.APPLICATION_JSON)) + { + REST.set_header_content_type(response, REST.type.APPLICATION_JSON); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'temperature':%d}", temperature); + + REST.set_response_payload(response, buffer, strlen((char *)buffer)); + } + else + { + REST.set_response_status(response, REST.status.UNSUPPORTED_MADIA_TYPE); + const char *msg = "Supporting content-types text/plain and application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} +#endif /* PLATFORM_HAS_TEMPERATURE */ + /******************************************************************************/ #if REST_RES_BATTERY && defined (PLATFORM_HAS_BATTERY) /* A simple getter example. Returns the reading from light sensor with a simple etag */ @@ -1138,6 +1177,10 @@ PROCESS_THREAD(rest_server_example, ev, data) SENSORS_ACTIVATE(light_sensor); rest_activate_resource(&resource_light); #endif +#if defined (PLATFORM_HAS_TEMPERATURE) && REST_RES_TEMPERATURE + SENSORS_ACTIVATE(temperature_sensor); + rest_activate_resource(&resource_temperature); +#endif #if defined (PLATFORM_HAS_BATTERY) && REST_RES_BATTERY SENSORS_ACTIVATE(battery_sensor); rest_activate_resource(&resource_battery); diff --git a/examples/osd/er-rest-example-dht11/project-conf.h b/examples/osd/er-rest-example-dht11/project-conf.h index 1c3dd8933..76558fdd5 100644 --- a/examples/osd/er-rest-example-dht11/project-conf.h +++ b/examples/osd/er-rest-example-dht11/project-conf.h @@ -32,6 +32,13 @@ #ifndef __PROJECT_RPL_WEB_CONF_H__ #define __PROJECT_RPL_WEB_CONF_H__ +//#define PLATFORM_HAS_LEDS 1 +//#define PLATFORM_HAS_BUTTON 1 +//#define PLATFORM_HAS_LIGHT 1 +#define PLATFORM_HAS_TEMPERATURE 1 +//#define PLATFORM_HAS_BATTERY 1 +//#define PLATFORM_HAS_SHT11 1 + #define SICSLOWPAN_CONF_FRAG 1 /* Disabling RDC for demo purposes. Core updates often require more memory. */