Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jun 14, 2024
1 parent e1dbcfb commit 8f931bd
Show file tree
Hide file tree
Showing 25 changed files with 297 additions and 299 deletions.
61 changes: 29 additions & 32 deletions data/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ <h1>YaSolR Configuration</h1>
const supportedConfig = {

"Output 1 Control": "TITLE",
o1_resistance: ["Output 1 Resistance (Ohm)", "uint"],
o1_dim_limit: ["Dimmer Limiter (limit dimmer duty 0-4095)", "uint"],
o1_ad_enable: ["Dimmer Automatic Control", "switch"],
o1_ab_enable: ["Bypass Automatic Control", "switch"],
Expand All @@ -73,6 +74,7 @@ <h1>YaSolR Configuration</h1>
o1_temp_stop: ["Bypass Stop Temperature (C)", "uint"],

"Output 2 Control": "TITLE",
o2_resistance: ["Output 2 Resistance (Ohm)", "uint"],
o2_dim_limit: ["Dimmer Limiter (limit dimmer duty 0-4095)", "uint"],
o2_ad_enable: ["Dimmer Automatic Control", "switch"],
o2_ab_enable: ["Bypass Automatic Control", "switch"],
Expand All @@ -89,6 +91,27 @@ <h1>YaSolR Configuration</h1>
"Management": "TITLE",
debug_enable: ["Enable Debug Logging", "switch"],

Network: "TITLE",
admin_pwd: ["Admin password", "password"],
ntp_server: ["NTP Server", "string"],
ntp_timezone: ["Timezone", "tz"],
wifi_ssid: ["WiFi SSID", "string"],
wifi_pwd: ["WiFi Password", "password"],
ap_mode_enable: ["Stay in AP Mode", "switch"],

MQTT: "TITLE",
mqtt_server: ["Server", "string"],
mqtt_port: ["Port", "uint"],
mqtt_user: ["Username", "string"],
mqtt_pwd: ["Password", "string"],
mqtt_secure: ["SSL / TLS", "switch"],
mqtt_pub_itvl: ["Publish Interval (s)", "uint"],
mqtt_topic: ["Base Topic", "string"],
ha_disco_enable: ["Home Assistant Integration", "switch"],
ha_disco_topic: ["Home Assistant Discovery Topic", "string"],
grid_volt_mqtt: ["Grid Voltage from MQTT Topic", "string"],
grid_pow_mqtt: ["Grid Power from MQTT Topic", "string"],

"GPIO (config)": "TITLE",
pin_o1_dim: ["Dimmer for Output 1", "pin"],
pin_o2_dim: ["Dimmer for Output 2", "pin"],
Expand Down Expand Up @@ -129,40 +152,14 @@ <h1>YaSolR Configuration</h1>
zcd_enable: ["Enable Zero-Cross Detection", "switch"],

"Hardware (config)": "TITLE",
relay1_type: ["Relay 1 Type", "select", "NO,NC"],
relay2_type: ["Relay 2 Type", "select", "NO,NC"],
o1_relay_type: ["Output 1 Bypass Relay Type", "select", "NO,NC"],
o2_relay_type: ["Output 2 Bypass Relay Type", "select", "NO,NC"],
grid_freq: ["Nominal Grid Frequency", "select", "50,60"],
disp_speed: ["Display Speed (s)", "select", "1,2,3,4,5,6,7,8,9,10"],
disp_type: ["Display Type", "select", "SH1106,SH1107,SSD1306"],
disp_angle: ["Display Rotation", "select", "0,90,180,270"],
disp_speed: ["Display Speed (s)", "select", "1,2,3,4,5,6,7,8,9,10"],
grid_freq: ["Grid frequency (default)", "select", "50,60"],

MQTT: "TITLE",
mqtt_server: ["Server", "string"],
mqtt_port: ["Port", "uint"],
mqtt_user: ["Username", "string"],
mqtt_pwd: ["Password", "string"],
mqtt_secure: ["SSL / TLS", "switch"],
mqtt_pub_itvl: ["Publish Interval (s)", "uint"],
mqtt_topic: ["Base Topic", "string"],
grid_volt_mqtt: ["Grid Voltage from MQTT Topic", "string"],
grid_pow_mqtt: ["Grid Power from MQTT Topic", "string"],
ha_disco_enable: ["Home Assistant Integration", "switch"],
ha_disco_topic: ["Home Assistant Discovery Topic", "string"],

Network: "TITLE",
admin_pwd: ["Admin password", "password"],
ntp_server: ["NTP Server", "string"],
ntp_timezone: ["NTP Timezone", "tz"],
wifi_ssid: ["WiFi SSID", "string"],
wifi_pwd: ["WiFi Password", "password"],
ap_mode_enable: ["Stay in AP Mode", "switch"],





o1_relay_type: ["Output 1 Bypass Relay Type", "select", "NO,NC"],
o2_relay_type: ["Output 2 Bypass Relay Type", "select", "NO,NC"],
relay1_type: ["Relay 1 Type", "select", "NO,NC"],
relay2_type: ["Relay 2 Type", "select", "NO,NC"],
};

