initial upload
This commit is contained in:
parent
6694583291
commit
aaf1e8b2b1
100
examples/osd/arduino-climate/Adafruit_HTU21DF.cpp
Normal file
100
examples/osd/arduino-climate/Adafruit_HTU21DF.cpp
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
/***************************************************
|
||||||
|
This is a library for the HTU21DF Humidity & Temp Sensor
|
||||||
|
|
||||||
|
Designed specifically to work with the HTU21DF sensor from Adafruit
|
||||||
|
----> https://www.adafruit.com/products/1899
|
||||||
|
|
||||||
|
These displays use I2C to communicate, 2 pins are required to
|
||||||
|
interface
|
||||||
|
Adafruit invests time and resources providing this open source code,
|
||||||
|
please support Adafruit and open-source hardware by purchasing
|
||||||
|
products from Adafruit!
|
||||||
|
|
||||||
|
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||||
|
BSD license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
|
||||||
|
#include "Adafruit_HTU21DF.h"
|
||||||
|
#if defined(__AVR__)
|
||||||
|
#include <util/delay.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Adafruit_HTU21DF::Adafruit_HTU21DF() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
boolean Adafruit_HTU21DF::begin(void) {
|
||||||
|
Wire.begin();
|
||||||
|
|
||||||
|
reset();
|
||||||
|
|
||||||
|
Wire.beginTransmission(HTU21DF_I2CADDR);
|
||||||
|
Wire.write(HTU21DF_READREG);
|
||||||
|
Wire.endTransmission();
|
||||||
|
Wire.requestFrom(HTU21DF_I2CADDR, 1);
|
||||||
|
return (Wire.read() == 0x2); // after reset should be 0x2
|
||||||
|
}
|
||||||
|
|
||||||
|
void Adafruit_HTU21DF::reset(void) {
|
||||||
|
Wire.beginTransmission(HTU21DF_I2CADDR);
|
||||||
|
Wire.write(HTU21DF_RESET);
|
||||||
|
Wire.endTransmission();
|
||||||
|
delay(15);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float Adafruit_HTU21DF::readTemperature(void) {
|
||||||
|
|
||||||
|
// OK lets ready!
|
||||||
|
Wire.beginTransmission(HTU21DF_I2CADDR);
|
||||||
|
Wire.write(HTU21DF_READTEMP);
|
||||||
|
Wire.endTransmission();
|
||||||
|
|
||||||
|
delay(60); // add delay between request and actual read!
|
||||||
|
|
||||||
|
Wire.requestFrom(HTU21DF_I2CADDR, 3);
|
||||||
|
while (!Wire.available()) {}
|
||||||
|
|
||||||
|
uint16_t t = Wire.read();
|
||||||
|
t <<= 8;
|
||||||
|
t |= Wire.read();
|
||||||
|
|
||||||
|
uint8_t crc = Wire.read();
|
||||||
|
|
||||||
|
float temp = t;
|
||||||
|
temp *= 175.72;
|
||||||
|
temp /= 65536;
|
||||||
|
temp -= 46.85;
|
||||||
|
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float Adafruit_HTU21DF::readHumidity(void) {
|
||||||
|
// OK lets ready!
|
||||||
|
Wire.beginTransmission(HTU21DF_I2CADDR);
|
||||||
|
Wire.write(HTU21DF_READHUM);
|
||||||
|
Wire.endTransmission();
|
||||||
|
|
||||||
|
delay(50); // add delay between request and actual read!
|
||||||
|
|
||||||
|
Wire.requestFrom(HTU21DF_I2CADDR, 3);
|
||||||
|
while (!Wire.available()) {}
|
||||||
|
|
||||||
|
uint16_t h = Wire.read();
|
||||||
|
h <<= 8;
|
||||||
|
h |= Wire.read();
|
||||||
|
|
||||||
|
uint8_t crc = Wire.read();
|
||||||
|
|
||||||
|
float hum = h;
|
||||||
|
hum *= 125;
|
||||||
|
hum /= 65536;
|
||||||
|
hum -= 6;
|
||||||
|
|
||||||
|
return hum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*********************************************************************/
|
44
examples/osd/arduino-climate/Adafruit_HTU21DF.h
Normal file
44
examples/osd/arduino-climate/Adafruit_HTU21DF.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/***************************************************
|
||||||
|
This is a library for the HTU21D-F Humidity & Temp Sensor
|
||||||
|
|
||||||
|
Designed specifically to work with the HTU21D-F sensor from Adafruit
|
||||||
|
----> https://www.adafruit.com/products/1899
|
||||||
|
|
||||||
|
These displays use I2C to communicate, 2 pins are required to
|
||||||
|
interface
|
||||||
|
Adafruit invests time and resources providing this open source code,
|
||||||
|
please support Adafruit and open-source hardware by purchasing
|
||||||
|
products from Adafruit!
|
||||||
|
|
||||||
|
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||||
|
BSD license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
|
||||||
|
//#if (ARDUINO >= 100)
|
||||||
|
#include "Arduino.h"
|
||||||
|
//#else
|
||||||
|
// #include "WProgram.h"
|
||||||
|
//#endif
|
||||||
|
#include "Wire.h"
|
||||||
|
|
||||||
|
#define HTU21DF_I2CADDR 0x40
|
||||||
|
#define HTU21DF_READTEMP 0xE3
|
||||||
|
#define HTU21DF_READHUM 0xE5
|
||||||
|
#define HTU21DF_WRITEREG 0xE6
|
||||||
|
#define HTU21DF_READREG 0xE7
|
||||||
|
#define HTU21DF_RESET 0xFE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Adafruit_HTU21DF {
|
||||||
|
public:
|
||||||
|
Adafruit_HTU21DF();
|
||||||
|
boolean begin(void);
|
||||||
|
float readTemperature(void);
|
||||||
|
float readHumidity(void);
|
||||||
|
void reset(void);
|
||||||
|
private:
|
||||||
|
boolean readData(void);
|
||||||
|
float humidity, temp;
|
||||||
|
};
|
||||||
|
|
63
examples/osd/arduino-climate/Makefile
Normal file
63
examples/osd/arduino-climate/Makefile
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
# Set this to the name of your sketch (without extension .pde)
|
||||||
|
SKETCH=sketch
|
||||||
|
EXE=arduino-example
|
||||||
|
|
||||||
|
all: $(EXE)
|
||||||
|
|
||||||
|
CONTIKI=../../../../osd-contiki
|
||||||
|
|
||||||
|
# Contiki IPv6 configuration
|
||||||
|
CONTIKI_WITH_IPV6 = 1
|
||||||
|
|
||||||
|
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
|
||||||
|
LFLAGS += -lm
|
||||||
|
|
||||||
|
PROJECT_SOURCEFILES += ${SKETCH}.cpp Adafruit_HTU21DF.cpp
|
||||||
|
|
||||||
|
# automatically build RESTful resources
|
||||||
|
REST_RESOURCES_DIR = ./resources
|
||||||
|
REST_RESOURCES_DIR_COMMON = $(CONTIKI)/examples/osd/resources-common
|
||||||
|
REST_RESOURCES_FILES= $(notdir \
|
||||||
|
$(shell find $(REST_RESOURCES_DIR) -name '*.c') \
|
||||||
|
$(shell find $(REST_RESOURCES_DIR_COMMON) -name '*.c') \
|
||||||
|
)
|
||||||
|
|
||||||
|
PROJECTDIRS += $(REST_RESOURCES_DIR) $(REST_RESOURCES_DIR_COMMON)
|
||||||
|
PROJECT_SOURCEFILES += $(REST_RESOURCES_FILES)
|
||||||
|
# variable for Makefile.include
|
||||||
|
ifneq ($(TARGET), minimal-net)
|
||||||
|
CFLAGS += -DUIP_CONF_IPV6_RPL=1
|
||||||
|
else
|
||||||
|
# minimal-net does not support RPL under Linux and is mostly used to test CoAP only
|
||||||
|
${info INFO: compiling without RPL}
|
||||||
|
CFLAGS += -DUIP_CONF_IPV6_RPL=0
|
||||||
|
CFLAGS += -DHARD_CODED_ADDRESS=\"fdfd::10\"
|
||||||
|
${info INFO: compiling with large buffers}
|
||||||
|
CFLAGS += -DUIP_CONF_BUFFER_SIZE=2048
|
||||||
|
CFLAGS += -DREST_MAX_CHUNK_SIZE=1024
|
||||||
|
CFLAGS += -DCOAP_MAX_HEADER_SIZE=640
|
||||||
|
endif
|
||||||
|
|
||||||
|
# linker optimizations
|
||||||
|
SMALL=1
|
||||||
|
|
||||||
|
|
||||||
|
# REST Engine shall use Erbium CoAP implementation
|
||||||
|
APPS += er-coap
|
||||||
|
APPS += rest-engine
|
||||||
|
APPS += arduino
|
||||||
|
|
||||||
|
include $(CONTIKI)/Makefile.include
|
||||||
|
include $(CONTIKI)/apps/arduino/Makefile.include
|
||||||
|
|
||||||
|
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
|
||||||
|
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)
|
||||||
|
|
||||||
|
connect-router: $(CONTIKI)/tools/tunslip6
|
||||||
|
sudo $(CONTIKI)/tools/tunslip6 aaaa::1/64
|
||||||
|
|
||||||
|
connect-router-cooja: $(CONTIKI)/tools/tunslip6
|
||||||
|
sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 aaaa::1/64
|
||||||
|
|
||||||
|
connect-minimal:
|
||||||
|
sudo ip address add fdfd::1/64 dev tap0
|
11
examples/osd/arduino-climate/README.md
Normal file
11
examples/osd/arduino-climate/README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
Arduino compatibility example
|
||||||
|
=============================
|
||||||
|
|
||||||
|
This example shows that it is now possible to re-use arduino sketches in
|
||||||
|
Contiki. This example documents the necessary magic. Arduino specifies
|
||||||
|
two routines, `setup` and `loop`. Before `setup` is called, the
|
||||||
|
framework initializes hardware. In original Arduino, all this is done in
|
||||||
|
a `main` function (in C). For contiki we define a process that does the
|
||||||
|
same.
|
||||||
|
|
||||||
|
See the documentation file in apps/contiki-compat/README.md
|
2
examples/osd/arduino-climate/arduino-example.c
Normal file
2
examples/osd/arduino-climate/arduino-example.c
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
#include <arduino-process.h>
|
||||||
|
AUTOSTART_PROCESSES(&arduino_sketch);
|
53
examples/osd/arduino-climate/arduino-merkurbaord.geany
Normal file
53
examples/osd/arduino-climate/arduino-merkurbaord.geany
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
[file_prefs]
|
||||||
|
final_new_line=true
|
||||||
|
ensure_convert_new_lines=false
|
||||||
|
strip_trailing_spaces=false
|
||||||
|
replace_tabs=false
|
||||||
|
|
||||||
|
[indentation]
|
||||||
|
indent_width=4
|
||||||
|
indent_type=1
|
||||||
|
indent_hard_tab_width=8
|
||||||
|
detect_indent=false
|
||||||
|
detect_indent_width=false
|
||||||
|
indent_mode=2
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name=arduino-merkurbaord
|
||||||
|
base_path=/home/harald/install/osd-contiki/examples/osd/arduino-climate/
|
||||||
|
description=
|
||||||
|
file_patterns=
|
||||||
|
|
||||||
|
[long line marker]
|
||||||
|
long_line_behaviour=1
|
||||||
|
long_line_column=72
|
||||||
|
|
||||||
|
[files]
|
||||||
|
current_page=0
|
||||||
|
FILE_NAME_0=0;C++;0;EUTF-8;1;1;0;%2Fhome%2Fharald%2Finstall%2Fosd-contiki%2Fexamples%2Fosd%2Farduino-merkurboard%2Fsketch.pde;0;4
|
||||||
|
|
||||||
|
[VTE]
|
||||||
|
last_dir=/home/harald
|
||||||
|
|
||||||
|
[build-menu]
|
||||||
|
NF_00_LB=_Make
|
||||||
|
NF_00_CM=make TARGET=osd-merkur-256
|
||||||
|
NF_00_WD=
|
||||||
|
NF_01_LB=Make flash
|
||||||
|
NF_01_CM=make TARGET=osd-merkur-256 flash
|
||||||
|
NF_01_WD=
|
||||||
|
NF_03_LB=Make Clean
|
||||||
|
NF_03_CM=make clean TARGET=osd-merkur-256
|
||||||
|
NF_03_WD=
|
||||||
|
C++FT_00_LB=_Kompilieren
|
||||||
|
C++FT_00_CM=make TARGET=osd-merkur-256
|
||||||
|
C++FT_00_WD=
|
||||||
|
C++FT_01_LB=_Erstellen
|
||||||
|
C++FT_01_CM=make TARGET=osd-merkur-256 flash
|
||||||
|
C++FT_01_WD=
|
||||||
|
filetypes=C++;
|
||||||
|
|
||||||
|
[editor]
|
||||||
|
line_wrapping=false
|
||||||
|
line_break_column=72
|
||||||
|
auto_continue_multiline=true
|
2
examples/osd/arduino-climate/flash.sh
Executable file
2
examples/osd/arduino-climate/flash.sh
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/bash
|
||||||
|
make TARGET=osd-merkur-128 flash
|
106
examples/osd/arduino-climate/project-conf.h
Normal file
106
examples/osd/arduino-climate/project-conf.h
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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_BATTERY 1
|
||||||
|
|
||||||
|
#define SICSLOWPAN_CONF_FRAG 1
|
||||||
|
|
||||||
|
#define LOOP_INTERVAL (30 * CLOCK_SECOND)
|
||||||
|
|
||||||
|
/* Save energy */
|
||||||
|
//#define RDC_CONF_PT_YIELD_OFF
|
||||||
|
|
||||||
|
/* For Debug: Dont allow MCU sleeping between channel checks */
|
||||||
|
//#undef RDC_CONF_MCU_SLEEP
|
||||||
|
//#define RDC_CONF_MCU_SLEEP 0
|
||||||
|
|
||||||
|
/* Disabling RDC for demo purposes. Core updates often require more memory. */
|
||||||
|
/* For projects, optimize memory and enable RDC again. */
|
||||||
|
// #undef NETSTACK_CONF_RDC
|
||||||
|
//#define NETSTACK_CONF_RDC nullrdc_driver
|
||||||
|
|
||||||
|
/* Increase rpl-border-router IP-buffer when using more than 64. */
|
||||||
|
#undef REST_MAX_CHUNK_SIZE
|
||||||
|
#define REST_MAX_CHUNK_SIZE 64
|
||||||
|
|
||||||
|
/* Estimate your header size, especially when using Proxy-Uri. */
|
||||||
|
/*
|
||||||
|
#undef COAP_MAX_HEADER_SIZE
|
||||||
|
#define COAP_MAX_HEADER_SIZE 70
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* The IP buffer size must fit all other hops, in particular the border router. */
|
||||||
|
|
||||||
|
#undef UIP_CONF_BUFFER_SIZE
|
||||||
|
#define UIP_CONF_BUFFER_SIZE 256
|
||||||
|
|
||||||
|
|
||||||
|
/* Multiplies with chunk size, be aware of memory constraints. */
|
||||||
|
#undef COAP_MAX_OPEN_TRANSACTIONS
|
||||||
|
#define COAP_MAX_OPEN_TRANSACTIONS 4
|
||||||
|
|
||||||
|
/* Must be <= open transaction number, default is COAP_MAX_OPEN_TRANSACTIONS-1. */
|
||||||
|
/*
|
||||||
|
#undef COAP_MAX_OBSERVERS
|
||||||
|
#define COAP_MAX_OBSERVERS 2
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Filtering .well-known/core per query can be disabled to save space. */
|
||||||
|
/*
|
||||||
|
#undef COAP_LINK_FORMAT_FILTERING
|
||||||
|
#define COAP_LINK_FORMAT_FILTERING 0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Save some memory for the sky platform. */
|
||||||
|
/*
|
||||||
|
#undef NBR_TABLE_CONF_MAX_NEIGHBORS
|
||||||
|
#define NBR_TABLE_CONF_MAX_NEIGHBORS 10
|
||||||
|
#undef UIP_CONF_MAX_ROUTES
|
||||||
|
#define UIP_CONF_MAX_ROUTES 10
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Reduce 802.15.4 frame queue to save RAM. */
|
||||||
|
/*
|
||||||
|
#undef QUEUEBUF_CONF_NUM
|
||||||
|
#define QUEUEBUF_CONF_NUM 4
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
#undef SICSLOWPAN_CONF_FRAG
|
||||||
|
#define SICSLOWPAN_CONF_FRAG 1
|
||||||
|
*/
|
||||||
|
|
||||||
|
#endif /* PROJECT_RPL_WEB_CONF_H_ */
|
80
examples/osd/arduino-climate/resources/res-htu21dhum.c
Normal file
80
examples/osd/arduino-climate/resources/res-htu21dhum.c
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
/*
|
||||||
|
* 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_htu21dhum,
|
||||||
|
"title=\"Moisture status\";rt=\"Moisture\"",
|
||||||
|
res_get_handler,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
extern char htu21d_hum_s[8];
|
||||||
|
|
||||||
|
|
||||||
|
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, "%s", htu21d_hum_s);
|
||||||
|
|
||||||
|
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':%s}", htu21d_hum_s);
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
79
examples/osd/arduino-climate/resources/res-htu21dtemp.c
Normal file
79
examples/osd/arduino-climate/resources/res-htu21dtemp.c
Normal 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=\"Temperature status\";rt=\"Temperatur\"",
|
||||||
|
res_get_handler,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
extern char htu21d_temp_s[8];
|
||||||
|
|
||||||
|
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, "%s", htu21d_temp_s);
|
||||||
|
|
||||||
|
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, "{'temperature':%s}", htu21d_temp_s);
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
5
examples/osd/arduino-climate/run.sh
Executable file
5
examples/osd/arduino-climate/run.sh
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# For the ages-old bootloader (before 2014) you want to use
|
||||||
|
# BOOTLOADER_GET_MAC=0x0001f3a0 as parameter to make below.
|
||||||
|
make clean TARGET=osd-merkur-128
|
||||||
|
make TARGET=osd-merkur-128
|
62
examples/osd/arduino-climate/sketch.pde
Normal file
62
examples/osd/arduino-climate/sketch.pde
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
* Sample arduino sketch using contiki features.
|
||||||
|
* We turn the LED off
|
||||||
|
* We allow read the moisture sensor
|
||||||
|
* Unfortunately sleeping for long times in loop() isn't currently
|
||||||
|
* possible, something turns off the CPU (including PWM outputs) if a
|
||||||
|
* Proto-Thread is taking too long. We need to find out how to sleep in
|
||||||
|
* a Contiki-compatible way.
|
||||||
|
* Note that for a normal arduino sketch you won't have to include any
|
||||||
|
* of the contiki-specific files here, the sketch should just work.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Wire.h>
|
||||||
|
#include "Adafruit_HTU21DF.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "arduino-process.h"
|
||||||
|
#include "rest-engine.h"
|
||||||
|
|
||||||
|
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
||||||
|
|
||||||
|
extern resource_t res_htu21dtemp, res_htu21dhum, res_battery;
|
||||||
|
float htu21d_hum;
|
||||||
|
float htu21d_temp;
|
||||||
|
char htu21d_hum_s[8];
|
||||||
|
char htu21d_temp_s[8];
|
||||||
|
|
||||||
|
#define LED_PIN 4
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup (void)
|
||||||
|
{
|
||||||
|
// switch off the led
|
||||||
|
pinMode(LED_PIN, OUTPUT);
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
// htu21d sensor
|
||||||
|
if (!htu.begin()) {
|
||||||
|
printf("Couldn't find sensor!");
|
||||||
|
}
|
||||||
|
// init coap resourcen
|
||||||
|
rest_init_engine ();
|
||||||
|
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||||
|
rest_activate_resource (&res_htu21dtemp, "s/temp");
|
||||||
|
rest_activate_resource (&res_htu21dhum, "s/hum");
|
||||||
|
rest_activate_resource (&res_battery, "s/battery");
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
mcu_sleep_set(128); // Power consumtion 278uA; average over 20 minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
// at project-conf.h
|
||||||
|
// LOOP_INTERVAL (30 * CLOCK_SECOND)
|
||||||
|
void loop (void)
|
||||||
|
{
|
||||||
|
htu21d_temp = htu.readTemperature();
|
||||||
|
htu21d_hum = htu.readHumidity();
|
||||||
|
dtostrf(htu21d_temp , 0, 2, htu21d_temp_s );
|
||||||
|
dtostrf(htu21d_hum , 0, 2, htu21d_hum_s );
|
||||||
|
|
||||||
|
// debug only
|
||||||
|
// printf("Temp: '%s'",htu21d_temp_s);
|
||||||
|
// printf("\t\tHum: '%s'\n",htu21d_hum_s);
|
||||||
|
}
|
Loading…
Reference in a new issue