SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2
15#include "sensesp_app.h"
16#include "sensesp_app_builder.h"
17
18using namespace sensesp;
19
20// This example implements a remote switch that can remotely control the
21// smart switch created in smart_switch_example.cpp
22
23#ifdef ESP32
24#define PIN_LED_R 6
25#define PIN_LED_G 7
26#define PIN_LED_B 8
27#define PIN_BUTTON 0
28#else
29#define PIN_LED_R D6
30#define PIN_LED_G D7
31#define PIN_LED_B D8
32#define PIN_BUTTON D0
33#endif
34
35#define LED_ON_COLOR 0x004700
36#define LED_OFF_COLOR 0x261900
37
38void setup() {
40
41 // Create a builder object
42 SensESPAppBuilder builder;
43
44 // Create the global SensESPApp() object.
45 sensesp_app = builder.set_hostname("sk-lights-vswitch")
46 ->set_sk_server("10.10.1.5", 3000)
47 ->set_wifi_client("YOUR_WIFI_SSID", "YOUR_WIFI_PASSWORD")
48 ->get_app();
49
50 // Define the SK Path of the device that controls the load that this
51 // switch should control remotely. Clicking the virtual switch will
52 // send a PUT request on this path to the main device. This virtual
53 // switch will also subscribe to this path, and will set its state
54 // internally to match any value reported on this path.
55 // To find valid Signal K Paths that fits your need you look at this link:
56 // https://signalk.org/specification/1.4.0/doc/vesselsBranch.html
57 const char* sk_path = "electrical.switches.lights.engineroom.state";
58
59 // "Configuration paths" are combined with "/config" to formulate a URL
60 // used by the RESTful API for retrieving or setting configuration data.
61 // It is ALSO used to specify a path to the SPIFFS file system
62 // where configuration data is saved on the MCU board. It should
63 // ALWAYS start with a forward slash if specified. If left blank,
64 // that indicates a sensor or transform does not have any
65 // configuration to save.
66 const char* config_path_button_c = "/button/clicktime";
67 const char* config_path_status_light = "/button/statusLight";
68 const char* config_path_sk_output = "/signalk/path";
69 const char* config_path_repeat = "/signalk/repeat";
70
71 // An led that represents the current state of the switch.
72 auto* led = new RgbLed(PIN_LED_R, PIN_LED_G, PIN_LED_B,
73 config_path_status_light, LED_ON_COLOR, LED_OFF_COLOR);
74
75 // Create a switch controller to handle the user press logic and
76 // connect it a server PUT request...
77 SmartSwitchController* controller = new SmartSwitchController(false);
78 controller->connect_to(new BoolSKPutRequest(sk_path));
79
80 // Also connect the controller to an onboard LED...
81 controller->connect_to(led->on_off_consumer_);
82
83 // Connect a physical button that will feed manual click types into the
84 // controller...
85 DigitalInputState* btn = new DigitalInputState(PIN_BUTTON, INPUT, 100);
86 PressRepeater* pr = new PressRepeater();
87 btn->connect_to(pr);
88
89 auto click_type = new ClickType(config_path_button_c);
90
91 ConfigItem(click_type)->set_title("Click Type")->set_sort_order(1000);
92
93 pr->connect_to(click_type)->connect_to(controller->click_consumer_);
94
95 // In addition to the manual button "click types", a
96 // SmartSwitchController accepts explicit state settings via
97 // any boolean producer or various "truth" values in human readable
98 // format via a String producer.
99 // Since this device is NOT the device that directly controls the
100 // load, we don't want to respond to PUT requests. Instead, we
101 // let the "real" switch respond to the PUT requests, and
102 // here we just listen to the published values that are
103 // sent across the Signal K network when the controlling device
104 // confirms it has made the change in state.
105 auto* sk_listener = new SKValueListener<bool>(sk_path);
106 sk_listener->connect_to(controller->swich_consumer_);
107}
108
109void loop() { event_loop()->tick(); }
ClickType is a transform that consumes button clicks and translates them as events of type ClickTypes...
Definition click_type.h:29
DigitalInputState polls the state of an input pin every read_delay ms.
A transform that takes boolean inputs and adds button behaviors familiar to many device end users....
A special device object that can be used to control a multi-channel color rgb LED light using up to 3...
Definition rgb_led.h:29
An ValueProducer that listens to specific Signal K paths and emits its value whenever it changes.
A class for quickly configuring a SensESP application object before wiring up your sensors.
SensESPAppBuilder * set_hostname(String hostname) override final
Set the device hostname.
SensESPAppBuilder * set_sk_server(String address, uint16_t port)
Set the Signal K server address and port.
SensESPAppBuilder * set_wifi_client(String ssid, String password)
Set the Wi-Fi network SSID and password.
std::shared_ptr< SensESPApp > get_app()
Get the SensESPApp object.
A high level transform designed to control a digital output (such as a relay) via manual button press...
LambdaConsumer< ClickTypes > click_consumer_
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, std::shared_ptr< VConsumer > >::type connect_to(std::shared_ptr< VConsumer > consumer)
Connect a producer to a transform with a different input type.
#define PIN_BUTTON
Definition main.cpp:32
void setup()
Definition main.cpp:38
#define PIN_LED_R
Definition main.cpp:29
#define LED_OFF_COLOR
Definition main.cpp:36
#define LED_ON_COLOR
Definition main.cpp:35
#define PIN_LED_B
Definition main.cpp:31
#define PIN_LED_G
Definition main.cpp:30
void loop()
Definition main.cpp:109
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
std::shared_ptr< SensESPApp > sensesp_app
SKPutRequest< bool > BoolSKPutRequest
std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.
void SetupLogging(esp_log_level_t default_level=ESP_LOG_VERBOSE)