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
main.cpp@38:9066ab490386, 2014-12-18 (annotated)
- Committer:
- erigow01
- Date:
- Thu Dec 18 08:55:33 2014 +0000
- Revision:
- 38:9066ab490386
- Parent:
- 35:b0c1b676403a
- Child:
- 46:807e9cf63f4c
Heartbeat ticks and some tweaks for reliability.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 6:6eaae34586b8 | 1 | /* mbed Microcontroller Library |
Kojto | 6:6eaae34586b8 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 6:6eaae34586b8 | 3 | * |
Kojto | 6:6eaae34586b8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 6:6eaae34586b8 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 6:6eaae34586b8 | 6 | * You may obtain a copy of the License at |
Kojto | 6:6eaae34586b8 | 7 | * |
Kojto | 6:6eaae34586b8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 6:6eaae34586b8 | 9 | * |
Kojto | 6:6eaae34586b8 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 6:6eaae34586b8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 6:6eaae34586b8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 6:6eaae34586b8 | 13 | * See the License for the specific language governing permissions and |
Kojto | 6:6eaae34586b8 | 14 | * limitations under the License. |
Kojto | 6:6eaae34586b8 | 15 | */ |
Kojto | 6:6eaae34586b8 | 16 | #include "mbed.h" |
erigow01 | 22:b361f7ae0508 | 17 | #include "EthernetInterface.h" |
erigow01 | 16:3fb612af0dc5 | 18 | #include "nsdl_support.h" |
erigow01 | 26:4cac6b346e4f | 19 | #include "node_cfg.h" |
andcor02 | 25:cb16c5248769 | 20 | #include "sensor_ctl.h" |
erigow01 | 38:9066ab490386 | 21 | #include "rtos.h" |
erigow01 | 26:4cac6b346e4f | 22 | |
erigow01 | 38:9066ab490386 | 23 | #include "Watchdog.h" |
andcor02 | 25:cb16c5248769 | 24 | |
erigow01 | 16:3fb612af0dc5 | 25 | //Serial pc(USBTX, USBRX); |
erigow01 | 38:9066ab490386 | 26 | #define WATCHDOG_TIMEOUT_SEC 30 //30 seconds |
erigow01 | 38:9066ab490386 | 27 | #define HEARTBEAT_PERIOD_MS 1000 //1 second... |
erigow01 | 22:b361f7ae0508 | 28 | |
erigow01 | 22:b361f7ae0508 | 29 | // Ethernet initialization |
erigow01 | 22:b361f7ae0508 | 30 | EthernetInterface eth; |
erigow01 | 22:b361f7ae0508 | 31 | static void ethernet_init() |
erigow01 | 22:b361f7ae0508 | 32 | { |
erigow01 | 22:b361f7ae0508 | 33 | /* Initialize network */ |
erigow01 | 22:b361f7ae0508 | 34 | #ifdef DHCP |
erigow01 | 22:b361f7ae0508 | 35 | printf("DHCP in use\r\n"); |
erigow01 | 22:b361f7ae0508 | 36 | eth.init(); |
erigow01 | 22:b361f7ae0508 | 37 | #else |
erigow01 | 22:b361f7ae0508 | 38 | eth.init(IP, MASK, GW); |
erigow01 | 22:b361f7ae0508 | 39 | #endif |
erigow01 | 22:b361f7ae0508 | 40 | if(eth.connect(30000) == 0) |
erigow01 | 22:b361f7ae0508 | 41 | printf("Connect OK\n\r"); |
erigow01 | 22:b361f7ae0508 | 42 | printf("IP Address:%s ", eth.getIPAddress()); |
erigow01 | 22:b361f7ae0508 | 43 | } |
Kojto | 6:6eaae34586b8 | 44 | |
Kojto | 6:6eaae34586b8 | 45 | |
erigow01 | 18:eb1a194d60d9 | 46 | //Hard Fault Handler (Watchdog) |
erigow01 | 18:eb1a194d60d9 | 47 | extern "C" void HardFault_Handler() { |
erigow01 | 18:eb1a194d60d9 | 48 | printf("Hard Fault!\r\n"); |
erigow01 | 18:eb1a194d60d9 | 49 | NVIC_SystemReset(); |
erigow01 | 18:eb1a194d60d9 | 50 | } |
Kojto | 6:6eaae34586b8 | 51 | |
erigow01 | 15:59f4cee0da79 | 52 | // **************************************************************************** |
erigow01 | 26:4cac6b346e4f | 53 | // Socket initialization |
erigow01 | 26:4cac6b346e4f | 54 | UDPSocket server; |
erigow01 | 26:4cac6b346e4f | 55 | Endpoint nsp; |
erigow01 | 15:59f4cee0da79 | 56 | |
erigow01 | 15:59f4cee0da79 | 57 | |
erigow01 | 17:8ca4a5801430 | 58 | /* The number of seconds between NSDL Ticks*/ |
erigow01 | 27:6017a643f386 | 59 | #define NSDL_TICK_PERIOD_MS 1000 |
erigow01 | 17:8ca4a5801430 | 60 | |
erigow01 | 26:4cac6b346e4f | 61 | void main_event_loop() { |
erigow01 | 17:8ca4a5801430 | 62 | //For timing control |
erigow01 | 35:b0c1b676403a | 63 | // Timer nsdlTickTimer; |
erigow01 | 17:8ca4a5801430 | 64 | Timer registrationTimer; |
erigow01 | 38:9066ab490386 | 65 | #if HEART_BEAT |
erigow01 | 38:9066ab490386 | 66 | Timer heartBeatTimer; |
erigow01 | 38:9066ab490386 | 67 | #endif |
erigow01 | 17:8ca4a5801430 | 68 | |
erigow01 | 26:4cac6b346e4f | 69 | //Sensor timers... |
erigow01 | 27:6017a643f386 | 70 | #if NODE_SENSOR_STATION |
erigow01 | 26:4cac6b346e4f | 71 | Timer temperatureReportTimer; |
erigow01 | 26:4cac6b346e4f | 72 | Timer microphoneSampleTimer; |
erigow01 | 26:4cac6b346e4f | 73 | Timer microphoneReportTimer; |
erigow01 | 27:6017a643f386 | 74 | #if NODE_PIR_STATION |
erigow01 | 26:4cac6b346e4f | 75 | Timer motionReportTimer; |
erigow01 | 27:6017a643f386 | 76 | #endif //NODE PIR STATION |
erigow01 | 27:6017a643f386 | 77 | #if NODE_KIOSK_STATION |
erigow01 | 27:6017a643f386 | 78 | Timer kioskReportTimer; |
erigow01 | 27:6017a643f386 | 79 | #endif //NODE KIOSK STATION |
erigow01 | 27:6017a643f386 | 80 | #if NODE_DOOR_TRIP_STATION |
erigow01 | 27:6017a643f386 | 81 | Timer doorTripReportTimer; |
erigow01 | 27:6017a643f386 | 82 | #endif //NODE TRIP STATION |
erigow01 | 27:6017a643f386 | 83 | #if NODE_HEIGHT_STATION |
andcor02 | 30:b74aa0729b07 | 84 | Timer heightReportTimer; |
erigow01 | 27:6017a643f386 | 85 | #endif //NODE HEIGHT STATION |
erigow01 | 27:6017a643f386 | 86 | #endif //NODE_SENSOR_STATION |
erigow01 | 27:6017a643f386 | 87 | |
erigow01 | 17:8ca4a5801430 | 88 | //For recieving NSP messages |
erigow01 | 17:8ca4a5801430 | 89 | sn_nsdl_addr_s received_packet_address; |
erigow01 | 17:8ca4a5801430 | 90 | uint8_t received_address[4]; |
erigow01 | 17:8ca4a5801430 | 91 | int8_t nsdl_result = 0; |
erigow01 | 18:eb1a194d60d9 | 92 | char buffer[256]; |
erigow01 | 17:8ca4a5801430 | 93 | Endpoint from; |
erigow01 | 17:8ca4a5801430 | 94 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
erigow01 | 17:8ca4a5801430 | 95 | received_packet_address.addr_ptr = received_address; |
erigow01 | 32:c957a1948ac1 | 96 | server.set_blocking(false, 50); |
erigow01 | 17:8ca4a5801430 | 97 | |
erigow01 | 17:8ca4a5801430 | 98 | //Check incoming socket... |
erigow01 | 17:8ca4a5801430 | 99 | int n = 0; |
erigow01 | 17:8ca4a5801430 | 100 | int32_t time = 0; |
erigow01 | 26:4cac6b346e4f | 101 | |
erigow01 | 26:4cac6b346e4f | 102 | //Start Timers |
erigow01 | 27:6017a643f386 | 103 | |
erigow01 | 35:b0c1b676403a | 104 | // nsdlTickTimer.start(); |
erigow01 | 17:8ca4a5801430 | 105 | registrationTimer.start(); |
erigow01 | 38:9066ab490386 | 106 | #if HEART_BEAT |
erigow01 | 38:9066ab490386 | 107 | heartBeatTimer.start(); |
erigow01 | 38:9066ab490386 | 108 | #endif //Heartbeat |
erigow01 | 27:6017a643f386 | 109 | #if NODE_SENSOR_STATION |
erigow01 | 26:4cac6b346e4f | 110 | temperatureReportTimer.start(); |
erigow01 | 26:4cac6b346e4f | 111 | microphoneSampleTimer.start(); |
erigow01 | 26:4cac6b346e4f | 112 | microphoneReportTimer.start(); |
erigow01 | 27:6017a643f386 | 113 | #if NODE_PIR_STATION |
erigow01 | 27:6017a643f386 | 114 | motionReportTimer.start(); |
erigow01 | 27:6017a643f386 | 115 | #endif //NODE PIR STATION |
erigow01 | 27:6017a643f386 | 116 | #if NODE_KIOSK_STATION |
erigow01 | 27:6017a643f386 | 117 | kioskReportTimer.start(); |
erigow01 | 27:6017a643f386 | 118 | #endif //NODE KIOSK STATION |
erigow01 | 27:6017a643f386 | 119 | #if NODE_DOOR_TRIP_STATION |
erigow01 | 26:4cac6b346e4f | 120 | doorTripReportTimer.start(); |
erigow01 | 27:6017a643f386 | 121 | #endif //NODE TRIP STATION |
erigow01 | 27:6017a643f386 | 122 | #if NODE_HEIGHT_STATION |
andcor02 | 30:b74aa0729b07 | 123 | heightReportTimer.start(); |
erigow01 | 27:6017a643f386 | 124 | #endif //NODE HEIGHT STATION |
erigow01 | 27:6017a643f386 | 125 | #endif //NODE_SENSOR_STATION |
erigow01 | 38:9066ab490386 | 126 | DigitalOut led2(LED2); |
erigow01 | 38:9066ab490386 | 127 | //Watchdog wdt; |
erigow01 | 38:9066ab490386 | 128 | // wdt.kick(WATCHDOG_TIMEOUT_SEC); |
erigow01 | 38:9066ab490386 | 129 | NSP_registration(); |
erigow01 | 26:4cac6b346e4f | 130 | while(true) { |
erigow01 | 38:9066ab490386 | 131 | //Kick the watchdog... |
erigow01 | 38:9066ab490386 | 132 | // wdt.kick(); |
erigow01 | 26:4cac6b346e4f | 133 | //UDP Packet Receive... |
erigow01 | 17:8ca4a5801430 | 134 | n = server.receiveFrom(from, buffer, sizeof(buffer)); |
erigow01 | 27:6017a643f386 | 135 | if (n <= 0) { |
erigow01 | 17:8ca4a5801430 | 136 | //No Data |
erigow01 | 26:4cac6b346e4f | 137 | } else { |
erigow01 | 17:8ca4a5801430 | 138 | //UDP |
erigow01 | 17:8ca4a5801430 | 139 | //wait(0.25); //Waiting seems to increase reliability of comms... |
erigow01 | 17:8ca4a5801430 | 140 | printf("Received %d bytes\r\n", n); |
erigow01 | 17:8ca4a5801430 | 141 | nsdl_result = sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); |
erigow01 | 17:8ca4a5801430 | 142 | printf("Processed COAP Packet: %d\r\n", nsdl_result); |
erigow01 | 17:8ca4a5801430 | 143 | n = 0; |
erigow01 | 17:8ca4a5801430 | 144 | } |
erigow01 | 17:8ca4a5801430 | 145 | |
erigow01 | 17:8ca4a5801430 | 146 | //NSDL Tick |
erigow01 | 35:b0c1b676403a | 147 | // if(nsdlTickTimer.read_ms() >= NSDL_TICK_PERIOD_MS) { |
erigow01 | 35:b0c1b676403a | 148 | // sn_nsdl_exec(time); |
erigow01 | 35:b0c1b676403a | 149 | // nsdlTickTimer.reset(); |
erigow01 | 35:b0c1b676403a | 150 | // } |
erigow01 | 35:b0c1b676403a | 151 | // |
erigow01 | 17:8ca4a5801430 | 152 | //Registration Tick |
erigow01 | 27:6017a643f386 | 153 | if(registrationTimer.read_ms() >= RD_UPDATE_PERIOD_MS) { |
erigow01 | 26:4cac6b346e4f | 154 | printf("Time to register...\r\n"); |
erigow01 | 26:4cac6b346e4f | 155 | NSP_registration(); |
erigow01 | 17:8ca4a5801430 | 156 | registrationTimer.reset(); |
erigow01 | 17:8ca4a5801430 | 157 | } |
erigow01 | 38:9066ab490386 | 158 | #if HEART_BEAT |
erigow01 | 38:9066ab490386 | 159 | if (heartBeatTimer.read_ms() >= HEARTBEAT_PERIOD_MS){ |
erigow01 | 38:9066ab490386 | 160 | //Toggle heartbeat led... |
erigow01 | 38:9066ab490386 | 161 | led2 = !led2; |
erigow01 | 38:9066ab490386 | 162 | heartBeatTimer.reset(); |
erigow01 | 38:9066ab490386 | 163 | } |
erigow01 | 38:9066ab490386 | 164 | #endif //HEart Beat |
erigow01 | 26:4cac6b346e4f | 165 | #if NODE_SENSOR_STATION |
erigow01 | 27:6017a643f386 | 166 | if (temperatureReportTimer.read_ms() >= TEMPERATURE_REPORT_PERIOD_MS){ |
erigow01 | 26:4cac6b346e4f | 167 | //debug("Event: Temperature Report Timer\r\n"); |
erigow01 | 26:4cac6b346e4f | 168 | handle_temperature_report_timer(); |
erigow01 | 26:4cac6b346e4f | 169 | temperatureReportTimer.reset(); |
erigow01 | 26:4cac6b346e4f | 170 | } |
erigow01 | 27:6017a643f386 | 171 | if (microphoneSampleTimer.read_ms() >= SOUND_SAMPLE_PERIOD_MS){ |
erigow01 | 26:4cac6b346e4f | 172 | handle_microphone_sample_timer(); |
erigow01 | 26:4cac6b346e4f | 173 | microphoneSampleTimer.reset(); |
erigow01 | 26:4cac6b346e4f | 174 | } |
erigow01 | 27:6017a643f386 | 175 | if (microphoneReportTimer.read_ms() >= SOUND_REPORT_PERIOD_MS){ |
erigow01 | 26:4cac6b346e4f | 176 | //debug("Event: Sound Report Timer\r\n"); |
erigow01 | 26:4cac6b346e4f | 177 | handle_microphone_report_timer(); |
erigow01 | 26:4cac6b346e4f | 178 | microphoneReportTimer.reset(); |
erigow01 | 26:4cac6b346e4f | 179 | } |
erigow01 | 26:4cac6b346e4f | 180 | #if NODE_PIR_STATION |
erigow01 | 27:6017a643f386 | 181 | if (motionReportTimer.read_ms() >= MOTION_REPORT_PERIOD_MS){ |
erigow01 | 26:4cac6b346e4f | 182 | //debug("Event: Motion Report Timer\r\n"); |
erigow01 | 26:4cac6b346e4f | 183 | handle_motion_report_timer(); |
erigow01 | 26:4cac6b346e4f | 184 | motionReportTimer.reset(); |
erigow01 | 26:4cac6b346e4f | 185 | } |
erigow01 | 26:4cac6b346e4f | 186 | #endif //NODE PIR STATION |
erigow01 | 26:4cac6b346e4f | 187 | #if NODE_KIOSK_STATION |
erigow01 | 27:6017a643f386 | 188 | if (kioskReportTimer.read_ms() >= KIOSK_REPORT_PERIOD_MS){ |
erigow01 | 27:6017a643f386 | 189 | //debug("Event: Motion Report Timer\r\n"); |
erigow01 | 27:6017a643f386 | 190 | handle_kiosk_report_timer(); |
erigow01 | 27:6017a643f386 | 191 | kioskReportTimer.reset(); |
erigow01 | 27:6017a643f386 | 192 | } |
erigow01 | 26:4cac6b346e4f | 193 | #endif //NODE KIOSK STATION |
erigow01 | 26:4cac6b346e4f | 194 | #if NODE_DOOR_TRIP_STATION |
erigow01 | 27:6017a643f386 | 195 | if (doorTripReportTimer.read_ms() >= DOOR_TRIP_REPORT_PERIOD_MS){ |
erigow01 | 26:4cac6b346e4f | 196 | //debug("Event: Door Trip Report Timer\r\n"); |
erigow01 | 26:4cac6b346e4f | 197 | handle_door_trip_report_timer(); |
erigow01 | 26:4cac6b346e4f | 198 | doorTripReportTimer.reset(); |
erigow01 | 26:4cac6b346e4f | 199 | } |
erigow01 | 26:4cac6b346e4f | 200 | #endif //NODE TRIP STATION |
erigow01 | 26:4cac6b346e4f | 201 | #if NODE_HEIGHT_STATION |
andcor02 | 30:b74aa0729b07 | 202 | if (heightReportTimer.read_ms() >= DOOR_HEIGHT_PERIOD_MS ){ |
andcor02 | 30:b74aa0729b07 | 203 | handle_door_height_sample_timer(); |
andcor02 | 29:9599a156f78b | 204 | heightReportTimer.reset(); |
andcor02 | 29:9599a156f78b | 205 | } |
erigow01 | 26:4cac6b346e4f | 206 | #endif //NODE HEIGHT STATION |
erigow01 | 26:4cac6b346e4f | 207 | #endif //NODE_SENSOR_STATION |
erigow01 | 17:8ca4a5801430 | 208 | } |
erigow01 | 17:8ca4a5801430 | 209 | |
erigow01 | 17:8ca4a5801430 | 210 | |
erigow01 | 17:8ca4a5801430 | 211 | } |
erigow01 | 15:59f4cee0da79 | 212 | |
erigow01 | 15:59f4cee0da79 | 213 | |
andcor02 | 25:cb16c5248769 | 214 | |
Kojto | 8:be68e827aa53 | 215 | /** |
Kojto | 7:afaa17c11965 | 216 | * \param none |
Kojto | 6:6eaae34586b8 | 217 | * \return int |
Kojto | 6:6eaae34586b8 | 218 | */ |
Kojto | 6:6eaae34586b8 | 219 | int main() { |
erigow01 | 22:b361f7ae0508 | 220 | printf("Initialising Ethernet...\r\n"); |
erigow01 | 22:b361f7ae0508 | 221 | // Initialize Ethernet interface first |
erigow01 | 22:b361f7ae0508 | 222 | ethernet_init(); |
erigow01 | 26:4cac6b346e4f | 223 | printf("Initialising NSDL...\r\n"); |
erigow01 | 26:4cac6b346e4f | 224 | //Run NSDL... |
erigow01 | 26:4cac6b346e4f | 225 | nsdl_run(); |
erigow01 | 26:4cac6b346e4f | 226 | //Init Sensors |
erigow01 | 26:4cac6b346e4f | 227 | init_sensors(); |
erigow01 | 38:9066ab490386 | 228 | |
Kojto | 6:6eaae34586b8 | 229 | |
erigow01 | 26:4cac6b346e4f | 230 | // Run the event loop (never returns) |
erigow01 | 26:4cac6b346e4f | 231 | main_event_loop(); |
Kojto | 6:6eaae34586b8 | 232 | } |