From e92678a847756536a560faf3a52f51ec4112e921 Mon Sep 17 00:00:00 2001 From: harald42 Date: Fri, 4 Jan 2013 15:44:48 +0100 Subject: [PATCH] add sensors, internal temperature, battery --- .../er-rest-example-dht11/er-example-server.c | 47 +++++++++- .../osd/er-rest-example-dht11/project-conf.h | 7 ++ platform/osd-er-lp24/Makefile.osd-er-lp24 | 2 +- platform/osd-er-lp24/dev/adc.c | 90 ++++++++++++++++++ platform/osd-er-lp24/dev/adc.h | 7 ++ platform/osd-er-lp24/dev/battery-sensor.c | 92 +++++++++++++++++++ platform/osd-er-lp24/dev/battery-sensor.h | 48 ++++++++++ platform/osd-er-lp24/dev/temperature-sensor.c | 66 +++++++++++++ platform/osd-er-lp24/dev/temperature-sensor.h | 49 ++++++++++ 9 files changed, 405 insertions(+), 3 deletions(-) create mode 100644 platform/osd-er-lp24/dev/adc.c create mode 100644 platform/osd-er-lp24/dev/adc.h create mode 100644 platform/osd-er-lp24/dev/battery-sensor.c create mode 100644 platform/osd-er-lp24/dev/battery-sensor.h create mode 100644 platform/osd-er-lp24/dev/temperature-sensor.c create mode 100644 platform/osd-er-lp24/dev/temperature-sensor.h 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. */ diff --git a/platform/osd-er-lp24/Makefile.osd-er-lp24 b/platform/osd-er-lp24/Makefile.osd-er-lp24 index 9cb38a782..ad6e7200f 100644 --- a/platform/osd-er-lp24/Makefile.osd-er-lp24 +++ b/platform/osd-er-lp24/Makefile.osd-er-lp24 @@ -4,7 +4,7 @@ CONTIKI_CORE=contiki-main CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o CONTIKI_TARGET_SOURCEFILES += contiki-main.c params.c node-id.c #Needed for slip -CONTIKI_TARGET_SOURCEFILES += button-sensor.c sensors.c slip_uart0.c slip.c +CONTIKI_TARGET_SOURCEFILES += temperature-sensor.c adc.c button-sensor.c sensors.c slip_uart0.c slip.c #Needed for DHT11 humidity sensor CONTIKI_TARGET_SOURCEFILES += dht11.c diff --git a/platform/osd-er-lp24/dev/adc.c b/platform/osd-er-lp24/dev/adc.c new file mode 100644 index 000000000..cdb7eb7a0 --- /dev/null +++ b/platform/osd-er-lp24/dev/adc.c @@ -0,0 +1,90 @@ +/* +* Copyright (c) 2012, BinaryLabs. +* 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. +* +* @(#)$Id: adc.c,v 1.1 2010/08/25 19:34:06 nifi Exp $ +*/ + +/** +* \file +* ADC file for Atmega128rfa1. +* \author +* Paulo Louro +*/ + +#include + +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +int readADC(uint8_t pin) +{ + int result = 0; + + if ( pin >= 14 ) + pin -= 14; + + ADMUX = _BV(REFS1) | _BV(REFS0) | ( pin & 7 ) ; + ADCSRA = _BV(ADEN) | _BV(ADPS0) | _BV(ADPS2) ; + + sbi(ADCSRA,ADSC); + loop_until_bit_is_clear(ADCSRA,ADSC); + + result = ADC; + + ADCSRA=0; //disable ADC + ADMUX=0; //turn off internal vref + + return result; +} + +/** +* \return Internal temperature in 0.01C, e.g. 25C is 2500 +*/ +int readInternalTemp(void) +{ + int reading = 0; + + ADCSRB |= _BV(MUX5); + ADMUX = _BV(REFS1) | _BV(REFS0) | 0b1001 ; + ADCSRA = _BV(ADEN) | _BV(ADPS0) | _BV(ADPS2) ; + + sbi(ADCSRA,ADSC); + loop_until_bit_is_clear(ADCSRA,ADSC); + + reading = ADC; + + ADCSRA=0; //disable ADC + ADCSRB=0; //disable ADC + ADMUX=0; //turn off internal vref + + return reading * 113 - 27280; +} \ No newline at end of file diff --git a/platform/osd-er-lp24/dev/adc.h b/platform/osd-er-lp24/dev/adc.h new file mode 100644 index 000000000..3fe894a1b --- /dev/null +++ b/platform/osd-er-lp24/dev/adc.h @@ -0,0 +1,7 @@ +#ifndef __ADC_ARCH_H__ +#define __ADC_ARCH_H__ + +int readADC(uint8_t pin); +int readInternalTemp(void); + +#endif /* __ADC_ARCH_H__ */ \ No newline at end of file diff --git a/platform/osd-er-lp24/dev/battery-sensor.c b/platform/osd-er-lp24/dev/battery-sensor.c new file mode 100644 index 000000000..495d39a1e --- /dev/null +++ b/platform/osd-er-lp24/dev/battery-sensor.c @@ -0,0 +1,92 @@ +/* +* Copyright (c) 2012, BinaryLabs. +* 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. +* +* @(#)$Id: battery-sensor.h,v 1.1 2012/08/25 19:34:06 nifi Exp $ +*/ + +/** +* \file +* Battery sensor header file for Atmega128rfa1. +* \author +* Paulo Louro +* Harald Pichler +*/ + +#include "dev/battery-sensor.h" +#include "adc.h" + +/* Connect Battery(+) to pin A1, via a 1000/470 voltage divider. +* This will case a battery voltage of 5.0V to read as the max analog +* voltage of 1.6V. +* +* Connect Battery(+) to pin A1, via a 1000/(470+470) voltage divider. +* This will case a battery voltage of 3.3V to read as the max analog +* voltage of 1.6V. +*/ +#define INPUT_CHANNEL 1 + +const struct sensors_sensor battery_sensor; +/*---------------------------------------------------------------------------*/ + +/** +* \return Voltage on battery measurement pin, 4096 is 5V. +*/ +static int +value(int type) +{ + uint16_t h; + uint8_t p1; + BATMON = 16; //give BATMON time to stabilize at highest range and lowest voltage + +/* Bandgap can't be measured against supply voltage in this chip. */ +/* Use BATMON register instead */ + for ( p1=16; p1<31; p1++) { + BATMON = p1; + // delay_us(100); //delay needed? + if ((BATMON&(1< +*/ + +#ifndef __BATTERY_SENSOR_H__ +#define __BATTERY_SENSOR_H__ + +#include "lib/sensors.h" + +extern const struct sensors_sensor battery_sensor; + +#define BATTERY_SENSOR "Battery" + +#endif /* __BATTERY_SENSOR_H__ */ \ No newline at end of file diff --git a/platform/osd-er-lp24/dev/temperature-sensor.c b/platform/osd-er-lp24/dev/temperature-sensor.c new file mode 100644 index 000000000..c85896178 --- /dev/null +++ b/platform/osd-er-lp24/dev/temperature-sensor.c @@ -0,0 +1,66 @@ +/* +* Copyright (c) 2012, BinaryLabs. +* 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. +* +* @(#)$Id: temperature-sensor.c,v 1.1 2010/08/25 19:34:06 nifi Exp $ +*/ + +/** +* \file +* Temperature sensor header file for Atmega128rfa1. +* \author +* Paulo Louro +*/ + +#include "contiki.h" +#include "dev/temperature-sensor.h" + +#define PRINTF(...) printf(__VA_ARGS__) + + +const struct sensors_sensor temperature_sensor; + +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + return readInternalTemp(); +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int c) +{ + return 1; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + return 1; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR, value, configure, status); \ No newline at end of file diff --git a/platform/osd-er-lp24/dev/temperature-sensor.h b/platform/osd-er-lp24/dev/temperature-sensor.h new file mode 100644 index 000000000..6cf5dbf26 --- /dev/null +++ b/platform/osd-er-lp24/dev/temperature-sensor.h @@ -0,0 +1,49 @@ +/* +* Copyright (c) 2012, BinaryLabs. +* 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. +* +* @(#)$Id: temperature-sensor.h,v 1.1 2012/08/25 19:34:06 nifi Exp $ +*/ + +/** +* \file +* Temperature sensor header file for Atmega128rfa1. +* \author +* Paulo Louro +*/ + +#ifndef __TEMPERATURE_SENSOR_H__ +#define __TEMPERATURE_SENSOR_H__ + +#include "lib/sensors.h" +#include "dev/adc.h" + +extern const struct sensors_sensor temperature_sensor; + +#define TEMPERATURE_SENSOR "Temperature" + +#endif /* __TEMPERATURE_SENSOR_H__ */ \ No newline at end of file