async function onInputChange(event) {
Expand Down
1 change: 1 addition & 0 deletions include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ extern Mycila::Task otaTask;
extern Mycila::Task profilerTask;
extern Mycila::Task resetTask;
extern Mycila::Task restartTask;
extern Mycila::Task routerDebugTask;
extern Mycila::Task dashboardTask;
#ifdef APP_MODEL_TRIAL
extern Mycila::Task trialTask;
Expand Down
4 changes: 4 additions & 0 deletions include/YaSolRDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define YASOLR_BOOL(a) ((a) ? YASOLR_TRUE : YASOLR_FALSE)
#define YASOLR_STATE(a) ((a) ? YASOLR_ON : YASOLR_OFF)

#define TAG "YASOLR"

// password configuration keys

#define KEY_ADMIN_PASSWORD "admin_pwd"
Expand Down Expand Up @@ -72,13 +74,15 @@
#define KEY_OUTPUT1_DAYS "o1_days"
#define KEY_OUTPUT1_DIMMER_LIMITER "o1_dim_limit"
#define KEY_OUTPUT1_RELAY_TYPE "o1_relay_type"
#define KEY_OUTPUT1_RESISTANCE "o1_resistance"
#define KEY_OUTPUT1_TEMPERATURE_START "o1_temp_start"
#define KEY_OUTPUT1_TEMPERATURE_STOP "o1_temp_stop"
#define KEY_OUTPUT1_TIME_START "o1_time_start"
#define KEY_OUTPUT1_TIME_STOP "o1_time_stop"
#define KEY_OUTPUT2_DAYS "o2_days"
#define KEY_OUTPUT2_DIMMER_LIMITER "o2_dim_limit"
#define KEY_OUTPUT2_RELAY_TYPE "o2_relay_type"
#define KEY_OUTPUT2_RESISTANCE "o2_resistance"
#define KEY_OUTPUT2_TEMPERATURE_START "o2_temp_start"
#define KEY_OUTPUT2_TEMPERATURE_STOP "o2_temp_stop"
#define KEY_OUTPUT2_TIME_START "o2_time_start"
Expand Down
14 changes: 7 additions & 7 deletions include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ namespace YaSolR {
Card _output1THDi = Card(&dashboard, ENERGY_CARD, "THDi", "%");
Card _output1Voltage = Card(&dashboard, ENERGY_CARD, "Voltage", "V");
Card _output1Current = Card(&dashboard, ENERGY_CARD, "Current", "A");
Card _output1Resistance = Card(&dashboard, ENERGY_CARD, "Resistance", "Ω");
Card _output1Energy = Card(&dashboard, ENERGY_CARD, "Energy", "kWh");
Card _output1Resistance = Card(&dashboard, TEXT_INPUT_CARD, "Output 1 Resistance", "Ω");
Card _output1DimmerAuto = Card(&dashboard, BUTTON_CARD, "Dimmer Automatic Control");
Card _output1DimmerLimiter = Card(&dashboard, SLIDER_CARD, "Dimmer Limiter", "", 0, YASOLR_DIMMER_MAX_LEVEL, 1);
Card _output1BypassAuto = Card(&dashboard, BUTTON_CARD, "Bypass Automatic Control");
Expand All @@ -121,8 +121,8 @@ namespace YaSolR {
Card _output2THDi = Card(&dashboard, ENERGY_CARD, "THDi", "%");
Card _output2Voltage = Card(&dashboard, ENERGY_CARD, "Voltage", "V");
Card _output2Current = Card(&dashboard, ENERGY_CARD, "Current", "A");
Card _output2Resistance = Card(&dashboard, ENERGY_CARD, "Resistance", "Ω");
Card _output2Energy = Card(&dashboard, ENERGY_CARD, "Energy", "kWh");
Card _output2Resistance = Card(&dashboard, TEXT_INPUT_CARD, "Output 2 Resistance", "Ω");
Card _output2DimmerAuto = Card(&dashboard, BUTTON_CARD, "Dimmer Automatic Control");
Card _output2DimmerLimiter = Card(&dashboard, SLIDER_CARD, "Dimmer Limiter", "", 0, YASOLR_DIMMER_MAX_LEVEL, 1);
Card _output2BypassAuto = Card(&dashboard, BUTTON_CARD, "Bypass Automatic Control");
Expand Down Expand Up @@ -214,15 +214,15 @@ namespace YaSolR {

Tab _hardwareConfigTab = Tab(&dashboard, "\u2699 Hardware Config");
Card _gridFreq = Card(&dashboard, DROPDOWN_CARD, "Nominal Grid Frequency");
Card _displayType = Card(&dashboard, DROPDOWN_CARD, "Display Type");
Card _displaySpeed = Card(&dashboard, SLIDER_CARD, "Display Speed", "s", 1, 10, 1);
Card _displayType = Card(&dashboard, DROPDOWN_CARD, "Display Type");
Card _displayRotation = Card(&dashboard, DROPDOWN_CARD, "Display Rotation");
Card _relay1Type = Card(&dashboard, DROPDOWN_CARD, "Relay 1 Type");
Card _relay2Type = Card(&dashboard, DROPDOWN_CARD, "Relay 2 Type");
Card _output1RelayType = Card(&dashboard, DROPDOWN_CARD, "Output 1 Bypass Relay Type");
Card _output2RelayType = Card(&dashboard, DROPDOWN_CARD, "Output 2 Bypass Relay Type");
Card _output1PZEMSync = Card(&dashboard, PUSH_BUTTON_CARD, "Output 1 PZEM Pairing");
Card _output2PZEMSync = Card(&dashboard, PUSH_BUTTON_CARD, "Output 2 PZEM Pairing");
Card _output1RelayType = Card(&dashboard, DROPDOWN_CARD, "Output 1 Bypass Relay Type");
Card _output2RelayType = Card(&dashboard, DROPDOWN_CARD, "Output 2 Bypass Relay Type");
Card _relay1Type = Card(&dashboard, DROPDOWN_CARD, "Relay 1 Type");
Card _relay2Type = Card(&dashboard, DROPDOWN_CARD, "Relay 2 Type");
#endif

private:
Expand Down
58 changes: 51 additions & 7 deletions lib/MycilaRouter/MycilaGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ namespace Mycila {
float voltage = 0;
} GridMetrics;

typedef enum {
SOURCE_NONE,
SOURCE_JSY,
SOURCE_MQTT
} GridSource;

class Grid {
public:
explicit Grid(JSY& jsy) : _jsy(&jsy) {}
Expand All @@ -37,7 +43,6 @@ namespace Mycila {
_mqttPower = power;
_mqttPowerTime = millis();
}

void applyExpiration() {
if (_mqttVoltageTime > 0 && millis() - _mqttVoltageTime >= _expiration) {
_mqttVoltage = 0;
Expand All @@ -49,24 +54,61 @@ namespace Mycila {
}
}

bool isConnected() const { return (_jsy->isConnected() && _jsy->getVoltage2()) > 0 || (_mqttVoltageTime > 0 && _mqttVoltage > 0); }
// grid source logic:
// for power: MQTT has priority
// for voltage: JSY has priority
GridSource getPowerSource() const {
if (_mqttPowerTime)
return SOURCE_MQTT;
if (_jsy->isConnected() && _jsy->getVoltage2() > 0)
return SOURCE_JSY;
return SOURCE_NONE;
}
GridSource getVoltageSource() const {
if (_jsy->isConnected() && _jsy->getVoltage2())
return SOURCE_JSY;
if (_mqttVoltageTime && _mqttVoltage)
return SOURCE_MQTT;
return SOURCE_NONE;
}

float getPower() const { return _mqttPowerTime ? _mqttPower : _jsy->getPower2(); }
bool isConnected() const { return getVoltageSource() != GridSource::SOURCE_NONE; }

float getVoltage() const { return _jsy->isConnected() ? _jsy->getVoltage2() : (_mqttVoltageTime && _mqttVoltage ? _mqttVoltage : 0); }
float getPower() const {
switch (getPowerSource()) {
case SOURCE_JSY:
return _jsy->getPower2();
case SOURCE_MQTT:
return _mqttPower;
default:
return 0;
break;
}
}

float getVoltage() const {
switch (getVoltageSource()) {
case SOURCE_JSY:
return _jsy->getVoltage2();
case SOURCE_MQTT:
return _mqttVoltage;
default:
return 0;
break;
}
}

void getMetrics(GridMetrics& metrics) const {
metrics.voltage = getVoltage();
metrics.power = getPower();

metrics.connected = metrics.voltage > 0;
// only available if connected to JSY
metrics.apparentPower = _jsy->getApparentPower2();
metrics.current = _jsy->getCurrent2();
metrics.energy = _jsy->getEnergy2();
metrics.energyReturned = _jsy->getEnergyReturned2();
metrics.frequency = _jsy->getFrequency();
metrics.powerFactor = _jsy->getPowerFactor2();

metrics.connected = metrics.voltage > 0;
}

#ifdef MYCILA_JSON_SUPPORT
Expand All @@ -84,6 +126,8 @@ namespace Mycila {
root["metrics"]["voltage"] = metrics.voltage;
root["mqtt"]["power"] = _mqttPower;
root["mqtt"]["voltage"] = _mqttVoltage;
root["source"]["power"] = getPowerSource() == SOURCE_JSY ? "jsy" : (getPowerSource() == SOURCE_MQTT ? "mqtt" : "none");
root["source"]["voltage"] = getVoltageSource() == SOURCE_JSY ? "jsy" : (getVoltageSource() == SOURCE_MQTT ? "mqtt" : "none");
}
#endif

Expand Down
57 changes: 16 additions & 41 deletions lib/MycilaRouter/MycilaRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <ArduinoJson.h>
#endif

#ifndef MYCILA_ROUTER_OUTPUT_COUNT
#define MYCILA_ROUTER_OUTPUT_COUNT 2
#endif

namespace Mycila {

typedef struct {
Expand All @@ -22,6 +26,7 @@ namespace Mycila {
float power = 0;
float powerFactor = 0;
float thdi = 0;
RouterOutputMetrics outputs[MYCILA_ROUTER_OUTPUT_COUNT] = {};
} RouterMetrics;

class Router {
Expand All @@ -40,55 +45,25 @@ namespace Mycila {
return false;
}

float getPower() const {
float power = 0;
bool routing = false;

for (const auto& output : _outputs) {
if (output->getState() == RouterOutputState::OUTPUT_ROUTING) {
power += output->getPower();
routing = true;
}
}

if (routing && power == 0)
power = _jsy->getPower1();

return power;
}

void getMetrics(RouterMetrics& metrics) const {
bool routing = false;
size_t index = 0;

for (const auto& output : _outputs) {
if (output->getState() == RouterOutputState::OUTPUT_ROUTING) {
routing = true;
Mycila::RouterOutputMetrics outputMetrics;
output->getMetrics(outputMetrics);
metrics.apparentPower += outputMetrics.apparentPower;
metrics.current += outputMetrics.current;
metrics.energy += outputMetrics.energy;
metrics.power += outputMetrics.power;
}
}

if (routing) {
if (metrics.apparentPower == 0)
metrics.apparentPower = _jsy->getApparentPower1();
assert(index < MYCILA_ROUTER_OUTPUT_COUNT);

if (metrics.current == 0)
metrics.current = _jsy->getCurrent1();
output->getMetrics(metrics.outputs[index]);

if (metrics.power == 0)
metrics.power = _jsy->getPower1();
metrics.apparentPower += metrics.outputs[index].apparentPower;
metrics.current += metrics.outputs[index].current;
metrics.energy += metrics.outputs[index].energy;
metrics.power += metrics.outputs[index].power;

metrics.powerFactor = metrics.apparentPower == 0 ? 0 : metrics.power / metrics.apparentPower;
if (metrics.powerFactor == 0)
metrics.powerFactor = _jsy->getPowerFactor1();

metrics.thdi = metrics.powerFactor == 0 ? 0 : sqrt(1 / pow(metrics.powerFactor, 2) - 1);
index++;
}

metrics.powerFactor = metrics.apparentPower == 0 ? 0 : metrics.power / metrics.apparentPower;
metrics.thdi = metrics.powerFactor == 0 ? 0 : sqrt(1 / pow(metrics.powerFactor, 2) - 1);

if (metrics.energy == 0)
metrics.energy = _jsy->getEnergy1() + _jsy->getEnergyReturned1();
}
Expand Down
Loading

0 comments on commit 8f931bd

Please sign in to comment.