Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using exceptions #284

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
402c97b
Use ESP-IDF for DS18B20 soil sensor
lptr Dec 9, 2024
f9d3799
Use esp-idf-lib for peripherals
lptr Dec 11, 2024
36e8f9c
Use esp-idf-lib for DS18B20 also
lptr Dec 11, 2024
a82189b
Remove unused includes
lptr Dec 11, 2024
d0814f0
Fix BQ27220 driver
lptr Dec 11, 2024
554799d
Merge pull request #276 from kivancsikert/peripherals/stop-using-arduino
lptr Dec 11, 2024
6652c99
Register component first
lptr Dec 11, 2024
27995a2
Use nvs from ESP-IDF instead of Arduino Preferences
lptr Dec 11, 2024
c8f6e8f
Merge pull request #279 from kivancsikert/no-arduino-steps/nvs
lptr Dec 11, 2024
27c8389
Use ESP-IDF only to access SPIFFS
lptr Dec 11, 2024
000249d
Merge pull request #280 from kivancsikert/no-arduino-steps/fs
lptr Dec 11, 2024
5fa89cb
Don't use ESP
lptr Dec 11, 2024
34628e6
Use Task::delay() everywhere
lptr Dec 11, 2024
7ea4e01
Do not use IPAddress
lptr Dec 11, 2024
4811ff5
Do not init Arduino
lptr Dec 11, 2024
b1b33c1
Remove Serial
lptr Dec 11, 2024
a526972
Do not do millis()
lptr Dec 12, 2024
318c629
Fix std::max()
lptr Dec 12, 2024
f6a0094
Handle pins ourselves
lptr Dec 12, 2024
929d3cc
Replace HIGH / LOW constants
lptr Dec 12, 2024
d137252
Fix mmore pinMode() calls
lptr Dec 12, 2024
326a85d
Use LEDC from ESP-IDF
lptr Dec 12, 2024
5c8ff34
Merge pull request #281 from kivancsikert/no-arduino-steps/small-bits
lptr Dec 12, 2024
4937674
Use std::string and remove Arduino.h includes
lptr Dec 12, 2024
ea4af06
Remove Arduino as a dependency
lptr Dec 12, 2024
38ee99b
Remove unnecessary Arduino-related configuration
lptr Dec 12, 2024
72cd0c8
Reduce binary size a bit
lptr Dec 12, 2024
ddc3cab
Merge pull request #282 from kivancsikert/no-arduino-steps/strings
lptr Dec 12, 2024
478da8a
Stop using exceptions
lptr Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,24 +0,0 @@
[submodule "components/SHT31"]
path = components/SHT31
url = https://github.com/RobTillaart/SHT31
[submodule "components/SHT2x"]
path = components/SHT2x
url = https://github.com/RobTillaart/SHT2x
[submodule "components/OneWire"]
path = components/OneWire
url = https://github.com/PaulStoffregen/OneWire
[submodule "components/DallasTemperature"]
path = components/DallasTemperature
url = https://github.com/milesburton/Arduino-Temperature-Control-Library
[submodule "components/BH1750"]
path = components/BH1750
url = https://github.com/claws/BH1750
[submodule "components/Adafruit_TSL2591_Library"]
path = components/Adafruit_TSL2591_Library
url = https://github.com/adafruit/Adafruit_TSL2591_Library
[submodule "components/Adafruit_Sensor"]
path = components/Adafruit_Sensor
url = https://github.com/adafruit/Adafruit_Sensor
[submodule "components/Adafruit_BusIO"]
path = components/Adafruit_BusIO
url = https://github.com/adafruit/Adafruit_BusIO
32 changes: 15 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
cmake_minimum_required(VERSION 3.16.0)


# Add esp-idf-lib components
include(FetchContent)
FetchContent_Declare(
espidflib
GIT_REPOSITORY https://github.com/UncleRus/esp-idf-lib.git
GIT_TAG "0.9.4"
)
FetchContent_MakeAvailable(espidflib)
set(EXTRA_COMPONENT_DIRS ${espidflib_SOURCE_DIR}/components)


if(NOT DEFINED UD_GEN)
set(UD_GEN "$ENV{UD_GEN}")
endif()
Expand Down Expand Up @@ -40,33 +52,19 @@ set_property(DIRECTORY PROPERTY UD_DEBUG_TRACKER "${UD_DEBUG}")
if (UD_DEBUG)
message("Building with debug options")
set(SDKCONFIG_DEFAULTS "sdkconfig.defaults;sdkconfig.${UD_GEN_LOWER}.defaults;sdkconfig.debug.defaults")
set(CMAKE_BUILD_TYPE Debug)
else()
message("Building with release options")
set(SDKCONFIG_DEFAULTS "sdkconfig.defaults;sdkconfig.${UD_GEN_LOWER}.defaults")
add_compile_options("-O2")
set(CMAKE_BUILD_TYPE Release)
endif()
add_link_options("-Wl,--gc-sections")

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

