150 lines
3.9 KiB
Arduino
150 lines
3.9 KiB
Arduino
|
|
#include <nodemanager_config.h>
|
|
#include <MySensors.h>
|
|
#include <NodeManager.h>
|
|
|
|
|
|
#define SENSOR_CHECK_INTERVAL 4000
|
|
#define MSG_SEND_CYCLES 15
|
|
#define MSG_SEND_INTERVAL 60000
|
|
|
|
#define PIN_DHT 8
|
|
#define PIN_LDR A7
|
|
#define PIN_MOTION 2
|
|
#define PIN_DOOR0 7
|
|
#define PIN_DOOR1 6
|
|
#define PIN_DOOR2 5
|
|
#define PIN_DOOR3 4
|
|
#define PIN_BACKLIGHT A0
|
|
|
|
#define SENSOR_ID_DHT_TEMP 0
|
|
#define SENSOR_ID_DHT_HUM 1
|
|
#define SENSOR_ID_BMP_TEMP 2
|
|
#define SENSOR_ID_BMP_BARO 3
|
|
#define SENSOR_ID_LDR 4
|
|
#define SENSOR_ID_MQ 5
|
|
//#define SENSOR_ID_SOUND 6
|
|
#define SENSOR_ID_MOTION 7
|
|
#define SENSOR_ID_DOOR0 8
|
|
#define SENSOR_ID_DOOR1 9
|
|
#define SENSOR_ID_DOOR2 10
|
|
#define SENSOR_ID_DOOR3 11
|
|
#define SENSOR_ID_BACKLIGHT 100
|
|
#define SENSOR_ID_VCC 210
|
|
|
|
#define ADS_CHANNEL_MQ 0
|
|
#define ADS_CHANNEL_SOUND 1
|
|
|
|
#define I2C_ADDR_ADS 0x48
|
|
#define I2C_ADDR_BMP 0x76
|
|
|
|
#define DIN_DEBOUNCE 6
|
|
|
|
#define MQ_RL 1000
|
|
#define MQ_EEPROM_BASE 0
|
|
|
|
|
|
|
|
// We need to subclass here to enable temperature/humidity compensation
|
|
template<
|
|
uint8_t _tchannel,
|
|
uint8_t _tsensorId,
|
|
unsigned _trl = 1000,
|
|
unsigned _teepromBase = 0,
|
|
unsigned long _checkInterval = 2000,
|
|
unsigned long _msgSendTimeout = 30
|
|
> class CustomADS1115ChannelMQ135Module : public ADS1115ChannelMQ135Module<_tchannel, _tsensorId, _trl, _teepromBase, _checkInterval, _msgSendTimeout> {
|
|
public:
|
|
virtual bool getTempAndHum(float& temp, float& hum) override;
|
|
};
|
|
|
|
|
|
// For convenience
|
|
|
|
typedef SensorDHT<DHT22, PIN_DHT, SENSOR_ID_DHT_TEMP, SENSOR_ID_DHT_HUM, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES> MySensorDHT;
|
|
|
|
typedef SensorADS1115<
|
|
Tuple<
|
|
CustomADS1115ChannelMQ135Module<ADS_CHANNEL_MQ, SENSOR_ID_MQ, MQ_RL, MQ_EEPROM_BASE, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES>
|
|
>, I2C_ADDR_ADS
|
|
> MySensorADS1115;
|
|
|
|
|
|
|
|
// create a NodeManager instance
|
|
typedef NodeManager<
|
|
Tuple<
|
|
MySensorDHT,
|
|
SensorBMP280<I2C_ADDR_BMP, SENSOR_ID_BMP_TEMP, SENSOR_ID_BMP_BARO, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES>,
|
|
SensorLDR<PIN_LDR, SENSOR_ID_LDR, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES>,
|
|
MySensorADS1115,
|
|
SensorMotion<PIN_MOTION, SENSOR_ID_MOTION, INPUT_PULLUP, DIN_DEBOUNCE, MSG_SEND_INTERVAL>,
|
|
SensorDoor<PIN_DOOR0, SENSOR_ID_DOOR0, INPUT_PULLUP, DIN_DEBOUNCE, MSG_SEND_INTERVAL>,
|
|
SensorDoor<PIN_DOOR1, SENSOR_ID_DOOR1, INPUT_PULLUP, DIN_DEBOUNCE, MSG_SEND_INTERVAL>,
|
|
SensorDoor<PIN_DOOR2, SENSOR_ID_DOOR2, INPUT_PULLUP, DIN_DEBOUNCE, MSG_SEND_INTERVAL>,
|
|
SensorDoor<PIN_DOOR3, SENSOR_ID_DOOR3, INPUT_PULLUP, DIN_DEBOUNCE, MSG_SEND_INTERVAL>,
|
|
SwitchBacklight<PIN_BACKLIGHT, SENSOR_ID_BACKLIGHT, MSG_SEND_INTERVAL>,
|
|
SensorVcc<SENSOR_ID_VCC, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES>
|
|
>
|
|
> MyNodeManagerBase;
|
|
|
|
|
|
class MyNodeManager : public MyNodeManagerBase {
|
|
protected:
|
|
virtual void processCommand(const char * message) override {
|
|
if (strcmp_P(message, PSTR("MQ_CAL")) == 0) {
|
|
getSensor<3, MySensorADS1115>().calibrateSubmodule<0>();
|
|
} else {
|
|
MyNodeManagerBase::processCommand(message);
|
|
}
|
|
}
|
|
} nodeManager;
|
|
|
|
|
|
|
|
template<
|
|
uint8_t _tchannel,
|
|
uint8_t _tsensorId,
|
|
unsigned _trl,
|
|
unsigned _teepromBase,
|
|
unsigned long _checkInterval,
|
|
unsigned long _msgSendTimeout
|
|
> bool CustomADS1115ChannelMQ135Module<_tchannel, _tsensorId, _trl, _teepromBase, _checkInterval, _msgSendTimeout>::getTempAndHum(float& temp, float& hum) {
|
|
auto& dht = nodeManager.getSensor<0, MySensorDHT>();
|
|
temp = dht.getTemperature();
|
|
hum = dht.getHumidity();
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
void before() {
|
|
//nodeManager.setSleep(ALWAYS_ON, 2, SECONDS);
|
|
nodeManager.before();
|
|
}
|
|
|
|
|
|
void setup() {
|
|
nodeManager.setup();
|
|
}
|
|
|
|
|
|
void presentation() {
|
|
nodeManager.presentation();
|
|
}
|
|
|
|
|
|
void receive(const MyMessage &message) {
|
|
nodeManager.receive(message);
|
|
}
|
|
|
|
|
|
void receiveTime(unsigned long ts) {
|
|
nodeManager.receiveTime(ts);
|
|
}
|
|
|
|
|
|
void loop() {
|
|
nodeManager.loop();
|
|
}
|