add merkurboard coap client demo

This commit is contained in:
harald42 2013-04-17 13:40:46 +02:00 committed by harald
parent 797c3da239
commit cdf4da8461
8 changed files with 174 additions and 71 deletions

View file

@ -1,4 +1,4 @@
all: er-example-server
all: er-example-server er-example-client
# Use this target explicitly if requried: er-plugtest-server
CONTIKI=../../..

View file

@ -0,0 +1,151 @@
/*
* Copyright (c) 2011, Matthias Kovatsch and other contributors.
* 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
* Erbium (Er) CoAP client example
* \author
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
#if !UIP_CONF_IPV6_RPL && !defined (CONTIKI_TARGET_MINIMAL_NET) && !defined (CONTIKI_TARGET_NATIVE)
#warning "Compiling with static routing!"
#include "static-routing.h"
#endif
#include "dev/button-sensor.h"
#include "dev/leds.h"
#if WITH_COAP == 3
#include "er-coap-03-engine.h"
#elif WITH_COAP == 6
#include "er-coap-06-engine.h"
#elif WITH_COAP == 7
#include "er-coap-07-engine.h"
#else
#error "CoAP version defined by WITH_COAP not implemented"
#endif
#define DEBUG 0
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
/* TODO: This server address is hard-coded for Cooja. */
#define SERVER_NODE(ipaddr) uip_ip6addr(ipaddr, 0xfe80, 0, 0, 0, 0x0221, 0x2eff, 0xff00, 0x1efb) /* cooja2 */
#define LOCAL_PORT UIP_HTONS(COAP_DEFAULT_PORT+1)
#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT)
PROCESS(coap_client_example, "COAP Client Example");
AUTOSTART_PROCESSES(&coap_client_example);
uip_ipaddr_t server_ipaddr;
/* Example URIs that can be queried. */
#define NUMBER_OF_URLS 4
/* leading and ending slashes only for demo purposes, get cropped automatically when setting the Uri-Path */
char* service_urls[NUMBER_OF_URLS] = {".well-known/core", "/actuators/toggle", "battery/", "error/in//path"};
/* This function is will be passed to COAP_BLOCKING_REQUEST() to handle responses. */
void
client_chunk_handler(void *response)
{
uint8_t *chunk;
int len = coap_get_payload(response, &chunk);
printf("|%.*s", len, (char *)chunk);
}
PROCESS_THREAD(coap_client_example, ev, data)
{
PROCESS_BEGIN();
leds_off(LEDS_RED);
static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */
SERVER_NODE(&server_ipaddr);
/* receives all CoAP messages */
coap_receiver_init();
#if PLATFORM_HAS_BUTTON
SENSORS_ACTIVATE(button_sensor);
printf("Press a button to request %s\n", service_urls[1]);
#endif
while(1) {
PROCESS_YIELD();
#if PLATFORM_HAS_BUTTON
if (ev == sensors_event && data == &button_sensor) {
/* send a request to notify the end of the process */
printf("--Toggle --\n");
leds_toggle(LEDS_RED);
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0 );
coap_set_header_uri_path(request, service_urls[1]);
const char msg[] = "Toggle!";
coap_set_payload(request, (uint8_t *)msg, sizeof(msg)-1);
PRINT6ADDR(&server_ipaddr);
PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));
COAP_BLOCKING_REQUEST(&server_ipaddr, REMOTE_PORT, request, client_chunk_handler);
printf("\n--Done--\n");
}
#endif
}
PROCESS_END();
}

View file

