mbed Sensor node for Instrumented Booth over ETH.

Dependencies:   EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib

Fork of Trenton_Switch_LPC1768_ETH by Demo Team

main.cpp

Committer:
erigow01
Date:
2014-04-04
Revision:
16:3fb612af0dc5
Parent:
15:59f4cee0da79
Child:
17:8ca4a5801430

File content as of revision 16:3fb612af0dc5:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
#include "WiflyInterface.h"
#include "main.h"
#include "rtos.h"

#include "nsdl_support.h"

//#include "pressure_mat.h"

//Serial pc(USBTX, USBRX);

/* wifly object where:
*     - p9 and p10 are for the serial communication
*     - p25 is for the reset pin
*     - p26 is for the connection status
*     - "mbed" is the ssid of the network
*     - "password" is the password
*     - WPA is the security
*/
//WiflyInterface wifly(p9, p10, p25, p26, SSID, AP_KEY, WPA); //LPC1768
WiflyInterface wifly(PTE16, PTE17, PTD4, PTD2, SSID, AP_KEY, WPA); //KL46Z

// NSP configuration
/* Change this IP address to that of your NanoService Platform installation */
Endpoint nsp;
UDPSocket server;
//extern TCPSocketConnection server;
char endpoint_name[] = {"welcomemat"};
uint8_t ep_type[] = {"mbed_KL46Z"};
uint8_t lifetime_ptr[] = {"86400"};

//static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
//static const char* NSP_ADDRESS = "10.2.131.119"; /* aseserver NSP, */ 
static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ 
static const int NSP_PORT = 5683;

/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
static void exec_call_thread(void const *args)
{
    int32_t time = 0;
    while (true)
    {
        wait(1);
        time++;
        sn_nsdl_exec(time);
    }
}

// ****************************************************************************
// NSP initialization

static void nsp_connect()
{
    printf("EP Name: %s", endpoint_name);
    printf("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT);
    
    // Bind the port
    //cellular->bind(EP_PORT);
    server.init();
    //server.connect(NSP_ADDRESS, NSP_PORT);
    server.bind(NSP_PORT);
    nsp.set_address(NSP_ADDRESS, NSP_PORT);

    printf("UDP connection to NSP successful.\r\n");  
}

static int create_resources()
{
    sn_nsdl_resource_info_s *resource_ptr = NULL;
    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
    
    printf("Creating resources\r\n");

    /* 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/1")-1, (uint8_t*) "3/0/1", 0, 0,  (uint8_t*) "KL46Z Welcome Mat", sizeof("KL46Z Welcome Mat")-1);

    // Dynamic resources
    //create_pressure_mat_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) {
        printf("NSP registering failed\r\n");
    } else {
        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;
}



/**
 *  \param none
 *  \return int
 */
int main() {
    //factory reset Wifly
    wifly.reset();
    printf("\nFactory Reset\n");
    //reboot wifly
    bool success = wifly.reboot();
    printf("Reboot: %d\n", success); //success = 1 -> successful process
    printf("Initialising Wifly...\n\r");
    wifly.init(); // use DHCP
    while (!wifly.connect()); // join the network
    printf("IP Address is %s\n\r", wifly.getIPAddress());
        
    // Bind the socket and configure NSP settings
    nsp_connect();
 
    // Initalize NanoService library
    nsdl_init();

    // Create resources & register with NSP
    create_resources();
    
    //Create the NSDL exec thread
    //static Thread exec_thread(exec_call_thread);
    
    // Run the NSDL event loop (never returns)
    nsdl_event_loop();

}