Demo Team / mbed-IBooth-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

Revision:
26:4cac6b346e4f
Parent:
25:cb16c5248769
Child:
27:6017a643f386
--- a/main.cpp	Wed Dec 03 09:03:29 2014 +0000
+++ b/main.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -15,35 +15,21 @@
  */
 #include "mbed.h"
 #include "EthernetInterface.h"
-#include "main.h"
 #include "nsdl_support.h"
+#include "node_cfg.h"
 #include "sensor_ctl.h"
-#include "switch.h"
+
 
 
 //Serial pc(USBTX, USBRX);
 
-// ****************************************************************************
-// 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.45.0.206"
-#define MASK    "255.255.255.0"
-#define GW      "10.45.0.1"
-
-// ****************************************************************************
 // Ethernet initialization
 
 EthernetInterface eth;
 
 static void ethernet_init()
 {
-    char mbed_uid[33]; // for creating unique name for the board
-
     /* Initialize network */
 #ifdef DHCP
     printf("DHCP in use\r\n");
@@ -53,29 +39,9 @@
 #endif
     if(eth.connect(30000) == 0)
         printf("Connect OK\n\r");
-/*
-    mbed_interface_uid(mbed_uid);
-    mbed_uid[32] = '\0';
-    strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
-
-    lcd.locate(0,11);
-    lcd.printf("IP:%s", eth.getIPAddress());
-*/
     printf("IP Address:%s ", eth.getIPAddress());
 }
 
-// NSP configuration
-/* Change this IP address to that of your NanoService Platform installation */
-Endpoint nsp;
-UDPSocket server;
-
-//extern TCPSocketConnection server;
-char endpoint_name[] = {"switch"};
-uint8_t ep_type[] = {"mbed_LPC1768"};
-uint8_t lifetime_ptr[] = {"86400"};
-
-static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ 
-static const int NSP_PORT = 5683;
 
 //Hard Fault Handler (Watchdog)
 extern "C" void HardFault_Handler() {
@@ -84,79 +50,26 @@
 }
 
 // ****************************************************************************
-// NSP initialization
-
-static void nsp_connect()
-{
-    printf("EP Name: %s\r\n", endpoint_name);
-    printf("NSP Location: coap://%s:%d\r\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");
+// Socket initialization
+UDPSocket server;
+Endpoint nsp;
 
-    /* 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("detail/name")-1, (uint8_t*) "detail/name", 0, 0,  (uint8_t*) "LPC1768 Switch", sizeof("LPC1768 Switch")-1);
-
-    // Dynamic resources
-    create_switch_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;
-}
 
 /* The number of seconds between NSDL Ticks*/
 #define NSDL_TICK_PERIOD  1
-/* The number of seconds between NSP registration messages */
-#define RD_UPDATE_PERIOD  300
 
-void nsdl_event_loop()
-{
-    //Thread registration_thread(registration_update_thread);
-    
+void main_event_loop() {    
     //For timing control
     Timer nsdlTickTimer;
     Timer registrationTimer;
     
-    //Re-registration
-    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+    //Sensor timers...
+    Timer temperatureReportTimer;
+    Timer microphoneSampleTimer;
+    Timer microphoneReportTimer;
+    Timer doorTripReportTimer;
+    Timer motionReportTimer;
     
     //For recieving NSP messages
     sn_nsdl_addr_s received_packet_address;
@@ -171,19 +84,22 @@
     //Check incoming socket...
     int n = 0;
     int32_t time = 0;
+    
+    //Start Timers
     nsdlTickTimer.start();
     registrationTimer.start();
-    while(true)
-    {
-        //Wifly UDP Packet Receive...
+    temperatureReportTimer.start();
+    microphoneSampleTimer.start();
+    microphoneReportTimer.start();
+    doorTripReportTimer.start();
+    motionReportTimer.start();
+    while(true) {
+        //UDP Packet Receive...
         n = server.receiveFrom(from, buffer, sizeof(buffer));
-        if (n < 0)
-        {
+        if (n < 0) {
             //No Data
             //printf("Socket error\n\r");
-        }
-        else
-        { 
+        } else { 
             //UDP
             //wait(0.25); //Waiting seems to increase reliability of comms...
             printf("Received %d bytes\r\n", n);
@@ -192,11 +108,6 @@
             n = 0;
         }
         
-
-        //Check if need to send pressure mat update...
-       handle_microphone_sample_timer();
-
-        
         //NSDL Tick
         if(nsdlTickTimer.read() >= NSDL_TICK_PERIOD) {
             sn_nsdl_exec(time);
@@ -205,15 +116,50 @@
                  
         //Registration Tick
         if(registrationTimer.read() >= RD_UPDATE_PERIOD) {
-            printf("NSP attempt re-register...\r\n");
-            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 re-registering failed\r\n");
-            else
-                printf("NSP re-registering OK\r\n");
-            nsdl_clean_register_endpoint(&endpoint_ptr);
+            printf("Time to register...\r\n");                                    
+            NSP_registration();
             registrationTimer.reset();
         }
+        
+        #if NODE_SENSOR_STATION
+        if (temperatureReportTimer.read() >= TEMPERATURE_REPORT_PERIOD){
+            //debug("Event: Temperature Report Timer\r\n");
+            handle_temperature_report_timer();
+            temperatureReportTimer.reset();
+        }
+        if (microphoneSampleTimer.read() >= SOUND_SAMPLE_PERIOD){
+            handle_microphone_sample_timer();
+            microphoneSampleTimer.reset();
+        }
+        if (microphoneReportTimer.read() >= SOUND_REPORT_PERIOD){
+            //debug("Event: Sound Report Timer\r\n");
+            handle_microphone_report_timer();
+            microphoneReportTimer.reset();
+        }
+        #if NODE_PIR_STATION
+        if (motionReportTimer.read() >= MOTION_REPORT_PERIOD){
+            //debug("Event: Motion Report Timer\r\n");
+            handle_motion_report_timer();
+            motionReportTimer.reset();
+        }
+        #endif //NODE PIR STATION
+        #if NODE_KIOSK_STATION
+        
+        #endif //NODE KIOSK STATION
+        #if NODE_DOOR_TRIP_STATION
+        if (doorTripReportTimer.read() >= DOOR_TRIP_REPORT_PERIOD){
+            //debug("Event: Door Trip Report Timer\r\n");
+            handle_door_trip_report_timer();
+            doorTripReportTimer.reset();
+        }
+        #endif //NODE TRIP STATION
+        #if NODE_HEIGHT_STATION
+ //       if (temperatureReportTimer.read() >= TEMPERATURE_REPORT_PERIOD){
+//            
+//            temperatureReportTimer.reset();
+//        }
+        #endif //NODE HEIGHT STATION
+        #endif //NODE_SENSOR_STATION
     }
     
     
@@ -229,15 +175,12 @@
     printf("Initialising Ethernet...\r\n");
     // Initialize Ethernet interface first
     ethernet_init();
-        
-    // Bind the socket and configure NSP settings
-    nsp_connect();
- 
-    // Initalize NanoService library
-    nsdl_init();
+    printf("Initialising NSDL...\r\n");
+    //Run NSDL...
+    nsdl_run();
+    //Init Sensors
+    init_sensors();
 
-    // Create resources & register with NSP
-    create_resources();
-    // Run the NSDL event loop (never returns)
-    nsdl_event_loop();
+    // Run the event loop (never returns)
+    main_event_loop();
 }