rc-gateway/devices/water_sensor/water_sensor.ino
Nicu Hodos 89da9a80e8 re-organize:
- move code for all devices in dedicated folder
- move code for gateway in the root folder
2025-09-20 10:00:42 +02:00

52 lines
915 B
C++

#include <TinyPower.h>
#include <Tiny.h>
// Pins
#define SWITCH 0
#define SENDER 2
#define CONTROLLER 4
ContactSensor sensor = ContactSensor(WATER_SENSOR, SENDER);
int counter = 0;
void setup() {
pinMode(SWITCH, INPUT_PULLUP);
pinMode(CONTROLLER, OUTPUT);
digitalWrite(CONTROLLER, HIGH);
sensor.setup();
sensor.sendStateAndVoltage(readState());
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
}
void loop() {
TinyPower::sleep();
}
bool readState() {
digitalWrite(CONTROLLER, LOW);
bool state = digitalRead(SWITCH);
digitalWrite(CONTROLLER, HIGH);
return state;
}
ISR(PCINT0_vect) {
sensor.sendStateAndVoltage(readState());
delay(5000);
sensor.sendStateAndVoltage(readState());
}
ISR(WDT_vect) {
counter++;
if (counter % 220 == 0) {
counter = 0;
sensor.sendStateAndVoltage(readState());
delay(10000);
sensor.sendStateAndVoltage(readState());
}
}