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

sensor_ctl.cpp

Committer:
andcor02
Date:
2014-12-12
Revision:
33:d39c30e9264b
Parent:
32:c957a1948ac1
Child:
40:b2e9bc654ca1

File content as of revision 33:d39c30e9264b:

/** Implements Sensor Control for CES Instrumented Booth */

#include "mbed.h"
#include "sensor_ctl.h"
#include "node_cfg.h"

//Sensor Drivers
#include "RHT03.h"
#include "MAX9814.h"
#include "sonar.h"
#include "Presence.h"
#include "SHARPIR.h"

//Sensor MDS Resources
#include "door_trip.h"
#include "height.h"
#include "presence_resource.h"
#include "sound_level.h"
#include "temperature.h"

//Common Sensors
#if NODE_SENSOR_STATION
RHT03 temperature(PTB2);
MAX9814 microphone(PTB3); //Analogue in required.
#if NODE_PIR_STATION
Presence pir(PTB11, false, PIR_SENSOR_DEBOUNCE_MS); //(InterruptPin), for PIR sensor, 
#endif //NODE PIR STATION
#if NODE_KIOSK_STATION
Presence kiosk(PTB10, true, KIOSK_SENSOR_DEBOUNCE_MS); //(Interrupt pinrequired, no timeout)
#endif //NODE KIOSK STATION
#if NODE_DOOR_TRIP_STATION
SHARPIR sharpir(PTB10); //(AnalogIn required), for IR door trip
#endif //NODE TRIP STATION
#if NODE_HEIGHT_STATION
Timer sonarTimer;
Timer debounceHeight;
Sonar sonar(PTC10, sonarTimer);     //(AnalogIn required, Leave as SW2.)

#define DOOR_HEIGHT_START_MEASURING_THRESHOLD_CM    150
#define DOOR_HEIGHT_STOP_MEASURING_THRESHOLD_CM     140
#define DOOR_HEIGHT_STOP_MEASURING_SAMPLE_COUNT       2
#define DOOR_HEIGHT_SENSOR_MOUNT_HEIGHT_CM          220
#endif //NODE HEIGHT STATION
#endif //NODE_SENSOR_STATION


//Variables provided to rest of applications
float    current_temperature_value = 0;
float  current_ambient_noise_value = 0;
//Either height XOR kiosk presence XOR PIR station...
float    current_height_value = 0;
bool     current_presence_value = false;         //Either from Kiosk or PIR
//And it might have a door trip..
bool     current_door_trip_value = false;

//Door trip...
float door_trip_starting_volts = 0;

//Door Height...
float door_height_max_value = 0;
bool door_height_measuring = 0;
int door_height_sample_count = 0;
    
    
//Initialisation
void init_sensors() {
    #if NODE_DOOR_TRIP_STATION
    door_trip_starting_volts = sharpir.volt();
    #endif
    #if NODE_HEIGHT_STATION
    sonarTimer.start();
    #endif
}

#if NODE_SENSOR_STATION

//timer handler functions
void handle_temperature_report_timer() {
    if(temperature.readData() == RHT_ERROR_NONE) { 
        //Only report valid data...
        current_temperature_value = temperature.getTemperatureC();
        printf("Temperature Sample: %2.2f\r\n", current_temperature_value);
        temperature_report();
    } else {
        printf("Temperature Sampleing Failure\r\n");
    }
}

void handle_microphone_sample_timer()
{
    float sample = microphone.sound_level();
    //printf("Sound Sample: %2.2f\r\n", sample);
    if (sample > current_ambient_noise_value){
        current_ambient_noise_value = sample;
    }
}

void handle_microphone_report_timer()
{
    //Report.
    sound_level_report();
    //Reset noise...
    current_ambient_noise_value = 0;
}

#if NODE_PIR_STATION
void handle_motion_report_timer(){
    bool new_pir = pir.isPresent();
    //printf("PIR Sample: %d\r\n", new_pir);
    //printf("Old PIR Sample: %d\r\n", current_presence_value);
    if(new_pir != current_presence_value) {
        //printf("Reporting PIR...\r\n");
        current_presence_value = new_pir;
        presence_report();
    }
}
#endif //NODE_PIR_STATION

#if NODE_KIOSK_STATION
void handle_kiosk_report_timer(){
    bool new_kiosk = kiosk.isPresent();
    if(new_kiosk != current_presence_value) {
        current_presence_value = new_kiosk;
        presence_report();
    }
}
#endif //NODE_KIOSK_STATION

#if NODE_DOOR_TRIP_STATION
void handle_door_trip_report_timer(){
    float value= sharpir.volt();
    printf ("\n\r door:%f", value);
    bool new_door_trip = 0;
    if (value>door_trip_starting_volts+0.2) {
        new_door_trip=true;
    } else if (value<door_trip_starting_volts+0.2) {
        new_door_trip=false;
    }
    
    if (new_door_trip != current_door_trip_value) {
        current_door_trip_value = new_door_trip;
        door_trip_report();
    }
}
#endif //NODE_DOOR_TRIP_STATION

#if NODE_HEIGHT_STATION
void handle_door_height_sample_timer(){
    int height_sample = DOOR_HEIGHT_SENSOR_MOUNT_HEIGHT_CM-sonar.read();
    printf("\n\r %d", height_sample);
    if(height_sample > DOOR_HEIGHT_START_MEASURING_THRESHOLD_CM) {
        door_height_sample_count=0;
        if (height_sample > door_height_max_value) {
            door_height_max_value = height_sample;
        }
        door_height_measuring=true;
    }

    if((height_sample < DOOR_HEIGHT_STOP_MEASURING_THRESHOLD_CM && door_height_measuring )){
            door_height_sample_count++;
            if (door_height_sample_count > DOOR_HEIGHT_STOP_MEASURING_SAMPLE_COUNT){
            current_height_value=door_height_max_value;
            door_height_max_value=0,door_height_measuring=false;
            door_height_sample_count = 0;
            height_report();  
        }
    }
}
#endif //NODE HEIGHT STATION

#endif //NODE_SENSOR_STATION