Skip to content

Commit

Permalink
Merge branch 'main' into esp32-adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
aschiffler authored Aug 17, 2022
2 parents 9073d2a + 3e1b120 commit c38afda
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 162 deletions.
46 changes: 23 additions & 23 deletions tools/esp8266/User_Manual.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# User Manual Ahoy DTU (on ESP8266)
15.08.2022
16.08.2022
## Introduction
see the repository [here](https://github.com/grindylow/ahoy/blob/main/tools/esp8266/README.md)
See the repository [here](https://github.com/grindylow/ahoy/blob/main/tools/esp8266/README.md)

## Setup
Assuming you have a running ahoy-dtu and you can access te setup page.
In the initial case or after click "erease settings" the fields for the inverter setup are empty.
Assuming you have a running ahoy-dtu and you can access the setup page.
In the initial case or after click "erase settings" the fields for the inverter setup are empty.

Set at least the serial number and a name for each inverter, check the "reboot after save" and click the "Save" button.

## Active Power Limit via Setup Page
I you leave the field "Active Power Limit" empty during the setup and reboot the ahoy-dtu a value of 65535 will be filled in.
That is the value you have to fill in case you want to operate the inverter without a active power limit.
If the value is 65535 or -1 after another reboot the value will be seted automatically to "100" and in the drop-down menu "relativ in percent persistent" will be seted. Of course you can do this also by your self.
That is the value you have to fill in case you want to operate the inverter without a active power limit.
If the value is 65535 or -1 after another reboot the value will be set automatically to "100" and in the drop-down menu "relative in percent persistent" will be set. Of course you can do this also by your self.

You can change the setting in the following manner.
Decide if you want to set
- an absolut value in Watt
- an relativ value in percent based on the maximum Power cababilities of the inverter
- an absolute value in Watt
- an relative value in percent based on the maximum Power capabilities of the inverter
and if this settings shall be
- persistent
- not persistent
after a power cylce of the inverter (P_DC=0 and P_AC=0 for at least 10 seconds)
after a power cycle of the inverter (P_DC=0 and P_AC=0 for at least 10 seconds)

The user has to ensure sensfule settings. Remember that for the inverters of 3rd generation the relative active power limit is in the range of 2% up to 100%.
Also an absolut active power limit below approx. 30Watt is not senseful because of the control capabilities and reactive power load.
The user has to ensure correct settings. Remember that for the inverters of 3rd generation the relative active power limit is in the range of 2% up to 100%.
Also an absolute active power limit below approx. 30Watt is not correct because of the control capabilities and reactive power load.

## Active Power Limit via MQTT
The ahoy-dtu subscribes on the topic <CHOOSEN_TOPIC_FROM_SETUP>/devcontrol/# if the mqtt broker is set-up correctly. The default topic is inverter/devcontrol/#.
Expand Down Expand Up @@ -58,10 +58,10 @@ The implementation allows to set any of the available <DevCntrlType> Commands:
Init = 0xff
} DevControlCmdType;
```
The MQTT payload will be seted on first to bytes and DATA2 will be seted on the second two bytes if the corresponding DevControlCmdType supports 4 byte data.
The MQTT payload will be set on first to bytes and DATA2 will be set on the second two bytes if the corresponding DevControlCmdType supports 4 byte data.

So as example sending any payload on inverter/devcontrol/0/1 will switch off the inverter.

## Active Power Limit via REST API
It is also implemented to set the power limit via REST API call. Therefore send a POST request to the endpoint /api.
The response will always be a json with {success:true}
Expand All @@ -76,15 +76,15 @@ The payload shall be a json formated string in the following manner
}
```
With the following value ranges


| Value | range | note |
| --------------------------- | ----------- | ------------------------------- |
| <TX_REQUEST_BYTE> | 81 or 21 | integer uint8, (0x15 or 0x51) |
| <SUB_CMD_BYTE> | [0...255] | integer uint8, subcmds eg. 0x0b |
| <PAYLOAD_INTEGER_TWO_BYTES> | [0...65535] | uint16 |
| <INVERTER_ID> | [0...3] | integer uint8 |

Example to set the active power limit non persistent to 10%
```json
{
Expand All @@ -105,28 +105,28 @@ Example to set the active power limit persistent to 600Watt
"payload2": 256
}
```

### Developer Information REST API
In the same approach as for MQTT any other SubCmd can be applied and the respsine payload can be observed in the serial logs. Eg. request the Alarm Data.
In the same approach as for MQTT any other SubCmd can be applied and the response payload can be observed in the serial logs. Eg. request the Alarm Data.



## Issues and Debuging for active power limit settings
Turn on the serial debuging in the setup. Try to have find out if the behavior is deterministic. That means can you reproduce the behavior. Be patient and wait on inverter reactions at least some minutes and beware that the DC-Power is sufficient.
Turn on the serial debugging in the setup. Try to have find out if the behavior is deterministic. That means can you reproduce the behavior. Be patient and wait on inverter reactions at least some minutes and beware that the DC-Power is sufficient.
In case of issues please report:
1. Version of firmware
2. The output of the serial debug esp. the TX messages starting with "0x51" and the RX messages starting with "0xD1" or "0xF1"
3. Which case you have tried: Setup-Page, MQTT, REST API and at what was shown on the "Visualization Page" at the Location "Limit"
4. The setting means payload, relativ, absolut, persisten, not persisten (see tables above)
4. The setting means payload, relative, absolute, persistent, not persistent (see tables above)



**Developer Information General for Active Power Limit**
⚡To be verified by field tests and feedback
Internally this values will be seted for the second two bytes for MainCmd: 0x51 SubCmd: 0x0b --> DevControl set ActivePowerLimit
Internally this values will be set for the second two bytes for MainCmd: 0x51 SubCmd: 0x0b --> DevControl set ActivePowerLimit
```C
typedef enum { // ToDo: to be verified by field tests
AbsolutNonPersistent = 0x0000, // 0
RelativNonPersistent = 0x0001, // 1
AbsolutPersistent = 0x0100, // 256
RelativPersistent = 0x0101 // 257
} PowerLimitControlType;
```
```
107 changes: 60 additions & 47 deletions tools/esp8266/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ void app::loop(void) {
Inverter<> *iv = mSys->findInverter(&p->packet[1]);
if(NULL != iv && p->packet[0] == (TX_REQ_INFO + 0x80)) { // response from get information command
DPRINTLN(DBG_DEBUG, F("Response from info request received"));
uint8_t *pid = &p->packet[9];
if (*pid == 0x00)
{
DPRINT(DBG_DEBUG, "fragment number zero received and ignored");
}
else
{
if ((*pid & 0x7F) < 5)
{
memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], len - 11);
mPayload[iv->id].len[(*pid & 0x7F) - 1] = len - 11;
}

if ((*pid & 0x80) == 0x80)
{ // Last packet
if ((*pid & 0x7f) > mPayload[iv->id].maxPackId)
{
mPayload[iv->id].maxPackId = (*pid & 0x7f);
if (*pid > 0x81)
mLastPacketId = *pid;
}
}
}
switch (mSys->InfoCmd){
case InverterDevInform_Simple:
{
Expand All @@ -94,7 +117,6 @@ void app::loop(void) {
case InverterDevInform_All:
{
DPRINT(DBG_INFO, "Response from inform all\n");
mSys->InfoCmd = RealTimeRunData_Debug; // Set back to default
break;
}
case GetLossRate:
Expand All @@ -117,23 +139,6 @@ void app::loop(void) {
}
case RealTimeRunData_Debug:
{
uint8_t *pid = &p->packet[9];
if (*pid == 0x00) {
DPRINT(DBG_DEBUG, "fragment number zero received and ignored");
} else {
if((*pid & 0x7F) < 5) {
memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], len-11);
mPayload[iv->id].len[(*pid & 0x7F) - 1] = len-11;
}

if((*pid & 0x80) == 0x80) { // Last packet
if((*pid & 0x7f) > mPayload[iv->id].maxPackId) {
mPayload[iv->id].maxPackId = (*pid & 0x7f);
if(*pid > 0x81)
mLastPacketId = *pid;
}
}
}
break;
}
}
Expand Down Expand Up @@ -175,7 +180,7 @@ void app::loop(void) {


if(rxRdy) {
processPayload(true);
processPayload(true,mSys->InfoCmd);
}
}

Expand Down Expand Up @@ -261,7 +266,7 @@ void app::loop(void) {

if(NULL != iv) {
if(!mPayload[iv->id].complete)
processPayload(false);
processPayload(false,mSys->InfoCmd);

if(!mPayload[iv->id].complete) {
mRxFailed++;
Expand All @@ -271,13 +276,7 @@ void app::loop(void) {
}
}

// reset payload data
memset(mPayload[iv->id].len, 0, MAX_PAYLOAD_ENTRIES);
mPayload[iv->id].retransmits = 0;
mPayload[iv->id].maxPackId = 0;
mPayload[iv->id].complete = false;
mPayload[iv->id].requested = true;
mPayload[iv->id].ts = mTimestamp;
resetPayload(iv);

yield();
if(mConfig.serialDebug)
Expand Down Expand Up @@ -335,6 +334,9 @@ bool app::buildPayload(uint8_t id) {

//-----------------------------------------------------------------------------
void app::processPayload(bool retransmit) {
processPayload(retransmit, RealTimeRunData_Debug);
}
void app::processPayload(bool retransmit, uint8_t cmd = RealTimeRunData_Debug) { // cmd value decides which parser is used to decode payload

#ifdef __MQTT_AFTER_RX__
boolean doMQTT = false;
Expand Down Expand Up @@ -390,12 +392,14 @@ void app::processPayload(bool retransmit) {
mSys->Radio.dumpBuf(NULL, payload, offs);
}
mRxSuccess++;
mSys->InfoCmd = RealTimeRunData_Debug; // On success set back to default

iv->getAssignment(cmd); // choose the parser
for(uint8_t i = 0; i < iv->listLen; i++) {
iv->addValue(i, payload);
iv->addValue(i, payload,cmd); // cmd value decides which parser is used to decode payload
yield();
}
iv->doCalculations();
iv->doCalculations(cmd); // cmd value decides which parser is used to decode payload

#ifdef __MQTT_AFTER_RX__
doMQTT = true;
Expand Down Expand Up @@ -432,7 +436,6 @@ void app::cbMqtt(char* topic, byte* payload, unsigned int length) {
if (std::strcmp(token,"devcontrol")==0){
token = strtok(NULL, "/");
uint8_t iv_id = std::stoi(token);
uint8_t powerLimitControl = 0;
if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){
Inverter<> *iv = this->mSys->getInverterByPos(iv_id);
if(NULL != iv) {
Expand All @@ -442,24 +445,22 @@ void app::cbMqtt(char* topic, byte* payload, unsigned int length) {
case ActivePowerContr: // Active Power Control
token = strtok(NULL, "/"); // get ControlMode aka "PowerPF.Desc" in DTU-Pro Code from topic string
if (token == NULL) // default via mqtt ommit the LimitControlMode
powerLimitControl = AbsolutNonPersistent;
iv->powerLimit[1] = AbsolutNonPersistent;
else
powerLimitControl = std::stoi(token);
// 0x0001 -> relativ limit in percent
// 0x0000 -> absolut limit in Watt
// 0x0101 -> persisten limit in percent (?)
// ...
if (true){ // if (std::stoi((char*)payload) > 0) error handling powerlimit needed?
iv->devControlCmd = ActivePowerContr;
iv->powerLimit[0] = std::stoi((char*)payload);
if (powerLimitControl >= AbsolutNonPersistent && powerLimitControl <= RelativNonPersistent)
iv->powerLimit[1] = powerLimitControl;
if (iv->powerLimit[1] & 0x0001)
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("%") );
else
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("W") );
iv->powerLimit[1] = std::stoi(token);
if (length<=5){ // if (std::stoi((char*)payload) > 0) more error handling powerlimit needed?
if (iv->powerLimit[1] >= AbsolutNonPersistent && iv->powerLimit[1] <= RelativPersistent){
iv->devControlCmd = ActivePowerContr;
iv->powerLimit[0] = std::stoi(std::string((char*)payload, (unsigned int)length)); // THX to @silversurfer
if (iv->powerLimit[1] & 0x0001)
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("%") );
else
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("W") );
}
iv->devControlRequest = true;
} else {
DPRINTLN(DBG_INFO, F("Invalid mqtt payload recevied: ") + String((char*)payload));
}
iv->devControlRequest = true;
break;
case TurnOn: // Turn On
iv->devControlCmd = TurnOn;
Expand Down Expand Up @@ -518,7 +519,7 @@ String app::getStatistics(void) {
iv = mSys->getInverterByPos(i);
if(NULL != iv) {
bool avail = true;
content += F("Inverter '") + String(iv->name) + F("' is ");
content += F("Inverter '") + String(iv->name) + F(" (FW-Version: ") + String(iv->fwVersion) +F(")") + F("' is ");
if(!iv->isAvailable(mTimestamp)) {
content += F("not ");
avail = false;
Expand Down Expand Up @@ -937,3 +938,15 @@ void app::setupMqtt(void) {
}
}
}

//-----------------------------------------------------------------------------
void app::resetPayload(Inverter<>* iv)
{
// reset payload data
memset(mPayload[iv->id].len, 0, MAX_PAYLOAD_ENTRIES);
mPayload[iv->id].retransmits = 0;
mPayload[iv->id].maxPackId = 0;
mPayload[iv->id].complete = false;
mPayload[iv->id].requested = true;
mPayload[iv->id].ts = mTimestamp;
}
4 changes: 3 additions & 1 deletion tools/esp8266/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class app {
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
void resetPayload(Inverter<>* iv);
String getStatistics(void);
String getLiveData(void);
String getJson(void);
Expand Down Expand Up @@ -147,9 +148,10 @@ class app {
void loadDefaultConfig(void);
void loadEEpconfig(void);
void setupMqtt(void);

bool buildPayload(uint8_t id);
void processPayload(bool retransmit);
void processPayload(bool retransmit, uint8_t cmd);

void sendMqttDiscoveryConfig(void);
const char* getFieldDeviceClass(uint8_t fieldId);
Expand Down
10 changes: 5 additions & 5 deletions tools/esp8266/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 12
#define VERSION_PATCH 14


//-------------------------------------
Expand Down Expand Up @@ -58,10 +58,10 @@ typedef enum {
} DevControlCmdType;

typedef enum { // ToDo: to be verified by field tests
AbsolutNonPersistent = 0x0000, // 0
RelativNonPersistent = 0x0001, // 1
AbsolutPersistent = 0x0100, // 256
RelativPersistent = 0x0101 // 257
AbsolutNonPersistent = 0UL, // 0x0000
RelativNonPersistent = 1UL, // 0x0001
AbsolutPersistent = 256UL, // 0x0100
RelativPersistent = 257UL // 0x0101
} PowerLimitControlType;

// minimum serial interval
Expand Down
Loading

0 comments on commit c38afda

Please sign in to comment.