Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Beep LM75B MMA7660 mbed nsdl_lib
Fork of NSDL_HelloWorld by
Diff: resources/relay.cpp
- Revision:
- 2:7e489126fe7a
- Child:
- 9:ccb9e53d5471
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/relay.cpp Tue Oct 15 12:45:46 2013 +0000
@@ -0,0 +1,58 @@
+// GPS resource implementation
+
+#include "mbed.h"
+#include "nsdl_support.h"
+#include "relay.h"
+#include "Beep.h"
+
+#define RELAY_RES_ID "beep/0/on"
+
+extern Serial pc;
+static Beep buzzer(p26);
+
+/* Only GET and PUT method allowed */
+static uint8_t relay_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 uint8_t relay_state = '0';
+
+ pc.printf("relay 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);
+
+ coap_res_ptr->payload_len = 1;
+ coap_res_ptr->payload_ptr = &relay_state;
+ sn_nsdl_send_coap_message(address, coap_res_ptr);
+ }
+ else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
+ {
+ if(received_coap_ptr->payload_len)
+ {
+ if(*(received_coap_ptr->payload_ptr) == '1')
+ {
+ buzzer.beep(1000,0);
+ relay_state = '1';
+
+ }
+ else if(*(received_coap_ptr->payload_ptr) == '0')
+ {
+ buzzer.nobeep();
+ relay_state = '0';
+ }
+ 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_relay_resource(sn_nsdl_resource_info_s *resource_ptr)
+{
+ nsdl_create_dynamic_resource(resource_ptr, sizeof(RELAY_RES_ID)-1, (uint8_t*)RELAY_RES_ID, 0, 0, 0, &relay_resource_cb, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
+ return 0;
+}
\ No newline at end of file

