From 944f1b06d6093720c32e0c3218e90ff2f3d8d551 Mon Sep 17 00:00:00 2001 From: Harald Pichler Date: Sun, 24 Apr 2016 17:45:35 +0200 Subject: [PATCH] add led resource --- .../resources/{res-door.c => res-led.c} | 44 ++++++++++++++----- examples/osd/arduino-merkurboard/sketch.pde | 14 +++--- 2 files changed, 40 insertions(+), 18 deletions(-) rename examples/osd/arduino-merkurboard/resources/{res-door.c => res-led.c} (72%) diff --git a/examples/osd/arduino-merkurboard/resources/res-door.c b/examples/osd/arduino-merkurboard/resources/res-led.c similarity index 72% rename from examples/osd/arduino-merkurboard/resources/res-door.c rename to examples/osd/arduino-merkurboard/resources/res-led.c index 97a5e9b72..fb0fa138f 100644 --- a/examples/osd/arduino-merkurboard/resources/res-door.c +++ b/examples/osd/arduino-merkurboard/resources/res-led.c @@ -43,35 +43,33 @@ #include "Arduino.h" static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset); +static void res_post_put_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_door, - "title=\"Moisture status\";rt=\"Moisture\"", +RESOURCE(res_led, + "title=\"LED: , POST/PUT mode=on|off\";rt=\"Control\"", res_get_handler, - NULL, - NULL, + res_post_put_handler, + res_post_put_handler, NULL); -extern uint8_t door_pin; -extern uint8_t door_status; +extern uint8_t led_pin; +extern uint8_t led_status; static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { - - door_status = digitalRead(door_pin); - 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", door_status); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d", led_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, "{'door':%d}", door_status); + snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'led':%d}", led_status); REST.set_response_payload(response, buffer, strlen((char *)buffer)); } else { @@ -80,3 +78,27 @@ res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr REST.set_response_payload(response, msg, strlen(msg)); } } + +static void +res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) +{ + size_t len = 0; + const char *mode = NULL; + int success = 1; + + if(success && (len = REST.get_post_variable(request, "mode", &mode))) { + if(strncmp(mode, "on", len) == 0) { + digitalWrite(led_pin, LOW); + led_status=1; + } else if(strncmp(mode, "off", len) == 0) { + digitalWrite(led_pin, HIGH); + led_status=0; + } else { + success = 0; + } + } else { + success = 0; + } if(!success) { + REST.set_response_status(response, REST.status.BAD_REQUEST); + } +} diff --git a/examples/osd/arduino-merkurboard/sketch.pde b/examples/osd/arduino-merkurboard/sketch.pde index c5b3bc3db..41df5a847 100644 --- a/examples/osd/arduino-merkurboard/sketch.pde +++ b/examples/osd/arduino-merkurboard/sketch.pde @@ -15,21 +15,21 @@ extern "C" { #include "rest-engine.h" #include "net/netstack.h" -extern resource_t res_leds, res_battery, res_cputemp; -uint8_t door_pin = 3; -uint8_t door_status = 0; +extern resource_t res_led, res_battery, res_cputemp; -#define LED_PIN 4 +uint8_t led_pin=4; +uint8_t led_status; } void setup (void) { // switch off the led - pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, HIGH); + pinMode(led_pin, OUTPUT); + digitalWrite(led_pin, HIGH); + led_status=0; // init coap resourcen rest_init_engine (); - rest_activate_resource (&res_leds, "s/leds"); + rest_activate_resource (&res_led, "s/led"); rest_activate_resource (&res_battery, "s/battery"); rest_activate_resource (&res_cputemp, "s/cputemp");