Basic websocket demo esp8266

Dependencies:   ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed-dev

Fork of ESP8266_HTTP_HelloWorld by ESP8266

main.cpp

Committer:
michaeljkoster
Date:
2015-04-20
Revision:
1:f61c1001ee60
Parent:
0:6a891da014a3
Child:
3:5175e53017e4

File content as of revision 1:f61c1001ee60:

#include "mbed.h"
#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

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();
}