add led resource
This commit is contained in:
parent
04bbba6c12
commit
944f1b06d6
2 changed files with 40 additions and 18 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue