2016-02-01 17:52:17 +01:00
|
|
|
/*
|
|
|
|
|
A interrupt based sensor device that reads multiple sensors and transmits
|
|
|
|
|
the data to a central location.
|
|
|
|
|
*/
|
2016-02-24 21:51:16 +01:00
|
|
|
#include <Arduino.h>
|
2016-02-01 17:52:17 +01:00
|
|
|
|
2016-02-24 21:51:16 +01:00
|
|
|
#include "HalConfiguration.h"
|
2016-05-09 17:49:24 +02:00
|
|
|
#include "HalInterfaces.h"
|
|
|
|
|
#include "HalInclude.h"
|
2016-02-24 21:51:16 +01:00
|
|
|
#include "Interrupt.h"
|
|
|
|
|
|
|
|
|
|
|
2016-05-09 17:49:24 +02:00
|
|
|
#define TIMER_MULTIPLIER_MAX \
|
|
|
|
|
POWER_TIMER_MULTIPLIER * TEMPERATURE_TIMER_MULTIPLIER * LIGHT_TIMER_MULTIPLIER
|
2016-02-24 14:37:48 +01:00
|
|
|
unsigned int timerMultiplier = 0;
|
|
|
|
|
|
|
|
|
|
// Sensors
|
2016-05-02 10:56:15 +02:00
|
|
|
SensorPowerConsumption* powerSensor;
|
|
|
|
|
SensorTemperature* tempSensor;
|
|
|
|
|
SensorLight* lightSensor;
|
2016-02-24 14:37:48 +01:00
|
|
|
|
|
|
|
|
// Protocols
|
2016-02-24 21:51:16 +01:00
|
|
|
ProtocolPowerConsumption* powerProtocol;
|
|
|
|
|
ProtocolTemperature* tempProtocol;
|
|
|
|
|
ProtocolLight* lightProtocol;
|
2016-02-24 14:37:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-24 21:51:16 +01:00
|
|
|
void timerInterruptFunc();
|
2016-02-24 15:31:51 +01:00
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
2016-05-09 17:49:24 +02:00
|
|
|
#ifdef ENABLE_DEBUG
|
|
|
|
|
Serial.begin(9600);
|
|
|
|
|
#endif
|
2016-05-10 16:04:39 +02:00
|
|
|
pinMode(INDICATOR_PIN, OUTPUT);
|
2016-05-09 17:49:24 +02:00
|
|
|
|
2016-02-24 15:31:51 +01:00
|
|
|
// Setup Sensors and protocols
|
|
|
|
|
#ifdef POWERCON_ENABLED
|
2016-05-09 17:49:24 +02:00
|
|
|
DEBUG("Setup POWERCON_SENSOR");
|
2016-05-02 10:56:15 +02:00
|
|
|
powerSensor = new POWERCON_SENSOR;
|
2016-02-24 21:51:16 +01:00
|
|
|
powerSensor->setup();
|
|
|
|
|
powerProtocol = new POWERCON_PROTOCOL;
|
|
|
|
|
powerProtocol->setup();
|
2016-02-24 15:31:51 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef TEMPERATURE_ENABLED
|
2016-05-09 17:49:24 +02:00
|
|
|
DEBUG("Setup TEMPERATURE_SENSOR");
|
2016-05-02 10:56:15 +02:00
|
|
|
tempSensor = new TEMPERATURE_SENSOR;
|
2016-02-24 21:51:16 +01:00
|
|
|
tempSensor->setup();
|
|
|
|
|
tempProtocol = new TEMPERATURE_PROTOCOL;
|
|
|
|
|
tempProtocol->setup();
|
2016-02-24 15:31:51 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef LIGHT_ENABLED
|
2016-05-09 17:49:24 +02:00
|
|
|
DEBUG("Setup LIGHT_SENSOR");
|
2016-05-02 10:56:15 +02:00
|
|
|
lightSensor = new LIGHT_SENSOR;
|
2016-02-24 21:51:16 +01:00
|
|
|
lightSensor->setup();
|
|
|
|
|
lightProtocol = new LIGHT_PROTOCOL;
|
|
|
|
|
lightProtocol->setup();
|
2016-02-24 15:31:51 +01:00
|
|
|
#endif
|
2016-05-09 17:49:24 +02:00
|
|
|
|
2016-05-31 21:11:53 +02:00
|
|
|
DEBUG("Setup SLEEP_INTERRUPT");
|
2016-05-26 16:35:22 +02:00
|
|
|
Interrupt::setWatchDogCallback(timerInterruptFunc);
|
2016-05-09 17:49:24 +02:00
|
|
|
Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt
|
|
|
|
|
|
2016-05-10 16:04:39 +02:00
|
|
|
|
|
|
|
|
pulse(INDICATOR_PIN, 3);
|
2016-05-09 17:49:24 +02:00
|
|
|
DEBUG("Ready");
|
2016-02-24 15:31:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-24 21:51:16 +01:00
|
|
|
void timerInterruptFunc()
|
2016-02-11 16:31:03 +01:00
|
|
|
{
|
2016-02-24 14:37:48 +01:00
|
|
|
++timerMultiplier;
|
2016-05-09 17:49:24 +02:00
|
|
|
if (timerMultiplier > TIMER_MULTIPLIER_MAX)
|
2016-02-24 14:37:48 +01:00
|
|
|
timerMultiplier = 1;
|
2016-05-02 16:39:39 +02:00
|
|
|
}
|
2016-02-01 17:52:17 +01:00
|
|
|
|
2016-05-02 16:39:39 +02:00
|
|
|
void loop()
|
|
|
|
|
{
|
2016-05-10 16:04:39 +02:00
|
|
|
digitalWrite(INDICATOR_PIN, HIGH);
|
2016-05-09 17:49:24 +02:00
|
|
|
|
2016-02-24 14:37:48 +01:00
|
|
|
// Send power consumption
|
2016-02-24 15:31:51 +01:00
|
|
|
#ifdef POWERCON_ENABLED
|
2016-06-10 21:50:59 +02:00
|
|
|
if(timerMultiplier % POWER_TIMER_MULTIPLIER == 0)
|
2016-02-24 15:31:51 +01:00
|
|
|
{
|
2016-05-10 16:04:39 +02:00
|
|
|
static PowerData powerData;
|
|
|
|
|
powerSensor->read(powerData); // not needed, only here for future use
|
|
|
|
|
DEBUGF("Read POWERCON_SENSOR= consumption:%d", powerData.consumption);
|
|
|
|
|
powerProtocol->send(powerData);
|
2016-02-24 15:31:51 +01:00
|
|
|
}
|
2016-02-24 14:37:48 +01:00
|
|
|
#endif
|
2016-02-01 17:52:17 +01:00
|
|
|
|
2016-02-24 14:37:48 +01:00
|
|
|
// Handle temperature sensor
|
2016-02-24 15:31:51 +01:00
|
|
|
#ifdef TEMPERATURE_ENABLED
|
2016-06-10 21:50:59 +02:00
|
|
|
if(timerMultiplier % TEMPERATURE_TIMER_MULTIPLIER == 0)
|
2016-02-24 14:37:48 +01:00
|
|
|
{
|
2016-05-10 16:04:39 +02:00
|
|
|
static TemperatureData tempData;
|
|
|
|
|
tempSensor->read(tempData);
|
2016-06-10 19:33:26 +02:00
|
|
|
DEBUGF("Read TEMPERATURE_SENSOR= temperature:%d, humidity:%d", (int)tempData.temperature, (int)tempData.humidity);
|
2016-05-10 16:04:39 +02:00
|
|
|
tempProtocol->send(tempData);
|
2016-02-24 14:37:48 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Handle light sensor
|
2016-05-09 17:49:24 +02:00
|
|
|
#ifdef LIGHT_ENABLED
|
2016-06-10 21:50:59 +02:00
|
|
|
if(timerMultiplier % LIGHT_TIMER_MULTIPLIER == 0)
|
2016-02-24 14:37:48 +01:00
|
|
|
{
|
2016-05-10 16:04:39 +02:00
|
|
|
static LightData lightData;
|
|
|
|
|
lightSensor->read(lightData);
|
|
|
|
|
DEBUG("Read LIGHT_SENSOR= lumen:%d", lightData.lumen);
|
|
|
|
|
lightProtocol->send(lightData);
|
2016-02-24 14:37:48 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
2016-02-01 17:52:17 +01:00
|
|
|
|
2016-05-10 16:04:39 +02:00
|
|
|
digitalWrite(INDICATOR_PIN, LOW);
|
2016-05-09 17:49:24 +02:00
|
|
|
|
|
|
|
|
DEBUG("Sleeping");
|
|
|
|
|
Interrupt::sleep();
|
|
|
|
|
DEBUG("Wakeup");
|
2016-02-01 17:52:17 +01:00
|
|
|
}
|
|
|
|
|
|