Simple HTTP GET and POST with ESP8266.
Dependencies: ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed
Fork of ESP8266_HTTP_HelloWorld by
Diff: resources/light.cpp
- Revision:
- 0:6a891da014a3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/light.cpp Wed Dec 24 19:02:13 2014 +0000 @@ -0,0 +1,102 @@ +// Light resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "light.h" + +#include "PololuLedStrip.h" +PololuLedStrip ledStrip(D2); +//#define LED_COUNT 60 +#define LED_COUNT 120 +rgb_color colors[LED_COUNT]; + +#define LIGHT_RES_ID "11100/0/5900" + +extern Serial pc; +char led_color[] = {"00000000"}; + +void set_led_color(char *color_string) +{ +static uint8_t red, green, blue, index ; +int color_int ; + + sscanf(color_string, "%X", &color_int); + + index = color_int & 255; + blue = color_int >> 8 & 255; + green = color_int >> 16 & 255; + red = color_int >> 24 & 255; + + if(index > 0 and index <= LED_COUNT) + { + colors[index-1] = (rgb_color) {red,green,blue} ; + ledStrip.write(colors, LED_COUNT); + } + else if(index == 0) + { + for(int i = 0; i < LED_COUNT; i++) + { + colors[i] = (rgb_color) {red,green,blue} ; + ledStrip.write(colors, LED_COUNT); + wait_ms(10); + } + } + else if(index == 255) + { + for( int i = LED_COUNT-1; i > 0; i-- ) + { + colors[i] = colors [i-1]; + } + colors[0] = (rgb_color) {red,green,blue} ; + ledStrip.write(colors, LED_COUNT); + } +} + +/* 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; + + //pc.printf("LED Strip 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 = strlen(led_color); + coap_res_ptr->payload_ptr = (uint8_t*)led_color; + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + //pc.printf("PUT: %d bytes\r\n", received_coap_ptr->payload_len); + if(received_coap_ptr->payload_len == 8) + { + memcpy(led_color, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + + led_color[received_coap_ptr->payload_len] = '\0'; + pc.printf("PUT: %s\r\n",led_color); + + //call LED strup update function here + set_led_color(led_color); + + 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)); + + set_led_color("10000000"); + set_led_color("00100000"); + set_led_color("00001000"); + set_led_color("00000000"); + + return 0; +} \ No newline at end of file