Simple HTTP GET and POST with ESP8266.
Dependencies: ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed
Fork of ESP8266_HTTP_HelloWorld by
Revision 3:5175e53017e4, committed 2015-04-28
- Comitter:
- mbedAustin
- Date:
- Tue Apr 28 20:18:04 2015 +0000
- Parent:
- 2:44dca280b049
- Child:
- 4:d02fdd139307
- Commit message:
- IP Function Works
Changed in this revision
--- a/ESP8266Interface.lib Mon Apr 20 20:29:26 2015 +0000 +++ b/ESP8266Interface.lib Tue Apr 28 20:18:04 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/MBED_DEMOS/code/ESP8266Interface/#9c6cefc12f0c +http://developer.mbed.org/teams/MBED_DEMOS/code/ESP8266Interface/#939372104145
--- a/PololuLedStrip.lib Mon Apr 20 20:29:26 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/michaeljkoster/code/PololuLedStripNucleo/#aa89fb2a5769
--- a/dbg.h Mon Apr 20 20:29:26 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#ifndef DEBUG_H
-#define DEBUG_H
-
-#include "nsdl_support.h"
-#include "mbed.h"
-
-//Debug is disabled by default
-#define DEBUG 1
-
-#if (DEBUG)
-extern Serial pc;
-#define NSDL_DEBUG(x, ...) pc.printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
-#else
-#define NSDL_DEBUG(x, ...)
-#endif
-
-#endif
\ No newline at end of file
--- a/main.cpp Mon Apr 20 20:29:26 2015 +0000
+++ b/main.cpp Tue Apr 28 20:18:04 2015 +0000
@@ -1,151 +1,64 @@
#include "mbed.h"
+#define DEBUG 1
#include "ESP8266Interface.h"
-#include "nsdl_support.h"
-#include "dbg.h"
#include "UDPSocket.h"
#include "Endpoint.h"
-// Include resources
-#include "light.h"
-
-Serial pc(USBTX, USBRX); // tx, rx
-
-// ****************************************************************************
-// Configuration section
-
-
-// Ethernet configuration
-/* Define this to enable DHCP, otherwise manual address configuration is used */
-#define DHCP
-
-/* Manual IP configurations, if DHCP not defined */
-#define IP "10.0.0.199"
-#define MASK "255.255.255.0"
-#define GW "10.0.0.1"
-
-
-// NSP configuration
-/* Change this IP address to that of your NanoService Platform installation */
-//static const char* NSP_ADDRESS = "191.239.5.150"; // barista2.cloudapp.net
-static const char* NSP_ADDRESS = "108.201.184.41"; // smartobjectservice.com
-//static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server
-static const int NSP_PORT = 5683;
-char endpoint_name[24] = "mbedDEMO-";
-uint8_t ep_type[] = {"DEMO"};
-uint8_t lifetime_ptr[] = {"60"};
-
-// ****************************************************************************
-// Ethernet initialization
-
-
-//ESP8266Interface wifi(PA_15,PB_7,PA_14,"demo","ARMDEMO1"); //hack wire
-ESP8266Interface wifi(D8,D3,D10,"demo","ARMDEMO1"); //LED strip board
-
-
-static void ethernet_init()
-{
- pc.printf("init\r\n");
- wifi.init(); // initialize the interface, reset the module
-
- pc.printf("join\r\n");
- wifi.join(); // join AP and get DHCP settings
-}
-
-// ****************************************************************************
-// NSP initialization
+RawSerial pc(USBTX, USBRX); // tx, rx
+ESP8266Interface wifi(D1,D0,D10,"demossid","password",115200); // TX,RX,Reset,SSID,Password
UDPSocket server;
-Endpoint nsp;
bool UDP_blocking = false;
unsigned int UDP_timeout = 100;
uint8_t macbytes[6] = {0};
char MAC[20] = "665544332211";
-static void nsp_init()
-{
- //pc.printf("socket init\r\n");
- server.init();
- //pc.printf("socket bind\r\n");
- server.bind(0);
- pc.printf("set blocking option\r\n");
- server.set_blocking(UDP_blocking, UDP_timeout);
-
- pc.printf("set ep address\r\n");
- nsp.set_address(NSP_ADDRESS, NSP_PORT);
-
- strncat(endpoint_name, MAC, 12);
-
- NSDL_DEBUG("name: %s", endpoint_name);
- NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
-
-}
-
-// ****************************************************************************
-// Resource creation
-
-static int create_resources()
-{
- sn_nsdl_resource_info_s *resource_ptr = NULL;
- sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
-
- NSDL_DEBUG("Creating resources");
-
- /* Create resources */
- resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
- if(!resource_ptr)
- return 0;
- memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
-
- resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
- if(!resource_ptr->resource_parameters_ptr)
- {
- nsdl_free(resource_ptr);
- return 0;
- }
- memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
-
- // Static resources
- nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1);
- nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1);
-
- // Dynamic resources
- create_light_resource(resource_ptr);
-
- /* Register with NSP */
- endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
- if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
- pc.printf("NSP registering failed\r\n");
- else
- pc.printf("NSP registering OK\r\n");
- nsdl_clean_register_endpoint(&endpoint_ptr);
-
- nsdl_free(resource_ptr->resource_parameters_ptr);
- nsdl_free(resource_ptr);
- return 1;
-}
-
-// ****************************************************************************
-// Program entry point
-
int main()
{
- pc.baud(9600); // why do we need to match the ESP8266 serial rate?
- pc.printf("SystemCoreClock=: %d\r\n", SystemCoreClock / 1000000) ;
- NSDL_DEBUG("Hello mDS Demo Endpoint Application\n");
-
- // Initialize Ethernet interface first
- ethernet_init();
-
- // Initialize NSP node
- nsp_init();
-
- // Initialize NSDL stack
- nsdl_init();
-
- // Create NSDL resources
- create_resources();
-
- // Run the NSDL event loop (never returns)
- nsdl_event_loop();
+ pc.baud(115200);
+ int check = 0;
+ pc.printf("\n\rSystemCoreClock=: %d", SystemCoreClock / 1000000) ;
+
+ // Init ESP8266
+ pc.printf("\r\nwifi.init: ");
+ check = wifi.init(); // initialize the interface, reset the module
+ pc.printf("%d",check);
+
+ // Initialize Socket
+ pc.printf("\r\nwifi.connect: ");
+ check = wifi.connect(); // join AP and get DHCP settings
+ pc.printf("%d",check);
+
+// printf("\r\nsocket init: ");
+// check = server.init();
+// printf("%d",check);
+//
+// printf("\r\nsocket bind: ");
+// check = server.bind(0);
+// printf("%d",check);
+//
+// printf("\r\nset blocking option");
+// server.set_blocking(UDP_blocking, UDP_timeout);
+ wifi.sendCommand("AT+CWJAP?", NULL, NULL, 1000);
+
+ check = wifi.is_connected();
+ pc.printf("\r\nwifi connected: %d,\r\n",check);
+
+ char * ip = NULL;
+ ip = wifi.getIPAddress();
+ pc.printf("\n\rIP = %s \n\r",ip);
+
+ pc.printf("\r\nStarting while1 loop\r\n");
+ while(1) {
+ if(pc.readable()) {
+ char c = pc.getc();
+ //pc.putc(c); // echo character back to computer for sanity
+ wifi.putc(c);
+ }
+ if(wifi.readable()) {
+ char c = wifi.getc();
+ pc.putc(c);
+ }
+ }
}
--- a/nsdl_lib.lib Mon Apr 20 20:29:26 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/teams/Sensinode/code/nsdl_lib/#388450b1e776
--- a/nsdl_support.cpp Mon Apr 20 20:29:26 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,210 +0,0 @@
-// NSDL library support functions
-
-#include "mbed.h"
-#include "nsdl_support.h"
-#include "mbed.h"
-#include "UDPSocket.h"
-#include "Endpoint.h"
-
-/* __heapvalid((__heapprt)fprintf,stdout, 1);\*/
-#define MEM_VALID(x) \
- int s##x=0;\
- int *h##x = new int [1];\
- std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\
- if (h##x > &s##x)\
- printf("collision\n");\
- else\
- delete [] h##x;\
- __nop()
-
-extern Serial pc;
-extern Endpoint nsp;
-extern UDPSocket server;
-extern char endpoint_name[24];
-extern uint8_t ep_type[];
-extern uint8_t lifetime_ptr[];
-char null_ep_name[] = "";
-uint8_t null_ep_type[] = "";
-uint8_t null_lifetime_ptr[] = "60";
-bool nsdl_reg_update_needed = false;
-
-/* The number of seconds between NSP registration messages */
-#define RD_UPDATE_PERIOD 30
-
-void *nsdl_alloc(uint16_t size)
-{
- void *buf = malloc(size);
-// printf("alloc\r\n");
-// MEM_VALID(0);
- return buf;
-}
-
-void nsdl_free(void* ptr_to_free)
-{
- free(ptr_to_free);
-// printf("de-alloc\r\n");
-// MEM_VALID(0);
-}
-
-/*
- * Create a static resoure
- * Only get is allowed
- */
-void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
-{
- resource_structure->access = SN_GRS_GET_ALLOWED;
- resource_structure->mode = SN_GRS_STATIC;
- resource_structure->pathlen = pt_len;
- resource_structure->path = pt;
- resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
- resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
- resource_structure->resource = rsc;
- resource_structure->resourcelen = rsc_len;
- sn_nsdl_create_resource(resource_structure);
-}
-
-void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
-{
- resource_structure->access = (sn_grs_resource_acl_e)access_right;
- resource_structure->resource = 0;
- resource_structure->resourcelen = 0;
- resource_structure->sn_grs_dyn_res_callback = callback_ptr;
- resource_structure->mode = SN_GRS_DYNAMIC;
- resource_structure->pathlen = pt_len;
- resource_structure->path = pt;
- resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
- resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
- resource_structure->resource_parameters_ptr->observable = is_observable;
- sn_nsdl_create_resource(resource_structure);
-}
-
-sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr)
-{
- if (NULL == endpoint_structure)
- {
- endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
- }
- if (endpoint_structure)
- {
- memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
- endpoint_structure->endpoint_name_ptr = name;
- endpoint_structure->endpoint_name_len = strlen((char*)name);
- endpoint_structure->type_ptr = typename_ptr;
- endpoint_structure->type_len = strlen((char*)typename_ptr);
- endpoint_structure->lifetime_ptr = lifetime_ptr;
- endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
- }
- return endpoint_structure;
-}
-
-void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
-{
- if (*endpoint_structure)
- {
- nsdl_free(*endpoint_structure);
- *endpoint_structure = NULL;
- }
-}
-
-static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
-{
- pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len);
-
- if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
- pc.printf("sending failed\n\r");
-
- return 1;
-}
-
-static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
-{
- pc.printf("RX callback!\r\n");
- pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code);
- pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len);
- int i;
- pc.printf("Payload:'");
- for (i=0; i < coap_packet_ptr->payload_len; i++)
- pc.printf("%c", *(coap_packet_ptr->payload_ptr + i));
- pc.printf("' \r\n");
- if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr)
- {
- pc.printf("Location: /");
- int i;
- for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) pc.printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i]));
- pc.printf(" \r\n");
- }
- //sn_coap_packet_debug(coap_packet_ptr);
- return 0;
-}
-
-void nsdl_init()
-{
- uint8_t nsp_addr[4];
- sn_nsdl_mem_s memory_cbs;
-
- /* Initialize libNsdl */
- memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
- memory_cbs.sn_nsdl_free = &nsdl_free;
- if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
- pc.printf("libNsdl init failed\r\n");
- else
- pc.printf("libNsdl init done\r\n");
-
- /* Set nsp address for library */
- set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
-}
-
-void nsdl_reg_update()
- {
- sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
- endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr);
- if(sn_nsdl_update_registration(endpoint_ptr) != 0)
- pc.printf("NSP registration update failed\r\n");
- else
- pc.printf("NSP registration update OK\r\n");
- nsdl_clean_register_endpoint(&endpoint_ptr);
- }
-
-void nsdl_reg_update_timeout()
- {
- nsdl_reg_update_needed = true;
- }
-
-Ticker regUpdateTicker;
-
-void nsdl_event_loop()
-{
- regUpdateTicker.attach(&nsdl_reg_update_timeout, RD_UPDATE_PERIOD);
-
- sn_nsdl_addr_s received_packet_address;
- uint8_t received_address[4];
- char buffer[1024];
- Endpoint from;
-
- memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
- received_packet_address.addr_ptr = received_address;
-
- uint16_t count = 0;
- while(1)
- {
- int n = server.receiveFrom(from, buffer, sizeof(buffer));
- if (n < 0)
- {
- pc.printf("Socket error: %d\n\r", n);
- }
- else if (n > 0)
- {
- count = 0;
- pc.printf("Received %d bytes\r\n", n);
- sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
- }
- if (nsdl_reg_update_needed)
- {
- nsdl_reg_update_needed = false;
- nsdl_reg_update();
- }
- //pc.printf("nsdl loop: %d\r\n", count++);
- }
-}
-
-
\ No newline at end of file
--- a/nsdl_support.h Mon Apr 20 20:29:26 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -// Support functions for the NSDL library - -#ifndef NSDL_SUPPORT_H -#define NSDL_SUPPORT_H - -#include "mbed.h" -#include <stdint.h> -#include "sn_nsdl.h" -#include "sn_coap_header.h" -#include "sn_coap_protocol.h" -#include "sn_nsdl_lib.h" - -typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *); - -extern "C" void *nsdl_alloc(uint16_t size); -extern "C" void nsdl_free(void* ptr_to_free); -void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len); -void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right); -sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr); -void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure); -void nsdl_init(); -void nsdl_event_loop(); - -#endif // NSDL_SUPPORT_H
--- a/resources/light.cpp Mon Apr 20 20:29:26 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,102 +0,0 @@
-// 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
--- a/resources/light.h Mon Apr 20 20:29:26 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -// Light resource implementation - -#ifndef LIGHT_H -#define LIGHT_H - -#include "nsdl_support.h" - -int create_light_resource(sn_nsdl_resource_info_s *resource_ptr); - -#endif
