add sensors, internal temperature, battery

This commit is contained in:
harald42 2013-01-04 15:44:48 +01:00 committed by harald
parent 17c93b259e
commit e92678a847
9 changed files with 405 additions and 3 deletions

View file

@ -45,8 +45,9 @@
/* Define which resources to include to meet memory constraints. */ /* Define which resources to include to meet memory constraints. */
#define REST_RES_INFO 1 #define REST_RES_INFO 1
#define REST_RES_DS1820 0 #define REST_RES_DS1820 1
#define REST_RES_DHT11 1 #define REST_RES_DHT11 1
#define REST_RES_TEMPERATURE 1
#define REST_RES_HELLO 0 #define REST_RES_HELLO 0
#define REST_RES_MIRROR 0 /* causes largest code size */ #define REST_RES_MIRROR 0 /* causes largest code size */
#define REST_RES_CHUNKS 0 #define REST_RES_CHUNKS 0
@ -89,6 +90,9 @@ uint8_t out_temp=0, humidity=0;
#if defined (PLATFORM_HAS_LIGHT) #if defined (PLATFORM_HAS_LIGHT)
#include "dev/light-sensor.h" #include "dev/light-sensor.h"
#endif #endif
#if defined (PLATFORM_HAS_TEMPERATURE)
#include "dev/temperature-sensor.h"
#endif
#if defined (PLATFORM_HAS_BATTERY) #if defined (PLATFORM_HAS_BATTERY)
#include "dev/battery-sensor.h" #include "dev/battery-sensor.h"
#endif #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)) else if (num && (accept[0]==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(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); length = strlen(message);
memcpy(buffer, message,length ); memcpy(buffer, message,length );
@ -936,6 +940,41 @@ light_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
} }
#endif /* PLATFORM_HAS_LIGHT */ #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) #if REST_RES_BATTERY && defined (PLATFORM_HAS_BATTERY)
/* A simple getter example. Returns the reading from light sensor with a simple etag */ /* 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); SENSORS_ACTIVATE(light_sensor);
rest_activate_resource(&resource_light); rest_activate_resource(&resource_light);
#endif #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 #if defined (PLATFORM_HAS_BATTERY) && REST_RES_BATTERY
SENSORS_ACTIVATE(battery_sensor); SENSORS_ACTIVATE(battery_sensor);
rest_activate_resource(&resource_battery); rest_activate_resource(&resource_battery);

View file

@ -32,6 +32,13 @@
#ifndef __PROJECT_RPL_WEB_CONF_H__ #ifndef __PROJECT_RPL_WEB_CONF_H__
#define __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 #define SICSLOWPAN_CONF_FRAG 1
/* Disabling RDC for demo purposes. Core updates often require more memory. */ /* Disabling RDC for demo purposes. Core updates often require more memory. */

View file

@ -4,7 +4,7 @@ CONTIKI_CORE=contiki-main
CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o
CONTIKI_TARGET_SOURCEFILES += contiki-main.c params.c node-id.c CONTIKI_TARGET_SOURCEFILES += contiki-main.c params.c node-id.c
#Needed for slip #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 #Needed for DHT11 humidity sensor
CONTIKI_TARGET_SOURCEFILES += dht11.c CONTIKI_TARGET_SOURCEFILES += dht11.c

View file

@ -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 <paulolouro@binarylabs.dk>
*/
#include <avr/io.h>
#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;
}

View file

@ -0,0 +1,7 @@
#ifndef __ADC_ARCH_H__
#define __ADC_ARCH_H__
int readADC(uint8_t pin);
int readInternalTemp(void);
#endif /* __ADC_ARCH_H__ */

View file

@ -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 <paulolouro@binarylabs.dk>
* Harald Pichler <harald@the-develop.net>
*/
#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<<BATMON_OK))==0) break;
}
h=2550-75*16-75+75*p1; //-75 to take the floor of the 75 mv transition window
return h;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int c)
{
// No configuration needed. readADC() handles all the config needed.
return type == SENSORS_ACTIVE;
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
// analog sensors are always ready
return 1;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR, value, configure, status);

View file

@ -0,0 +1,48 @@
/*
* 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 <paulolouro@binarylabs.dk>
*/
#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__ */

View file

@ -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 <paulolouro@binarylabs.dk>
*/
#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);

View file

@ -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 <paulolouro@binarylabs.dk>
*/
#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__ */