CoAP Device Server Client

Dependencies:   EthernetInterface mbed-rtos mbed nsdl_lib

Fork of COAPmbed by th.iotkit2.ch

Constrained Application Protocol (Coap) ist ein Software-Protokoll welches für Internet der Dinge Geräte zugeschnitten ist.

COAP ist auf den meisten Geräten, die UDP Unterstützen, lauffähig.

Ein COAP fähiges Gerät publiziert seine Sensoren und Aktoren in einem Resource Directory oder stellt selber ein solches zur Verfügung.

Mittels Resource Discovery können die vorhandenen Sensoren und Aktoren mit ihren Attributen abgefragt werden.

Zeile 29: Node Name aendern und folgende Adresse aufrufen: http://nsp.cloudapp.net:8083/, User/PW = demo

resources/light.cpp

Committer:
marcel1691
Date:
2015-02-04
Revision:
11:99178da0f3fa
Parent:
9:18f4959c2bac

File content as of revision 11:99178da0f3fa:

// Light resource implementation
 
#include "mbed.h"
#include "nsdl_support.h"
#include "light.h"
 
#define LIGHT_RES_ID    "lt/0/dim"
 
extern Serial pc;
//PwmOut led1(LED1);
static PwmOut led2(D10);
static PwmOut led3(D11);
//PwmOut led4(LED4);
 
/* Only GET and PUT method allowed */
static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
{
    sn_coap_hdr_s *coap_res_ptr = 0;
    static float led_dimm = 0;
    int led_state = 0;
    char led_dimm_temp[4];
 
    pc.printf("light callback\r\n");
 
    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
    {
        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
 
        led_state = led_dimm * 100;
        sprintf(led_dimm_temp, "%d", led_state);
 
        coap_res_ptr->payload_len = strlen(led_dimm_temp);
        coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp;
        sn_nsdl_send_coap_message(address, coap_res_ptr);
    }
    else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
    {
        memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
 
        led_dimm_temp[received_coap_ptr->payload_len] = '\0';
 
        led_dimm = atof(led_dimm_temp);
        led_dimm = led_dimm/100;
 
        //led1.write(led_dimm);
        led2.write(led_dimm);
        led3.write(led_dimm);
        //led4.write(led_dimm);
 
        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
        sn_nsdl_send_coap_message(address, coap_res_ptr);
    }
 
    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
    return 0;
}
 
int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
{
    nsdl_create_dynamic_resource(resource_ptr, sizeof(LIGHT_RES_ID)-1, (uint8_t*)LIGHT_RES_ID, 0, 0, 0, &light_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
    return 0;
}