Files
MyOutdoorSensor01/MyOutdoorSensor01.ino
2018-03-16 22:21:29 +01:00

125 lines
2.7 KiB
Arduino

#include <nodemanager_config.h>
#include <MySensors.h>
#include <NodeManager.h>
#define SENSOR_CHECK_INTERVAL 2000
#define MSG_SEND_CYCLES 0
#define PIN_DHT 4
#define PIN_LDR A0
#define PIN_BAT A1
#define PIN_MOSFET 8
#define SENSOR_ID_DHT_TEMP 0
#define SENSOR_ID_DHT_HUM 1
#define SENSOR_ID_LDR 2
#define SENSOR_ID_VCC 210
// For convenience
typedef SensorDHT<DHT22, PIN_DHT, SENSOR_ID_DHT_TEMP, SENSOR_ID_DHT_HUM, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES> MySensorDHT;
// create a NodeManager instance
typedef NodeManager<
Tuple<
MySensorDHT,
SensorLDR<PIN_LDR, SENSOR_ID_LDR, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES>,
SensorVcc<SENSOR_ID_VCC, SENSOR_CHECK_INTERVAL, MSG_SEND_CYCLES>
>
> MyNodeManagerBase;
class MyNodeManager : public MyNodeManagerBase {
protected:
virtual void _power_on_before() override {
if (pin_backup_valid) {
// Restore pin state
DDRB = ddrb_backup;
PORTB = portb_backup;
DDRC = ddrc_backup;
PORTC = portc_backup;
DDRD = ddrd_backup;
PORTD = portd_backup;
}
}
virtual void _power_on_after() override {
// Enable NRF24
power_spi_enable();
transportInit();
}
virtual void _power_off_after() override {
power_spi_disable();
// Set all pins as INPUTS and disable pull-ups to avoid current leakage
ddrb_backup = DDRB;
portb_backup = PORTB;
DDRB = 0x00;
PORTB = 0x00;
ddrc_backup = DDRC;
portc_backup = PORTC;
DDRC = 0x00;
PORTC = 0x00;
ddrd_backup = DDRD;
portd_backup = PORTD;
DDRD = 0x00;
PORTD = 0x00;
pin_backup_valid = true;
}
virtual void _sleep_before() override {
powerOff();
}
virtual void _sleep_after() override {
powerOn();
wdt_enable(WDTO_2S);
WDTCSR &= ~_BV(WDIE); /* Disable the WD interrupt. */
}
uint8_t ddrb_backup, ddrc_backup, ddrd_backup;
uint8_t portb_backup, portc_backup, portd_backup;
bool pin_backup_valid = false;
} nodeManager;
void before() {
nodeManager.setSleep(POWER_DOWN_WDT, 5, MINUTES);
nodeManager.setPowerPins(PIN_MOSFET, -1, 0);
nodeManager.setAutoPowerPins(false);
nodeManager.setBatteryInternalVcc(false);
nodeManager.setBatteryPin(PIN_BAT);
nodeManager.setBatteryVoltsPerBit(5.0 / 1023.0);
nodeManager.setBatteryReportCycles(0);
nodeManager.setBatteryMax(4.1);
nodeManager.setBatteryMin(3.4);
nodeManager.before();
}
void presentation() {
nodeManager.presentation();
}
void receive(const MyMessage &message) {
nodeManager.receive(message);
}
void receiveTime(unsigned long ts) {
nodeManager.receiveTime(ts);
}
void setup() {
ACSR &= (1<<7); // Disable the AC
DIDR0 = 0xFF; // We don't use need digital input from the analog pins, so disable the digital input buffer
//DIDR1 = 0x3; // The same for the AC pins?
nodeManager.setup();
}
void loop() {
nodeManager.loop();
}