diff --git a/examples/osd/arduino-windsonic/Makefile b/examples/osd/arduino-windsonic/Makefile new file mode 100644 index 000000000..703b27365 --- /dev/null +++ b/examples/osd/arduino-windsonic/Makefile @@ -0,0 +1,71 @@ +# Set this to the name of your sketch (without extension .pde) +SKETCH=sketch +EXE=arduino-example + +all: $(EXE) + +CONTIKI=../../.. + +# Contiki IPv6 configuration +CONTIKI_WITH_IPV6 = 1 + +CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\" +LFLAGS += -lm + +PROJECT_SOURCEFILES += ${SKETCH}.cpp + +# automatically build RESTful resources +REST_RESOURCES_DIR = ./resources +REST_RESOURCES_DIR_COMMON = ../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 + +avr-size: $(EXE).$(TARGET).sz + +flash: $(EXE).$(TARGET).u $(EXE).$(TARGET).eu + +.PHONY: flash avr-size +.PRECIOUS: $(EXE).$(TARGET).hex $(EXE).$(TARGET).eep diff --git a/examples/osd/arduino-windsonic/README.md b/examples/osd/arduino-windsonic/README.md new file mode 100644 index 000000000..35bf8acb9 --- /dev/null +++ b/examples/osd/arduino-windsonic/README.md @@ -0,0 +1,26 @@ +Access to Gill Windsonic Anemometer +=================================== + +We use the second serial port to receive data from the Gill Windsonic +ultrasonic anemometer: + + http://gillinstruments.com/products/anemometer/windsonic.htm + +The Windsonic needs to be set up properly to send data periodically +(every two seconds in my setup) in the Polar Format with fixed CSV +sizes at 9600 baud. + +I used a MAX3323EEPE to translate the RS232 from Windsonic to Merkurboard +3.3V levels. + +The complete setup is driven by a 12V solar charged acid battery. The +/power_supply ressource measures the voltage level on the input. +The voltage is measured by a simple 1M/100k voltage divider directly +connected to the merkurboard. + +A small DC/DC converter is used to power the merkurboard. + +Next step maybe put the merkurboard and MAX3323 in the Windsonic's case +to have the first open source ipv6 addressable windsonic on earth ;-) + + diff --git a/examples/osd/arduino-windsonic/arduino-example.c b/examples/osd/arduino-windsonic/arduino-example.c new file mode 100644 index 000000000..ea74dd8b8 --- /dev/null +++ b/examples/osd/arduino-windsonic/arduino-example.c @@ -0,0 +1,2 @@ +#include +AUTOSTART_PROCESSES(&arduino_sketch); diff --git a/examples/osd/arduino-windsonic/flash.sh b/examples/osd/arduino-windsonic/flash.sh new file mode 100755 index 000000000..e82962073 --- /dev/null +++ b/examples/osd/arduino-windsonic/flash.sh @@ -0,0 +1,2 @@ +#!/bin/bash +make TARGET=osd-merkur-128 flash diff --git a/examples/osd/arduino-windsonic/project-conf.h b/examples/osd/arduino-windsonic/project-conf.h new file mode 100644 index 000000000..0c0cbd254 --- /dev/null +++ b/examples/osd/arduino-windsonic/project-conf.h @@ -0,0 +1,118 @@ +#/* + * Copyright (C) 2010-2017, 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_ + + +#ifdef IEEE802154_CONF_PANID +#undef IEEE802154_CONF_PANID +#define IEEE802154_CONF_PANID 0xA001 // default panid +#endif // IEEE802154_CONF_PANID + +#ifdef CHANNEL_802_15_4 +#undef CHANNEL_802_15_4 +#define CHANNEL_802_15_4 25 // default frequency (11-26) +#endif + +//#define PLATFORM_HAS_LEDS 1 +//#define PLATFORM_HAS_BUTTON 1 +//#define PLATFORM_HAS_BATTERY 1 + +#define SICSLOWPAN_CONF_FRAG 1 + +#define LOOP_INTERVAL (CLOCK_SECOND) +#define WIND_SPEEDS 30 + +/* 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_ */ diff --git a/examples/osd/arduino-windsonic/resources/res_power_supply.c b/examples/osd/arduino-windsonic/resources/res_power_supply.c new file mode 100644 index 000000000..4bc8e1cfe --- /dev/null +++ b/examples/osd/arduino-windsonic/resources/res_power_supply.c @@ -0,0 +1,82 @@ +/* +** Copyright (C) 2017 Marcus Priesch, All rights reserved +** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at +** **************************************************************************** +** +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** **************************************************************************** +** +**++ +** Name +** power_supply +** +** Purpose +** provide power_supply resource +** +** +** Revision Dates +** 8-Aug-2017 (MPR) Creation +** ««revision-date»»··· +**-- +*/ + +#include "contiki.h" + +#include +#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); + +RESOURCE(res_power_supply, + "title=\"Power Supply\";rt=\"Voltage\"", + res_get_handler, + NULL, + NULL, + NULL); + +extern int supply_voltage_hi, supply_voltage_lo; + +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, "%d.%02d", + supply_voltage_hi, supply_voltage_lo); + + 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, + "{'power_supply':%d.%02d,'unit':'Volts'}", supply_voltage_hi, supply_voltage_lo); + + 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 application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} + + + diff --git a/examples/osd/arduino-windsonic/resources/res_wind.c b/examples/osd/arduino-windsonic/resources/res_wind.c new file mode 100644 index 000000000..e3af143de --- /dev/null +++ b/examples/osd/arduino-windsonic/resources/res_wind.c @@ -0,0 +1,89 @@ +/* +** Copyright (C) 2017 Marcus Priesch, All rights reserved +** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at +** **************************************************************************** +** +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** **************************************************************************** +** +**++ +** Name +** wind +** +** Purpose +** provide wind resource +** +** +** Revision Dates +** 8-Aug-2017 (MPR) Creation +** ««revision-date»»··· +**-- +*/ + +#include "contiki.h" + +#include +#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); + +RESOURCE(res_wind, + "title=\"Wind\";rt=\"Wind\"", + res_get_handler, + NULL, + NULL, + NULL); + +extern int ws_speed_hi ; +extern int ws_speed_lo ; +extern int ws_max_speed_hi, ws_max_speed_lo; +extern char ws_unit [8]; +extern int ws_direction ; + +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, "cur: %d.%02d%s, max: %d.%02d%s, dir: %ddeg", + ws_speed_hi, ws_speed_lo, ws_unit, + ws_max_speed_hi, ws_max_speed_lo, ws_unit, + ws_direction); + + 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, + "{'cur':%d.%02d,'max':%d.%02d, 'unit':'%s','direction':%d}", + ws_speed_hi, ws_speed_lo, ws_max_speed_hi, ws_max_speed_lo, ws_unit, ws_direction); + + 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 application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} + + + diff --git a/examples/osd/arduino-windsonic/resources/res_wind_direction.c b/examples/osd/arduino-windsonic/resources/res_wind_direction.c new file mode 100644 index 000000000..417b1c9a4 --- /dev/null +++ b/examples/osd/arduino-windsonic/resources/res_wind_direction.c @@ -0,0 +1,81 @@ +/* +** Copyright (C) 2017 Marcus Priesch, All rights reserved +** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at +** **************************************************************************** +** +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** **************************************************************************** +** +**++ +** Name +** wind_direction +** +** Purpose +** provide wind resource +** +** +** Revision Dates +** 8-Aug-2017 (MPR) Creation +** ««revision-date»»··· +**-- +*/ + +#include "contiki.h" + +#include +#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); + +RESOURCE(res_wind_direction, + "title=\"Winddirection\";rt=\"Winddirection\"", + res_get_handler, + NULL, + NULL, + NULL); + +extern int ws_direction; + +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, "%d", ws_direction); + + 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, + "{'direction':%d,'unit':'degrees'}", ws_direction); + + 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 application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} + + + diff --git a/examples/osd/arduino-windsonic/resources/res_wind_speed.c b/examples/osd/arduino-windsonic/resources/res_wind_speed.c new file mode 100644 index 000000000..715fbc155 --- /dev/null +++ b/examples/osd/arduino-windsonic/resources/res_wind_speed.c @@ -0,0 +1,83 @@ +/* +** Copyright (C) 2017 Marcus Priesch, All rights reserved +** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at +** **************************************************************************** +** +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** **************************************************************************** +** +**++ +** Name +** wind_speed +** +** Purpose +** provide wind resource +** +** +** Revision Dates +** 8-Aug-2017 (MPR) Creation +** ««revision-date»»··· +**-- +*/ + +#include "contiki.h" + +#include +#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); + +RESOURCE(res_wind_speed, + "title=\"Windspeed\";rt=\"Windspeed\"", + res_get_handler, + NULL, + NULL, + NULL); + +extern int ws_speed_hi ; +extern int ws_speed_lo ; +extern char ws_unit [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, "%d.%02d", ws_speed_hi, ws_speed_lo); + + 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, + "{'speed':%d.%02d,'unit':'%s'}", ws_speed_hi, ws_speed_lo, ws_unit); + + 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 application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} + + + diff --git a/examples/osd/arduino-windsonic/resources/res_wind_speed_max.c b/examples/osd/arduino-windsonic/resources/res_wind_speed_max.c new file mode 100644 index 000000000..7964321a1 --- /dev/null +++ b/examples/osd/arduino-windsonic/resources/res_wind_speed_max.c @@ -0,0 +1,81 @@ +/* +** Copyright (C) 2017 Marcus Priesch, All rights reserved +** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at +** **************************************************************************** +** +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** **************************************************************************** +** +**++ +** Name +** wind_speed_max +** +** Purpose +** provide wind_speed_max +** +** +** Revision Dates +** 8-Aug-2017 (MPR) Creation +** ««revision-date»»··· +**-- +*/ + +#include "contiki.h" + +#include +#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); + +RESOURCE(res_wind_speed_max, + "title=\"Windspeed max last minute\";rt=\"m/s\"", + res_get_handler, + NULL, + NULL, + NULL); + +extern int ws_max_speed_hi, ws_max_speed_lo; +extern char ws_unit [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, "%d.%02d", ws_max_speed_hi, ws_max_speed_lo); + + 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, "{'max':%d.%02d,'unit':'%s'}", ws_max_speed_hi, ws_max_speed_lo, ws_unit); + + 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 application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} + + + diff --git a/examples/osd/arduino-windsonic/resources/res_wind_status.c b/examples/osd/arduino-windsonic/resources/res_wind_status.c new file mode 100644 index 000000000..101aff71d --- /dev/null +++ b/examples/osd/arduino-windsonic/resources/res_wind_status.c @@ -0,0 +1,80 @@ +/* +** Copyright (C) 2017 Marcus Priesch, All rights reserved +** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at +** **************************************************************************** +** +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** **************************************************************************** +** +**++ +** Name +** wind +** +** Purpose +** provide wind resource +** +** +** Revision Dates +** 8-Aug-2017 (MPR) Creation +** ««revision-date»»··· +**-- +*/ + +#include "contiki.h" + +#include +#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); + +RESOURCE(res_wind_status, + "title=\"Wind status\";rt=\"Wind status\"", + res_get_handler, + NULL, + NULL, + NULL); + +extern char ws_status [32] ; + +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", ws_status); + + 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, "{'status':'%s'}", ws_status); + + 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 application/json"; + REST.set_response_payload(response, msg, strlen(msg)); + } +} + + + diff --git a/examples/osd/arduino-windsonic/run.sh b/examples/osd/arduino-windsonic/run.sh new file mode 100755 index 000000000..5d5cbbbb4 --- /dev/null +++ b/examples/osd/arduino-windsonic/run.sh @@ -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 diff --git a/examples/osd/arduino-windsonic/sketch.pde b/examples/osd/arduino-windsonic/sketch.pde new file mode 100644 index 000000000..de1f7c1ac --- /dev/null +++ b/examples/osd/arduino-windsonic/sketch.pde @@ -0,0 +1,273 @@ +/* + * Sample arduino sketch using contiki features. + * 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. + */ + + +extern "C" { +#include +#include "arduino-process.h" +#include "rest-engine.h" + +extern resource_t res_wind, res_wind_speed_max, res_wind_status, + res_wind_speed, res_wind_direction, res_power_supply; + +#define STX 0x02 +#define ETX 0x03 +#define STRING_TERMINATOR 0x00 + +#define LED_PIN 4 +} + +int state = 0; +char ws_status [32]; +int ws_speed_hi = 0; +int ws_speed_lo = 0; +char ws_unit [8]; +int ws_direction = 0; +int supply_voltage_hi = 0; +int supply_voltage_lo = 0; +int ws_max_speed = 0; +int ws_max_speed_lo = 0; +int ws_max_speed_hi = 0; + +int wind_speeds [WIND_SPEEDS]; // every 2 seconds we get a value +int wind_speed_idx = 0; + +uint32_t idx; + +void set_no_data (void) +{ + printf ("set no data\n"); + strcpy (ws_status, "No data from sensor"); + ws_speed_lo = 000; + ws_speed_hi = 000; + ws_direction = 000; + ws_max_speed = 0; + ws_max_speed_hi = 0; + ws_max_speed_lo = 0; + wind_speed_idx = 0; + strcpy (ws_unit, ""); + + for (idx = 0; idx < WIND_SPEEDS; idx++) + wind_speeds [idx] = 0; + +} + +void setup (void) +{ + // switch off the led + pinMode(LED_PIN, OUTPUT); + digitalWrite(LED_PIN, HIGH); + + state = 0; + + // init coap resourcen + rest_init_engine (); + +#pragma GCC diagnostic ignored "-Wwrite-strings" + rest_activate_resource (&res_wind, "s/wind"); + rest_activate_resource (&res_wind_speed, "s/wind_speed"); + rest_activate_resource (&res_wind_speed_max, "s/wind_speed_max"); + rest_activate_resource (&res_wind_direction, "s/wind_direction"); + rest_activate_resource (&res_wind_status, "s/wind_status"); + rest_activate_resource (&res_power_supply, "s/power_supply"); +#pragma GCC diagnostic pop + + //mcu_sleep_set(128); // Power consumtion 278uA; average over 20 minutes + Serial1.begin (9600); + + set_no_data (); +} + +void loop (void) +{ + static int d, count, msg_length, state0_counter = 0; + static unsigned char msg_buffer [128], checksum_buffer [3], checksum; + + printf ("state = %d", state); + + switch (state) { + case 0 : + printf ("state0\n"); + state0_counter ++; + if (state0_counter > 10) { + set_no_data (); + state0_counter = 0; + } + // wait for stx + while (Serial1.available () > 0) { + d = Serial1.read (); + printf ("%d ", d); + if (d == STX) { + state = 1; + count = 0; + break; + } + } + if (state == 0) + break; + + case 1 : + printf ("state1\n"); + state0_counter = 0; + + // wait for etx and read message + while (Serial1.available () > 0) { + d = Serial1.read (); + printf ("%d ", d); + if (d == ETX) { + msg_length = count; + msg_buffer [count] = STRING_TERMINATOR; + count = 0; + checksum = 0; + state = 2; + break; + } else { + msg_buffer [count] = (unsigned char) d; + count ++; + if (count > 127) { + state = 0; + break; + } + } + } + if (state == 1) + break; + + case 2 : + printf ("state2\n"); + // read checksum + while (Serial1.available () > 0) { + d = Serial1.read (); + printf ("%d ", d); + printf (" %d (%d)\n", d, count); + checksum_buffer [count] = (unsigned char) (d & 0xff); + count ++; + if (count == 2) { + checksum_buffer [count] = STRING_TERMINATOR; + state = 3; + break; + } + } + if (state == 2) + break; + + case 3 : + unsigned int checksum_rx; + int cnt; + + printf ("state3\n"); + // calc and compare checksum + checksum = 0; + sscanf ((const char*)checksum_buffer, "%2X", &checksum_rx); + for (cnt = 0; cnt < msg_length; cnt++) { + checksum ^= msg_buffer [cnt]; + } + + if (checksum_rx != checksum) { + printf ("checksum mismatch %02x != %02x\n", checksum, checksum_rx); + state = 0; + break; + } else { + state = 4; + } + + case 4 : + // parse message Q,079,000.08,M,00, + char unit [1]; + int status; + + printf ("state4\n"); + printf ("parse messge: %s\n", msg_buffer); + sscanf + ( (const char*) msg_buffer + , "Q,%3d,%3d.%2d,%c,%2d" + , &ws_direction + , &ws_speed_hi + , &ws_speed_lo + , unit + , &status + ); + + switch (unit[0]) { + case 'M' : + strcpy (ws_unit, "m/s"); + break; + + case 'N' : + strcpy (ws_unit, "knots"); + break; + + case 'P' : + strcpy (ws_unit, "mph"); + break; + + case 'K' : + strcpy (ws_unit, "km/h"); + break; + + case 'F' : + strcpy (ws_unit, "fpm"); + break; + } + + switch (status) { + case 0x00 : + strcpy (ws_status, "OK"); + break; + + case 0x01 : + strcpy (ws_status, "Axis 1 failed"); + break; + + case 0x02 : + strcpy (ws_status, "Axis 2 failed"); + break; + + case 0x03 : + strcpy (ws_status, "Axis 1+2 failed"); + break; + + case 0x08 : + strcpy (ws_status, "NVM error"); + break; + + case 0x09 : + strcpy (ws_status, "ROM error"); + break; + } + + wind_speeds [wind_speed_idx] = ws_speed_hi * 100 + ws_speed_lo; + wind_speed_idx ++; + if (wind_speed_idx > WIND_SPEEDS) + wind_speed_idx = 0; + + ws_max_speed = 0; + for (idx = 0; idx < WIND_SPEEDS; idx++) { + if (wind_speeds [idx] > ws_max_speed) + ws_max_speed = wind_speeds [idx]; + } + + ws_max_speed_lo = ws_max_speed % 100; + ws_max_speed_hi = ws_max_speed / 100; + + printf ("speed: %d.%02d %s, dir: %d, status: %s, max: %d.%02d\n" + , ws_speed_hi, ws_speed_lo, ws_unit, ws_direction, ws_status + , ws_max_speed_hi, ws_max_speed_lo); + + state = 0; + break; + } /* end switch (state) */ + + idx = analogRead (A2); + idx *= 1000; + idx /= 565; + supply_voltage_lo = idx % 100; + supply_voltage_hi = idx / 100; +} /* end loop */