|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +// RPM counter example using the Pulse Counter peripheral |
| 4 | + |
| 5 | +// #define SERIAL_DEBUG_DISABLED |
| 6 | + |
| 7 | +#include "sensesp/sensors/digital_pcnt_input.h" |
| 8 | +#include "sensesp/signalk/signalk_output.h" |
| 9 | +#include "sensesp/transforms/frequency.h" |
| 10 | +#include "sensesp_app_builder.h" |
| 11 | + |
| 12 | +using namespace sensesp; |
| 13 | + |
| 14 | +void setup() { |
| 15 | + SetupLogging(); |
| 16 | + |
| 17 | + SensESPAppBuilder builder; |
| 18 | + sensesp_app = builder.get_app(); |
| 19 | + |
| 20 | + // The "Signal K path" identifies the output of the sensor to the Signal K |
| 21 | + // network. If you have multiple sensors connected to your microcontoller |
| 22 | + // (ESP), each one of them will (probably) have its own Signal K path |
| 23 | + // variable. For example, if you have two propulsion engines, and you want the |
| 24 | + // RPM of each of them to go to Signal K, you might have sk_path_portEngine = |
| 25 | + // "propulsion.port.revolutions" and sk_path_starboardEngine = |
| 26 | + // "propulsion.starboard.revolutions" In this example, there is only one |
| 27 | + // propulsion engine, and its RPM is the only thing being reported to Signal |
| 28 | + // K. To find valid Signal K Paths that fits your need you look at this link: |
| 29 | + // https://signalk.org/specification/1.4.0/doc/vesselsBranch.html |
| 30 | + const char* sk_path = "propulsion.main.revolutions"; |
| 31 | + |
| 32 | + // The "Configuration path" is combined with "/config" to formulate a URL |
| 33 | + // used by the RESTful API for retrieving or setting configuration data. |
| 34 | + // It is ALSO used to specify a path to the SPIFFS file system |
| 35 | + // where configuration data is saved on the microcontroller. It should |
| 36 | + // ALWAYS start with a forward slash if specified. If left blank, |
| 37 | + // that indicates this sensor or transform does not have any |
| 38 | + // configuration to save. |
| 39 | + // Note that if you want to be able to change the sk_path at runtime, |
| 40 | + // you will need to specify a configuration path. |
| 41 | + // As with the Signal K path, if you have multiple configurable sensors |
| 42 | + // connected to the microcontroller, you will have a configuration path |
| 43 | + // for each of them, such as config_path_portEngine = |
| 44 | + // "/sensors/portEngine/rpm" and config_path_starboardEngine = |
| 45 | + // "/sensor/starboardEngine/rpm". |
| 46 | + const char* config_path = "/sensors/engine_rpm"; |
| 47 | + |
| 48 | + // These two are necessary until a method is created to synthesize them. |
| 49 | + // Everything after "/sensors" in each of these ("/engine_rpm/calibrate" and |
| 50 | + // "/engine_rpm/sk") is simply a label to display what you're configuring in |
| 51 | + // the Configuration UI. |
| 52 | + const char* config_path_calibrate = "/sensors/engine_rpm/calibrate"; |
| 53 | + const char* config_path_skpath = "/sensors/engine_rpm/sk"; |
| 54 | + |
| 55 | + ////////// |
| 56 | + // connect a RPM meter. A DigitalInputPcntCounter implements |
| 57 | + // access to a pulse counter peripheral. Every read_delay ms, it |
| 58 | + // reads the number of pulses that have occurred since the last |
| 59 | + // read, and reports that number (500ms in the example). A Frequency |
| 60 | + // transform takes a number of pulses and converts that into |
| 61 | + // a frequency. The sample multiplier converts the 97 tooth |
| 62 | + // tach output into Hz, SK native units. |
| 63 | + const float multiplier = 1.0 / 97.0; |
| 64 | + const unsigned int read_delay = 500; |
| 65 | + |
| 66 | + // Wire it all up by connecting the producer directly to the consumer |
| 67 | + // ESP32 pins are specified as just the X in GPIOX |
| 68 | + uint8_t pin = 4; |
| 69 | + |
| 70 | + auto* sensor = new DigitalInputPcntCounter(pin, INPUT_PULLUP, RISING, read_delay); |
| 71 | + |
| 72 | + auto frequency = new Frequency(multiplier, config_path_calibrate); |
| 73 | + |
| 74 | + ConfigItem(frequency) |
| 75 | + ->set_title("Frequency") |
| 76 | + ->set_description("Frequency of the engine RPM signal") |
| 77 | + ->set_sort_order(1000); |
| 78 | + |
| 79 | + auto frequency_sk_output = new SKOutput<float>(sk_path, config_path_skpath); |
| 80 | + |
| 81 | + ConfigItem(frequency_sk_output) |
| 82 | + ->set_title("Frequency SK Output Path") |
| 83 | + ->set_sort_order(1001); |
| 84 | + |
| 85 | + sensor |
| 86 | + ->connect_to(frequency) // connect the output of sensor |
| 87 | + // to the input of Frequency() |
| 88 | + ->connect_to(frequency_sk_output); // connect the output of Frequency() |
| 89 | + // to a Signal K Output as a number |
| 90 | +} |
| 91 | + |
| 92 | +void loop() { event_loop()->tick(); } |
0 commit comments