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

Committer:
marcel1691
Date:
Wed Feb 04 19:49:52 2015 +0000
Revision:
11:99178da0f3fa
Parent:
9:18f4959c2bac
Abgestimmt mit IoTKit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 2:7e489126fe7a 1 // Light resource implementation
marcel1691 9:18f4959c2bac 2
bogdanm 2:7e489126fe7a 3 #include "mbed.h"
bogdanm 2:7e489126fe7a 4 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 5 #include "light.h"
marcel1691 9:18f4959c2bac 6
bogdanm 2:7e489126fe7a 7 #define LIGHT_RES_ID "lt/0/dim"
marcel1691 9:18f4959c2bac 8
bogdanm 2:7e489126fe7a 9 extern Serial pc;
bogdanm 2:7e489126fe7a 10 //PwmOut led1(LED1);
marcel1691 11:99178da0f3fa 11 static PwmOut led2(D10);
marcel1691 11:99178da0f3fa 12 static PwmOut led3(D11);
bogdanm 2:7e489126fe7a 13 //PwmOut led4(LED4);
marcel1691 9:18f4959c2bac 14
bogdanm 2:7e489126fe7a 15 /* Only GET and PUT method allowed */
bogdanm 2:7e489126fe7a 16 static uint8_t light_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
bogdanm 2:7e489126fe7a 17 {
bogdanm 2:7e489126fe7a 18 sn_coap_hdr_s *coap_res_ptr = 0;
bogdanm 2:7e489126fe7a 19 static float led_dimm = 0;
bogdanm 2:7e489126fe7a 20 int led_state = 0;
bogdanm 2:7e489126fe7a 21 char led_dimm_temp[4];
marcel1691 9:18f4959c2bac 22
bogdanm 2:7e489126fe7a 23 pc.printf("light callback\r\n");
marcel1691 9:18f4959c2bac 24
bogdanm 2:7e489126fe7a 25 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
bogdanm 2:7e489126fe7a 26 {
bogdanm 2:7e489126fe7a 27 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
marcel1691 9:18f4959c2bac 28
bogdanm 2:7e489126fe7a 29 led_state = led_dimm * 100;
bogdanm 2:7e489126fe7a 30 sprintf(led_dimm_temp, "%d", led_state);
marcel1691 9:18f4959c2bac 31
bogdanm 2:7e489126fe7a 32 coap_res_ptr->payload_len = strlen(led_dimm_temp);
bogdanm 2:7e489126fe7a 33 coap_res_ptr->payload_ptr = (uint8_t*)led_dimm_temp;
bogdanm 2:7e489126fe7a 34 sn_nsdl_send_coap_message(address, coap_res_ptr);
bogdanm 2:7e489126fe7a 35 }
bogdanm 2:7e489126fe7a 36 else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
bogdanm 2:7e489126fe7a 37 {
bogdanm 2:7e489126fe7a 38 memcpy(led_dimm_temp, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
marcel1691 9:18f4959c2bac 39
bogdanm 2:7e489126fe7a 40 led_dimm_temp[received_coap_ptr->payload_len] = '\0';
marcel1691 9:18f4959c2bac 41
bogdanm 2:7e489126fe7a 42 led_dimm = atof(led_dimm_temp);
bogdanm 2:7e489126fe7a 43 led_dimm = led_dimm/100;
marcel1691 9:18f4959c2bac 44
bogdanm 2:7e489126fe7a 45 //led1.write(led_dimm);
bogdanm 2:7e489126fe7a 46 led2.write(led_dimm);
bogdanm 2:7e489126fe7a 47 led3.write(led_dimm);
bogdanm 2:7e489126fe7a 48 //led4.write(led_dimm);
marcel1691 9:18f4959c2bac 49
bogdanm 2:7e489126fe7a 50 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
bogdanm 2:7e489126fe7a 51 sn_nsdl_send_coap_message(address, coap_res_ptr);
bogdanm 2:7e489126fe7a 52 }
marcel1691 9:18f4959c2bac 53
bogdanm 2:7e489126fe7a 54 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
bogdanm 2:7e489126fe7a 55 return 0;
bogdanm 2:7e489126fe7a 56 }
marcel1691 9:18f4959c2bac 57
bogdanm 2:7e489126fe7a 58 int create_light_resource(sn_nsdl_resource_info_s *resource_ptr)
bogdanm 2:7e489126fe7a 59 {
bogdanm 2:7e489126fe7a 60 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));
bogdanm 2:7e489126fe7a 61 return 0;
bogdanm 2:7e489126fe7a 62 }