Greg Young - Help with sensESP code ..please 🙂...

Help with sensESP code ..please 🙂 To date all my sensESP projects have used "sensors" and hence reading and sending status, values etc up to signalk. Ive not had any experience with "controlling"something via sensESP.. and have looked in examples .. but didnt find anything comparable. Im trying to "control" a relay from sensESP using a change of state in an skpath. In particular i need to "pulse"the relay (aka GPIO output ) for around 500mS duration (turn it On for 500mS then off.. exact timing is not critical) I know how to set the GPIO output based on the skpath (using below) .. But how would i create a short pulse of the GPIO output? Im aware that there are certain restrictions on putting delays into the sensesp code .. because of how react? works? any examples of code others have used in a similar context would be very appreciated ** // sensESP monitors an SKPath that is toggled "true/false" by NodeRed flow auto* CockpitLightsListener = new BoolSKListener(CockpitLightsControlSKPath); CockpitLightsListener ->connect_to(new DigitalOutput(5));
2 Replies
Matti Airas
Matti Airas•4d ago
You're right that you shouldn't use delay in SensESP code - that blocks the whole event loop from working. But creating a pulse is still quite simple.
# first, trigger the positive edge
digitalWrite(output_gpio, true);

# then, create a delay and a callback that is executed when the delay event gets triggered
event_loop()->onDelay(pulse_length_ms, []() {
# inside this callback function, trigger the negative edge
digitalWrite(output_gpio, false);
});
# first, trigger the positive edge
digitalWrite(output_gpio, true);

# then, create a delay and a callback that is executed when the delay event gets triggered
event_loop()->onDelay(pulse_length_ms, []() {
# inside this callback function, trigger the negative edge
digitalWrite(output_gpio, false);
});
The delays are not totally accurate but unless something is off, should be there within a fraction of a ms. To wrap this code into a consumer that can be used with ValueListeners, you use a LambdaConsumer:
auto pulse_consumer = new LambdaConsumer<bool>([](bool value) {
digitalWrite(output_pin, value);
event_loop()->onDelay(pulse_length_ms, [value]() {
digitalWrite(output_gpio, !value);
});
}

cockpit_lights_listener->connect_to(pulse_consumer);
auto pulse_consumer = new LambdaConsumer<bool>([](bool value) {
digitalWrite(output_pin, value);
event_loop()->onDelay(pulse_length_ms, [value]() {
digitalWrite(output_gpio, !value);
});
}

cockpit_lights_listener->connect_to(pulse_consumer);
Greg Young
Greg YoungOP•4d ago
coool... much appreciated. thanks
Want results from more Discord servers?
Add your server