Skip to content

Commit

Permalink
Merge branch 'release/v1.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocarnelos committed Apr 27, 2022
2 parents 0595f19 + 470aad0 commit f7a9afe
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 315 deletions.
47 changes: 26 additions & 21 deletions examples/SimpleCounter/SimpleCounter.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
#define USE_ESP_IDF_LOG

#include <Arduino.h>
#include <SailtrackModule.h>

SailtrackModule stm;
// -------------------------- Configuration -------------------------- //

#define MQTT_PUBLISH_FREQ_HZ 1

#define LOOP_TASK_DELAY_MS 1000 / MQTT_PUBLISH_FREQ_HZ

static const char * LOG_TAG = "SAILTRACK_COUNTER";
// ------------------------------------------------------------------- //

class TestCallbacks: public SailtrackModuleCallbacks {
void onDeepSleepEnter() {}
void onWifiConnectionBegin() {}
void onWifiConnectionResult(wl_status_t status) {}
void onWifiDisconnected() {}
void onMqttConnectionBegin() {}
void onMqttConnectionResult(bool connected) {}
void onMqttDisconnected() {}
SailtrackModule stm;

class ModuleCallbacks: public SailtrackModuleCallbacks {
void onLogMessage() {
log_i("Extra log entry");
log_printf("\n");
}

DynamicJsonDocument * getStatus() { return NULL; }
void onStatusMessage(JsonObject status) {
status["extra"] = "extraStatus";
}

void onMqttMessage(const char * topic, const char * message) {
ESP_LOGI(LOG_TAG, "New message! Topic: %s, Message: %s", topic, message);
void onMqttMessage(const char * topic, JsonObjectConst payload) {
char message[STM_MQTT_DATA_BUFFER_SIZE];
serializeJson(payload, message);
log_i("New message! Topic: %s, Message: %s", topic, message);
}
};

int counter = 0;

void setup() {
stm.begin("counter", IPAddress(192, 168, 42, 100), new TestCallbacks());
stm.begin("counter", IPAddress(192, 168, 42, 100), new ModuleCallbacks());
stm.subscribe("sensor/counter0");
esp_log_level_set(LOG_TAG, ESP_LOG_INFO);
}

void loop() {
DynamicJsonDocument data(100);
data["count"] = counter++;
stm.publish("sensor/counter0", &data);
delay(1000);
TickType_t lastWakeTime = xTaskGetTickCount();
StaticJsonDocument<STM_JSON_DOCUMENT_SMALL_SIZE> doc;
doc["count"] = counter++;
stm.publish("sensor/counter0", doc.as<JsonObjectConst>());
vTaskDelayUntil(&lastWakeTime, pdMS_TO_TICKS(LOOP_TASK_DELAY_MS));
}
99 changes: 9 additions & 90 deletions include/SailtrackModule.h
Original file line number Diff line number Diff line change
@@ -1,121 +1,40 @@
#ifndef SAILTRACK_MODULE_H
#define SAILTRACK_MODULE_H

#define USE_ESP_IDF_LOG
#undef LOG_LOCAL_LEVEL
#define LOG_LOCAL_LEVEL ESP_LOG_INFO

#include <Arduino.h>
#include <WiFi.h>
#include <mqtt_client.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>

#include "SailtrackModuleCallbacks.h"

#define TASK_HIGH_PRIORITY 3
#define TASK_MEDIUM_PRIORITY 2
#define TASK_LOW_PRIORITY 1

#define TASK_BIG_STACK_SIZE 8192
#define TASK_MEDIUM_STACK_SIZE 4096
#define TASK_SMALL_STACK_SIZE 2048

#define MQTT_STATUS_PUBLISH_RATE_HZ 0.1
#define MQTT_LOG_PUBLISH_RATE_HZ 0.1
#define OTA_HANDLE_RATE_HZ 1

#define WIFI_DEFAULT_SSID "SailTrack-Net"
#define WIFI_DEFAULT_PASSWORD "sailtracknet"
#define WIFI_DEFAULT_GATEWAY IPAddress(192, 168, 42, 1)
#define WIFI_DEFAULT_SUBNET IPAddress(255, 255, 255, 0)
#define WIFI_CONNECTION_TIMEOUT_MS 10 * 1e3
#define WIFI_SLEEP_DURATION_US 60 * 1e6

#define MQTT_DEFAULT_HOST WIFI_DEFAULT_GATEWAY
#define MQTT_DEFAULT_PORT 1883
#define MQTT_DEFAULT_USERNAME "mosquitto"
#define MQTT_DEFAULT_PASSWORD "dietpi"
#define MQTT_CONNECTION_TIMEOUT_MS 20 * 1e3
#define MQTT_OUTPUT_BUFFER_SIZE 800

struct NotificationLed {
int pin;
bool reversed;
};

struct WifiConfig {
const char * hostname;
const char * ssid;
const char * password;
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
};
#include "SailtrackModuleConfig.h"

/**
* Main static class and entrypoint of the `SailtrackModule` library.
*/
class SailtrackModule {
static const char * name;
static char name[];
static char hostname[];
static SailtrackModuleCallbacks * callbacks;
static NotificationLed * notificationLed;
static WifiConfig wifiConfig;
static esp_mqtt_client_config_t mqttConfig;
static esp_mqtt_client_handle_t mqttClient;
static bool mqttConnected;
static int publishedMessagesCount;
static int receivedMessagesCount;

#ifdef STM_NOTIFICATION_LED_PIN
static void beginNotificationLed();
static void beginLogging();
static void beginWifi();
#endif
static void beginWifi(IPAddress ip);
static void beginOTA();
static void beginMqtt();
static void mqttEventHandler(void * handlerArgs, esp_event_base_t base, int32_t eventId, void * eventData);
static void notificationLedTask(void * pvArguments);
static void statusTask(void * pvArguments);
static void logTask(void * pvArguments);
static void otaTask(void * pvArguments);
static int m_vprintf(const char * format, va_list args);

public:

/**
* Set the WiFi configuration. This method is particularly useful to connect to a custom WiFi network, different
* from the default one (e.g. for testing/development purposes). The default configuration can be found in
* `SailtrackModuleConfig.h` under the 'WiFi Configuration' section.
*
* @param ssid The SSID of the WiFi network.
* @param password The password of the WiFi network.
* @param gateway The IP address of the gateway of the WiFi network.
* @param subnet The subnet of the WiFi network. Ususally equal to `WIFI_DEFAULT_SUBNET`.
*/
static void configWifi(const char * ssid, const char * password, IPAddress gateway, IPAddress subnet);

/**
* Set the MQTT configuration. This method is particularly useful to connect to a custom MQTT broker, different
* from the default one (e.g. for testing/development purposes). The default configuration can be found in
* `SailtrackModuleConfig.h` under the 'MQTT Configuration' section.
*
* @param host The IP address of the MQTT broker.
* @param port The port of the MQTT broker. Usually equal to `MQTT_DEFAULT_PORT`.
* @param username The username used to authenticate to the MQTT broker. Leave empty if authentication is not
* required.
* @param password The password used to authenticate to the MQTT broker. Leave empty if authentication is not
* required.
*/
static void configMqtt(IPAddress host, int port, const char * username = "", const char * password = "");

/**
* Set the notification LED. It will be used to notify the user (e.g. for the connection status).
*
* @param pin The pin number the notification LED is connected to.
* @param reversed Set to `true` if the LED is reversed (`pin` connected to the cathode of the LED). If
* reversed, the LED will be turned on by setting `pin` to `LOW`.
*/
static void setNotificationLed(int pin, bool reversed = false);

/**
* Initialize the module and the peripherals, connecting it to the WiFi network and the MQTT broker. This method
* must be called in the `setup()` function in order to correctly use the library.
Expand All @@ -131,11 +50,11 @@ class SailtrackModule {
/**
* Publish data to MQTT. Use this method to publish a JSON formatted payload to the given MQTT topic.
*
* @param topic The topic `payload` will be published to.
* @param payload The payload to publish formatted as a JSON document.
* @param topic The topic `message` will be published to.
* @param message The message to publish as a JSON object.
* @return The ID of the published message on success, -1 on failure.
*/
static int publish(const char * topic, DynamicJsonDocument * payload);
static int publish(const char * topic, JsonObjectConst message);

/**
* Subscribe to a MQTT topic. To notify new messages, the `onMqttMessage(...)` callback will be used.
Expand Down
59 changes: 10 additions & 49 deletions include/SailtrackModuleCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,26 @@
*/
class SailtrackModuleCallbacks {
public:

/**
* Called when the module is entering deep sleep mode. Use this callback to power off peripherals so to save
* power during deep sleep.
*/
virtual void onDeepSleepEnter() {}

/**
* Called when the connection to the WiFi network begins.
*/
virtual void onWifiConnectionBegin() {}

/**
* Called when the connection to the WiFi network ends, either successfully or not.
*
* @param status The resulted status of the connection.
*/
virtual void onWifiConnectionResult(wl_status_t status) {}

/**
* Called when the connection to the WiFi network is lost. Note that once the callback returns the ESP is
* rebooted, therefore remember not to start any non-blocking task from this callback, as it will be killed
* immediately.
*/
virtual void onWifiDisconnected() {}

/**
* Called when the connection to the MQTT broker begins.
*/
virtual void onMqttConnectionBegin() {}

/**
* Called when the connection to the MQTT broker ends, either successfully or not.
* Called when a new MQTT message has been published to one of the subscribed topics.
*
* @param connected The resulted status of the connection.
*/
virtual void onMqttConnectionResult(bool connected) {}

/**
* Called when the connection to the MQTT broker is lost. Note that once the callback returns the ESP is
* rebooted, therefore remember not to start any non-blocking task from this callback, as it will be killed
* immediately.
* @param topic The topic the message has been published to.
* @param message The received message as a JSON object.
*/
virtual void onMqttDisconnected() {}
virtual void onMqttMessage(const char * topic, JsonObjectConst message) {}

/**
* Called when a new MQTT message is published to one of the subscribed topics.
*
* @param topic The topic the message has been published to.
* @param message The received message.
* Called when a new log entry is being sent through the serial port.
*/
virtual void onMqttMessage(const char * topic, const char * message) {}
virtual void onLogPrint() {}

/**
* Called when the library needs the status of the module (e.g. to publish it to MQTT).
* Called when a new status message is being published through MQTT.
*
* @return The status of the module, as a JSON document.
* @param status The JsonObject with the status of the module, you can modify it to include extra status
* information of the module.
*/
virtual DynamicJsonDocument * getStatus() { return NULL; }
virtual void onStatusPublish(JsonObject status) {}
};

#endif
86 changes: 86 additions & 0 deletions include/SailtrackModuleConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef SAILTRACK_MODULE_CONFIG_H
#define SAILTRACK_MODULE_CONFIG_H

// ----------------------- Module Configuration ----------------------- //

#define STM_MODULE_NAME_MAX_LENGTH 16

// ------------------- JSON Documents Configuration ------------------- //

#define STM_JSON_DOCUMENT_BIG_SIZE 1024
#define STM_JSON_DOCUMENT_MEDIUM_SIZE 512
#define STM_JSON_DOCUMENT_SMALL_SIZE 256

// ------------------ Notification LED Configuration ------------------ //

#ifndef STM_NOTIFICATION_LED_PIN
#ifdef LED_BUILTIN
#define STM_NOTIFICATION_LED_PIN LED_BUILTIN
#else
#ifdef BUILTIN_LED
#define STM_NOTIFICATION_LED_PIN BUILTIN_LED
#endif
#endif
#endif
#ifndef STM_NOTIFICATION_LED_ON_STATE
#define STM_NOTIFICATION_LED_ON_STATE HIGH
#endif

// ----------------------- Tasks Configuration ----------------------- //

#define STM_TASK_HIGH_PRIORITY 3
#define STM_TASK_MEDIUM_PRIORITY 2
#define STM_TASK_LOW_PRIORITY 1

#define STM_TASK_BIG_STACK_SIZE 8192
#define STM_TASK_MEDIUM_STACK_SIZE 4096
#define STM_TASK_SMALL_STACK_SIZE 2048

// ------------------- Library Tasks Configuration ------------------- //

#define STM_STATUS_PUBLISH_FREQ_HZ 0.1
#define STM_LOG_PRINT_FREQ_HZ 0.1
#define STM_OTA_HANDLE_FREQ_HZ 1

#define STM_STATUS_TASK_DELAY_MS 1000 / STM_STATUS_PUBLISH_FREQ_HZ
#define STM_LOG_TASK_DELAY_MS 1000 / STM_LOG_PRINT_FREQ_HZ
#define STM_OTA_TASK_DELAY_MS 1000 / STM_OTA_HANDLE_FREQ_HZ

// ------------------------ WiFi Configuration ------------------------ //

#ifndef STM_WIFI_SSID
#define STM_WIFI_SSID "SailTrack-Net"
#endif
#ifndef STM_WIFI_PASSWORD
#define STM_WIFI_PASSWORD "sailtracknet"
#endif
#ifndef STM_WIFI_GATEWAY_ADDR
#define STM_WIFI_GATEWAY_ADDR "192.168.42.1"
#endif
#ifndef STM_WIFI_SUBNET
#define STM_WIFI_SUBNET "255.255.255.0"
#endif

#define STM_WIFI_CONNECTION_TIMEOUT_MS 10 * 1e3
#define STM_WIFI_SLEEP_DURATION_US 60 * 1e6

// ------------------------ MQTT Configuration ------------------------ //

#ifndef STM_MQTT_HOST_ADDR
#define STM_MQTT_HOST_ADDR STM_WIFI_GATEWAY_ADDR
#endif
#ifndef STM_MQTT_PORT
#define STM_MQTT_PORT MQTT_TCP_DEFAULT_PORT
#endif
#ifndef STM_MQTT_USERNAME
#define STM_MQTT_USERNAME "mosquitto"
#endif
#ifndef STM_MQTT_PASSWORD
#define STM_MQTT_PASSWORD "dietpi"
#endif
#define STM_MQTT_DATA_BUFFER_SIZE 1024
#define STM_MQTT_TOPIC_BUFFER_SIZE 32

#define STM_MQTT_CONNECTION_TIMEOUT_MS STM_WIFI_CONNECTION_TIMEOUT_MS

#endif
7 changes: 5 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SailtrackModule",
"version": "1.5.1",
"version": "1.6.0",
"description": "Base library required for all SailTrack's ESP32-based modules.",
"keywords": "sailtrack, module, sailing",
"repository": {
Expand All @@ -16,7 +16,7 @@
],
"license": "GPL-3.0",
"dependencies": {
"bblanchon/ArduinoJson": "^6.19.3"
"bblanchon/ArduinoJson": "^6.19.4"
},
"frameworks": "arduino",
"platforms": "espressif32",
Expand All @@ -25,5 +25,8 @@
"test",
".github"
]
},
"build": {
"flags": "-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO"
}
}
Loading

0 comments on commit f7a9afe

Please sign in to comment.