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