forked from SensESP/SensESP-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_example_modified.cpp
More file actions
174 lines (138 loc) · 7.13 KB
/
main_example_modified.cpp
File metadata and controls
174 lines (138 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Signal K application template file.
//
// This application demonstrates core SensESP concepts in a very
// concise manner. You can build and upload the application as is
// and observe the value changes on the serial port monitor.
//
// You can use this source file as a basis for your own projects.
// Remove the parts that are not relevant to you, and add your own code
// for external hardware libraries.
#include <memory>
#include "sensesp.h"
#include "sensesp/sensors/analog_input.h"
#include "sensesp/sensors/digital_input.h"
#include "sensesp/sensors/sensor.h"
#include "sensesp/signalk/signalk_output.h"
#include "sensesp/system/lambda_consumer.h"
#include "sensesp/transforms/curveinterpolator.h"
#include "sensesp_app_builder.h"
using namespace sensesp;
// Global definitions
const float kAnalogInputScale = 29. / 2.048; // ADS1115 input hardware scale factor (input voltage vs voltage at ADS1115)
const float kMeasurementCurrent = 0.01; // Engine Hat constant measurement current (A)
const uint sensor_read_interval = 1000; // ms, the delay / interval to read the sensors, used for all sensors in this program
// The setup function performs one-time application initialization.
void setup() {
SetupLogging(ESP_LOG_DEBUG);
// Construct the global SensESPApp() object
SensESPAppBuilder builder;
sensesp_app = (&builder)
// Set a custom hostname for the app.
->set_hostname("sh-esp32-engine-hat")
// Optionally, hard-code the WiFi and Signal K server
// settings. This is normally not needed.
//->set_wifi_client("My WiFi SSID", "my_wifi_password")
//->set_wifi_access_point("My AP SSID", "my_ap_password")
//->set_sk_server("192.168.10.3", 80)
->get_app();
// Definition: Engine temperature sender
uint8_t enginehat_pin_temp = 0; // attached to pin A on the Engine Hat.
const char* config_engine1_temp_resistance_sk_path = "/engine1/temp/resistance/sk_path"; // the sk_path of the resistance of the temp sender in the engine.
const char* config_engine1_temp_temperature_sk_path = "/engine1/temp/temperature/sk_path"; // the sk_path of the temperature of the temp sender in the engine.
const char* config_engine1_temp_level_curve = "/engine1/temp/Level Curve";
const char* value_engine1_temp_resistance_sk_path = "propulsion.1.temperature.senderResistance";
const char* value_engine1_temp_temperature_sk_path = "propulsion.1.temperature";
auto metadata_engine1_temp_resistance = new SKMetadata("ohm", "Resistance", "Measured resistance of temperature sender", "Resistance", 10);
auto metadata_engine1_temp_temperature = new SKMetadata("K", "Engine temperature", "Temperature of cooling water sender in engine", "Temperature", 10);
// Create a new Analog Input Sensor that reads an analog input pin
// periodically.
auto analog_input_temp = std::make_shared<AnalogInput>(
enginehat_pin_temp, sensor_read_interval, "", kAnalogInputScale);
// Add an observer that prints out the current value of the analog input
// every time it changes.
analog_input_temp->attach([analog_input_temp]() {
debugD("Analog input value: %f", analog_input_temp->get());
});
// EXAMPLE BELOW
// GPIO number to use for the analog input
const uint8_t kAnalogInputPin = 36;
// Define how often (in milliseconds) new samples are acquired
const unsigned int kAnalogInputReadInterval = 500;
// Define the produced value at the maximum input voltage (3.3V).
// A value of 3.3 gives output equal to the input voltage.
const float kAnalogInputScale = 3.3;
// Create a new Analog Input Sensor that reads an analog input pin
// periodically.
auto analog_input = std::make_shared<AnalogInput>(
kAnalogInputPin, kAnalogInputReadInterval, "", kAnalogInputScale);
// Add an observer that prints out the current value of the analog input
// every time it changes.
analog_input->attach([analog_input]() {
debugD("Analog input value: %f", analog_input->get());
});
// Set GPIO pin 15 to output and toggle it every 650 ms
const uint8_t kDigitalOutputPin = 15;
const unsigned int kDigitalOutputInterval = 650;
pinMode(kDigitalOutputPin, OUTPUT);
event_loop()->onRepeat(kDigitalOutputInterval, [kDigitalOutputPin]() {
digitalWrite(kDigitalOutputPin, !digitalRead(kDigitalOutputPin));
});
// Read GPIO 14 every time it changes
const uint8_t kDigitalInput1Pin = 14;
auto digital_input1 = std::make_shared<DigitalInputChange>(
kDigitalInput1Pin, INPUT_PULLUP, CHANGE);
// Connect the digital input to a lambda consumer that prints out the
// value every time it changes.
// Test this yourself by connecting pin 15 to pin 14 with a jumper wire and
// see if the value changes!
auto digital_input1_consumer = std::make_shared<LambdaConsumer<bool>>(
[](bool input) { debugD("Digital input value changed: %d", input); });
digital_input1->connect_to(digital_input1_consumer);
// Create another digital input, this time with RepeatSensor. This approach
// can be used to connect external sensor library to SensESP!
const uint8_t kDigitalInput2Pin = 13;
const unsigned int kDigitalInput2Interval = 1000;
// Configure the pin. Replace this with your custom library initialization
// code!
pinMode(kDigitalInput2Pin, INPUT_PULLUP);
// Define a new RepeatSensor that reads the pin every 100 ms.
// Replace the lambda function internals with the input routine of your custom
// library.
// Again, test this yourself by connecting pin 15 to pin 13 with a jumper
// wire and see if the value changes!
auto digital_input2 = std::make_shared<RepeatSensor<bool>>(
kDigitalInput2Interval,
[kDigitalInput2Pin]() { return digitalRead(kDigitalInput2Pin); });
// Connect the analog input to Signal K output. This will publish the
// analog input value to the Signal K server every time it changes.
auto aiv_metadata = std::make_shared<SKMetadata>("V", "Analog input voltage");
auto aiv_sk_output = std::make_shared<SKOutput<float>>(
"sensors.analog_input.voltage", // Signal K path
"/Sensors/Analog Input/Voltage", // configuration path, used in the
// web UI and for storing the
// configuration
aiv_metadata
);
ConfigItem(aiv_sk_output)
->set_title("Analog Input Voltage SK Output Path")
->set_description("The SK path to publish the analog input voltage")
->set_sort_order(100);
analog_input->connect_to(aiv_sk_output);
// Connect digital input 2 to Signal K output.
auto di2_metadata = std::make_shared<SKMetadata>("", "Digital input 2 value");
auto di2_sk_output = std::make_shared<SKOutput<bool>>(
"sensors.digital_input2.value", // Signal K path
"/Sensors/Digital Input 2/Value", // configuration path
di2_metadata
);
ConfigItem(di2_sk_output)
->set_title("Digital Input 2 SK Output Path")
->set_sort_order(200);
digital_input2->connect_to(di2_sk_output);
// To avoid garbage collecting all shared pointers created in setup(),
// loop from here.
while (true) {
loop();
}
}
void loop() { event_loop()->tick(); }