125 lines
3 KiB
C++
Executable file
125 lines
3 KiB
C++
Executable file
/*
|
|
A interrupt based sensor device that reads multiple sensors and transmits
|
|
the data to a central location.
|
|
*/
|
|
#include <Arduino.h>
|
|
|
|
#include "HalConfiguration.h"
|
|
#include "HalInterfaces.h"
|
|
#include "HalInclude.h"
|
|
#include "Interrupt.h"
|
|
|
|
|
|
#define TIMER_MULTIPLIER_MAX \
|
|
POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER
|
|
unsigned int timerMultiplier = 0;
|
|
|
|
// Sensors
|
|
SensorPowerConsumption* powerSensor;
|
|
SensorTemperature* tempSensor;
|
|
SensorLight* lightSensor;
|
|
|
|
// Protocols
|
|
ProtocolPowerConsumption* powerProtocol;
|
|
ProtocolTemperature* tempProtocol;
|
|
ProtocolLight* lightProtocol;
|
|
|
|
|
|
|
|
void timerInterruptFunc();
|
|
|
|
void setup()
|
|
{
|
|
#ifdef ENABLE_DEBUG
|
|
Serial.begin(9600);
|
|
#endif
|
|
pinMode(INDICATOR_PIN, OUTPUT);
|
|
|
|
// Setup Sensors and protocols
|
|
#ifdef POWERCON_ENABLED
|
|
DEBUG("Setup POWERCON_SENSOR");
|
|
powerSensor = new POWERCON_SENSOR;
|
|
powerSensor->setup();
|
|
powerProtocol = new POWERCON_PROTOCOL;
|
|
powerProtocol->setup();
|
|
#endif
|
|
|
|
#ifdef TEMPERATURE_ENABLED
|
|
DEBUG("Setup TEMPERATURE_SENSOR");
|
|
tempSensor = new TEMPERATURE_SENSOR;
|
|
tempSensor->setup();
|
|
tempProtocol = new TEMPERATURE_PROTOCOL;
|
|
tempProtocol->setup();
|
|
#endif
|
|
|
|
#ifdef LIGHT_ENABLED
|
|
DEBUG("Setup LIGHT_SENSOR");
|
|
lightSensor = new LIGHT_SENSOR;
|
|
lightSensor->setup();
|
|
lightProtocol = new LIGHT_PROTOCOL;
|
|
lightProtocol->setup();
|
|
#endif
|
|
|
|
DEBUG("Setup INTERRUPT");
|
|
Interrupt::setWatchDogCallback(timerInterruptFunc);
|
|
Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt
|
|
|
|
|
|
pulse(INDICATOR_PIN, 3);
|
|
DEBUG("Ready");
|
|
}
|
|
|
|
|
|
void timerInterruptFunc()
|
|
{
|
|
++timerMultiplier;
|
|
if (timerMultiplier > TIMER_MULTIPLIER_MAX)
|
|
timerMultiplier = 1;
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
digitalWrite(INDICATOR_PIN, HIGH);
|
|
noInterrupts();
|
|
|
|
// Send power consumption
|
|
#ifdef POWERCON_ENABLED
|
|
if(timerMultiplier == POWER_TIMER_MULTIPLIER)
|
|
{
|
|
static PowerData powerData;
|
|
powerSensor->read(powerData); // not needed, only here for future use
|
|
DEBUGF("Read POWERCON_SENSOR= consumption:%d", powerData.consumption);
|
|
powerProtocol->send(powerData);
|
|
}
|
|
#endif
|
|
|
|
// Handle temperature sensor
|
|
#ifdef TEMPERATURE_ENABLED
|
|
if(timerMultiplier == TEMPERATURE_TIMER_MULTIPLIER)
|
|
{
|
|
static TemperatureData tempData;
|
|
tempSensor->read(tempData);
|
|
DEBUGF("Read TEMPERATURE_SENSOR= temperature:%d, humidity:%d", tempData.temperature, tempData.humidity);
|
|
tempProtocol->send(tempData);
|
|
}
|
|
#endif
|
|
|
|
// Handle light sensor
|
|
#ifdef LIGHT_ENABLED
|
|
if(timerMultiplier == LIGHT_TIMER_MULTIPLIER)
|
|
{
|
|
static LightData lightData;
|
|
lightSensor->read(lightData);
|
|
DEBUG("Read LIGHT_SENSOR= lumen:%d", lightData.lumen);
|
|
lightProtocol->send(lightData);
|
|
}
|
|
#endif
|
|
|
|
interrupts();
|
|
digitalWrite(INDICATOR_PIN, LOW);
|
|
|
|
DEBUG("Sleeping");
|
|
Interrupt::sleep();
|
|
DEBUG("Wakeup");
|
|
}
|
|
|