add htu21d sensor and resources and set mesure inteval to 10 seconds

This commit is contained in:
Harald Pichler 2015-09-11 21:41:24 +02:00
parent 7f09f96e88
commit 617d42663c
5 changed files with 108 additions and 26 deletions

View file

@ -15,7 +15,7 @@ CONTIKI_WITH_IPV6 = 1
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
PROJECT_SOURCEFILES += ${SKETCH}.cpp
PROJECT_SOURCEFILES += ${SKETCH}.cpp Adafruit_HTU21DF.cpp Wire.cpp twi.c new.cpp WString.cpp Stream.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources

View file

@ -38,6 +38,8 @@
#define SICSLOWPAN_CONF_FRAG 1
#define LOOP_INTERVAL (10 * CLOCK_SECOND)
/* Save energy */
#define RDC_CONF_PT_YIELD_OFF

View file

@ -45,40 +45,31 @@
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from the sensor with a simple etag */
RESOURCE(res_moisture,
RESOURCE(res_htu21dhum,
"title=\"Moisture status\";rt=\"Moisture\"",
res_get_handler,
NULL,
NULL,
NULL);
extern uint8_t moisture_pin;
extern uint8_t moisture_vcc;
extern uint16_t moisture_voltage;
extern float htu21d_hum;
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int a=0;
for(a=0;a<10;a++){
digitalWrite(moisture_vcc, HIGH);
digitalWrite(moisture_vcc, LOW);
}
digitalWrite(moisture_vcc, HIGH);
moisture_voltage = analogRead(moisture_pin);
digitalWrite(moisture_vcc, LOW);
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, "%d", moisture_voltage);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%f", htu21d_hum);
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':%d}", moisture_voltage);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'moisture':%f}", htu21d_hum);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
* 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
* Moisture resource
* \author
* Harald Pichler <harald@the-develop.net>
*/
#include "contiki.h"
#include <string.h>
#include "rest-engine.h"
#include "Arduino.h"
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from the sensor with a simple etag */
RESOURCE(res_htu21dtemp,
"title=\"Moisture status\";rt=\"Moisture\"",
res_get_handler,
NULL,
NULL,
NULL);
extern float htu21d_temp;
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
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, "%f", htu21d_temp);
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':%f}", htu21d_temp);
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 and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}

View file

@ -11,16 +11,19 @@
*/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
extern "C" {
#include "Adafruit_HTU21DF.h"
#include "rest-engine.h"
extern resource_t res_moisture, res_battery;
uint8_t moisture_pin = A5;
uint8_t moisture_vcc = 19;
uint16_t moisture_voltage = 0;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
extern resource_t res_htu21dtemp, res_htu21dhum, res_battery;
float htu21d_hum;
float htu21d_temp;
#define LED_PIN 4
}
@ -30,16 +33,23 @@ void setup (void)
// switch off the led
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
// init moisture sensor
pinMode(moisture_vcc, OUTPUT);
digitalWrite(moisture_vcc, LOW);
// htu21d sensor
if (!htu.begin()) {
printf("Couldn't find sensor!");
}
// init coap resourcen
rest_init_engine ();
rest_activate_resource (&res_moisture, "s/moisture");
rest_activate_resource (&res_htu21dtemp, "s/temp");
rest_activate_resource (&res_htu21dhum, "s/hum");
rest_activate_resource (&res_battery, "s/battery");
}
// at project-conf.h
// LOOP_INTERVAL (10 * CLOCK_SECOND)
void loop (void)
{
htu21d_temp = htu.readTemperature();
htu21d_hum = htu.readHumidity();
printf("Temp: %f",htu21d_temp);
printf("\t\tHum: %f",htu21d_hum);
}