💫 Initial plans for device configuration service

This commit is contained in:
Rune Harlyk
2024-05-30 22:10:11 +02:00
committed by Rune Harlyk
parent 83a9007b51
commit d1567fa2dd
17 changed files with 15236 additions and 14556 deletions
+1
View File
@@ -8,3 +8,4 @@ build_flags =
-D FT_UPLOAD_FIRMWARE=0
-D FT_DOWNLOAD_FIRMWARE=0
-D FT_ANALYTICS=1
-D FT_IMU=1
@@ -0,0 +1,109 @@
#pragma once
#include <EventEndpoint.h>
#include <FSPersistence.h>
#include <HttpEndpoint.h>
#include <SecurityManager.h>
#include <StatefulService.h>
#include <SPI.h>
#include <Wire.h>
#define DEVICE_CONFIG_FILE "/config/deviceConfig.json"
#define EVENT_CONFIGURATION_SETTINGS "ConfigurationSettings"
#define CONFIGURATION_SETTINGS_PATH "/api/configuration"
/*
* OLED Settings
*/
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_RESET -1
/*
* I2C software connection
*/
#define SDA 14
#define SCL 15
/*
* Ultra sonic sensors
*/
#define USS_LEFT 12
#define USS_RIGHT 13
#define USS_MAX_DISTANCE 200
class PinConfig {
public:
int pin;
String mode;
String type;
String role;
PinConfig(int p, String m, String t, String r) : pin(p), mode(m), type(t), role(r) {}
};
class DeviceConfiguration {
public:
int sda = SDA;
int scl = SCL;
std::vector<PinConfig> pins;
static void read(DeviceConfiguration &settings, JsonObject &root) {
root["sda"] = settings.sda;
root["scl"] = settings.scl;
}
static StateUpdateResult update(JsonObject &root, DeviceConfiguration &settings) {
settings.sda = root["sda"] | SDA;
settings.scl = root["scl"] | SCL;
return StateUpdateResult::CHANGED;
};
};
class DeviceConfigurationService : public StatefulService<DeviceConfiguration> {
public:
DeviceConfigurationService(PsychicHttpServer *server, FS *fs,
SecurityManager *securityManager,
EventSocket *socket)
: _server(server),
_securityManager(securityManager),
_httpEndpoint(DeviceConfiguration::read, DeviceConfiguration::update,
this, server, CONFIGURATION_SETTINGS_PATH,
securityManager, AuthenticationPredicates::IS_ADMIN),
_eventEndpoint(DeviceConfiguration::read, DeviceConfiguration::update,
this, socket, EVENT_CONFIGURATION_SETTINGS),
_fsPersistence(DeviceConfiguration::read, DeviceConfiguration::update,
this, fs, DEVICE_CONFIG_FILE)
{
addUpdateHandler([&](const String &originId) { updatePins(); },
false);
};
void begin() {
_httpEndpoint.begin();
_eventEndpoint.begin();
_fsPersistence.readFromFS();
updatePins();
};
void updatePins() {
if (i2c_active){
Wire.end();
}
if (_state.sda != -1 && _state.scl != -1) {
Wire.begin(_state.sda, _state.scl);
i2c_active = true;
}
}
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
HttpEndpoint<DeviceConfiguration> _httpEndpoint;
EventEndpoint<DeviceConfiguration> _eventEndpoint;
FSPersistence<DeviceConfiguration> _fsPersistence;
bool i2c_active = false;
};
+12 -3
View File
@@ -64,8 +64,10 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server,
_factoryResetService(server, &ESPFS, &_securitySettingsService),
_systemStatus(server, &_securitySettingsService),
_fileExplorer(server, &_securitySettingsService),
_motionService(_server, &_socket, &_securitySettingsService,
&_taskManager) {
_motionService(_server, &_socket, &_securitySettingsService,&_taskManager),
_deviceConfiguration(server, &ESPFS, &_securitySettingsService, &_socket),
_imuService(&_socket)
{
}
void ESP32SvelteKit::begin() {
@@ -89,7 +91,7 @@ void ESP32SvelteKit::begin() {
void ESP32SvelteKit::setupServer() {
_server->config.max_uri_handlers = _numberEndpoints;
_server->listen(80);
_server->listen(100);
#ifdef EMBED_WWW
ESP_LOGV("ESP32SvelteKit",
@@ -204,6 +206,10 @@ void ESP32SvelteKit::startServices() {
#if FT_ENABLED(FT_CAMERA)
_cameraService.begin();
_cameraSettingsService.begin();
#endif
_deviceConfiguration.begin();
#if FT_ENABLED(FT_IMU)
_imuService.begin();
#endif
}
@@ -216,6 +222,9 @@ void IRAM_ATTR ESP32SvelteKit::_loop() {
#endif
#if FT_ENABLED(FT_ANALYTICS)
_analyticsService.loop();
#endif
#if FT_ENABLED(FT_IMU)
_imuService.loop();
#endif
_motionService.loop();
vTaskDelay(20 / portTICK_PERIOD_MS);
@@ -25,11 +25,13 @@
#include <BatteryService.h>
#include <FileExplorerService.h>
#include <DownloadFirmwareService.h>
#include <DeviceConfigurationService.h>
#include <ESPFS.h>
#include <ESPmDNS.h>
#include <EventSocket.h>
#include <FactoryResetService.h>
#include <FeaturesService.h>
#include <IMUService.h>
#include <MqttSettingsService.h>
#include <MqttStatus.h>
#include <MotionService.h>
@@ -166,17 +168,30 @@ public:
{
return &_motionService;
}
#if FT_ENABLED(FT_CAMERA)
CameraService *getCameraService()
{
return &_cameraService;
}
CameraSettingsService *getCameraSettingsService()
{
return &_cameraSettingsService;
}
#endif
DeviceConfigurationService *getDeviceConfigurationService()
{
return &_deviceConfiguration;
}
#if FT_ENABLED(FT_IMU)
IMUService *getIMUService()
{
return &_imuService;
}
#endif
void factoryReset()
{
_factoryResetService.factoryReset();
@@ -239,6 +254,10 @@ private:
CameraService _cameraService;
CameraSettingsService _cameraSettingsService;
#endif
DeviceConfigurationService _deviceConfiguration;
#if FT_ENABLED(FT_IMU)
IMUService _imuService;
#endif
String _appName = APP_NAME;
+5
View File
@@ -62,4 +62,9 @@
#define FT_CAMERA 0
#endif
// ESP32 IMU on by default
#ifndef FT_IMU
#define FT_IMU 1
#endif
#endif
@@ -31,6 +31,7 @@ void FeaturesService::begin() {
root["battery"] = FT_BATTERY;
root["analytics"] = FT_ANALYTICS;
root["camera"] = FT_CAMERA;
root["imu"] = FT_IMU;
root["firmware_version"] = APP_VERSION;
root["firmware_name"] = APP_NAME;
root["firmware_built_target"] = BUILD_TARGET;
+54 -19
View File
@@ -1,55 +1,90 @@
#pragma once
#include <MPU6050_light.h>
#include <ArduinoJson.h>
#include <EventSocket.h>
#define IMU_INTERVAL 2000
#define IMU_INTERVAL 200
#define MAX_ESP_IMU_SIZE 250
#define EVENT_IMU "imu"
class IMUService
{
public:
IMUService():_imu(Wire){};
IMUService(EventSocket *socket) : _socket(socket), _imu(Wire) {};
void begin()
{
byte status = _imu.begin();
imu_success = status == 0;
if(status != 0) {
ESP_LOGE("IMUService", "MPU initialize failed");
vTaskDelete(NULL);
return;
ESP_LOGE("IMUService", "MPU initialize failed: %d", status);
}
xTaskCreatePinnedToCore(this->_loopImpl, "IMU Service", 4096, this, tskIDLE_PRIORITY, NULL, ESP32SVELTEKIT_RUNNING_CORE);
_socket->registerEvent(EVENT_IMU);
calibrate();
};
bool isIMUSuccess() {
return imu_success;
}
float getTemp() {
return _imu.getTemp();
return imu_success ? _imu.getTemp() : -1;
}
float getAngleX() {
return _imu.getAngleX();
return imu_success ? _imu.getAngleX() : -1;
}
float getAngleY() {
return _imu.getAngleX();
return imu_success ? _imu.getAngleX() : -1;
}
float getAngleZ() {
return _imu.getAngleZ();
return imu_success ? _imu.getAngleZ() : -1;
}
protected:
static void _loopImpl(void *_this) { static_cast<IMUService *>(_this)->_loop(); }
void _loop()
void calibrate() {
if (imu_success) {
_imu.calcOffsets(true, true);
}
}
double round2(double value) {
return (int)(value * 100 + 0.5) / 100.0;
}
void loop()
{
vTaskDelay(100);
_imu.calcOffsets(true,true);
TickType_t xLastWakeTime = xTaskGetTickCount();
while (1)
unsigned long currentMillis = millis();
if (currentMillis - _lastUpdate >= _updateInterval)
{
_imu.update();
vTaskDelayUntil(&xLastWakeTime, IMU_INTERVAL / portTICK_PERIOD_MS);
_lastUpdate = currentMillis;
updateImu();
}
};
protected:
JsonDocument doc;
void updateImu() {
_imu.update();
doc.clear();
doc["x"] = round2(getAngleX());
doc["y"] = round2(getAngleY());
doc["z"] = round2(getAngleZ());
doc["temp"] = round2(getTemp());
serializeJson(doc, message);
_socket->emit(EVENT_IMU, message);
}
private:
MPU6050 _imu;
EventSocket *_socket;
unsigned long _lastUpdate {0};
unsigned long _updateInterval {IMU_INTERVAL};
bool imu_success {false};
char message[MAX_ESP_IMU_SIZE];
};
@@ -0,0 +1,30 @@
#include <Adafruit_PWMServoDriver.h>
#include <DeviceConfigurationService.h>
class ServoController : public Adafruit_PWMServoDriver {
public:
ServoController(DeviceConfigurationService deviceConfigurationService)
: Adafruit_PWMServoDriver(), _config(deviceConfigurationService) {
begin();
}
void configure() {
setOscillatorFrequency(_config.servo_oscillator_frequency());
setPWMFreq(_config.servo_pwm_frequency());
}
void deactivate() {
isActive = false;
sleep();
}
void activate() {
isActive = true;
sleep();
}
bool isActive{false};
private:
DeviceConfigurationService _config;
};
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -39,15 +39,15 @@ lib_deps =
ArduinoJson@>=7.0.0
https://github.com/theelims/PsychicMqttClient.git#0.1.1
teckel12/NewPing@^1.9.7
rfetick/MPU6050_light@^1.1.0
adafruit/Adafruit SSD1306@^2.5.7
adafruit/Adafruit GFX Library@^1.11.5
adafruit/Adafruit BusIO@^1.9.3
rfetick/MPU6050_light@^1.1.0
adafruit/Adafruit PWM Servo Driver Library@^2.4.1
;adafruit/Adafruit HMC5883 Unified@^1.2.1
SPI
; thomasfredericks/Bounce2@ ^2.7.0
; adafruit/Adafruit PWM Servo Driver Library@^2.4.1
; adafruit/Adafruit ADS1X15@^2.4.0
; adafruit/Adafruit HMC5883 Unified@^1.2.1
; adafruit/Adafruit Unified Sensor@^1.1.11
; plageoj/UrlEncode@ ^1.0.1
; board_build.partitions = config/no_oat.csv