Activate webhook only once

I need to activate a webhook for a customers gate that will send him a text anytime someone puts an authorized code in. The problem is, he holds his gate open for most of the day, between 6 and 10 hours at a time, so I don’t want to just send a webhook on the condition of that pin state, because he would be getting texts all day long. Is there a way to condition this so that it sends the first time, then ignores additional inputs until the relay is reset?

have you thought of using timers? The assumption here would be that you are aware of the time during which the gate is kept open and you ignore sending the message during that duration.

Normally you’d have a variable set that tells you about the last reported state and only when the current state and the last state don’t match trigger a new report.

However, that’s not a question of webhooks but firmware design.

2 Likes

Unfortunately its not that easy, this guys works different hours each day so today it could be open until 5pm, but tomorrow he could keep it open until 7.

@ScruffR I think that’s what I’m looking for, I’ll look for an example for doing that. I say activating a webhook because it does nothing if my code doesn’t tell it to.

Going off of what @ScruffR said, the customer’s gate is a state machine with two possible states, open or closed, and two possible state transitions, opening or closing. When the gate undergoes the opening transition, you would trigger the webhook. While the gate remains open or when it finally closes you would not trigger the webhook, but you would trigger the webhook the next time the gate opens, and so on.

The simplest way to way to store the state and catch state transitions would be to use a boolean for the last read state and a boolean for the currently read state. Whenever the last state and current state are not equal, a state transition has occurred, meaning the gate has just opened or just closed. Depending on the current state reading, you can know if the gate is opened or closed. When the gate is opened, you would trigger the webhook. When the gate is closed, you wouldn’t do anything.

Here is some pseudocode for your loop() if it helps:

static bool lastState = HIGH;
// Assuming LOW means open and HIGH means closed
bool currentState = digitalRead(GATE_PIN);

// Check for state transition
if (currentState != lastState) {
    if (currentState) {
        // Gate was closed. Do nothing
    } else {
        // Gate was opened. Trigger webhook
        Particle.publish("GATE_WEBHOOK", PRIVATE);
    }
    lastState = currentState;
}
2 Likes

Thanks @nrobinson2000 , this is very similar to what I came up with. It will come in handy to double check my code and make sure everything is correct.

1 Like