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-12-16
Revision:
34:5678fdb45d84
Parent:
32:c957a1948ac1

File content as of revision 34:5678fdb45d84:

/* 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 "EthernetInterface.h"
#include "nsdl_support.h"
#include "node_cfg.h"
#include "sensor_ctl.h"



//Serial pc(USBTX, USBRX);


// Ethernet initialization

EthernetInterface eth;

static void ethernet_init()
{
    /* Initialize network */
#ifdef DHCP
    printf("DHCP in use\r\n");
    eth.init();
#else
    eth.init(IP, MASK, GW);
#endif
    if(eth.connect(30000) == 0)
        printf("Connect OK\n\r");
    printf("IP Address:%s ", eth.getIPAddress());
}


//Hard Fault Handler (Watchdog)
extern "C" void HardFault_Handler() {
    printf("Hard Fault!\r\n");
    NVIC_SystemReset();
}

// ****************************************************************************
// Socket initialization
UDPSocket server;
Endpoint nsp;



/* The number of seconds between NSDL Ticks*/
#define NSDL_TICK_PERIOD_MS  1000

void main_event_loop() {    
    //For timing control
//    Timer nsdlTickTimer;
    Timer registrationTimer;
    
    //Sensor timers...
    #if NODE_SENSOR_STATION
    Timer temperatureReportTimer;
    Timer microphoneSampleTimer;
    Timer microphoneReportTimer;
    #if NODE_PIR_STATION
    Timer motionReportTimer;
    #endif //NODE PIR STATION
    #if NODE_KIOSK_STATION
    Timer kioskReportTimer;
    #endif //NODE KIOSK STATION
    #if NODE_DOOR_TRIP_STATION
    Timer doorTripReportTimer;
    #endif //NODE TRIP STATION
    #if NODE_HEIGHT_STATION
    Timer heightReportTimer;
    #endif //NODE HEIGHT STATION
    #endif //NODE_SENSOR_STATION
       
    //For recieving NSP messages
    sn_nsdl_addr_s received_packet_address;
    uint8_t received_address[4];
    int8_t nsdl_result = 0;
    char buffer[256];
    Endpoint from;
    memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
    received_packet_address.addr_ptr = received_address;
    server.set_blocking(false, 50);
    
    //Check incoming socket...
    int n = 0;
    int32_t time = 0;
    
    //Start Timers

//    nsdlTickTimer.start();
    registrationTimer.start();
    #if NODE_SENSOR_STATION
    temperatureReportTimer.start();
    microphoneSampleTimer.start();
    microphoneReportTimer.start();
    #if NODE_PIR_STATION
    motionReportTimer.start();
    #endif //NODE PIR STATION
    #if NODE_KIOSK_STATION
    kioskReportTimer.start();
    #endif //NODE KIOSK STATION
    #if NODE_DOOR_TRIP_STATION
    doorTripReportTimer.start();
    #endif //NODE TRIP STATION
    #if NODE_HEIGHT_STATION
    heightReportTimer.start();
    #endif //NODE HEIGHT STATION
    #endif //NODE_SENSOR_STATION

    while(true) {
        //UDP Packet Receive...
        n = server.receiveFrom(from, buffer, sizeof(buffer));
        if (n <= 0) {
            //No Data
        } else { 
            //UDP
            //wait(0.25); //Waiting seems to increase reliability of comms...
            printf("Received %d bytes\r\n", n);
            nsdl_result = sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
            printf("Processed COAP Packet: %d\r\n", nsdl_result);
            n = 0;
        }
        
        //NSDL Tick
//        if(nsdlTickTimer.read_ms() >= NSDL_TICK_PERIOD_MS) {
//            sn_nsdl_exec(time);
//            nsdlTickTimer.reset();
//        }
                 
        //Registration Tick
        if(registrationTimer.read_ms() >= RD_UPDATE_PERIOD_MS) {
            printf("Time to register...\r\n");                                    
            NSP_registration();
            registrationTimer.reset();
        }
        
        #if NODE_SENSOR_STATION
        if (temperatureReportTimer.read_ms() >= TEMPERATURE_REPORT_PERIOD_MS){
            //debug("Event: Temperature Report Timer\r\n");
            handle_temperature_report_timer();
            temperatureReportTimer.reset();
        }
        if (microphoneSampleTimer.read_ms() >= SOUND_SAMPLE_PERIOD_MS){
            handle_microphone_sample_timer();
            microphoneSampleTimer.reset();
        }
        if (microphoneReportTimer.read_ms() >= SOUND_REPORT_PERIOD_MS){
            //debug("Event: Sound Report Timer\r\n");
            handle_microphone_report_timer();
            microphoneReportTimer.reset();
        }
        #if NODE_PIR_STATION
        if (motionReportTimer.read_ms() >= MOTION_REPORT_PERIOD_MS){
            //debug("Event: Motion Report Timer\r\n");
            handle_motion_report_timer();
            motionReportTimer.reset();
        }
        #endif //NODE PIR STATION
        #if NODE_KIOSK_STATION
        if (kioskReportTimer.read_ms() >= KIOSK_REPORT_PERIOD_MS){
            //debug("Event: Motion Report Timer\r\n");
            handle_kiosk_report_timer();
            kioskReportTimer.reset();
        }
        #endif //NODE KIOSK STATION
        #if NODE_DOOR_TRIP_STATION
        if (doorTripReportTimer.read_ms() >= DOOR_TRIP_REPORT_PERIOD_MS){
            //debug("Event: Door Trip Report Timer\r\n");
            handle_door_trip_report_timer();
            doorTripReportTimer.reset();
        }
        #endif //NODE TRIP STATION
        #if NODE_HEIGHT_STATION
        if (heightReportTimer.read_ms() >= DOOR_HEIGHT_PERIOD_MS ){
            handle_door_height_sample_timer();
            heightReportTimer.reset();
        }
        #endif //NODE HEIGHT STATION
        #endif //NODE_SENSOR_STATION
    }
    
    
}



/**
 *  \param none
 *  \return int
 */
int main() {
    printf("Initialising Ethernet...\r\n");
    // Initialize Ethernet interface first
    ethernet_init();
    printf("Initialising NSDL...\r\n");
    //Run NSDL...
    nsdl_run();
    //Init Sensors
    init_sensors();

    // Run the event loop (never returns)
    main_event_loop();
}