idf_build_set_property(DEPENDENCIES_LOCK "dependencies.lock")

# list(APPEND EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/components)
list(APPEND EXTRA_COMPONENT_DIRS components)

# Adds necessary definitions for compiling it using Serial symbol attached to the HW USB CDC port
add_compile_definitions(ARDUINO_USB_CDC_ON_BOOT=1)
add_compile_definitions(ARDUINO_USB_MODE=1)

if(UD_GEN STREQUAL MK4)
add_compile_definitions(HWCDCSerial=Serial0)
endif()

# Keep struct initializers simple
add_compile_options("-Wno-missing-field-initializers")

# TODO Make this depend on some flag
set(CMAKE_BUILD_TYPE Debug)

project(ugly-duckling)

# Use `idf.py -DFSUPLOAD=1 flash` to upload the data partition
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Ugly Duckling is a firmware for IoT devices participating in the FarmHub ecosystem.

The devices are built around the Espressif ESP32 micro-controller using FreeRTOS and the Arduino framework.
The devices are built around the Espressif ESP32 micro-controller using the ESP-IDF framework and FreeRTOS.
The devices can report telemetry to, and receive configuration and commands from the FarmHub server via MQTT over WiFi.
They can also receive firmware updates via HTTP(S).

Expand Down Expand Up @@ -150,8 +150,6 @@ See `FileCommands` for more information.

- ESP-IDF v5.3.2 (see [installation instructions](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/get-started/index.html))

We are using this version because it is the latest version that is compatible with Arduino-ESP32 3.1.0-rc-1.

### Building