@ -53,14 +53,13 @@
#define REST_RES_PUSHING 0
#define REST_RES_EVENT 1
#define REST_RES_SUB 0
#define REST_RES_LEDS 0
#define REST_RES_TOGGLE 0
#define REST_RES_LEDS 1
#define REST_RES_TOGGLE 1
#define REST_RES_LIGHT 0
#define REST_RES_BATTERY 1
#define REST_RES_RADIO 0
#if !UIP_CONF_IPV6_RPL && !defined (CONTIKI_TARGET_MINIMAL_NET) && !defined (CONTIKI_TARGET_NATIVE)
#warning "Compiling with static routing!"
#include "static-routing.h"
@ -68,9 +67,7 @@
#include "erbium.h"
// todo OSD-Testboard move to platform/dev
#include "dev/key.h"
#include "dev/led.h"
#if REST_RES_DS1820
#include "dev/ds1820.h"
#endif
@ -115,10 +112,6 @@
#define PRINTLLADDR(addr)
#endif
#include "dev/key.c" // todo: move platform /dev
#include "dev/led.c"
/******************************************************************************/
#if REST_RES_INFO
@ -143,7 +136,7 @@ info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
/* Some data that has the length up to REST_MAX_CHUNK_SIZE. For more, see the chunk resource. */
// jSON Format
index += sprintf(message + index,"{\n \"version\" : \"V0.3\",\n");
index += sprintf(message + index,"{\n \"version\" : \"V0.4\",\n");
index += sprintf(message + index," \"name\" : \"Button,LED\"\n");
index += sprintf(message + index,"}\n");
@ -155,57 +148,8 @@ info_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
}
#endif
// mybutton
/*A simple actuator example. read the key button status*/
RESOURCE(button, METHOD_GET | METHOD_PUT , "button", "title=\"Button\";rt=\"Text\"");
void
button_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
static char bname[17]="button1";
int success = 1;
char temp[100];
int index = 0;
int length = 0; /* |<-------->| */
const char *name = NULL;
size_t len = 0;
switch(REST.get_method_type(request)){
case METHOD_GET:
// jSON Format
index += sprintf(temp + index,"{\n \"name\" : \"%s\",\n",bname);
if(is_button())
index += sprintf(temp + index," \"state\" : \"on\"\n");
else
index += sprintf(temp + index," \"state\" : \"off\"\n");
index += sprintf(temp + index,"}\n");
length = strlen(temp);
memcpy(buffer, temp,length );
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
REST.set_response_payload(response, buffer, length);
break;
case METHOD_PUT:
if (success && (len=REST.get_post_variable(request, "name", &name))) {
PRINTF("name %s\n", name);
memcpy(bname, name,len);
bname[len]=0;
} else {
success = 0;
}
break;
default:
success = 0;
}
if (!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
/*A simple actuator example, post variable mode, relay is activated or deactivated*/
/*
RESOURCE(led1, METHOD_GET | METHOD_PUT , "led1", "title=\"Led1\";rt=\"Text\"");
void
led1_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
@ -271,6 +215,7 @@ led1_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
*/
#if REST_RES_DS1820
/*A simple getter example. Returns the reading from ds1820 sensor*/
@ -983,8 +928,6 @@ radio_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
void
hw_init()
{
key_init();
led1_off();
#if REST_RES_DS1820
ds1820_temp();
#endif
@ -1004,8 +947,6 @@ PROCESS_THREAD(rest_server_example, ev, data)
PROCESS_BEGIN();
PRINTF("Starting Erbium Example Server\n");
#ifdef RF_CHANNEL
@ -1033,9 +974,6 @@ PROCESS_THREAD(rest_server_example, ev, data)
rest_init_engine();
/* Activate the application-specific resources. */
rest_activate_resource(&resource_button);
rest_activate_resource(&resource_led1);
/* Activate the application-specific resources. */
#if REST_RES_DS1820
rest_activate_resource(&resource_ds1820);
#endif

View file

@ -0,0 +1,2 @@
#!/bin/bash
sudo avrdude -pm128rfa1 -c arduino -P/dev/ttyUSB0 -b57600 -e -U flash:w:er-example-client.osd-merkur.hex:a -U eeprom:w:er-example-client.osd-merkur.eep:a

View file

@ -32,7 +32,7 @@
#ifndef __PROJECT_RPL_WEB_CONF_H__
#define __PROJECT_RPL_WEB_CONF_H__
//#define PLATFORM_HAS_LEDS 1
#define PLATFORM_HAS_LEDS 1
#define PLATFORM_HAS_BUTTON 1
#define PLATFORM_HAS_TEMPERATURE 1
#define PLATFORM_HAS_BATTERY 1

View file

@ -0,0 +1,6 @@
#!/bin/bash
make clean TARGET=osd-merkur
make TARGET=osd-merkur
avr-size -C --mcu=MCU=atmega128rfa1 er-example-client.osd-merkur
avr-objcopy -j .text -j .data -O ihex er-example-client.osd-merkur er-example-client.osd-merkur.hex
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex er-example-client.osd-merkur er-example-client.osd-merkur.eep

View file

@ -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 += temperature-sensor.c adc.c led.c sensors.c slip_uart0.c slip.c
CONTIKI_TARGET_SOURCEFILES += temperature-sensor.c adc.c led.c sensors.c slip_uart0.c slip.c leds-arch.c
#Needed for Button
CONTIKI_TARGET_SOURCEFILES += button-sensor.c
#Needed for DHT11 humidity sensor

View file

@ -172,6 +172,12 @@ rng_get_uint8(void) {
return j;
}
void
clock_delay(unsigned int howlong)
{
if(howlong<2) return;
clock_delay_usec((45*howlong)>>4);
}
/*-------------------------Low level initialization------------------------*/
/*------Done in a subroutine to keep main routine stack usage small--------*/
void initialize(void)