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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "EthernetInterface.h"
00018 #include "nsdl_support.h"
00019 #include "node_cfg.h"
00020 #include "sensor_ctl.h"
00021 #include "rtos.h"
00022 
00023 #include "Watchdog.h"
00024 
00025 Serial pc(PTC17,PTC16);
00026 #define WATCHDOG_TIMEOUT_SEC 30 //30 seconds
00027 #define HEARTBEAT_PERIOD_MS 1000 //1 second...
00028 
00029 // Ethernet initialization
00030 EthernetInterface eth;
00031 static void ethernet_init()
00032 {
00033     /* Initialize network */
00034 #ifdef DHCP
00035     pc.printf("DHCP in use\r\n");
00036     eth.init();
00037 #else
00038     eth.init(IP, MASK, GW);
00039 #endif
00040     if(eth.connect(30000) == 0)
00041         pc.printf("Connect OK\n\r");
00042     pc.printf("IP Address:%s ", eth.getIPAddress());
00043 }
00044 
00045 
00046 //Hard Fault Handler (Watchdog)
00047 extern "C" void HardFault_Handler() {
00048     pc.printf("Hard Fault!\r\n");
00049     NVIC_SystemReset();
00050 }
00051 
00052 // ****************************************************************************
00053 // Socket initialization
00054 UDPSocket server;
00055 Endpoint nsp;
00056 
00057 
00058 /* The number of seconds between NSDL Ticks*/
00059 #define NSDL_TICK_PERIOD_MS  1000
00060 
00061 void main_event_loop() {    
00062     //For timing control
00063 //    Timer nsdlTickTimer;
00064     Timer registrationTimer;
00065     #if HEART_BEAT
00066     Timer heartBeatTimer;
00067     #endif
00068     
00069     //Sensor timers...
00070     #if NODE_SENSOR_STATION
00071     Timer temperatureReportTimer;
00072     Timer microphoneSampleTimer;
00073     Timer microphoneReportTimer;
00074     #if NODE_PIR_STATION
00075     Timer motionReportTimer;
00076     #endif //NODE PIR STATION
00077     #if NODE_KIOSK_STATION
00078     Timer kioskReportTimer;
00079     #endif //NODE KIOSK STATION
00080     #if NODE_DOOR_TRIP_STATION
00081     Timer doorTripReportTimer;
00082     #endif //NODE TRIP STATION
00083     #if NODE_HEIGHT_STATION
00084     Timer heightReportTimer;
00085     #endif //NODE HEIGHT STATION
00086     #endif //NODE_SENSOR_STATION
00087        
00088     //For recieving NSP messages
00089     sn_nsdl_addr_s received_packet_address;
00090     uint8_t received_address[4];
00091     int8_t nsdl_result = 0;
00092     char buffer[256];
00093     Endpoint from;
00094     memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
00095     received_packet_address.addr_ptr = received_address;
00096     server.set_blocking(false, 50);
00097     
00098     //Check incoming socket...
00099     int n = 0;
00100     int32_t time = 0;
00101     
00102     //Start Timers
00103 
00104 //    nsdlTickTimer.start();
00105     registrationTimer.start();
00106     #if HEART_BEAT
00107     heartBeatTimer.start();
00108     #endif //Heartbeat
00109     #if NODE_SENSOR_STATION
00110     temperatureReportTimer.start();
00111     microphoneSampleTimer.start();
00112     microphoneReportTimer.start();
00113     #if NODE_PIR_STATION
00114     motionReportTimer.start();
00115     #endif //NODE PIR STATION
00116     #if NODE_KIOSK_STATION
00117     kioskReportTimer.start();
00118     #endif //NODE KIOSK STATION
00119     #if NODE_DOOR_TRIP_STATION
00120     doorTripReportTimer.start();
00121     #endif //NODE TRIP STATION
00122     #if NODE_HEIGHT_STATION
00123     heightReportTimer.start();
00124     #endif //NODE HEIGHT STATION
00125     #endif //NODE_SENSOR_STATION
00126     DigitalOut led2(LED2);
00127     //Watchdog wdt;
00128 //    wdt.kick(WATCHDOG_TIMEOUT_SEC);
00129     NSP_registration();
00130     while(true) {
00131         //Kick the watchdog...
00132 //        wdt.kick();
00133         //UDP Packet Receive...
00134         n = server.receiveFrom(from, buffer, sizeof(buffer));
00135         if (n <= 0) {
00136             //No Data
00137         } else { 
00138             //UDP
00139             //wait(0.25); //Waiting seems to increase reliability of comms...
00140             pc.printf("Received %d bytes\r\n", n);
00141             nsdl_result = sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
00142             pc.printf("Processed COAP Packet: %d\r\n", nsdl_result);
00143             n = 0;
00144         }
00145         
00146         //NSDL Tick
00147 //        if(nsdlTickTimer.read_ms() >= NSDL_TICK_PERIOD_MS) {
00148 //            sn_nsdl_exec(time);
00149 //            nsdlTickTimer.reset();
00150 //        }
00151 //                 
00152         //Registration Tick
00153         if(registrationTimer.read_ms() >= RD_UPDATE_PERIOD_MS) {
00154             pc.printf("Time to register...\r\n");                                    
00155             NSP_registration();
00156             registrationTimer.reset();
00157         }
00158         #if HEART_BEAT
00159         if (heartBeatTimer.read_ms() >= HEARTBEAT_PERIOD_MS){
00160             //Toggle heartbeat led...
00161             led2 = !led2;
00162             heartBeatTimer.reset();
00163         }
00164         #endif //HEart Beat
00165         #if NODE_SENSOR_STATION
00166         if (temperatureReportTimer.read_ms() >= TEMPERATURE_REPORT_PERIOD_MS){
00167             //debug("Event: Temperature Report Timer\r\n");
00168             handle_temperature_report_timer();
00169             temperatureReportTimer.reset();
00170         }
00171         if (microphoneSampleTimer.read_ms() >= SOUND_SAMPLE_PERIOD_MS){
00172             handle_microphone_sample_timer();
00173             microphoneSampleTimer.reset();
00174         }
00175         if (microphoneReportTimer.read_ms() >= SOUND_REPORT_PERIOD_MS){
00176             //debug("Event: Sound Report Timer\r\n");
00177             handle_microphone_report_timer();
00178             microphoneReportTimer.reset();
00179         }
00180         #if NODE_PIR_STATION
00181         if (motionReportTimer.read_ms() >= MOTION_REPORT_PERIOD_MS){
00182             //debug("Event: Motion Report Timer\r\n");
00183             handle_motion_report_timer();
00184             motionReportTimer.reset();
00185         }
00186         #endif //NODE PIR STATION
00187         #if NODE_KIOSK_STATION
00188         if (kioskReportTimer.read_ms() >= KIOSK_REPORT_PERIOD_MS){
00189             //debug("Event: Motion Report Timer\r\n");
00190             handle_kiosk_report_timer();
00191             kioskReportTimer.reset();
00192         }
00193         #endif //NODE KIOSK STATION
00194         #if NODE_DOOR_TRIP_STATION
00195         if (doorTripReportTimer.read_ms() >= DOOR_TRIP_REPORT_PERIOD_MS){
00196             //debug("Event: Door Trip Report Timer\r\n");
00197             handle_door_trip_report_timer();
00198             doorTripReportTimer.reset();
00199         }
00200         #endif //NODE TRIP STATION
00201         #if NODE_HEIGHT_STATION
00202         if (heightReportTimer.read_ms() >= DOOR_HEIGHT_PERIOD_MS ){
00203             handle_door_height_sample_timer();
00204             heightReportTimer.reset();
00205         }
00206         #endif //NODE HEIGHT STATION
00207         #endif //NODE_SENSOR_STATION
00208     }
00209     
00210     
00211 }
00212 
00213 
00214 
00215 /**
00216  *  \param none
00217  *  \return int
00218  */
00219 int main() {
00220     pc.printf("Initialising Ethernet...\r\n");
00221     // Initialize Ethernet interface first
00222     ethernet_init();
00223     pc.printf("Initialising NSDL...\r\n");
00224     //Run NSDL...
00225     nsdl_run();
00226     //Init Sensors
00227     init_sensors();
00228     
00229 
00230     // Run the event loop (never returns)
00231     main_event_loop();
00232 }