There are two ways to build the firmware:
Expand Down
1 change: 0 additions & 1 deletion components/Adafruit_BusIO
Submodule Adafruit_BusIO deleted from 15fbda
1 change: 0 additions & 1 deletion components/Adafruit_Sensor
Submodule Adafruit_Sensor deleted from 7b2473
1 change: 0 additions & 1 deletion components/Adafruit_TSL2591_Library
Submodule Adafruit_TSL2591_Library deleted from a857bf
1 change: 0 additions & 1 deletion components/BH1750
Submodule BH1750 deleted from b6986b
31 changes: 0 additions & 31 deletions components/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion components/DallasTemperature
Submodule DallasTemperature deleted from 4a0ccc
1 change: 0 additions & 1 deletion components/OneWire
Submodule OneWire deleted from 72249e
1 change: 0 additions & 1 deletion components/SHT2x
Submodule SHT2x deleted from b25998
1 change: 0 additions & 1 deletion components/SHT31
Submodule SHT31 deleted from e5361f
23 changes: 23 additions & 0 deletions data-templates/multiplexer-mk6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"instance": "DEVICE_INSTANCE",
"id": "DEVICE_ID",
"peripherals": [
{
"name": "relay",
"type": "multiplexer:xl9535",
"params": {
"sda": "C1",
"scl": "C4",
"address": "0x20"
}
},
{
"name": "valve-1",
"type": "valve",
"params": {
"pin": "relay:0"
}
}
],
"sleepWhenIdle": true
}
12 changes: 6 additions & 6 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
FILE(GLOB_RECURSE app_sources main.cpp **/*.cpp)

idf_component_register(
SRCS ${app_sources}
INCLUDE_DIRS "."
EMBED_TXTFILES "${CMAKE_SOURCE_DIR}/partitions.csv"
)

if(NOT DEFINED WOKWI)
set(WOKWI "$ENV{WOKWI}")
endif()
Expand All @@ -11,12 +17,6 @@ if(WOKWI)
component_compile_definitions(WOKWI)
endif()

idf_component_register(
SRCS ${app_sources}
INCLUDE_DIRS "."
EMBED_TXTFILES "${CMAKE_SOURCE_DIR}/partitions.csv"
)

component_compile_definitions("${UD_GEN}")
component_compile_definitions(FARMHUB_REPORT_MEMORY)

Expand Down
62 changes: 17 additions & 45 deletions main/devices/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
#include <chrono>
#include <memory>

#include <Arduino.h>

#include "esp_netif.h"
#include "esp_wifi.h"
#include <esp_pm.h>
#include <esp_private/esp_clk.h>

#include <Print.h>

#include <kernel/BootClock.hpp>
#include <kernel/Command.hpp>
#include <kernel/Concurrent.hpp>
Expand Down Expand Up @@ -114,42 +110,18 @@ class ConsolePrinter {
static const int spinnerLength = strlen(spinner);
auto uptime = duration_cast<milliseconds>(boot_clock::now().time_since_epoch());

status.clear();
counter = (counter + 1) % spinnerLength;
status.concat("[");
status.concat(spinner[counter]);
status.concat("] ");

status.concat("\033[33m");
status.concat(farmhubVersion);
status.concat("\033[0m");

status.concat(", uptime: \033[33m");
status.concat(String(uptime.count() / 1000.0, 1));
status.concat("\033[0m s");

status.concat(", WIFI: ");
status.concat(wifiStatus());
status.concat(" (up \033[33m");
status.concat(String(double(wifi.getUptime().count()) / 1000.0, 1));
status.concat("\033[0m s)");

status.concat(", RTC: \033[33m");
status.concat(RtcDriver::isTimeSet() ? "OK" : "UNSYNCED");
status.concat("\033[0m");

status.concat(", heap: \033[33m");
status.concat(String(double(heap_caps_get_free_size(MALLOC_CAP_INTERNAL)) / 1024.0, 2));
status.concat("\033[0m kB");

status.concat(", CPU: \033[33m");
status.concat(esp_clk_cpu_freq() / 1000000);
status.concat("\033[0m MHz");
status.clear();
status += std::format("[{}] ", spinner[counter]);
status += std::format("\033[33m{}\033[0m", farmhubVersion);
status += std::format(", uptime: \033[33m{:.2f}\033[0m s", uptime.count() / 1000.0);
status += std::format(", WIFI: {} (up \033[33m{:.1f}\033[0m s)", wifiStatus(), wifi.getUptime().count() / 1000.0);
status += std::format(", RTC \033[33m{}\033[0m", RtcDriver::isTimeSet() ? "OK" : "UNSYNCED");
status += std::format(", heap \033[33m{:.2f}\033[0m kB", heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024.0);
status += std::format(", CPU: \033[33m{}\033[0m MHz", esp_clk_cpu_freq() / 1000000);

if (battery != nullptr) {
status.concat(", battery: \033[33m");
status.concat(String(battery->getVoltage(), 2));
status.concat("\033[0m V");
status += std::format(", battery: \033[33m{:.2f}\033[0m V", battery->getVoltage());
}

printf("\033[1G\033[0K%s", status.c_str());
Expand Down Expand Up @@ -205,7 +177,7 @@ class ConsolePrinter {
}

int counter;
String status;
std::string status;
const std::shared_ptr<BatteryDriver> battery;
WiFiDriver& wifi;
};
Expand Down Expand Up @@ -335,14 +307,14 @@ class ConfiguredKernel {
}
printf("Shutdown process finished\n");
});
task.delay(LOW_BATTERY_SHUTDOWN_TIMEOUT);
Task::delay(LOW_BATTERY_SHUTDOWN_TIMEOUT);
enterLowPowerDeepSleep();
}
}

[[noreturn]] inline void enterLowPowerDeepSleep() {
printf("Entering low power deep sleep\n");
ESP.deepSleep(duration_cast<microseconds>(LOW_POWER_SLEEP_CHECK_INTERVAL).count());
esp_deep_sleep(duration_cast<microseconds>(LOW_POWER_SLEEP_CHECK_INTERVAL).count());
// Signal to the compiler that we are not returning for real
abort();
}
Expand Down Expand Up @@ -435,10 +407,10 @@ class Device {
// Remove the level prefix
auto messageStart = 2;
// Remove trailing newline
auto messageEnd = record.message.charAt(length - 1) == '\n'
auto messageEnd = record.message[length - 1] == '\n'
? length - 1
: length;
String message = record.message.substring(messageStart, messageEnd);
std::string message = record.message.substr(messageStart, messageEnd - messageStart);

mqttDeviceRoot->publish(
"log", [&](JsonObject& json) {
Expand Down Expand Up @@ -513,7 +485,7 @@ class Device {
kernel.getKernelReadyState().set();

LOGI("Device ready in %.2f s (kernel version %s on %s instance '%s' with hostname '%s' and IP '%s', SSID '%s', current time is %lld)",
millis() / 1000.0,
duration_cast<milliseconds>(boot_clock::now().time_since_epoch()).count() / 1000.0,
kernel.version.c_str(),
deviceConfig.model.get().c_str(),
deviceConfig.instance.get().c_str(),
Expand All @@ -534,7 +506,7 @@ class Device {
peripheralManager.publishTelemetry();
}

String locationPrefix() {
std::string locationPrefix() {
if (deviceConfig.location.hasValue()) {
return deviceConfig.location.get() + "/";
} else {
Expand Down Expand Up @@ -565,7 +537,7 @@ class Device {
FileReadCommand fileReadCommand { fs };
FileWriteCommand fileWriteCommand { fs };
FileRemoveCommand fileRemoveCommand { fs };
HttpUpdateCommand httpUpdateCommand { [this](const String& url) {
HttpUpdateCommand httpUpdateCommand { [this](const std::string& url) {
kernel.prepareUpdate(url);
} };

Expand Down
Loading
Loading