Feature: do output action on input trigger
This commit is contained in:
parent
b5c3a49513
commit
782750a1e4
26
src/main.cpp
26
src/main.cpp
|
@ -17,6 +17,7 @@ const char *postboxFlapTopic = "postboxFlap";
|
||||||
const char *postboxDoorTopic = "postboxDoor";
|
const char *postboxDoorTopic = "postboxDoor";
|
||||||
const char *doorBellTriggerSoundTopic = "doorBellTrigger";
|
const char *doorBellTriggerSoundTopic = "doorBellTrigger";
|
||||||
const char *postboxLightTopic = "postboxLight";
|
const char *postboxLightTopic = "postboxLight";
|
||||||
|
const char *doorLightRequestTopic = "doorLightRequest";
|
||||||
|
|
||||||
EthernetClient ethClient;
|
EthernetClient ethClient;
|
||||||
EthernetServer server(80);
|
EthernetServer server(80);
|
||||||
|
@ -24,21 +25,26 @@ PubSubClient mqttClient(ethClient);
|
||||||
#define DOOR_BELL_BUTTON_PIN 7
|
#define DOOR_BELL_BUTTON_PIN 7
|
||||||
#define POSTBOX_FLAP_PIN 5
|
#define POSTBOX_FLAP_PIN 5
|
||||||
#define POSTBOX_DOOR_PIN 3
|
#define POSTBOX_DOOR_PIN 3
|
||||||
|
#define LIGHT_REQUEST_BUTTON_PIN 2
|
||||||
// Output pins
|
// Output pins
|
||||||
#define LED_PIN 13
|
#define LED_PIN 13
|
||||||
#define DOOR_BELL_BUZZER_PIN 6
|
#define DOOR_BELL_BUZZER_PIN 6
|
||||||
#define POSTBOX_LIGHT_PIN 4
|
#define POSTBOX_LIGHT_PIN 4
|
||||||
|
|
||||||
|
#define OUTPUT_NONE 0xff
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
const uint8_t pin;
|
const uint8_t pin;
|
||||||
const unsigned long quietAfterTriggerFor_ms;
|
const unsigned long quietAfterTriggerFor_ms;
|
||||||
unsigned long lastTrigger_ts;
|
unsigned long lastTrigger_ts;
|
||||||
bool isTriggered;
|
bool isTriggered;
|
||||||
const char *topic;
|
const char *topic;
|
||||||
|
const uint8_t triggerOutputPin;
|
||||||
} myInputs[] = {
|
} myInputs[] = {
|
||||||
{ DOOR_BELL_BUTTON_PIN, 500, 0 , false, doorBellTopic },
|
{ DOOR_BELL_BUTTON_PIN, 500, 0 , false, doorBellTopic, DOOR_BELL_BUZZER_PIN },
|
||||||
{ POSTBOX_FLAP_PIN, 1000, 0, false, postboxFlapTopic },
|
{ LIGHT_REQUEST_BUTTON_PIN, 500, 0 , false, doorLightRequestTopic, POSTBOX_LIGHT_PIN },
|
||||||
{ POSTBOX_DOOR_PIN, 1000, 0, false, postboxDoorTopic }
|
{ POSTBOX_FLAP_PIN, 1000, 0, false, postboxFlapTopic, OUTPUT_NONE },
|
||||||
|
{ POSTBOX_DOOR_PIN, 1000, 0, false, postboxDoorTopic, OUTPUT_NONE }
|
||||||
};
|
};
|
||||||
#define NUM_INPUTS (sizeof(myInputs) / sizeof(myInputs[0]))
|
#define NUM_INPUTS (sizeof(myInputs) / sizeof(myInputs[0]))
|
||||||
int ledState = LOW;
|
int ledState = LOW;
|
||||||
|
@ -55,6 +61,15 @@ struct {
|
||||||
};
|
};
|
||||||
#define NUM_OUTPUTS (sizeof(myOutputs) / sizeof(myOutputs[0]))
|
#define NUM_OUTPUTS (sizeof(myOutputs) / sizeof(myOutputs[0]))
|
||||||
|
|
||||||
|
uint8_t getOutputIndexByPin(uint8_t pin) {
|
||||||
|
for (unsigned int i=0; i<NUM_OUTPUTS; ++i) {
|
||||||
|
if (myOutputs[i].pin == pin) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.print(F("ERROR: Could not find output by pin")); Serial.println(pin);
|
||||||
|
return OUTPUT_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
void switchOutputOn(unsigned int outputNumber) {
|
void switchOutputOn(unsigned int outputNumber) {
|
||||||
Serial.print(F("Switch output on: "));
|
Serial.print(F("Switch output on: "));
|
||||||
|
@ -196,7 +211,7 @@ void setup()
|
||||||
pinMode(myInputs[i].pin, INPUT_PULLUP);
|
pinMode(myInputs[i].pin, INPUT_PULLUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the LED :
|
// Setup the output pins :
|
||||||
for (unsigned int i=0; i < NUM_OUTPUTS; ++i) {
|
for (unsigned int i=0; i < NUM_OUTPUTS; ++i) {
|
||||||
pinMode(myOutputs[i].pin, OUTPUT);
|
pinMode(myOutputs[i].pin, OUTPUT);
|
||||||
switchOutputOff(i);
|
switchOutputOff(i);
|
||||||
|
@ -226,6 +241,9 @@ void checkInputSignals() {
|
||||||
Serial.print(F("Trigger pin ")); Serial.println(myInputs[i].pin);
|
Serial.print(F("Trigger pin ")); Serial.println(myInputs[i].pin);
|
||||||
sendMqttMessage(myInputs[i].topic, "trigger");
|
sendMqttMessage(myInputs[i].topic, "trigger");
|
||||||
needToToggleLed = true;
|
needToToggleLed = true;
|
||||||
|
if (myInputs[i].triggerOutputPin != OUTPUT_NONE) {
|
||||||
|
switchOutputOn(getOutputIndexByPin(myInputs[i].triggerOutputPin));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
myInputs[i].lastTrigger_ts = millis();
|
myInputs[i].lastTrigger_ts = millis();
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue