Skip to content

Commit

Permalink
Merge pull request #799 from SignalK/new_minimal_app
Browse files Browse the repository at this point in the history
New minimal_app example
  • Loading branch information
mairas authored Dec 13, 2024
2 parents 97e95b1 + 0673e0e commit eba5c3b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 50 deletions.
93 changes: 45 additions & 48 deletions examples/minimal_app.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
#include <math.h>

#include "sensesp/net/http_server.h"
#include "sensesp/net/networking.h"
#include "sensesp/sensors/digital_input.h"
#include "sensesp/transforms/lambda_transform.h"
#include "sensesp/transforms/linear.h"
#include "sensesp/transforms/typecast.h"
#include "sensesp/net/web/config_handler.h"
#include "sensesp/net/web/static_file_handler.h"
#include "sensesp/signalk/signalk_delta_queue.h"
#include "sensesp/signalk/signalk_ws_client.h"
#include "sensesp/system/button.h"
#include "sensesp_minimal_app_builder.h"

using namespace sensesp;

const unsigned int read_delay = 500;

const uint8_t input_pin1 = 15;
const uint8_t output_pin1 = 18;
const uint8_t input_pin2 = 13;
const uint8_t output_pin2 = 21;

// This is a sample program to demonstrate how to instantiate a
// SensESPMinimalApp application and only enable some required components
// explicitly.
Expand All @@ -32,43 +26,46 @@ void setup() {
SensESPMinimalAppBuilder builder;
auto sensesp_app = builder.set_hostname("counter-test")->get_app();

// manually create Networking and HTTPServer objects to enable
// the HTTP configuration interface

auto networking = std::make_shared<Networking>("/system/networking", "", "");
auto http_server = std::make_shared<HTTPServer>();

auto digin1 = std::make_shared<DigitalInputCounter>(input_pin1, INPUT, RISING,
read_delay);
auto digin2 = std::make_shared<DigitalInputCounter>(input_pin2, INPUT, CHANGE,
read_delay);

auto scaled1 = std::make_shared<Linear>(2, 1, "/digin1/scale");
auto scaled2 = std::make_shared<Linear>(4, -1, "/digin2/scale");
digin1->connect_to(scaled1);

auto lambda_transform1 =
std::make_shared<LambdaTransform<int, int>>([](int input) {
Serial.printf("millis: %d\n", millis());
Serial.printf("Counter 1: %d\n", input);
return input;
});
scaled1->connect_to(lambda_transform1);
auto lambda_transform2 =
std::make_shared<LambdaTransform<int, int>>([](int input) {
Serial.printf("Counter 2: %d\n", input);
return input;
});

digin2->connect_to(scaled2)->connect_to(lambda_transform2);

pinMode(output_pin1, OUTPUT);
event_loop()->onRepeat(
5, []() { digitalWrite(output_pin1, !digitalRead(output_pin1)); });

pinMode(output_pin2, OUTPUT);
event_loop()->onRepeat(
100, []() { digitalWrite(output_pin2, !digitalRead(output_pin2)); });
auto button_handler = std::make_shared<ButtonHandler>(0);

// As an example, we'll connect to WiFi manually using the Arduino ESP32 WiFi
// library instead of using the SensESP Networking class.

WiFi.mode(WIFI_STA); // Optional
WiFi.begin("ssid", "password");
Serial.println("\nConnecting");

while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}

Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());

// Initiate the objects you need for your application here. Have a look
// at sensesp_app.h and pick the necessary items from there.

// create the SK delta queue
auto sk_delta_queue = std::make_shared<SKDeltaQueue>();

// Use this if you want to hardcode the Signal K server address
String sk_server_address = "openplotter.local";
// Use this if you want to hardcode the Signal K server port
uint16_t sk_server_port = 3000;
// Set this to true if you want to use mDNS to discover the Signal K server
bool use_mdns = false;

auto ws_client =
std::make_shared<SKWSClient>("/System/Signal K Settings", sk_delta_queue,
sk_server_address, sk_server_port, use_mdns);

// To avoid garbage collecting all shared pointers
// created in setup(), loop from here.
while (true) {
loop();
}
}

void loop() { event_loop()->tick(); }
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
; examples/platformio.ini instead.

[env:esp32dev]
platform = espressif32
platform = espressif32 @ ^6.9.0
board = esp32dev
framework = arduino
lib_ldf_mode = deep
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class SensESPApp : public SensESPBaseApp {

ConfigItem(this->http_server_);

// create the SK delta object
// create the SK delta queue
sk_delta_queue_ = std::make_shared<SKDeltaQueue>();

// create the websocket client
Expand Down

0 comments on commit eba5c3b

Please sign in to comment.