Browse Source

Merge branch 'development03' into development03

pull/1541/head
geronet1 8 months ago
committed by GitHub
parent
commit
76b9bd8330
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      .gitattributes
  2. 1
      .gitignore
  3. 71
      manual/factory_firmware.md
  4. 392
      patches/GxEPD2_HAL.patch
  5. 362
      patches/GxEPD2_SW_SPI.patch
  6. 55
      scripts/add_littlefs_binary.py
  7. 4
      scripts/applyPatches.py
  8. 42
      src/CHANGES.md
  9. 75
      src/app.cpp
  10. 25
      src/app.h
  11. 1
      src/appInterface.h
  12. 5
      src/config/config.h
  13. 42
      src/config/settings.h
  14. 2
      src/defines.h
  15. 27
      src/hm/CommQueue.h
  16. 4
      src/hm/Communication.h
  17. 2
      src/hm/hmDefines.h
  18. 101
      src/hm/hmInverter.h
  19. 6
      src/hm/hmSystem.h
  20. 18
      src/hm/nrfHal.h
  21. 24
      src/hms/CmtRadio.h
  22. 16
      src/hms/cmtHal.h
  23. 54
      src/network/AhoyEthernet.h
  24. 31
      src/network/AhoyEthernetSpi.h
  25. 12
      src/network/AhoyNetwork.h
  26. 62
      src/network/AhoyWifiEsp32.h
  27. 15
      src/network/AhoyWifiEsp8266.h
  28. 4
      src/platformio.ini
  29. 24
      src/plugins/Display/Display_ePaper.cpp
  30. 7
      src/plugins/Display/Display_ePaper.h
  31. 304
      src/plugins/Display/epdHal.h
  32. 67
      src/plugins/MaxPower.h
  33. 6
      src/plugins/history.h
  34. 2
      src/publisher/pubMqtt.h
  35. 10
      src/publisher/pubMqttIvData.h
  36. 2
      src/utils/dbg.h
  37. 3
      src/utils/spiPatcher.cpp
  38. 78
      src/utils/spiPatcher.h
  39. 61
      src/web/RestApi.h
  40. 2
      src/web/html/about.html
  41. 37
      src/web/html/history.html
  42. 10
      src/web/html/setup.html
  43. 3
      src/web/html/style.css
  44. 32
      src/web/html/visualization.html
  45. 13
      src/web/html/wizard.html
  46. 18
      src/web/lang.json
  47. 5
      src/web/web.h

1
.gitattributes

@ -0,0 +1 @@
patches/GxEPD2_HAL.patch eol=lf

1
.gitignore

@ -7,6 +7,7 @@
src/config/config_override.h src/config/config_override.h
src/web/html/h/* src/web/html/h/*
src/web/html/tmp/* src/web/html/tmp/*
src/data/*
/**/Debug /**/Debug
/**/v16/* /**/v16/*
*.db *.db

71
manual/factory_firmware.md

@ -0,0 +1,71 @@
# Generate factory firmware (ESP32)
If the firmware should already contain predefined settings this guide will help you to compile these into a single binary file.
## Generate default settings
First install on the requested platform the standard firmware and configure everything to your needs. Once you did all changes store them and export them to a `json` file.
## Further prepare default settings
First create a directory `data` inside the following project path: `src/`.
As the export removes all your password you need to add them again to the `json` file. Open the `json` file with a text editor and search for all the `"pwd": ""`. Between the second bunch of quotation marks you have to place the password.
*Note: It's recommended to keep all information in one line to save space on the ESP littlefs partition*
Next rename your export file to `settings.json` and move it to the new created directory. It should be look similar to this:
```
ahoy
|-- src
|-- data
|-- settings.json
|-- config
|-- network
...
```
## modify platform.ini to build factory binary
Open the file `src/platformio.ini` and uncomment the following line `#post:../scripts/add_littlefs_binary.py` (remove the `#`)
## build firmware
Choose your prefered environment and build firmware as usual. Once the process is finished you should find along with the standard `firmware.bin` an additional file called `firmware.factory.bin`. Both files are located here: `src/.pio/build/[ENVIRONMENT]/`
## Upload to device
Navigate to the firmware output directory `src/.pio/build/[ENVIRONMENT]/` and open a terminal.
### ESP32
Python:
`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x1000 bootloader.bin 0x8000 partitions.bin 0x10000 firmware.factory.bin`
Windows:
`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x1000 bootloader.bin 0x8000 partitions.bin 0x10000 firmware.factory.bin`
### ESP32-S3 (OpenDTU Fusion Board)
Python:
`esptool.py -b 921600 write_flash --flash_mode dio --flash_size detect 0x0 firmware.factory.bin`
Windows:
`esptool.exe -b 921600 write_flash --flash_mode dio --flash_size detect 0x0 firmware.factory.bin`
For a 4MB flash size the upload should be finished within 22 seconds.
## Testing
Reboot your ESP an check if all your settings are present.
## Keep updated with 'Mainline'
From time to time a new version of AhoyDTU will be published. To get this changes into your alread prepared factory binary generation environment you have to do only a few steps:
1. revert the changes of `platformio.ini` by executing from repository root: `git checkout src/platformio.ini`
2. pull new changes from remote: `git pull`
3. modify the `platformio.ini` again as you can read above (remove comment)
4. build and upload
5. enjoy

392
patches/GxEPD2_HAL.patch

@ -0,0 +1,392 @@
diff --git a/src/GxEPD2_EPD.cpp b/src/GxEPD2_EPD.cpp
index 8df8bef..e9dfb19 100644
--- a/src/GxEPD2_EPD.cpp
+++ b/src/GxEPD2_EPD.cpp
@@ -17,11 +17,10 @@
#include <avr/pgmspace.h>
#endif
-GxEPD2_EPD::GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout,
+GxEPD2_EPD::GxEPD2_EPD(GxEPD2_HalInterface *hal, int16_t busy_level, uint32_t busy_timeout,
uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu) :
WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu),
- _cs(cs), _dc(dc), _rst(rst), _busy(busy), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false),
- _pSPIx(&SPI), _spi_settings(4000000, MSBFIRST, SPI_MODE0)
+ _hal(hal), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false)
{
_initial_write = true;
_initial_refresh = true;
@@ -54,44 +53,10 @@ void GxEPD2_EPD::init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset
Serial.begin(serial_diag_bitrate);
_diag_enabled = true;
}
- if (_cs >= 0)
- {
- digitalWrite(_cs, HIGH); // preset (less glitch for any analyzer)
- pinMode(_cs, OUTPUT);
- digitalWrite(_cs, HIGH); // set (needed e.g. for RP2040)
- }
- if (_dc >= 0)
- {
- digitalWrite(_dc, HIGH); // preset (less glitch for any analyzer)
- pinMode(_dc, OUTPUT);
- digitalWrite(_dc, HIGH); // set (needed e.g. for RP2040)
- }
- _reset();
- if (_busy >= 0)
- {
- pinMode(_busy, INPUT);
- }
- _pSPIx->begin();
- if (_busy == MISO) // may be overridden
- {
- pinMode(_busy, INPUT);
- }
- if (_dc == MISO) // may be overridden, TTGO T5 V2.66
- {
- pinMode(_dc, OUTPUT);
- }
- if (_cs == MISO) // may be overridden
- {
- pinMode(_cs, INPUT);
- }
}
void GxEPD2_EPD::end()
{
- _pSPIx->end();
- if (_cs >= 0) pinMode(_cs, INPUT);
- if (_dc >= 0) pinMode(_dc, INPUT);
- if (_rst >= 0) pinMode(_rst, INPUT);
}
void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter)
@@ -100,34 +65,27 @@ void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void*
_busy_callback_parameter = busy_callback_parameter;
}
-void GxEPD2_EPD::selectSPI(SPIClass& spi, SPISettings spi_settings)
-{
- _pSPIx = &spi;
- _spi_settings = spi_settings;
-}
-
void GxEPD2_EPD::_reset()
{
- if (_rst >= 0)
{
if (_pulldown_rst_mode)
{
- digitalWrite(_rst, LOW);
- pinMode(_rst, OUTPUT);
- digitalWrite(_rst, LOW);
+ _hal->rst(LOW);
+ _hal->rstMode(OUTPUT);
+ _hal->rst(LOW);
delay(_reset_duration);
- pinMode(_rst, INPUT_PULLUP);
+ _hal->rstMode(INPUT_PULLUP);
delay(_reset_duration > 10 ? _reset_duration : 10);
}
else
{
- digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, preset (less glitch for any analyzer)
- pinMode(_rst, OUTPUT);
- digitalWrite(_rst, HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, set (needed e.g. for RP2040)
+ _hal->rst(HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, preset (less glitch for any analyzer)
+ _hal->rstMode(OUTPUT);
+ _hal->rst(HIGH); // NEEDED for Waveshare "clever" reset circuit, power controller before reset pulse, set (needed e.g. for RP2040)
delay(10); // NEEDED for Waveshare "clever" reset circuit, at least delay(2);
- digitalWrite(_rst, LOW);
+ _hal->rst(LOW);
delay(_reset_duration);
- digitalWrite(_rst, HIGH);
+ _hal->rst(HIGH);
delay(_reset_duration > 10 ? _reset_duration : 10);
}
_hibernating = false;
@@ -136,16 +94,15 @@ void GxEPD2_EPD::_reset()
void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time)
{
- if (_busy >= 0)
{
delay(1); // add some margin to become active
unsigned long start = micros();
while (1)
{
- if (digitalRead(_busy) != _busy_level) break;
+ if (_hal->getBusy() != _busy_level) break;
if (_busy_callback) _busy_callback(_busy_callback_parameter);
else delay(1);
- if (digitalRead(_busy) != _busy_level) break;
+ if (_hal->getBusy() != _busy_level) break;
if (micros() - start > _busy_timeout)
{
Serial.println("Busy Timeout!");
@@ -169,120 +126,59 @@ void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time)
}
(void) start;
}
- else delay(busy_time);
}
void GxEPD2_EPD::_writeCommand(uint8_t c)
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_dc >= 0) digitalWrite(_dc, LOW);
- if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(c);
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- if (_dc >= 0) digitalWrite(_dc, HIGH);
- _pSPIx->endTransaction();
+ _hal->writeCmd(c);
}
void GxEPD2_EPD::_writeData(uint8_t d)
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(d);
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _hal->write(d);
}
void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n)
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_cs >= 0) digitalWrite(_cs, LOW);
- for (uint16_t i = 0; i < n; i++)
- {
- _pSPIx->transfer(*data++);
- }
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _hal->write(data, n);
}
void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_cs >= 0) digitalWrite(_cs, LOW);
- for (uint16_t i = 0; i < n; i++)
- {
- _pSPIx->transfer(pgm_read_byte(&*data++));
- }
- while (fill_with_zeroes > 0)
- {
- _pSPIx->transfer(0x00);
- fill_with_zeroes--;
- }
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _hal->write(data, n, fill_with_zeroes);
}
void GxEPD2_EPD::_writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
{
- _pSPIx->beginTransaction(_spi_settings);
- for (uint8_t i = 0; i < n; i++)
- {
- if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(pgm_read_byte(&*data++));
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- }
- while (fill_with_zeroes > 0)
- {
- if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(0x00);
- fill_with_zeroes--;
- if (_cs >= 0) digitalWrite(_cs, HIGH);
+ _hal->write(data, n);
+ if (fill_with_zeroes > 0) {
+ uint8_t buf[fill_with_zeroes];
+ memset(buf, 0, fill_with_zeroes);
+ _hal->write(buf, fill_with_zeroes);
}
- _pSPIx->endTransaction();
}
void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen)
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_dc >= 0) digitalWrite(_dc, LOW);
- if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(*pCommandData++);
- if (_dc >= 0) digitalWrite(_dc, HIGH);
- for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
- {
- _pSPIx->transfer(*pCommandData++);
- }
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _hal->writeCmd(pCommandData, datalen, false);
}
void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen)
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_dc >= 0) digitalWrite(_dc, LOW);
- if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
- if (_dc >= 0) digitalWrite(_dc, HIGH);
- for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
- {
- _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
- }
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _hal->writeCmd(pCommandData, datalen, true);
}
void GxEPD2_EPD::_startTransfer()
{
- _pSPIx->beginTransaction(_spi_settings);
- if (_cs >= 0) digitalWrite(_cs, LOW);
+ _hal->startTransfer();
}
void GxEPD2_EPD::_transfer(uint8_t value)
{
- _pSPIx->transfer(value);
+ _hal->transfer(value);
}
void GxEPD2_EPD::_endTransfer()
{
- if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _hal->endTransfer();
}
diff --git a/src/GxEPD2_EPD.h b/src/GxEPD2_EPD.h
index 34c1145..1e8ea64 100644
--- a/src/GxEPD2_EPD.h
+++ b/src/GxEPD2_EPD.h
@@ -13,9 +13,9 @@
#define _GxEPD2_EPD_H_
#include <Arduino.h>
-#include <SPI.h>
#include <GxEPD2.h>
+#include <GxEPD2_Hal.h>
#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wsign-compare"
@@ -31,7 +31,7 @@ class GxEPD2_EPD
const bool hasPartialUpdate;
const bool hasFastPartialUpdate;
// constructor
- GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout,
+ GxEPD2_EPD(GxEPD2_HalInterface *hal, int16_t busy_level, uint32_t busy_timeout,
uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu);
virtual void init(uint32_t serial_diag_bitrate = 0); // serial_diag_bitrate = 0 : disabled
virtual void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false);
@@ -97,7 +97,6 @@ class GxEPD2_EPD
{
return (a > b ? a : b);
};
- void selectSPI(SPIClass& spi, SPISettings spi_settings);
protected:
void _reset();
void _waitWhileBusy(const char* comment = 0, uint16_t busy_time = 5000);
@@ -112,16 +111,15 @@ class GxEPD2_EPD
void _transfer(uint8_t value);
void _endTransfer();
protected:
- int16_t _cs, _dc, _rst, _busy, _busy_level;
+ GxEPD2_HalInterface *_hal;
+ int16_t _busy_level;
uint32_t _busy_timeout;
bool _diag_enabled, _pulldown_rst_mode;
- SPIClass* _pSPIx;
- SPISettings _spi_settings;
bool _initial_write, _initial_refresh;
bool _power_is_on, _using_partial_mode, _hibernating;
bool _init_display_done;
uint16_t _reset_duration;
- void (*_busy_callback)(const void*);
+ void (*_busy_callback)(const void*);
const void* _busy_callback_parameter;
};
diff --git a/src/GxEPD2_Hal.h b/src/GxEPD2_Hal.h
new file mode 100644
index 0000000..13424b6
--- /dev/null
+++ b/src/GxEPD2_Hal.h
@@ -0,0 +1,19 @@
+#pragma once
+
+class GxEPD2_HalInterface {
+ public:
+ virtual void rstMode(uint8_t mode) = 0;
+ virtual void rst(bool level) = 0;
+ virtual int getBusy(void) = 0;
+ virtual bool isRst(void) = 0;
+
+ virtual void write(uint8_t buf) = 0;
+ virtual void write(const uint8_t *buf, uint16_t n) = 0;
+ virtual void write(const uint8_t *buf, uint16_t n, int16_t fill_with_zeroes) = 0;
+ virtual void writeCmd(const uint8_t val) = 0;
+ virtual void writeCmd(const uint8_t* pCommandData, uint8_t datalen, bool isPGM) = 0;
+
+ virtual void startTransfer(void) = 0;
+ virtual void endTransfer(void) = 0;
+ virtual void transfer(const uint8_t val) = 0;
+};
diff --git a/src/epd/GxEPD2_150_BN.cpp b/src/epd/GxEPD2_150_BN.cpp
index bfb3ddf..dba3d78 100644
--- a/src/epd/GxEPD2_150_BN.cpp
+++ b/src/epd/GxEPD2_150_BN.cpp
@@ -14,8 +14,8 @@
#include "GxEPD2_150_BN.h"
-GxEPD2_150_BN::GxEPD2_150_BN(int16_t cs, int16_t dc, int16_t rst, int16_t busy) :
- GxEPD2_EPD(cs, dc, rst, busy, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
+GxEPD2_150_BN::GxEPD2_150_BN(GxEPD2_HalInterface *hal) :
+ GxEPD2_EPD(hal, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
{
}
@@ -269,7 +269,7 @@ void GxEPD2_150_BN::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
int16_t y1 = y < 0 ? 0 : y; // limit
w1 = x1 + w1 < int16_t(WIDTH) ? w1 : int16_t(WIDTH) - x1; // limit
h1 = y1 + h1 < int16_t(HEIGHT) ? h1 : int16_t(HEIGHT) - y1; // limit
- if ((w1 <= 0) || (h1 <= 0)) return;
+ if ((w1 <= 0) || (h1 <= 0)) return;
// make x1, w1 multiple of 8
w1 += x1 % 8;
if (w1 % 8 > 0) w1 += 8 - w1 % 8;
@@ -287,7 +287,7 @@ void GxEPD2_150_BN::powerOff()
void GxEPD2_150_BN::hibernate()
{
_PowerOff();
- if (_rst >= 0)
+ if (_hal->isRst())
{
_writeCommand(0x10); // deep sleep mode
_writeData(0x1); // enter deep sleep
diff --git a/src/epd/GxEPD2_150_BN.h b/src/epd/GxEPD2_150_BN.h
index bc46a45..954b9c4 100644
--- a/src/epd/GxEPD2_150_BN.h
+++ b/src/epd/GxEPD2_150_BN.h
@@ -16,6 +16,7 @@
#define _GxEPD2_150_BN_H_
#include "../GxEPD2_EPD.h"
+#include "../GxEPD2_Hal.h"
class GxEPD2_150_BN : public GxEPD2_EPD
{
@@ -33,7 +34,7 @@ class GxEPD2_150_BN : public GxEPD2_EPD
static const uint16_t full_refresh_time = 4000; // ms, e.g. 3825000us
static const uint16_t partial_refresh_time = 800; // ms, e.g. 736000us
// constructor
- GxEPD2_150_BN(int16_t cs, int16_t dc, int16_t rst, int16_t busy);
+ GxEPD2_150_BN(GxEPD2_HalInterface *hal);
// methods (virtual)
// Support for Bitmaps (Sprites) to Controller Buffer and to Screen
void clearScreen(uint8_t value = 0xFF); // init controller memory and screen (default white)

362
patches/GxEPD2_SW_SPI.patch

@ -1,362 +0,0 @@
diff --git a/src/GxEPD2_EPD.cpp b/src/GxEPD2_EPD.cpp
index 8df8bef..91d7f49 100644
--- a/src/GxEPD2_EPD.cpp
+++ b/src/GxEPD2_EPD.cpp
@@ -19,9 +19,9 @@
GxEPD2_EPD::GxEPD2_EPD(int16_t cs, int16_t dc, int16_t rst, int16_t busy, int16_t busy_level, uint32_t busy_timeout,
uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu) :
- WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu),
+ WIDTH(w), HEIGHT(h), panel(p), hasColor(c), hasPartialUpdate(pu), hasFastPartialUpdate(fpu), _sck(-1), _mosi(-1),
_cs(cs), _dc(dc), _rst(rst), _busy(busy), _busy_level(busy_level), _busy_timeout(busy_timeout), _diag_enabled(false),
- _pSPIx(&SPI), _spi_settings(4000000, MSBFIRST, SPI_MODE0)
+ _spi_settings(4000000, MSBFIRST, SPI_MODE0)
{
_initial_write = true;
_initial_refresh = true;
@@ -71,27 +71,30 @@ void GxEPD2_EPD::init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset
{
pinMode(_busy, INPUT);
}
- _pSPIx->begin();
- if (_busy == MISO) // may be overridden
- {
- pinMode(_busy, INPUT);
- }
- if (_dc == MISO) // may be overridden, TTGO T5 V2.66
- {
- pinMode(_dc, OUTPUT);
- }
- if (_cs == MISO) // may be overridden
+ if (_sck < 0) SPI.begin();
+}
+
+void GxEPD2_EPD::init(int16_t sck, int16_t mosi, uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration, bool pulldown_rst_mode)
+{
+ if ((sck >= 0) && (mosi >= 0))
{
- pinMode(_cs, INPUT);
- }
+ _sck = sck;
+ _mosi = mosi;
+ digitalWrite(_sck, LOW);
+ digitalWrite(_mosi, LOW);
+ pinMode(_sck, OUTPUT);
+ pinMode(_mosi, OUTPUT);
+ } else _sck = -1;
+ init(serial_diag_bitrate, initial, reset_duration, pulldown_rst_mode);
}
void GxEPD2_EPD::end()
{
- _pSPIx->end();
if (_cs >= 0) pinMode(_cs, INPUT);
if (_dc >= 0) pinMode(_dc, INPUT);
if (_rst >= 0) pinMode(_rst, INPUT);
+ if (_sck >= 0) pinMode(_sck, INPUT);
+ if (_mosi >= 0) pinMode(_mosi, INPUT);
}
void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void* busy_callback_parameter)
@@ -100,12 +103,6 @@ void GxEPD2_EPD::setBusyCallback(void (*busyCallback)(const void*), const void*
_busy_callback_parameter = busy_callback_parameter;
}
-void GxEPD2_EPD::selectSPI(SPIClass& spi, SPISettings spi_settings)
-{
- _pSPIx = &spi;
- _spi_settings = spi_settings;
-}
-
void GxEPD2_EPD::_reset()
{
if (_rst >= 0)
@@ -174,115 +171,201 @@ void GxEPD2_EPD::_waitWhileBusy(const char* comment, uint16_t busy_time)
void GxEPD2_EPD::_writeCommand(uint8_t c)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_dc >= 0) digitalWrite(_dc, LOW);
if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(c);
+ _spi_write(c);
if (_cs >= 0) digitalWrite(_cs, HIGH);
if (_dc >= 0) digitalWrite(_dc, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_writeData(uint8_t d)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(d);
+ _spi_write(d);
if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_writeData(const uint8_t* data, uint16_t n)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
- for (uint16_t i = 0; i < n; i++)
+ for (uint8_t i = 0; i < n; i++)
{
- _pSPIx->transfer(*data++);
+ _spi_write(*data++);
}
if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_writeDataPGM(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
- for (uint16_t i = 0; i < n; i++)
+ for (uint8_t i = 0; i < n; i++)
{
- _pSPIx->transfer(pgm_read_byte(&*data++));
+ _spi_write(pgm_read_byte(&*data++));
}
while (fill_with_zeroes > 0)
{
- _pSPIx->transfer(0x00);
+ _spi_write(0x00);
fill_with_zeroes--;
}
if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_writeDataPGM_sCS(const uint8_t* data, uint16_t n, int16_t fill_with_zeroes)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
for (uint8_t i = 0; i < n; i++)
{
if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(pgm_read_byte(&*data++));
+ _spi_write(pgm_read_byte(&*data++));
if (_cs >= 0) digitalWrite(_cs, HIGH);
}
while (fill_with_zeroes > 0)
{
if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(0x00);
+ _spi_write(0x00);
fill_with_zeroes--;
if (_cs >= 0) digitalWrite(_cs, HIGH);
}
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_writeCommandData(const uint8_t* pCommandData, uint8_t datalen)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_dc >= 0) digitalWrite(_dc, LOW);
if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(*pCommandData++);
+ _spi_write(*pCommandData++);
if (_dc >= 0) digitalWrite(_dc, HIGH);
for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
{
- _pSPIx->transfer(*pCommandData++);
+ _spi_write(*pCommandData++);
}
if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_writeCommandDataPGM(const uint8_t* pCommandData, uint8_t datalen)
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_dc >= 0) digitalWrite(_dc, LOW);
if (_cs >= 0) digitalWrite(_cs, LOW);
- _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
+ _spi_write(pgm_read_byte(&*pCommandData++));
if (_dc >= 0) digitalWrite(_dc, HIGH);
for (uint8_t i = 0; i < datalen - 1; i++) // sub the command
{
- _pSPIx->transfer(pgm_read_byte(&*pCommandData++));
+ _spi_write(pgm_read_byte(&*pCommandData++));
}
if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
}
void GxEPD2_EPD::_startTransfer()
{
- _pSPIx->beginTransaction(_spi_settings);
+ _beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
}
void GxEPD2_EPD::_transfer(uint8_t value)
{
- _pSPIx->transfer(value);
+ _spi_write(value);
}
void GxEPD2_EPD::_endTransfer()
{
if (_cs >= 0) digitalWrite(_cs, HIGH);
- _pSPIx->endTransaction();
+ _endTransaction();
+}
+
+void GxEPD2_EPD::_beginTransaction(const SPISettings& settings)
+{
+ if (_sck < 0) SPI.beginTransaction(settings);
+}
+
+void GxEPD2_EPD::_spi_write(uint8_t data)
+{
+ if (_sck < 0) SPI.transfer(data);
+ else
+ {
+#if defined (ESP8266)
+ yield();
+#endif
+ for (int i = 0; i < 8; i++)
+ {
+ digitalWrite(_mosi, (data & 0x80) ? HIGH : LOW);
+ data <<= 1;
+ digitalWrite(_sck, HIGH);
+ digitalWrite(_sck, LOW);
+ }
+ }
+}
+
+void GxEPD2_EPD::_endTransaction()
+{
+ if (_sck < 0) SPI.endTransaction();
+}
+
+uint8_t GxEPD2_EPD::_readData()
+{
+ uint8_t data = 0;
+ _beginTransaction(_spi_settings);
+ if (_cs >= 0) digitalWrite(_cs, LOW);
+ if (_sck < 0)
+ {
+ data = SPI.transfer(0);
+ }
+ else
+ {
+ pinMode(_mosi, INPUT);
+ for (int i = 0; i < 8; i++)
+ {
+ data <<= 1;
+ digitalWrite(_sck, HIGH);
+ data |= digitalRead(_mosi);
+ digitalWrite(_sck, LOW);
+ }
+ pinMode(_mosi, OUTPUT);
+ }
+ if (_cs >= 0) digitalWrite(_cs, HIGH);
+ _endTransaction();
+ return data;
+}
+
+void GxEPD2_EPD::_readData(uint8_t* data, uint16_t n)
+{
+ _beginTransaction(_spi_settings);
+ if (_cs >= 0) digitalWrite(_cs, LOW);
+ if (_sck < 0)
+ {
+ for (uint8_t i = 0; i < n; i++)
+ {
+ *data++ = SPI.transfer(0);
+ }
+ }
+ else
+ {
+ pinMode(_mosi, INPUT);
+ for (uint8_t i = 0; i < n; i++)
+ {
+ *data = 0;
+ for (int i = 0; i < 8; i++)
+ {
+ *data <<= 1;
+ digitalWrite(_sck, HIGH);
+ *data |= digitalRead(_mosi);
+ digitalWrite(_sck, LOW);
+ }
+ data++;
+ }
+ pinMode(_mosi, OUTPUT);
+ }
+ if (_cs >= 0) digitalWrite(_cs, HIGH);
+ _endTransaction();
}
diff --git a/src/GxEPD2_EPD.h b/src/GxEPD2_EPD.h
index 34c1145..c480b7d 100644
--- a/src/GxEPD2_EPD.h
+++ b/src/GxEPD2_EPD.h
@@ -8,6 +8,10 @@
// Version: see library.properties
//
// Library: https://github.com/ZinggJM/GxEPD2
+// To use SW SPI with GxEPD2:
+// add the special call to the added init method BEFORE the normal init method:
+// display.epd2.init(SW_SCK, SW_MOSI, 115200, true, 20, false); // define or replace SW_SCK, SW_MOSI
+// display.init(115200); // needed to init upper level
#ifndef _GxEPD2_EPD_H_
#define _GxEPD2_EPD_H_
@@ -35,6 +39,7 @@ class GxEPD2_EPD
uint16_t w, uint16_t h, GxEPD2::Panel p, bool c, bool pu, bool fpu);
virtual void init(uint32_t serial_diag_bitrate = 0); // serial_diag_bitrate = 0 : disabled
virtual void init(uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 10, bool pulldown_rst_mode = false);
+ virtual void init(int16_t sck, int16_t mosi, uint32_t serial_diag_bitrate, bool initial, uint16_t reset_duration = 20, bool pulldown_rst_mode = false);
virtual void end(); // release SPI and control pins
// Support for Bitmaps (Sprites) to Controller Buffer and to Screen
virtual void clearScreen(uint8_t value) = 0; // init controller memory and screen (default white)
@@ -97,7 +102,6 @@ class GxEPD2_EPD
{
return (a > b ? a : b);
};
- void selectSPI(SPIClass& spi, SPISettings spi_settings);
protected:
void _reset();
void _waitWhileBusy(const char* comment = 0, uint16_t busy_time = 5000);
@@ -111,17 +115,22 @@ class GxEPD2_EPD
void _startTransfer();
void _transfer(uint8_t value);
void _endTransfer();
+ void _beginTransaction(const SPISettings& settings);
+ void _spi_write(uint8_t data);
+ void _endTransaction();
+ public:
+ uint8_t _readData();
+ void _readData(uint8_t* data, uint16_t n);
protected:
- int16_t _cs, _dc, _rst, _busy, _busy_level;
+ int16_t _cs, _dc, _rst, _busy, _busy_level, _sck, _mosi;;
uint32_t _busy_timeout;
bool _diag_enabled, _pulldown_rst_mode;
- SPIClass* _pSPIx;
SPISettings _spi_settings;
bool _initial_write, _initial_refresh;
bool _power_is_on, _using_partial_mode, _hibernating;
bool _init_display_done;
uint16_t _reset_duration;
- void (*_busy_callback)(const void*);
+ void (*_busy_callback)(const void*);
const void* _busy_callback_parameter;
};

55
scripts/add_littlefs_binary.py

@ -0,0 +1,55 @@
import os
import subprocess
import shutil
Import("env")
def build_littlefs():
result = subprocess.run(["pio", "run", "--target", "buildfs", "--environment", env['PIOENV']])
if result.returncode != 0:
print("Error building LittleFS:")
exit(1)
else:
print("LittleFS build successful")
def merge_bins():
flash_size = int(env.get("BOARD_FLASH_SIZE", "4MB").replace("MB", ""))
app0_offset = 0x10000
if env['PIOENV'][:7] == "esp8266":
app0_offset = 0
elif env['PIOENV'][:7] == "esp8285":
app0_offset = 0
littlefs_offset = 0x290000
if flash_size == 8:
littlefs_offset = 0x670000
elif flash_size == 16:
littlefs_offset = 0xc90000
# save current wd
start = os.getcwd()
os.chdir('.pio/build/' + env['PIOENV'] + '/')
with open("firmware.bin", "rb") as firmware_file:
firmware_data = firmware_file.read()
with open("littlefs.bin", "rb") as littlefs_file:
littlefs_data = littlefs_file.read()
with open("firmware.factory.bin", "wb") as merged_file:
# fill gap with 0xff
merged_file.write(firmware_data)
if len(firmware_data) < (littlefs_offset - app0_offset):
merged_file.write(b'\xFF' * ((littlefs_offset - app0_offset) - len(firmware_data)))
merged_file.write(littlefs_data)
os.chdir(start)
def main(target, source, env):
build_littlefs()
merge_bins()
# ensure that script is called once firmeware was compiled
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", main)

4
scripts/applyPatches.py

@ -28,8 +28,8 @@ def applyPatch(libName, patchFile):
# list of patches to apply (relative to /src) # list of patches to apply (relative to /src)
applyPatch("ESPAsyncWebServer-esphome", "../patches/AsyncWeb_Prometheus.patch") applyPatch("ESPAsyncWebServer-esphome", "../patches/AsyncWeb_Prometheus.patch")
if env['PIOENV'][:13] == "opendtufusion": if (env['PIOENV'][:5] == "esp32") or (env['PIOENV'][:13] == "opendtufusion"):
applyPatch("GxEPD2", "../patches/GxEPD2_SW_SPI.patch") applyPatch("GxEPD2", "../patches/GxEPD2_HAL.patch")
if (env['PIOENV'][:13] == "opendtufusion") or (env['PIOENV'][:5] == "esp32"): if (env['PIOENV'][:13] == "opendtufusion") or (env['PIOENV'][:5] == "esp32"):
applyPatch("RF24", "../patches/RF24_Hal.patch") applyPatch("RF24", "../patches/RF24_Hal.patch")

42
src/CHANGES.md

@ -1,5 +1,47 @@
# Development Changes # Development Changes
## 0.8.119 - 2024-05-17
* fix reset values at midnight if WiFi isn't available #1620
* fix typo in English versions
* add yield day to history graph #1614
* added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings
* merge PR: Fix MI overnight behaviour #1626
## 0.8.118 - 2024-05-10
* possible fix reset max values #1609
* slightly improved WiFi reconnect
* update AsyncWebserver to `3.2.0`
## 0.8.117 - 2024-05-09
* fix reboot issue #1607 #1606
* fix max temperature tooltip if only one inverter is configured #1605
## 0.8.116 - 2024-05-05
* calculation of max AC power
* fix counter overflow communication queue
* added max inverter temperature
## 0.8.115 - 2024-05-03
* fix inverter communication with manual time sync #1603
* improved queue, only add new object once they not exist in queue
* added option to reset values on communication start (sunrise)
* fixed calculation of max AC power (API, MqTT)
## 0.8.114 - 2024-04-29
* fix ESP8266 compile
* fix history graph
* fix close button color of modal windows in dark mode #1598
* fix only one total field in `/live` #1579
## 0.8.113 - 2024-04-25
* code cleanup
* fix ESP32-C3 compile
## 0.8.112 - 2024-04-24
* improved wizard
* converted ePaper and Ethernet to hal-SPI
* improved network connection
## 0.8.111 - 2024-04-17 ## 0.8.111 - 2024-04-17
* fix MqTT discovery field `ALARM_MES_ID` #1591 * fix MqTT discovery field `ALARM_MES_ID` #1591
* fix Wifi reconnect for ESP32 #1589 #1575 * fix Wifi reconnect for ESP32 #1589 #1575

75
src/app.cpp

@ -33,23 +33,16 @@ void app::setup() {
resetSystem(); resetSystem();
esp_task_wdt_reset(); esp_task_wdt_reset();
mSettings.setup(); mSettings.setup(mConfig);
mSettings.getPtr(mConfig);
ah::Scheduler::setup(mConfig->inst.startWithoutTime); ah::Scheduler::setup(mConfig->inst.startWithoutTime);
DPRINT(DBG_INFO, F("Settings valid: ")); DPRINT(DBG_INFO, F("Settings valid: "));
DSERIAL.flush(); DBGPRINTLN(mConfig->valid ? F("true") : F("false"));
if (mSettings.getValid())
DBGPRINTLN(F("true"));
else
DBGPRINTLN(F("false"));
esp_task_wdt_reset(); esp_task_wdt_reset();
mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf); mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf);
#if defined(ESP32) #if defined(ESP32)
if(mConfig->cmt.enabled) { mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->cmt, mConfig->sys.region);
mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, mConfig->cmt.pinSclk, mConfig->cmt.pinSdio, mConfig->cmt.pinCsb, mConfig->cmt.pinFcsb, mConfig->sys.region);
}
#endif #endif
#ifdef ETHERNET #ifdef ETHERNET
@ -57,16 +50,16 @@ void app::setup() {
mNetwork = static_cast<AhoyNetwork*>(new AhoyEthernet()); mNetwork = static_cast<AhoyNetwork*>(new AhoyEthernet());
#else #else
mNetwork = static_cast<AhoyNetwork*>(new AhoyWifi()); mNetwork = static_cast<AhoyNetwork*>(new AhoyWifi());
#endif // ETHERNET #endif
mNetwork->setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); }); mNetwork->setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); });
mNetwork->begin(); mNetwork->begin();
esp_task_wdt_reset(); esp_task_wdt_reset();
mCommunication.setup(&mTimestamp, &mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace); mCommunication.setup(&mTimestamp, &mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace);
mCommunication.addPayloadListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1, std::placeholders::_2)); mCommunication.addPayloadListener([this] (uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); });
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); }); mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); });
#endif #endif
mSys.setup(&mTimestamp, &mConfig->inst, this); mSys.setup(&mTimestamp, &mConfig->inst, this);
for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) {
@ -81,7 +74,6 @@ void app::setup() {
esp_task_wdt_reset(); esp_task_wdt_reset();
// when WiFi is in client mode, then enable mqtt broker // when WiFi is in client mode, then enable mqtt broker
#if !defined(AP_ONLY)
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
mMqttEnabled = (mConfig->mqtt.broker[0] > 0); mMqttEnabled = (mConfig->mqtt.broker[0] > 0);
if (mMqttEnabled) { if (mMqttEnabled) {
@ -90,7 +82,6 @@ void app::setup() {
mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); }); mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); });
} }
#endif #endif
#endif
setupLed(); setupLed();
esp_task_wdt_reset(); esp_task_wdt_reset();
@ -103,6 +94,7 @@ void app::setup() {
mDbgSyslog.setup(mConfig); // be sure to init after mWeb.setup (webSerial uses also debug callback) mDbgSyslog.setup(mConfig); // be sure to init after mWeb.setup (webSerial uses also debug callback)
#endif #endif
// Plugins // Plugins
mMaxPower.setup(&mTimestamp, mConfig->inst.sendInterval);
#if defined(PLUGIN_DISPLAY) #if defined(PLUGIN_DISPLAY)
if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type) if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
#if defined(ESP32) #if defined(ESP32)
@ -126,9 +118,7 @@ void app::setup() {
#if defined(ENABLE_SIMULATOR) #if defined(ENABLE_SIMULATOR)
mSimulator.setup(&mSys, &mTimestamp, 0); mSimulator.setup(&mSys, &mTimestamp, 0);
mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); });
payloadEventListener(cmd, iv);
});
#endif /*ENABLE_SIMULATOR*/ #endif /*ENABLE_SIMULATOR*/
esp_task_wdt_reset(); esp_task_wdt_reset();
@ -142,8 +132,7 @@ void app::loop(void) {
mNrfRadio.loop(); mNrfRadio.loop();
#if defined(ESP32) #if defined(ESP32)
if(mConfig->cmt.enabled) mCmtRadio.loop();
mCmtRadio.loop();
#endif #endif
ah::Scheduler::loop(); ah::Scheduler::loop();
@ -159,12 +148,14 @@ void app::loop(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::onNetwork(bool gotIp) { void app::onNetwork(bool gotIp) {
mNetworkConnected = gotIp; mNetworkConnected = gotIp;
ah::Scheduler::resetTicker(); if(gotIp) {
regularTickers(); //reinstall regular tickers ah::Scheduler::resetTicker();
every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend"); regularTickers(); //reinstall regular tickers
mTickerInstallOnce = true; every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend");
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! mTickerInstallOnce = true;
once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2");
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -174,6 +165,9 @@ void app::regularTickers(void) {
everySec([this]() { mProtection->tickSecond(); }, "prot"); everySec([this]() { mProtection->tickSecond(); }, "prot");
everySec([this]() {mNetwork->tickNetworkLoop(); }, "net"); everySec([this]() {mNetwork->tickNetworkLoop(); }, "net");
if(mConfig->inst.startWithoutTime && !mNetworkConnected)
every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend");
// Plugins // Plugins
#if defined(PLUGIN_DISPLAY) #if defined(PLUGIN_DISPLAY)
if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type) if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
@ -287,6 +281,8 @@ void app::tickIVCommunication(void) {
if (mTimestamp >= (mSunset + mConfig->sun.offsetSecEvening)) { // current time is past communication stop, nothing to do. Next update will be done at midnight by tickCalcSunrise if (mTimestamp >= (mSunset + mConfig->sun.offsetSecEvening)) { // current time is past communication stop, nothing to do. Next update will be done at midnight by tickCalcSunrise
nxtTrig = 0; nxtTrig = 0;
} else { // current time lies within communication start/stop time, set next trigger to communication stop } else { // current time lies within communication start/stop time, set next trigger to communication stop
if((!iv->commEnabled) && mConfig->inst.rstValsCommStart)
zeroValues = true;
iv->commEnabled = true; iv->commEnabled = true;
nxtTrig = mSunset + mConfig->sun.offsetSecEvening; nxtTrig = mSunset + mConfig->sun.offsetSecEvening;
} }
@ -359,18 +355,9 @@ void app::tickMidnight(void) {
// reset alarms // reset alarms
if(InverterStatus::OFF == iv->getStatus()) if(InverterStatus::OFF == iv->getStatus())
iv->resetAlarms(); iv->resetAlarms();
// clear max values
if(mConfig->inst.rstMaxValsMidNight) {
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
for(uint8_t i = 0; i <= iv->channels; i++) {
uint8_t pos = iv->getPosByChFld(i, FLD_MP, rec);
iv->setValue(pos, rec, 0.0f);
}
}
} }
if (mConfig->inst.rstYieldMidNight) { if (mConfig->inst.rstValsAtMidNight) {
zeroIvValues(!CHECK_AVAIL, !SKIP_YIELD_DAY); zeroIvValues(!CHECK_AVAIL, !SKIP_YIELD_DAY);
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
@ -441,6 +428,9 @@ bool app::sendIv(Inverter<> *iv) {
void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) { void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) {
Inverter<> *iv; Inverter<> *iv;
bool changed = false; bool changed = false;
mMaxPower.reset();
// set values to zero, except yields // set values to zero, except yields
for (uint8_t id = 0; id < mSys.getNumInverters(); id++) { for (uint8_t id = 0; id < mSys.getNumInverters(); id++) {
iv = mSys.getInverterByPos(id); iv = mSys.getInverterByPos(id);
@ -471,18 +461,21 @@ void app:: zeroIvValues(bool checkAvail, bool skipYieldDay) {
pos = iv->getPosByChFld(ch, fld, rec); pos = iv->getPosByChFld(ch, fld, rec);
iv->setValue(pos, rec, 0.0f); iv->setValue(pos, rec, 0.0f);
} }
// zero max power // zero max power and max temperature
if(!skipYieldDay) { if(mConfig->inst.rstIncludeMaxVals) {
pos = iv->getPosByChFld(ch, FLD_MP, rec); pos = iv->getPosByChFld(ch, FLD_MP, rec);
iv->setValue(pos, rec, 0.0f); iv->setValue(pos, rec, 0.0f);
} pos = iv->getPosByChFld(ch, FLD_MT, rec);
iv->resetAlarms(); iv->setValue(pos, rec, 0.0f);
iv->resetAlarms(true);
} else
iv->resetAlarms();
iv->doCalculations(); iv->doCalculations();
} }
} }
if(changed) if(changed)
payloadEventListener(RealTimeRunData_Debug, NULL); payloadEventListener(RealTimeRunData_Debug, nullptr);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

25
src/app.h

@ -31,6 +31,7 @@
#include "utils/syslog.h" #include "utils/syslog.h"
#include "web/RestApi.h" #include "web/RestApi.h"
#include "web/Protection.h" #include "web/Protection.h"
#include "plugins/MaxPower.h"
#if defined(ENABLE_HISTORY) #if defined(ENABLE_HISTORY)
#include "plugins/history.h" #include "plugins/history.h"
#endif /*ENABLE_HISTORY*/ #endif /*ENABLE_HISTORY*/
@ -217,7 +218,7 @@ class app : public IApp, public ah::Scheduler {
} }
bool getSettingsValid() override { bool getSettingsValid() override {
return mSettings.getValid(); return mConfig->valid;
} }
bool getRebootRequestState() override { bool getRebootRequestState() override {
@ -303,8 +304,14 @@ class app : public IApp, public ah::Scheduler {
DBGPRINTLN(String(newTime)); DBGPRINTLN(String(newTime));
if(0 == newTime) if(0 == newTime)
mNetwork->updateNtpTime(); mNetwork->updateNtpTime();
else else {
Scheduler::setTimestamp(newTime); Scheduler::setTimestamp(newTime);
onNtpUpdate(false);
}
}
float getTotalMaxPower(void) override {
return mMaxPower.getTotalMaxPower();
} }
uint16_t getHistoryValue(uint8_t type, uint16_t i) override { uint16_t getHistoryValue(uint8_t type, uint16_t i) override {
@ -354,15 +361,14 @@ class app : public IApp, public ah::Scheduler {
void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true); void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true);
void payloadEventListener(uint8_t cmd, Inverter<> *iv) { void payloadEventListener(uint8_t cmd, Inverter<> *iv) {
#if !defined(AP_ONLY) mMaxPower.payloadEvent(cmd, iv);
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
if (mMqttEnabled) if (mMqttEnabled)
mMqtt.payloadEventListener(cmd, iv); mMqtt.payloadEventListener(cmd, iv);
#endif /*ENABLE_MQTT*/
#endif #endif
#if defined(PLUGIN_DISPLAY) #if defined(PLUGIN_DISPLAY)
if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type) if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
mDisplay.payloadEventListener(cmd); mDisplay.payloadEventListener(cmd);
#endif #endif
updateLed(); updateLed();
} }
@ -425,8 +431,7 @@ class app : public IApp, public ah::Scheduler {
#ifdef ENABLE_SYSLOG #ifdef ENABLE_SYSLOG
DbgSyslog mDbgSyslog; DbgSyslog mDbgSyslog;
#endif #endif
//PayloadType mPayload;
//MiPayloadType mMiPayload;
PubSerialType mPubSerial; PubSerialType mPubSerial;
#if !defined(ETHERNET) #if !defined(ETHERNET)
//Improv mImprov; //Improv mImprov;
@ -447,10 +452,9 @@ class app : public IApp, public ah::Scheduler {
bool mNetworkConnected = false; bool mNetworkConnected = false;
// mqtt
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
PubMqttType mMqtt; PubMqttType mMqtt;
#endif /*ENABLE_MQTT*/ #endif
bool mTickerInstallOnce = false; bool mTickerInstallOnce = false;
bool mMqttEnabled = false; bool mMqttEnabled = false;
@ -459,6 +463,7 @@ class app : public IApp, public ah::Scheduler {
uint32_t mSunrise = 0, mSunset = 0; uint32_t mSunrise = 0, mSunset = 0;
// plugins // plugins
MaxPower<float> mMaxPower;
#if defined(PLUGIN_DISPLAY) #if defined(PLUGIN_DISPLAY)
DisplayType mDisplay; DisplayType mDisplay;
DisplayData mDispData; DisplayData mDispData;

1
src/appInterface.h

@ -64,6 +64,7 @@ class IApp {
virtual void resetLockTimeout(void) = 0; virtual void resetLockTimeout(void) = 0;
virtual bool isProtected(const char *clientIp, const char *token, bool askedFromWeb) const = 0; virtual bool isProtected(const char *clientIp, const char *token, bool askedFromWeb) const = 0;
virtual float getTotalMaxPower(void) = 0;
virtual uint16_t getHistoryValue(uint8_t type, uint16_t i) = 0; virtual uint16_t getHistoryValue(uint8_t type, uint16_t i) = 0;
virtual uint32_t getHistoryPeriod(uint8_t type) = 0; virtual uint32_t getHistoryPeriod(uint8_t type) = 0;
virtual uint16_t getHistoryMaxDay() = 0; virtual uint16_t getHistoryMaxDay() = 0;

5
src/config/config.h

@ -28,6 +28,11 @@
// If the next line is uncommented, Ahoy will stay in access point mode all the time // If the next line is uncommented, Ahoy will stay in access point mode all the time
//#define AP_ONLY //#define AP_ONLY
#if defined(AP_ONLY)
#if defined(ENABLE_MQTT)
#undef ENABLE_MQTT
#endif
#endif
// timeout for automatic logoff (20 minutes) // timeout for automatic logoff (20 minutes)
#define LOGOUT_TIMEOUT (20 * 60) #define LOGOUT_TIMEOUT (20 * 60)

42
src/config/settings.h

@ -186,10 +186,11 @@ typedef struct {
cfgIv_t iv[MAX_NUM_INVERTERS]; cfgIv_t iv[MAX_NUM_INVERTERS];
uint16_t sendInterval; uint16_t sendInterval;
bool rstYieldMidNight; bool rstValsAtMidNight;
bool rstValsNotAvail; bool rstValsNotAvail;
bool rstValsCommStop; bool rstValsCommStop;
bool rstMaxValsMidNight; bool rstValsCommStart;
bool rstIncludeMaxVals;
bool startWithoutTime; bool startWithoutTime;
bool readGrid; bool readGrid;
} cfgInst_t; } cfgInst_t;
@ -240,8 +241,9 @@ class settings {
std::fill(reinterpret_cast<char*>(&mCfg), reinterpret_cast<char*>(&mCfg) + sizeof(mCfg), 0); std::fill(reinterpret_cast<char*>(&mCfg), reinterpret_cast<char*>(&mCfg) + sizeof(mCfg), 0);
} }
void setup() { void setup(settings_t *&c) {
DPRINTLN(DBG_INFO, F("Initializing FS ..")); DPRINTLN(DBG_INFO, F("Initializing FS .."));
c = &mCfg;
mCfg.valid = false; mCfg.valid = false;
#if !defined(ESP32) #if !defined(ESP32)
@ -277,14 +279,6 @@ class settings {
DPRINTLN(DBG_INFO, F("FS stopped")); DPRINTLN(DBG_INFO, F("FS stopped"));
} }
void getPtr(settings_t *&cfg) {
cfg = &mCfg;
}
bool getValid(void) {
return mCfg.valid;
}
inline bool getLastSaveSucceed() { inline bool getLastSaveSucceed() {
return mLastSaveSucceed; return mLastSaveSucceed;
} }
@ -495,13 +489,14 @@ class settings {
mCfg.mqtt.json = 0; // off mCfg.mqtt.json = 0; // off
mCfg.mqtt.enableRetain = true; mCfg.mqtt.enableRetain = true;
mCfg.inst.sendInterval = SEND_INTERVAL; mCfg.inst.sendInterval = SEND_INTERVAL;
mCfg.inst.rstYieldMidNight = false; mCfg.inst.rstValsAtMidNight = false;
mCfg.inst.rstValsNotAvail = false; mCfg.inst.rstValsNotAvail = false;
mCfg.inst.rstValsCommStop = false; mCfg.inst.rstValsCommStop = false;
mCfg.inst.startWithoutTime = false; mCfg.inst.rstValsCommStart = false;
mCfg.inst.rstMaxValsMidNight = false; mCfg.inst.startWithoutTime = false;
mCfg.inst.readGrid = true; mCfg.inst.rstIncludeMaxVals = false;
mCfg.inst.readGrid = true;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) {
mCfg.inst.iv[i].powerLevel = 0xff; // impossible high value mCfg.inst.iv[i].powerLevel = 0xff; // impossible high value
@ -830,21 +825,23 @@ class settings {
if(set) { if(set) {
obj[F("intvl")] = mCfg.inst.sendInterval; obj[F("intvl")] = mCfg.inst.sendInterval;
// obj[F("en")] = (bool)mCfg.inst.enabled; // obj[F("en")] = (bool)mCfg.inst.enabled;
obj[F("rstMidNight")] = (bool)mCfg.inst.rstYieldMidNight; obj[F("rstMidNight")] = (bool)mCfg.inst.rstValsAtMidNight;
obj[F("rstNotAvail")] = (bool)mCfg.inst.rstValsNotAvail; obj[F("rstNotAvail")] = (bool)mCfg.inst.rstValsNotAvail;
obj[F("rstComStop")] = (bool)mCfg.inst.rstValsCommStop; obj[F("rstComStop")] = (bool)mCfg.inst.rstValsCommStop;
obj[F("rstComStart")] = (bool)mCfg.inst.rstValsCommStart;
obj[F("strtWthtTime")] = (bool)mCfg.inst.startWithoutTime; obj[F("strtWthtTime")] = (bool)mCfg.inst.startWithoutTime;
obj[F("rstMaxMidNight")] = (bool)mCfg.inst.rstMaxValsMidNight; obj[F("rstMaxMidNight")] = (bool)mCfg.inst.rstIncludeMaxVals;
obj[F("rdGrid")] = (bool)mCfg.inst.readGrid; obj[F("rdGrid")] = (bool)mCfg.inst.readGrid;
} }
else { else {
getVal<uint16_t>(obj, F("intvl"), &mCfg.inst.sendInterval); getVal<uint16_t>(obj, F("intvl"), &mCfg.inst.sendInterval);
// getVal<bool>(obj, F("en"), &mCfg.inst.enabled); // getVal<bool>(obj, F("en"), &mCfg.inst.enabled);
getVal<bool>(obj, F("rstMidNight"), &mCfg.inst.rstYieldMidNight); getVal<bool>(obj, F("rstMidNight"), &mCfg.inst.rstValsAtMidNight);
getVal<bool>(obj, F("rstNotAvail"), &mCfg.inst.rstValsNotAvail); getVal<bool>(obj, F("rstNotAvail"), &mCfg.inst.rstValsNotAvail);
getVal<bool>(obj, F("rstComStop"), &mCfg.inst.rstValsCommStop); getVal<bool>(obj, F("rstComStop"), &mCfg.inst.rstValsCommStop);
getVal<bool>(obj, F("rstComStart"), &mCfg.inst.rstValsCommStart);
getVal<bool>(obj, F("strtWthtTime"), &mCfg.inst.startWithoutTime); getVal<bool>(obj, F("strtWthtTime"), &mCfg.inst.startWithoutTime);
getVal<bool>(obj, F("rstMaxMidNight"), &mCfg.inst.rstMaxValsMidNight); getVal<bool>(obj, F("rstMaxMidNight"), &mCfg.inst.rstIncludeMaxVals);
getVal<bool>(obj, F("rdGrid"), &mCfg.inst.readGrid); getVal<bool>(obj, F("rdGrid"), &mCfg.inst.readGrid);
} }
@ -919,6 +916,7 @@ class settings {
} }
#endif #endif
private:
settings_t mCfg; settings_t mCfg;
bool mLastSaveSucceed = 0; bool mLastSaveSucceed = 0;
}; };

2
src/defines.h

@ -13,7 +13,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 8 #define VERSION_MINOR 8
#define VERSION_PATCH 111 #define VERSION_PATCH 119
//------------------------------------- //-------------------------------------
typedef struct { typedef struct {
uint8_t ch; uint8_t ch;

27
src/hm/CommQueue.h

@ -19,13 +19,19 @@ template <uint8_t N=100>
class CommQueue { class CommQueue {
public: public:
void addImportant(Inverter<> *iv, uint8_t cmd) { void addImportant(Inverter<> *iv, uint8_t cmd) {
dec(&mRdPtr); queue_s q(iv, cmd, true);
mQueue[mRdPtr] = queue_s(iv, cmd, true); if(!isIncluded(&q)) {
dec(&mRdPtr);
mQueue[mRdPtr] = q;
}
} }
void add(Inverter<> *iv, uint8_t cmd) { void add(Inverter<> *iv, uint8_t cmd) {
mQueue[mWrPtr] = queue_s(iv, cmd, false); queue_s q(iv, cmd, false);
inc(&mWrPtr); if(!isIncluded(&q)) {
mQueue[mWrPtr] = q;
inc(&mWrPtr);
}
} }
void chgCmd(Inverter<> *iv, uint8_t cmd) { void chgCmd(Inverter<> *iv, uint8_t cmd) {
@ -117,6 +123,19 @@ class CommQueue {
--(*ptr); --(*ptr);
} }
private:
bool isIncluded(const queue_s *q) {
uint8_t ptr = mRdPtr;
while (ptr != mWrPtr) {
if(mQueue[ptr].cmd == q->cmd) {
if(mQueue[ptr].iv->id == q->iv->id)
return true;
}
inc(&ptr);
}
return false;
}
protected: protected:
std::array<queue_s, N> mQueue; std::array<queue_s, N> mQueue;
uint8_t mWrPtr = 0; uint8_t mWrPtr = 0;

4
src/hm/Communication.h

@ -159,8 +159,6 @@ class Communication : public CommQueue<> {
mFirstTry = false; mFirstTry = false;
mHeu.evalTxChQuality(q->iv, false, 0, 0); mHeu.evalTxChQuality(q->iv, false, 0, 0);
mHeu.getTxCh(q->iv); mHeu.getTxCh(q->iv);
//q->iv->radioStatistics.rxFailNoAnser++; // should only be one of fail or retransmit.
//q->iv->radioStatistics.txCnt--;
q->iv->radioStatistics.retransmits++; q->iv->radioStatistics.retransmits++;
q->iv->radio->mRadioWaitTime.stopTimeMonitor(); q->iv->radio->mRadioWaitTime.stopTimeMonitor();
mState = States::START; mState = States::START;
@ -897,7 +895,7 @@ class Communication : public CommQueue<> {
uint8_t oldState = rec->record[q->iv->getPosByChFld(0, FLD_EVT, rec)]; uint8_t oldState = rec->record[q->iv->getPosByChFld(0, FLD_EVT, rec)];
if ( prntsts != oldState ) { // sth.'s changed? if ( prntsts != oldState ) { // sth.'s changed?
stsok = false; stsok = false;
if(!oldState) { // initial zero value? => just write this channel to main state and raise changed flags if( (!oldState) || (!q->iv->alarmCnt) ) { // initial zero value? => just write this channel to main state and raise changed flags
changedStatus = true; changedStatus = true;
q->iv->alarmCnt = 1; // minimum... q->iv->alarmCnt = 1; // minimum...
} else { } else {

2
src/hm/hmDefines.h

@ -304,7 +304,7 @@ const byteAssign_t hm4chAssignment[] = {
{ FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC }, { FLD_PDC, UNIT_W, CH0, CALC_PDC_CH0, 0, CMD_CALC },
{ FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC }, { FLD_EFF, UNIT_PCT, CH0, CALC_EFF_CH0, 0, CMD_CALC },
{ FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC }, { FLD_MP, UNIT_W, CH0, CALC_MPAC_CH0, 0, CMD_CALC },
{ FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC } { FLD_MT, UNIT_C, CH0, CALC_MT_CH0, 0, CMD_CALC }
}; };
#define HM4CH_LIST_LEN (sizeof(hm4chAssignment) / sizeof(byteAssign_t)) #define HM4CH_LIST_LEN (sizeof(hm4chAssignment) / sizeof(byteAssign_t))
#define HM4CH_PAYLOAD_LEN 62 #define HM4CH_PAYLOAD_LEN 62

101
src/hm/hmInverter.h

@ -59,6 +59,9 @@ T calcMaxTempCh0(Inverter<> *iv, uint8_t arg0);
template<class T=float> template<class T=float>
T calcMaxPowerDc(Inverter<> *iv, uint8_t arg0); T calcMaxPowerDc(Inverter<> *iv, uint8_t arg0);
template<class T=float>
T calcMaxTemperature(Inverter<> *iv, uint8_t arg0);
template<class T=float> template<class T=float>
using func_t = T (Inverter<> *, uint8_t); using func_t = T (Inverter<> *, uint8_t);
@ -87,7 +90,7 @@ struct record_t {
byteAssign_t* assign = nullptr; // assignment of bytes in payload byteAssign_t* assign = nullptr; // assignment of bytes in payload
uint8_t length = 0; // length of the assignment list uint8_t length = 0; // length of the assignment list
T *record = nullptr; // data pointer T *record = nullptr; // data pointer
uint32_t ts = 0; // timestamp of last received payload uint32_t ts = 0; // Timestamp of last received payload
uint8_t pyldLen = 0; // expected payload length for plausibility check uint8_t pyldLen = 0; // expected payload length for plausibility check
MqttSentStatus mqttSentStatus = MqttSentStatus:: NEW_DATA; // indicates the current MqTT sent status MqttSentStatus mqttSentStatus = MqttSentStatus:: NEW_DATA; // indicates the current MqTT sent status
}; };
@ -103,15 +106,15 @@ struct alarm_t {
// list of all available functions, mapped in hmDefines.h // list of all available functions, mapped in hmDefines.h
template<class T=float> template<class T=float>
const calcFunc_t<T> calcFunctions[] = { const calcFunc_t<T> calcFunctions[] = {
{ CALC_YT_CH0, &calcYieldTotalCh0 }, { CALC_YT_CH0, &calcYieldTotalCh0 },
{ CALC_YD_CH0, &calcYieldDayCh0 }, { CALC_YD_CH0, &calcYieldDayCh0 },
{ CALC_UDC_CH, &calcUdcCh }, { CALC_UDC_CH, &calcUdcCh },
{ CALC_PDC_CH0, &calcPowerDcCh0 }, { CALC_PDC_CH0, &calcPowerDcCh0 },
{ CALC_EFF_CH0, &calcEffiencyCh0 }, { CALC_EFF_CH0, &calcEffiencyCh0 },
{ CALC_IRR_CH, &calcIrradiation }, { CALC_IRR_CH, &calcIrradiation },
{ CALC_MPAC_CH0, &calcMaxPowerAcCh0 }, { CALC_MPAC_CH0, &calcMaxPowerAcCh0 },
{ CALC_MPDC_CH, &calcMaxPowerDc }, { CALC_MPDC_CH, &calcMaxPowerDc },
{ CALC_MT_CH0, &calcMaxTempCh0 } { CALC_MT_CH0, &calcMaxTemperature }
}; };
template <class REC_TYP> template <class REC_TYP>
@ -150,8 +153,8 @@ class Inverter {
statistics_t radioStatistics; // information about transmitted, failed, ... packets statistics_t radioStatistics; // information about transmitted, failed, ... packets
HeuristicInv heuristics; // heuristic information / logic HeuristicInv heuristics; // heuristic information / logic
uint8_t curCmtFreq = 0; // current used CMT frequency, used to check if freq. was changed during runtime uint8_t curCmtFreq = 0; // current used CMT frequency, used to check if freq. was changed during runtime
uint32_t tsMaxAcPower = 0; // holds the timestamp when the MaxAC power was seen uint32_t tsMaxAcPower = 0; // holds the Timestamp when the MaxAC power was seen
uint32_t tsMaxTemp = 0; // holds the timestamp when the max temp was seen uint32_t tsMaxTemperature = 0; // holds the Timestamp when the max temperature was seen
bool commEnabled = true; // 'pause night communication' sets this field to false bool commEnabled = true; // 'pause night communication' sets this field to false
public: public:
@ -194,7 +197,7 @@ class Inverter {
cb(InverterDevInform_Simple, false); // get hardware version cb(InverterDevInform_Simple, false); // get hardware version
} else if((alarmLastId != alarmMesIndex) && (alarmMesIndex != 0)) { } else if((alarmLastId != alarmMesIndex) && (alarmMesIndex != 0)) {
cb(AlarmData, false); // get last alarms cb(AlarmData, false); // get last alarms
} else if((0 == mGridLen) && generalConfig->readGrid) { // read grid profile } else if((0 == mGridLen) && GeneralConfig->readGrid) { // read grid profile
cb(GridOnProFilePara, false); cb(GridOnProFilePara, false);
} else if (mGetLossInterval > AHOY_GET_LOSS_INTERVAL) { // get loss rate } else if (mGetLossInterval > AHOY_GET_LOSS_INTERVAL) { // get loss rate
mGetLossInterval = 1; mGetLossInterval = 1;
@ -218,7 +221,7 @@ class Inverter {
if (getChannelFieldValue(CH0, FLD_PART_NUM, rec) == 0) { if (getChannelFieldValue(CH0, FLD_PART_NUM, rec) == 0) {
cb(0x0f, false); // hard- and firmware version for missing HW part nr, delivered by frame 1 cb(0x0f, false); // hard- and firmware version for missing HW part nr, delivered by frame 1
mIvRxCnt +=2; mIvRxCnt +=2;
} else if((getChannelFieldValue(CH0, FLD_GRID_PROFILE_CODE, rec) == 0) && generalConfig->readGrid) // read grid profile } else if((getChannelFieldValue(CH0, FLD_GRID_PROFILE_CODE, rec) == 0) && GeneralConfig->readGrid) // read grid profile
cb(0x10, false); // legacy GPF command cb(0x10, false); // legacy GPF command
} }
} }
@ -234,19 +237,20 @@ class Inverter {
initAssignment(&recordAlarm, AlarmData); initAssignment(&recordAlarm, AlarmData);
toRadioId(); toRadioId();
curCmtFreq = this->config->frequency; // update to frequency read from settings curCmtFreq = this->config->frequency; // update to frequency read from settings
resetAlarms(true);
} }
uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId, record_t<> *rec) { uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId, record_t<> *rec) {
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getPosByChFld")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getPosByChFld"));
if(NULL != rec) { if(nullptr == rec)
uint8_t pos = 0;
for(; pos < rec->length; pos++) {
if((rec->assign[pos].ch == channel) && (rec->assign[pos].fieldId == fieldId))
break;
}
return (pos >= rec->length) ? 0xff : pos;
} else
return 0xff; return 0xff;
uint8_t pos = 0;
for(; pos < rec->length; pos++) {
if((rec->assign[pos].ch == channel) && (rec->assign[pos].fieldId == fieldId))
break;
}
return (pos >= rec->length) ? 0xff : pos;
} }
byteAssign_t *getByteAssign(uint8_t pos, record_t<> *rec) { byteAssign_t *getByteAssign(uint8_t pos, record_t<> *rec) {
@ -275,16 +279,18 @@ class Inverter {
if(InverterStatus::OFF != status) { if(InverterStatus::OFF != status) {
mDevControlRequest = true; mDevControlRequest = true;
devControlCmd = cmd; devControlCmd = cmd;
//assert(App); assert(App);
//App->triggerTickSend(0); App->triggerTickSend(id);
return true;
} }
return (InverterStatus::OFF != status); return false;
} }
bool setDevCommand(uint8_t cmd) { bool setDevCommand(uint8_t cmd) {
if(InverterStatus::OFF != status) bool retval = (InverterStatus::OFF != status);
if(retval)
devControlCmd = cmd; devControlCmd = cmd;
return (InverterStatus::OFF != status); return retval;
} }
void addValue(uint8_t pos, const uint8_t buf[], record_t<> *rec) { void addValue(uint8_t pos, const uint8_t buf[], record_t<> *rec) {
@ -360,7 +366,7 @@ class Inverter {
bool setValue(uint8_t pos, record_t<> *rec, REC_TYP val) { bool setValue(uint8_t pos, record_t<> *rec, REC_TYP val) {
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:setValue")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:setValue"));
if(NULL == rec) if(nullptr == rec)
return false; return false;
if(pos > rec->length) if(pos > rec->length)
return false; return false;
@ -414,14 +420,14 @@ class Inverter {
if(recordMeas.ts == 0) if(recordMeas.ts == 0)
return false; return false;
if(((*timestamp) - recordMeas.ts) < INVERTER_INACT_THRES_SEC) if(((*Timestamp) - recordMeas.ts) < INVERTER_INACT_THRES_SEC)
avail = true; avail = true;
if(avail) { if(avail) {
if(status < InverterStatus::PRODUCING) if(status < InverterStatus::PRODUCING)
status = InverterStatus::STARTING; status = InverterStatus::STARTING;
} else { } else {
if(((*timestamp) - recordMeas.ts) > INVERTER_OFF_THRES_SEC) { if(((*Timestamp) - recordMeas.ts) > INVERTER_OFF_THRES_SEC) {
if(status != InverterStatus::OFF) { if(status != InverterStatus::OFF) {
status = InverterStatus::OFF; status = InverterStatus::OFF;
actPowerLimit = 0xffff; // power limit will be read once inverter becomes available actPowerLimit = 0xffff; // power limit will be read once inverter becomes available
@ -582,7 +588,7 @@ class Inverter {
} }
} }
void resetAlarms() { void resetAlarms(bool clearTs = false) {
lastAlarm.fill({0, 0, 0}); lastAlarm.fill({0, 0, 0});
mAlarmNxtWrPos = 0; mAlarmNxtWrPos = 0;
alarmCnt = 0; alarmCnt = 0;
@ -590,6 +596,11 @@ class Inverter {
memset(mOffYD, 0, sizeof(float) * 6); memset(mOffYD, 0, sizeof(float) * 6);
memset(mLastYD, 0, sizeof(float) * 6); memset(mLastYD, 0, sizeof(float) * 6);
if(clearTs) {
tsMaxAcPower = *Timestamp;
tsMaxTemperature = *Timestamp;
}
} }
bool parseGetLossRate(const uint8_t pyld[], uint8_t len) { bool parseGetLossRate(const uint8_t pyld[], uint8_t len) {
@ -822,8 +833,8 @@ class Inverter {
} }
public: public:
static uint32_t *timestamp; // system timestamp static uint32_t *Timestamp; // system timestamp
static cfgInst_t *generalConfig; // general inverter configuration from setup static cfgInst_t *GeneralConfig; // general inverter configuration from setup
static IApp *App; static IApp *App;
uint16_t mDtuRxCnt = 0; uint16_t mDtuRxCnt = 0;
@ -842,9 +853,9 @@ class Inverter {
}; };
template <class REC_TYP> template <class REC_TYP>
uint32_t *Inverter<REC_TYP>::timestamp {0}; uint32_t *Inverter<REC_TYP>::Timestamp {0};
template <class REC_TYP> template <class REC_TYP>
cfgInst_t *Inverter<REC_TYP>::generalConfig {0}; cfgInst_t *Inverter<REC_TYP>::GeneralConfig {0};
template <class REC_TYP> template <class REC_TYP>
IApp *Inverter<REC_TYP>::App {nullptr}; IApp *Inverter<REC_TYP>::App {nullptr};
@ -953,7 +964,7 @@ T calcMaxPowerAcCh0(Inverter<> *iv, uint8_t arg0) {
} }
} }
if(acPower > acMaxPower) { if(acPower > acMaxPower) {
iv->tsMaxAcPower = *iv->timestamp; iv->tsMaxAcPower = *iv->Timestamp;
return acPower; return acPower;
} }
} }
@ -1002,4 +1013,22 @@ T calcMaxPowerDc(Inverter<> *iv, uint8_t arg0) {
return dcMaxPower; return dcMaxPower;
} }
template<class T=float>
T calcMaxTemperature(Inverter<> *iv, uint8_t arg0) {
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcMaxTemperature"));
// arg0 = channel
if(NULL != iv) {
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
T temp = iv->getChannelFieldValue(arg0, FLD_T, rec);
T maxTemp = iv->getChannelFieldValue(arg0, FLD_MT, rec);
if(temp > maxTemp) {
iv->tsMaxTemperature = *iv->Timestamp;
return temp;
}
return maxTemp;
}
return 0;
}
#endif /*__HM_INVERTER_H__*/ #endif /*__HM_INVERTER_H__*/

6
src/hm/hmSystem.h

@ -16,8 +16,8 @@ class HmSystem {
HmSystem() {} HmSystem() {}
void setup(uint32_t *timestamp, cfgInst_t *config, IApp *app) { void setup(uint32_t *timestamp, cfgInst_t *config, IApp *app) {
INVERTERTYPE::timestamp = timestamp; INVERTERTYPE::Timestamp = timestamp;
INVERTERTYPE::generalConfig = config; INVERTERTYPE::GeneralConfig = config;
INVERTERTYPE::App = app; INVERTERTYPE::App = app;
//mInverter[0].app = app; //mInverter[0].app = app;
} }
@ -26,7 +26,7 @@ class HmSystem {
DPRINTLN(DBG_VERBOSE, F("hmSystem.h:addInverter")); DPRINTLN(DBG_VERBOSE, F("hmSystem.h:addInverter"));
INVERTERTYPE *iv = &mInverter[id]; INVERTERTYPE *iv = &mInverter[id];
iv->id = id; iv->id = id;
iv->config = &mInverter[0].generalConfig->iv[id]; iv->config = &INVERTERTYPE::GeneralConfig->iv[id];
DPRINT(DBG_VERBOSE, "SERIAL: " + String(iv->config->serial.b[5], HEX)); DPRINT(DBG_VERBOSE, "SERIAL: " + String(iv->config->serial.b[5], HEX));
DPRINTLN(DBG_VERBOSE, " " + String(iv->config->serial.b[4], HEX)); DPRINTLN(DBG_VERBOSE, " " + String(iv->config->serial.b[4], HEX));
if((iv->config->serial.b[5] == 0x11) || (iv->config->serial.b[5] == 0x10)) { if((iv->config->serial.b[5] == 0x11) || (iv->config->serial.b[5] == 0x10)) {

18
src/hm/nrfHal.h

@ -9,7 +9,6 @@
#pragma once #pragma once
#include "../utils/spiPatcher.h" #include "../utils/spiPatcher.h"
#include <esp_rom_gpio.h> #include <esp_rom_gpio.h>
#include <RF24.h> #include <RF24.h>
@ -18,9 +17,7 @@
class nrfHal: public RF24_hal, public SpiPatcherHandle { class nrfHal: public RF24_hal, public SpiPatcherHandle {
public: public:
nrfHal() { nrfHal() {}
mSpiPatcher = SpiPatcher::getInstance(SPI2_HOST);
}
void patch() override { void patch() override {
esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false); esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false);
@ -42,7 +39,13 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle {
mPinEn = static_cast<gpio_num_t>(en); mPinEn = static_cast<gpio_num_t>(en);
mSpiSpeed = speed; mSpiSpeed = speed;
mHostDevice = mSpiPatcher->getDevice(); #if defined(CONFIG_IDF_TARGET_ESP32S3)
mHostDevice = SPI2_HOST;
#else
mHostDevice = (14 == sclk) ? SPI2_HOST : SPI_HOST_OTHER;
#endif
mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
gpio_reset_pin(mPinMosi); gpio_reset_pin(mPinMosi);
gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT); gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT);
@ -56,6 +59,7 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle {
gpio_set_level(mPinClk, 0); gpio_set_level(mPinClk, 0);
gpio_reset_pin(mPinCs); gpio_reset_pin(mPinCs);
request_spi();
spi_device_interface_config_t devcfg = { spi_device_interface_config_t devcfg = {
.command_bits = 0, .command_bits = 0,
.address_bits = 0, .address_bits = 0,
@ -72,14 +76,14 @@ class nrfHal: public RF24_hal, public SpiPatcherHandle {
.pre_cb = nullptr, .pre_cb = nullptr,
.post_cb = nullptr .post_cb = nullptr
}; };
ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg, &spi)); mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi);
release_spi();
gpio_reset_pin(mPinEn); gpio_reset_pin(mPinEn);
gpio_set_direction(mPinEn, GPIO_MODE_OUTPUT); gpio_set_direction(mPinEn, GPIO_MODE_OUTPUT);
gpio_set_level(mPinEn, 0); gpio_set_level(mPinEn, 0);
} }
bool begin() override { bool begin() override {
return true; return true;
} }

24
src/hms/CmtRadio.h

@ -15,16 +15,25 @@ template<uint32_t DTU_SN = 0x81001765>
class CmtRadio : public Radio { class CmtRadio : public Radio {
typedef Cmt2300a CmtType; typedef Cmt2300a CmtType;
public: public:
void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, uint8_t pinSclk, uint8_t pinSdio, uint8_t pinCsb, uint8_t pinFcsb, uint8_t region = 0, bool genDtuSn = true) { void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, cfgCmt_t *cfg, uint8_t region = 0, bool genDtuSn = true) {
mCmt.setup(pinSclk, pinSdio, pinCsb, pinFcsb); mCfg = cfg;
reset(genDtuSn, static_cast<RegionCfg>(region));
if(!cfg->enabled)
return;
mPrivacyMode = privacyMode; mPrivacyMode = privacyMode;
mSerialDebug = serialDebug; mSerialDebug = serialDebug;
mPrintWholeTrace = printWholeTrace; mPrintWholeTrace = printWholeTrace;
mTxBuf.fill(0); mTxBuf.fill(0);
mCmt.setup(cfg->pinSclk, cfg->pinSdio, cfg->pinCsb, cfg->pinFcsb);
reset(genDtuSn, static_cast<RegionCfg>(region));
} }
void loop() override { void loop() override {
if(!mCfg->enabled)
return;
mCmt.loop(); mCmt.loop();
if((!mIrqRcvd) && (!mRqstGetRx)) if((!mIrqRcvd) && (!mRqstGetRx))
return; return;
@ -41,6 +50,9 @@ class CmtRadio : public Radio {
} }
void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override { void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override {
if(!mCfg->enabled)
return;
DPRINT(DBG_INFO, F("sendControlPacket cmd: ")); DPRINT(DBG_INFO, F("sendControlPacket cmd: "));
DBGHEXLN(cmd); DBGHEXLN(cmd);
initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME); initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME);
@ -59,6 +71,9 @@ class CmtRadio : public Radio {
} }
bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override { bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override {
if(!isChipConnected())
return false;
uint8_t fromCh = mCmt.freq2Chan(fromkHz); uint8_t fromCh = mCmt.freq2Chan(fromkHz);
uint8_t toCh = mCmt.freq2Chan(tokHz); uint8_t toCh = mCmt.freq2Chan(tokHz);
@ -68,6 +83,8 @@ class CmtRadio : public Radio {
bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override { bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override {
if((0xff == fromCh) || (0xff == toCh)) if((0xff == fromCh) || (0xff == toCh))
return false; return false;
if(!isChipConnected())
return false;
mCmt.switchChannel(fromCh); mCmt.switchChannel(fromCh);
sendSwitchChCmd(iv, toCh); sendSwitchChCmd(iv, toCh);
@ -188,6 +205,7 @@ class CmtRadio : public Radio {
} }
CmtType mCmt; CmtType mCmt;
cfgCmt_t *mCfg = nullptr;
bool mCmtAvail = false; bool mCmtAvail = false;
bool mRqstGetRx = false; bool mRqstGetRx = false;
uint32_t mMillis = 0; uint32_t mMillis = 0;

16
src/hms/cmtHal.h

@ -16,9 +16,7 @@
class cmtHal : public SpiPatcherHandle { class cmtHal : public SpiPatcherHandle {
public: public:
cmtHal() { cmtHal() {}
mSpiPatcher = SpiPatcher::getInstance(DEF_CMT_SPI_HOST);
}
void patch() override { void patch() override {
esp_rom_gpio_connect_out_signal(mPinSdio, spi_periph_signal[mHostDevice].spid_out, false, false); esp_rom_gpio_connect_out_signal(mPinSdio, spi_periph_signal[mHostDevice].spid_out, false, false);
@ -39,7 +37,13 @@ class cmtHal : public SpiPatcherHandle {
mPinFcs = static_cast<gpio_num_t>(fcs); mPinFcs = static_cast<gpio_num_t>(fcs);
mSpiSpeed = speed; mSpiSpeed = speed;
mHostDevice = mSpiPatcher->getDevice(); #if defined(CONFIG_IDF_TARGET_ESP32S3)
mHostDevice = SPI2_HOST;
#else
mHostDevice = (14 == clk) ? SPI2_HOST : SPI_HOST_OTHER;
#endif
mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
gpio_reset_pin(mPinSdio); gpio_reset_pin(mPinSdio);
gpio_set_direction(mPinSdio, GPIO_MODE_INPUT_OUTPUT); gpio_set_direction(mPinSdio, GPIO_MODE_INPUT_OUTPUT);
@ -50,6 +54,7 @@ class cmtHal : public SpiPatcherHandle {
gpio_set_level(mPinClk, 0); gpio_set_level(mPinClk, 0);
gpio_reset_pin(mPinCs); gpio_reset_pin(mPinCs);
request_spi();
spi_device_interface_config_t devcfg_reg = { spi_device_interface_config_t devcfg_reg = {
.command_bits = 1, .command_bits = 1,
.address_bits = 7, .address_bits = 7,
@ -66,7 +71,8 @@ class cmtHal : public SpiPatcherHandle {
.pre_cb = nullptr, .pre_cb = nullptr,
.post_cb = nullptr .post_cb = nullptr
}; };
ESP_ERROR_CHECK(spi_bus_add_device(mHostDevice, &devcfg_reg, &spi_reg)); mSpiPatcher->addDevice(mHostDevice, &devcfg_reg, &spi_reg);
release_spi();
gpio_reset_pin(mPinFcs); gpio_reset_pin(mPinFcs);
spi_device_interface_config_t devcfg_fifo = { spi_device_interface_config_t devcfg_fifo = {

54
src/network/AhoyEthernet.h

@ -23,30 +23,20 @@ class AhoyEthernet : public AhoyNetwork {
mEthSpi.begin(mConfig->sys.eth.pinMiso, mConfig->sys.eth.pinMosi, mConfig->sys.eth.pinSclk, mConfig->sys.eth.pinCs, mConfig->sys.eth.pinIrq, mConfig->sys.eth.pinRst); mEthSpi.begin(mConfig->sys.eth.pinMiso, mConfig->sys.eth.pinMosi, mConfig->sys.eth.pinSclk, mConfig->sys.eth.pinCs, mConfig->sys.eth.pinIrq, mConfig->sys.eth.pinRst);
ETH.setHostname(mConfig->sys.deviceName); ETH.setHostname(mConfig->sys.deviceName);
// static IP
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool {
return ETH.config(ip, gateway, mask, dns1, dns2);
});
} }
void tickNetworkLoop() override { void OnEvent(WiFiEvent_t event) override {
if(mAp.isEnabled()) switch(event) {
mAp.tickLoop(); case ARDUINO_EVENT_ETH_CONNECTED:
if(NetworkState::CONNECTED != mStatus) {
switch(mStatus) { mStatus = NetworkState::CONNECTED;
case NetworkState::DISCONNECTED: DPRINTLN(DBG_INFO, F("Network connected"));
if(mConnected) { setStaticIp();
mConnected = false;
mOnNetworkCB(false);
mAp.enable();
} }
break; break;
case NetworkState::CONNECTED: case ARDUINO_EVENT_ETH_GOT_IP:
break; mStatus = NetworkState::GOT_IP;
case NetworkState::GOT_IP:
if(!mConnected) { if(!mConnected) {
mAp.disable(); mAp.disable();
mConnected = true; mConnected = true;
@ -55,13 +45,39 @@ class AhoyEthernet : public AhoyNetwork {
mOnNetworkCB(true); mOnNetworkCB(true);
} }
break; break;
case ARDUINO_EVENT_ETH_STOP:
[[fallthrough]];
case ARDUINO_EVENT_ETH_DISCONNECTED:
mStatus = NetworkState::DISCONNECTED;
if(mConnected) {
mConnected = false;
mOnNetworkCB(false);
mAp.enable();
}
break;
default:
break;
} }
} }
void tickNetworkLoop() override {
if(mAp.isEnabled())
mAp.tickLoop();
}
String getIp(void) override { String getIp(void) override {
return ETH.localIP().toString(); return ETH.localIP().toString();
} }
private:
void setStaticIp() override {
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool {
return ETH.config(ip, gateway, mask, dns1, dns2);
});
}
private: private:
AhoyEthernetSpi mEthSpi; AhoyEthernetSpi mEthSpi;
}; };

31
src/network/AhoyEthernetSpi.h

@ -12,7 +12,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <esp_netif.h> #include <esp_netif.h>
#include <WiFiGeneric.h> #include <WiFiGeneric.h>
#include <driver/spi_master.h> #include "../utils/spiPatcher.h"
// Functions from WiFiGeneric // Functions from WiFiGeneric
void tcpipInit(); void tcpipInit();
@ -44,23 +44,14 @@ class AhoyEthernetSpi {
gpio_reset_pin(static_cast<gpio_num_t>(pin_int)); gpio_reset_pin(static_cast<gpio_num_t>(pin_int));
gpio_set_pull_mode(static_cast<gpio_num_t>(pin_int), GPIO_PULLUP_ONLY); gpio_set_pull_mode(static_cast<gpio_num_t>(pin_int), GPIO_PULLUP_ONLY);
#if defined(CONFIG_IDF_TARGET_ESP32S3)
mHostDevice = SPI3_HOST;
#else
mHostDevice = (14 == pin_sclk) ? SPI2_HOST : SPI3_HOST;
#endif
spi_bus_config_t buscfg = { mSpiPatcher = SpiPatcher::getInstance(mHostDevice, false);
.mosi_io_num = pin_mosi, mSpiPatcher->initBus(pin_mosi, pin_miso, pin_sclk, SPI_DMA_CH_AUTO);
.miso_io_num = pin_miso,
.sclk_io_num = pin_sclk,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.data4_io_num = -1,
.data5_io_num = -1,
.data6_io_num = -1,
.data7_io_num = -1,
.max_transfer_sz = 0, // uses default value internally
.flags = 0,
.intr_flags = 0
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
spi_device_interface_config_t devcfg = { spi_device_interface_config_t devcfg = {
.command_bits = 16, // actually address phase .command_bits = 16, // actually address phase
@ -79,8 +70,7 @@ class AhoyEthernetSpi {
.post_cb = nullptr .post_cb = nullptr
}; };
spi_device_handle_t spi; mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi);
ESP_ERROR_CHECK(spi_bus_add_device(SPI3_HOST, &devcfg, &spi));
// Reset sequence // Reset sequence
if(-1 != pin_rst) { if(-1 != pin_rst) {
@ -137,6 +127,9 @@ class AhoyEthernetSpi {
private: private:
esp_eth_handle_t eth_handle; esp_eth_handle_t eth_handle;
esp_netif_t *eth_netif; esp_netif_t *eth_netif;
spi_host_device_t mHostDevice;
spi_device_handle_t spi;
SpiPatcher *mSpiPatcher;
}; };
#endif /*__ETH_SPI_H__*/ #endif /*__ETH_SPI_H__*/

12
src/network/AhoyNetwork.h

@ -28,7 +28,6 @@ class AhoyNetwork {
if('\0' == mConfig->sys.deviceName[0]) if('\0' == mConfig->sys.deviceName[0])
snprintf(mConfig->sys.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME); snprintf(mConfig->sys.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME);
WiFi.hostname(mConfig->sys.deviceName);
mAp.setup(&mConfig->sys); mAp.setup(&mConfig->sys);
@ -117,13 +116,17 @@ class AhoyNetwork {
void scan(void) { void scan(void) {
mScanActive = true; mScanActive = true;
if(NetworkState::GOT_IP != mStatus) if(mWifiConnecting) {
mWifiConnecting = false;
WiFi.disconnect(); WiFi.disconnect();
}
WiFi.scanNetworks(true, true); WiFi.scanNetworks(true, true);
} }
#endif #endif
protected: protected:
virtual void setStaticIp() = 0;
void setupIp(std::function<bool(IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2)> cb) { void setupIp(std::function<bool(IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2)> cb) {
if(mConfig->sys.ip.ip[0] != 0) { if(mConfig->sys.ip.ip[0] != 0) {
IPAddress ip(mConfig->sys.ip.ip); IPAddress ip(mConfig->sys.ip.ip);
@ -136,7 +139,7 @@ class AhoyNetwork {
} }
} }
void OnEvent(WiFiEvent_t event) { virtual void OnEvent(WiFiEvent_t event) {
switch(event) { switch(event) {
case SYSTEM_EVENT_STA_CONNECTED: case SYSTEM_EVENT_STA_CONNECTED:
[[fallthrough]]; [[fallthrough]];
@ -231,6 +234,9 @@ class AhoyNetwork {
uint32_t *mUtcTimestamp = nullptr; uint32_t *mUtcTimestamp = nullptr;
bool mConnected = false; bool mConnected = false;
bool mScanActive = false; bool mScanActive = false;
#if !defined(ETHERNET)
bool mWifiConnecting = false;
#endif
OnNetworkCB mOnNetworkCB; OnNetworkCB mOnNetworkCB;
OnTimeCB mOnTimeCB; OnTimeCB mOnTimeCB;

62
src/network/AhoyWifiEsp32.h

@ -17,16 +17,16 @@ class AhoyWifi : public AhoyNetwork {
void begin() override { void begin() override {
mAp.enable(); mAp.enable();
// static IP if(String(FB_WIFI_SSID) == mConfig->sys.stationSsid)
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { return; // no station wifi defined
return WiFi.config(ip, gateway, mask, dns1, dns2);
});
WiFi.disconnect(); // clean up
WiFi.setHostname(mConfig->sys.deviceName); WiFi.setHostname(mConfig->sys.deviceName);
#if !defined(AP_ONLY) #if !defined(AP_ONLY)
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, WIFI_ALL_CHANNEL_SCAN); WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, WIFI_ALL_CHANNEL_SCAN);
mWifiConnecting = true;
DBGPRINT(F("connect to network '")); DBGPRINT(F("connect to network '"));
DBGPRINT(mConfig->sys.stationSsid); DBGPRINT(mConfig->sys.stationSsid);
@ -34,24 +34,19 @@ class AhoyWifi : public AhoyNetwork {
#endif #endif
} }
void tickNetworkLoop() override { void OnEvent(WiFiEvent_t event) override {
if(mAp.isEnabled()) switch(event) {
mAp.tickLoop(); case SYSTEM_EVENT_STA_CONNECTED:
if(NetworkState::CONNECTED != mStatus) {
switch(mStatus) { mStatus = NetworkState::CONNECTED;
case NetworkState::DISCONNECTED: mWifiConnecting = false;
if(mConnected) { DPRINTLN(DBG_INFO, F("Network connected"));
mConnected = false; setStaticIp();
mOnNetworkCB(false);
MDNS.end();
begin();
} }
break; break;
case NetworkState::CONNECTED: case SYSTEM_EVENT_STA_GOT_IP:
break; mStatus = NetworkState::GOT_IP;
case NetworkState::GOT_IP:
if(mAp.isEnabled()) if(mAp.isEnabled())
mAp.disable(); mAp.disable();
@ -62,12 +57,41 @@ class AhoyWifi : public AhoyNetwork {
mOnNetworkCB(true); mOnNetworkCB(true);
} }
break; break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
[[fallthrough]];
case ARDUINO_EVENT_WIFI_STA_STOP:
[[fallthrough]];
case SYSTEM_EVENT_STA_DISCONNECTED:
mStatus = NetworkState::DISCONNECTED;
if(mConnected) {
mConnected = false;
mOnNetworkCB(false);
MDNS.end();
begin();
}
break;
default:
break;
} }
} }
void tickNetworkLoop() override {
if(mAp.isEnabled())
mAp.tickLoop();
}
String getIp(void) override { String getIp(void) override {
return WiFi.localIP().toString(); return WiFi.localIP().toString();
} }
private:
void setStaticIp() override {
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool {
return WiFi.config(ip, gateway, mask, dns1, dns2);
});
}
}; };
#endif /*ESP32 & !ETHERNET*/ #endif /*ESP32 & !ETHERNET*/

15
src/network/AhoyWifiEsp8266.h

@ -18,11 +18,6 @@ class AhoyWifi : public AhoyNetwork {
void begin() override { void begin() override {
mAp.enable(); mAp.enable();
// static IP
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool {
return WiFi.config(ip, gateway, mask, dns1, dns2);
});
WiFi.setHostname(mConfig->sys.deviceName); WiFi.setHostname(mConfig->sys.deviceName);
mBSSIDList.clear(); mBSSIDList.clear();
} }
@ -37,6 +32,7 @@ class AhoyWifi : public AhoyNetwork {
case NetworkState::DISCONNECTED: case NetworkState::DISCONNECTED:
if(mConnected) { if(mConnected) {
mConnected = false; mConnected = false;
mWifiConnecting = false;
mOnNetworkCB(false); mOnNetworkCB(false);
mAp.enable(); mAp.enable();
MDNS.end(); MDNS.end();
@ -76,16 +72,19 @@ class AhoyWifi : public AhoyNetwork {
} }
DBGPRINTLN(""); DBGPRINTLN("");
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]); WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]);
mWifiConnecting = true;
break; break;
case NetworkState::CONNECTING: case NetworkState::CONNECTING:
if (isTimeout(TIMEOUT)) { if (isTimeout(TIMEOUT)) {
WiFi.disconnect(); WiFi.disconnect();
mWifiConnecting = false;
mStatus = mBSSIDList.empty() ? NetworkState::DISCONNECTED : NetworkState::SCAN_READY; mStatus = mBSSIDList.empty() ? NetworkState::DISCONNECTED : NetworkState::SCAN_READY;
} }
break; break;
case NetworkState::CONNECTED: case NetworkState::CONNECTED:
setStaticIp();
break; break;
case NetworkState::GOT_IP: case NetworkState::GOT_IP:
@ -117,6 +116,12 @@ class AhoyWifi : public AhoyNetwork {
} }
private: private:
void setStaticIp() override {
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool {
return WiFi.config(ip, gateway, mask, dns1, dns2);
});
}
bool getBSSIDs() { bool getBSSIDs() {
bool result = false; bool result = false;
int n = WiFi.scanComplete(); int n = WiFi.scanComplete();

4
src/platformio.ini

@ -23,11 +23,11 @@ extra_scripts =
pre:../scripts/convertHtml.py pre:../scripts/convertHtml.py
pre:../scripts/applyPatches.py pre:../scripts/applyPatches.py
pre:../scripts/reduceGxEPD2.py pre:../scripts/reduceGxEPD2.py
#post:../scripts/add_littlefs_binary.py
lib_deps = lib_deps =
https://github.com/esphome/ESPAsyncWebServer @ ^3.1.0 https://github.com/esphome/ESPAsyncWebServer @ ^3.2.0
https://github.com/nRF24/RF24.git#v1.4.8 https://github.com/nRF24/RF24.git#v1.4.8
paulstoffregen/Time @ ^1.6.1 paulstoffregen/Time @ ^1.6.1
https://github.com/bertmelis/espMqttClient#v1.6.0 https://github.com/bertmelis/espMqttClient#v1.6.0
bblanchon/ArduinoJson @ ^6.21.3 bblanchon/ArduinoJson @ ^6.21.3

24
src/plugins/Display/Display_ePaper.cpp

@ -1,17 +1,12 @@
#include "Display_ePaper.h" #include "Display_ePaper.h"
#ifdef ESP8266 #if defined(ESP32)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h> #include <WiFi.h>
#endif
#include "../../utils/helper.h" #include "../../utils/helper.h"
#include "imagedata.h" #include "imagedata.h"
#include "defines.h" #include "defines.h"
#include "../plugin_lang.h" #include "../plugin_lang.h"
#if defined(ESP32)
static const uint32_t spiClk = 4000000; // 4 MHz static const uint32_t spiClk = 4000000; // 4 MHz
#if defined(ESP32) && defined(USE_HSPI_FOR_EPD) #if defined(ESP32) && defined(USE_HSPI_FOR_EPD)
@ -34,13 +29,18 @@ void DisplayEPaper::init(uint8_t type, uint8_t _CS, uint8_t _DC, uint8_t _RST, u
if (DISP_TYPE_T10_EPAPER == type) { if (DISP_TYPE_T10_EPAPER == type) {
Serial.begin(115200); Serial.begin(115200);
_display = new GxEPD2_BW<GxEPD2_150_BN, GxEPD2_150_BN::HEIGHT>(GxEPD2_150_BN(_CS, _DC, _RST, _BUSY));
#if defined(ESP32) && defined(USE_HSPI_FOR_EPD) #if defined(SPI_HAL)
hspi.begin(_SCK, _BUSY, _MOSI, _CS); hal.init(_MOSI, _DC, _SCK, _CS, _RST, _BUSY);
_display->epd2.selectSPI(hspi, SPISettings(spiClk, MSBFIRST, SPI_MODE0)); _display = new GxEPD2_BW<GxEPD2_150_BN, GxEPD2_150_BN::HEIGHT>(GxEPD2_150_BN(&hal));
#elif defined(ESP32) && defined(PLUGIN_DISPLAY) #else
_display->epd2.init(_SCK, _MOSI, 115200, true, 20, false); _display = new GxEPD2_BW<GxEPD2_150_BN, GxEPD2_150_BN::HEIGHT>(GxEPD2_150_BN(_CS, _DC, _RST, _BUSY));
#if defined(USE_HSPI_FOR_EPD)
hspi.begin(_SCK, _BUSY, _MOSI, _CS);
_display->epd2.selectSPI(hspi, SPISettings(spiClk, MSBFIRST, SPI_MODE0));
#elif defined(PLUGIN_DISPLAY)
_display->epd2.init(_SCK, _MOSI, 115200, true, 20, false);
#endif
#endif #endif
_display->init(115200, true, 20, false); _display->init(115200, true, 20, false);
_display->setRotation(mDisplayRotation); _display->setRotation(mDisplayRotation);

7
src/plugins/Display/Display_ePaper.h

@ -12,7 +12,11 @@
#define EPAPER_MAX_TEXT_LEN 35 #define EPAPER_MAX_TEXT_LEN 35
#include <GxEPD2_BW.h> #include <GxEPD2_BW.h>
#if defined(SPI_HAL)
#include "epdHal.h"
#else
#include <SPI.h> #include <SPI.h>
#endif
// FreeFonts from Adafruit_GFX // FreeFonts from Adafruit_GFX
#include <Fonts/FreeSans12pt7b.h> #include <Fonts/FreeSans12pt7b.h>
@ -60,6 +64,9 @@ class DisplayEPaper {
const char* _version; const char* _version;
RefreshStatus mRefreshState, mNextRefreshState; RefreshStatus mRefreshState, mNextRefreshState;
uint8_t mSecondCnt; uint8_t mSecondCnt;
#if defined(SPI_HAL)
epdHal hal;
#endif
}; };
#endif // ESP32 #endif // ESP32

304
src/plugins/Display/epdHal.h

@ -0,0 +1,304 @@
//-----------------------------------------------------------------------------
// 2024 Ahoy, https://github.com/lumpapu/ahoy
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed
//-----------------------------------------------------------------------------
#ifndef __EPD_HAL_H__
#define __EPD_HAL_H__
#pragma once
#include "../../utils/spiPatcher.h"
#include <esp_rom_gpio.h>
#include <GxEPD2_BW.h>
#define EPD_DEFAULT_SPI_SPEED 4000000 // 4 MHz
class epdHal: public GxEPD2_HalInterface, public SpiPatcherHandle {
public:
epdHal() {}
void patch() override {
esp_rom_gpio_connect_out_signal(mPinMosi, spi_periph_signal[mHostDevice].spid_out, false, false);
esp_rom_gpio_connect_in_signal(mPinBusy, spi_periph_signal[mHostDevice].spiq_in, false);
esp_rom_gpio_connect_out_signal(mPinClk, spi_periph_signal[mHostDevice].spiclk_out, false, false);
}
void unpatch() override {
esp_rom_gpio_connect_out_signal(mPinMosi, SIG_GPIO_OUT_IDX, false, false);
esp_rom_gpio_connect_in_signal(mPinBusy, GPIO_MATRIX_CONST_ZERO_INPUT, false);
esp_rom_gpio_connect_out_signal(mPinClk, SIG_GPIO_OUT_IDX, false, false);
}
void init(int8_t mosi, int8_t dc, int8_t sclk, int8_t cs, int8_t rst, int8_t busy, int32_t speed = EPD_DEFAULT_SPI_SPEED) {
mPinMosi = static_cast<gpio_num_t>(mosi);
mPinDc = static_cast<gpio_num_t>(dc);
mPinClk = static_cast<gpio_num_t>(sclk);
mPinCs = static_cast<gpio_num_t>(cs);
mPinRst = static_cast<gpio_num_t>(rst);
mPinBusy = static_cast<gpio_num_t>(busy);
mSpiSpeed = speed;
#if defined(CONFIG_IDF_TARGET_ESP32S3)
mHostDevice = SPI3_HOST;
#else
mHostDevice = (14 == sclk) ? SPI2_HOST : SPI_HOST_OTHER;
#endif
mSpiPatcher = SpiPatcher::getInstance(mHostDevice);
gpio_reset_pin(mPinMosi);
gpio_set_direction(mPinMosi, GPIO_MODE_OUTPUT);
gpio_set_level(mPinMosi, 1);
gpio_reset_pin(mPinClk);
gpio_set_direction(mPinClk, GPIO_MODE_OUTPUT);
gpio_set_level(mPinClk, 0);
gpio_reset_pin(mPinCs);
spi_device_interface_config_t devcfg = {
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0,
.mode = 0,
.duty_cycle_pos = 0,
.cs_ena_pretrans = 0,
.cs_ena_posttrans = 0,
.clock_speed_hz = mSpiSpeed,
.input_delay_ns = 0,
.spics_io_num = mPinCs,
.flags = 0,
.queue_size = 1,
.pre_cb = nullptr,
.post_cb = nullptr
};
mSpiPatcher->addDevice(mHostDevice, &devcfg, &spi);
if(GPIO_NUM_NC != mPinRst) {
gpio_reset_pin(mPinRst);
gpio_set_direction(mPinRst, GPIO_MODE_OUTPUT);
gpio_set_level(mPinRst, HIGH);
}
gpio_reset_pin(mPinDc);
gpio_set_direction(mPinDc, GPIO_MODE_OUTPUT);
gpio_set_level(mPinDc, HIGH);
//gpio_reset_pin(mPinBusy);
//gpio_set_direction(mPinBusy, GPIO_MODE_INPUT);
}
void rstMode(uint8_t mode) override {
if(GPIO_NUM_NC != mPinRst)
gpio_set_direction(mPinRst, static_cast<gpio_mode_t>(mode));
}
void rst(bool level) override {
if(GPIO_NUM_NC != mPinRst)
gpio_set_level(mPinRst, level);
}
int getBusy(void) override {
return gpio_get_level(mPinBusy);
}
bool isRst(void) override {
return (GPIO_NUM_NC != mPinRst);
}
void write(uint8_t buf) override {
uint8_t data[1];
data[0] = buf;
request_spi();
size_t spiLen = static_cast<size_t>(1u) << 3;
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = spiLen,
.rxlength = spiLen,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
release_spi();
}
void write(const uint8_t *buf, uint16_t n) override {
uint8_t data[n];
std::copy(&buf[0], &buf[n], &data[0]);
request_spi();
size_t spiLen = static_cast<size_t>(n) << 3;
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = spiLen,
.rxlength = spiLen,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
release_spi();
}
void write(const uint8_t *buf, uint16_t n, int16_t fill_with_zeroes) override {
uint8_t data[n + fill_with_zeroes];
memset(data, 0, (n + fill_with_zeroes));
for (uint16_t i = 0; i < n; i++) {
data[i] = pgm_read_byte(&*buf++);
}
request_spi();
spi_transaction_t t = {
.flags = SPI_TRANS_CS_KEEP_ACTIVE,
.cmd = 0,
.addr = 0,
.length = 1u,
.rxlength = 1u,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
size_t offs = 0;
spi_device_acquire_bus(spi, portMAX_DELAY);
while(offs < (n + fill_with_zeroes)) {
t.length = (64u << 3);
t.rxlength = t.length;
t.tx_buffer = &data[offs];
offs += 64;
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
}
spi_device_release_bus(spi);
release_spi();
}
void writeCmd(const uint8_t val) override {
uint8_t data[1];
data[0] = val;
request_spi();
gpio_set_level(mPinDc, LOW);
size_t spiLen = static_cast<size_t>(1u) << 3;
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = spiLen,
.rxlength = spiLen,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
gpio_set_level(mPinDc, HIGH);
release_spi();
}
void writeCmd(const uint8_t *buf, uint8_t n, bool isPGM) override {
uint8_t data[n-1];
data[0] = (isPGM) ? pgm_read_byte(&*buf++) : buf[0];
request_spi();
gpio_set_level(mPinDc, LOW);
spi_device_acquire_bus(spi, portMAX_DELAY);
size_t spiLen = static_cast<size_t>(1u) << 3;
spi_transaction_t t = {
.flags = SPI_TRANS_CS_KEEP_ACTIVE,
.cmd = 0,
.addr = 0,
.length = spiLen,
.rxlength = spiLen,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
gpio_set_level(mPinDc, HIGH);
if(isPGM) {
for (uint16_t i = 0; i < n; i++) {
data[i] = pgm_read_byte(&*buf++);
}
} else
std::copy(&buf[1], &buf[n], &data[0]);
spiLen = static_cast<size_t>(n-1) << 3;
spi_transaction_t t1 = {
.flags = SPI_TRANS_CS_KEEP_ACTIVE,
.cmd = 0,
.addr = 0,
.length = spiLen,
.rxlength = spiLen,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t1));
spi_device_release_bus(spi);
release_spi();
}
void startTransfer(void) override {
request_spi();
}
void endTransfer(void) override {
release_spi();
}
void transfer(const uint8_t val) override {
uint8_t data[1];
data[0] = val;
size_t spiLen = static_cast<size_t>(1u) << 3;
spi_transaction_t t = {
.flags = 0,
.cmd = 0,
.addr = 0,
.length = spiLen,
.rxlength = spiLen,
.user = NULL,
.tx_buffer = data,
.rx_buffer = data
};
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
}
private:
inline void request_spi() {
mSpiPatcher->request(this);
}
inline void release_spi() {
mSpiPatcher->release();
}
private:
gpio_num_t mPinMosi = GPIO_NUM_NC;
gpio_num_t mPinDc = GPIO_NUM_NC;
gpio_num_t mPinClk = GPIO_NUM_NC;
gpio_num_t mPinCs = GPIO_NUM_NC;
gpio_num_t mPinRst = GPIO_NUM_NC;
gpio_num_t mPinBusy = GPIO_NUM_NC;
int32_t mSpiSpeed = EPD_DEFAULT_SPI_SPEED;
spi_host_device_t mHostDevice;
spi_device_handle_t spi;
SpiPatcher *mSpiPatcher;
};
#endif /*__EPD_HAL_H__*/

67
src/plugins/MaxPower.h

@ -0,0 +1,67 @@
//-----------------------------------------------------------------------------
// 2024 Ahoy, https://github.com/lumpapu/ahoy
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed
//-----------------------------------------------------------------------------
#ifndef __MAX_VALUE__
#define __MAX_VALUE__
#pragma once
#include <array>
#include <utility>
#include "../hm/hmDefines.h"
template<class T=float>
class MaxPower {
public:
MaxPower() {
mTs = nullptr;
mMaxDiff = 60;
reset();
}
void setup(uint32_t *ts, uint16_t interval) {
mTs = ts;
mMaxDiff = interval * 4;
}
void reset(void) {
mValues.fill(std::make_pair(0, 0.0));
mLast = 0.0;
}
void payloadEvent(uint8_t cmd, Inverter<> *iv) {
if(RealTimeRunData_Debug != cmd)
return;
if(nullptr == iv)
return;
if(iv->id >= MAX_NUM_INVERTERS)
return;
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
mValues[iv->id] = std::make_pair(*mTs, iv->getChannelFieldValue(CH0, FLD_PAC, rec));
}
T getTotalMaxPower(void) {
T val = 0;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
if((mValues[i].first + mMaxDiff) >= *mTs)
val += mValues[i].second;
else if(mValues[i].first > 0)
return mLast; // old data
}
if(val > mLast)
mLast = val;
return mLast;
}
private:
uint32_t *mTs;
uint32_t mMaxDiff;
float mLast;
std::array<std::pair<uint32_t, T>, MAX_NUM_INVERTERS> mValues;
};
#endif

6
src/plugins/history.h

@ -57,17 +57,15 @@ class HistoryData {
void tickerSecond() { void tickerSecond() {
float curPwr = 0; float curPwr = 0;
//float maxPwr = 0;
float yldDay = -0.1; float yldDay = -0.1;
uint32_t ts = 0; uint32_t ts = 0;
for (uint8_t i = 0; i < mSys->getNumInverters(); i++) { for (uint8_t i = 0; i < mSys->getNumInverters(); i++) {
Inverter<> *iv = mSys->getInverterByPos(i); Inverter<> *iv = mSys->getInverterByPos(i);
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
if (iv == NULL) if (iv == NULL)
continue; continue;
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
curPwr += iv->getChannelFieldValue(CH0, FLD_PAC, rec); curPwr += iv->getChannelFieldValue(CH0, FLD_PAC, rec);
//maxPwr += iv->getChannelFieldValue(CH0, FLD_MP, rec);
yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec); yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec);
if (rec->ts > ts) if (rec->ts > ts)
ts = rec->ts; ts = rec->ts;
@ -81,8 +79,6 @@ class HistoryData {
if (curPwr > mMaximumDay) if (curPwr > mMaximumDay)
mMaximumDay = roundf(curPwr); mMaximumDay = roundf(curPwr);
} }
//if (maxPwr > 0)
// mMaximumDay = roundf(maxPwr);
} }
if ((++mCurPwrDay.loopCnt % mCurPwrDay.refreshCycle) == 0) { if ((++mCurPwrDay.loopCnt % mCurPwrDay.refreshCycle) == 0) {

2
src/publisher/pubMqtt.h

@ -62,7 +62,7 @@ class PubMqtt {
mUptime = uptime; mUptime = uptime;
mIntervalTimeout = 1; mIntervalTimeout = 1;
SendIvData.setup(sys, cfg_mqtt->json, utcTs, &mSendList); SendIvData.setup(app, sys, utcTs, &mSendList);
SendIvData.setPublishFunc([this](const char *subTopic, const char *payload, bool retained, uint8_t qos) { SendIvData.setPublishFunc([this](const char *subTopic, const char *payload, bool retained, uint8_t qos) {
publish(subTopic, payload, retained, true, qos); publish(subTopic, payload, retained, true, qos);
}); });

10
src/publisher/pubMqttIvData.h

@ -24,7 +24,8 @@ class PubMqttIvData {
public: public:
PubMqttIvData() : mTotal{}, mSubTopic{}, mVal{} {} PubMqttIvData() : mTotal{}, mSubTopic{}, mVal{} {}
void setup(HMSYSTEM *sys, bool json, uint32_t *utcTs, std::queue<sendListCmdIv> *sendList) { void setup(IApp *app, HMSYSTEM *sys, uint32_t *utcTs, std::queue<sendListCmdIv> *sendList) {
mApp = app;
mSys = sys; mSys = sys;
mJson = json; mJson = json;
mUtcTimestamp = utcTs; mUtcTimestamp = utcTs;
@ -169,9 +170,6 @@ class PubMqttIvData {
case FLD_PDC: case FLD_PDC:
mTotal[3] += mIv->getValue(mPos, rec); mTotal[3] += mIv->getValue(mPos, rec);
break; break;
case FLD_MP:
mTotal[4] += mIv->getValue(mPos, rec);
break;
} }
} else } else
mAllTotalFound = false; mAllTotalFound = false;
@ -299,6 +297,7 @@ class PubMqttIvData {
case 4: case 4:
fieldId = FLD_MP; fieldId = FLD_MP;
retained = false; retained = false;
mTotal[4] = mApp->getTotalMaxPower();
break; break;
} }
if (!mJson) { if (!mJson) {
@ -328,6 +327,9 @@ class PubMqttIvData {
} }
} }
private:
IApp *mApp = nullptr;
HMSYSTEM *mSys = nullptr; HMSYSTEM *mSys = nullptr;
uint32_t *mUtcTimestamp = nullptr; uint32_t *mUtcTimestamp = nullptr;
pubMqttPublisherType mPublish; pubMqttPublisherType mPublish;

2
src/utils/dbg.h

@ -110,7 +110,7 @@
#if DEBUG_LEVEL >= DBG_ERROR #if DEBUG_LEVEL >= DBG_ERROR
#define PERR(str) DBGPRINT(F("E: ")); DBGPRINT(str); #define PERR(str) DBGPRINT(F("E: ")); DBGPRINT(str);
#define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str); #define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str); DSERIAL.flush();
#else #else
#define PERR(str) #define PERR(str)
#define PERRLN(str) #define PERRLN(str)

3
src/utils/spiPatcher.cpp

@ -5,5 +5,6 @@
#if defined(ESP32) #if defined(ESP32)
#include "spiPatcher.h" #include "spiPatcher.h"
SpiPatcher *SpiPatcher::mInstance = nullptr; SpiPatcher *SpiPatcher::InstanceHost2 = nullptr;
SpiPatcher *SpiPatcher::InstanceHost3 = nullptr;
#endif #endif

78
src/utils/spiPatcher.h

@ -9,23 +9,38 @@
#if defined(ESP32) #if defined(ESP32)
#include "dbg.h"
#include "spiPatcherHandle.h" #include "spiPatcherHandle.h"
#include <driver/spi_master.h> #include <driver/spi_master.h>
#include <freertos/semphr.h> #include <freertos/semphr.h>
#if (SOC_SPI_PERIPH_NUM > 2)
#define SPI_HOST_OTHER SPI3_HOST
#else
#define SPI_HOST_OTHER SPI2_HOST
#endif
class SpiPatcher { class SpiPatcher {
protected: protected:
explicit SpiPatcher(spi_host_device_t dev) : explicit SpiPatcher(spi_host_device_t dev) :
mHostDevice(dev), mCurHandle(nullptr) { mCurHandle(nullptr) {
// Use binary semaphore instead of mutex for performance reasons // Use binary semaphore instead of mutex for performance reasons
mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer); mutex = xSemaphoreCreateBinaryStatic(&mutex_buffer);
xSemaphoreGive(mutex); xSemaphoreGive(mutex);
mDev = dev;
mBusState = ESP_FAIL;
}
spi_bus_config_t buscfg = { public:
.mosi_io_num = -1, SpiPatcher(const SpiPatcher &other) = delete;
.miso_io_num = -1, void operator=(const SpiPatcher &) = delete;
.sclk_io_num = -1,
esp_err_t initBus(int mosi = -1, int miso = -1, int sclk = -1, spi_common_dma_t dmaType = SPI_DMA_DISABLED) {
mBusConfig = spi_bus_config_t {
.mosi_io_num = mosi,
.miso_io_num = miso,
.sclk_io_num = sclk,
.quadwp_io_num = -1, .quadwp_io_num = -1,
.quadhd_io_num = -1, .quadhd_io_num = -1,
.data4_io_num = -1, .data4_io_num = -1,
@ -36,26 +51,48 @@ class SpiPatcher {
.flags = 0, .flags = 0,
.intr_flags = 0 .intr_flags = 0
}; };
ESP_ERROR_CHECK(spi_bus_initialize(mHostDevice, &buscfg, SPI_DMA_DISABLED)); ESP_ERROR_CHECK((mBusState = spi_bus_initialize(mDev, &mBusConfig, dmaType)));
}
public: return mBusState;
SpiPatcher(SpiPatcher &other) = delete; }
void operator=(const SpiPatcher &) = delete;
static SpiPatcher* getInstance(spi_host_device_t dev) { static SpiPatcher* getInstance(spi_host_device_t dev, bool initialize = true) {
if(nullptr == mInstance) if(SPI2_HOST == dev) {
mInstance = new SpiPatcher(dev); if(nullptr == InstanceHost2) {
return mInstance; InstanceHost2 = new SpiPatcher(dev);
if(initialize)
InstanceHost2->initBus();
}
return InstanceHost2;
} else { // SPI3_HOST
if(nullptr == InstanceHost3) {
InstanceHost3 = new SpiPatcher(dev);
if(initialize)
InstanceHost3->initBus();
}
return InstanceHost3;
}
} }
~SpiPatcher() { vSemaphoreDelete(mutex); } ~SpiPatcher() { vSemaphoreDelete(mutex); }
spi_host_device_t getDevice() { inline void addDevice(spi_host_device_t host_id, const spi_device_interface_config_t *dev_config, spi_device_handle_t *handle) {
return mHostDevice; assert(mBusState == ESP_OK);
if(SPI2_HOST == host_id)
mHost2Cnt++;
#if (SOC_SPI_PERIPH_NUM > 2)
if(SPI3_HOST == host_id)
mHost3Cnt++;
#endif
if((mHost2Cnt > 3) || (mHost3Cnt > 3))
DPRINTLN(DBG_ERROR, F("maximum number of SPI devices reached (3)"));
ESP_ERROR_CHECK(spi_bus_add_device(host_id, dev_config, handle));
} }
inline void request(SpiPatcherHandle* handle) { inline void request(SpiPatcherHandle* handle) {
assert(mBusState == ESP_OK);
xSemaphoreTake(mutex, portMAX_DELAY); xSemaphoreTake(mutex, portMAX_DELAY);
if (mCurHandle != handle) { if (mCurHandle != handle) {
@ -70,17 +107,22 @@ class SpiPatcher {
} }
inline void release() { inline void release() {
assert(mBusState == ESP_OK);
xSemaphoreGive(mutex); xSemaphoreGive(mutex);
} }
protected: protected:
static SpiPatcher *mInstance; static SpiPatcher *InstanceHost2;
static SpiPatcher *InstanceHost3;
private: private:
const spi_host_device_t mHostDevice;
SpiPatcherHandle* mCurHandle; SpiPatcherHandle* mCurHandle;
SemaphoreHandle_t mutex; SemaphoreHandle_t mutex;
StaticSemaphore_t mutex_buffer; StaticSemaphore_t mutex_buffer;
uint8_t mHost2Cnt = 0, mHost3Cnt = 0;
spi_host_device_t mDev = SPI2_HOST;
esp_err_t mBusState = ESP_FAIL;
spi_bus_config_t mBusConfig;
}; };
#endif /*ESP32*/ #endif /*ESP32*/

61
src/web/RestApi.h

@ -103,9 +103,10 @@ class RestApi {
else if(path == "setup/getip") getIp(root); else if(path == "setup/getip") getIp(root);
#endif /* !defined(ETHERNET) */ #endif /* !defined(ETHERNET) */
else if(path == "live") getLive(request,root); else if(path == "live") getLive(request,root);
else if (path == "powerHistory") getPowerHistory(request, root); #if defined(ENABLE_HISTORY)
else if (path == "powerHistoryDay") getPowerHistoryDay(request, root); else if (path == "powerHistory") getPowerHistory(request, root, HistoryStorageType::POWER);
else if (path == "yieldDayHistory") getYieldDayHistory(request, root); else if (path == "powerHistoryDay") getPowerHistory(request, root, HistoryStorageType::POWER_DAY);
#endif /*ENABLE_HISTORY*/
else { else {
if(path.substring(0, 12) == "inverter/id/") if(path.substring(0, 12) == "inverter/id/")
getInverter(root, request->url().substring(17).toInt()); getInverter(root, request->url().substring(17).toInt());
@ -298,7 +299,6 @@ class RestApi {
#if defined(ENABLE_HISTORY) #if defined(ENABLE_HISTORY)
ep[F("powerHistory")] = url + F("powerHistory"); ep[F("powerHistory")] = url + F("powerHistory");
ep[F("powerHistoryDay")] = url + F("powerHistoryDay"); ep[F("powerHistoryDay")] = url + F("powerHistoryDay");
ep[F("yieldDayHistory")] = url + F("yieldDayHistory");
#endif #endif
} }
@ -574,12 +574,13 @@ class RestApi {
} }
obj[F("interval")] = String(mConfig->inst.sendInterval); obj[F("interval")] = String(mConfig->inst.sendInterval);
obj[F("max_num_inverters")] = MAX_NUM_INVERTERS; obj[F("max_num_inverters")] = MAX_NUM_INVERTERS;
obj[F("rstMid")] = (bool)mConfig->inst.rstYieldMidNight; obj[F("rstMid")] = (bool)mConfig->inst.rstValsAtMidNight;
obj[F("rstNotAvail")] = (bool)mConfig->inst.rstValsNotAvail; obj[F("rstNotAvail")] = (bool)mConfig->inst.rstValsNotAvail;
obj[F("rstComStop")] = (bool)mConfig->inst.rstValsCommStop; obj[F("rstComStop")] = (bool)mConfig->inst.rstValsCommStop;
obj[F("rstComStart")] = (bool)mConfig->inst.rstValsCommStart;
obj[F("strtWthtTm")] = (bool)mConfig->inst.startWithoutTime; obj[F("strtWthtTm")] = (bool)mConfig->inst.startWithoutTime;
obj[F("rdGrid")] = (bool)mConfig->inst.readGrid; obj[F("rdGrid")] = (bool)mConfig->inst.readGrid;
obj[F("rstMaxMid")] = (bool)mConfig->inst.rstMaxValsMidNight; obj[F("rstMaxMid")] = (bool)mConfig->inst.rstIncludeMaxVals;
} }
void getInverter(JsonObject obj, uint8_t id) { void getInverter(JsonObject obj, uint8_t id) {
@ -604,7 +605,7 @@ class RestApi {
obj[F("alarm_cnt")] = iv->alarmCnt; obj[F("alarm_cnt")] = iv->alarmCnt;
obj[F("rssi")] = iv->rssi; obj[F("rssi")] = iv->rssi;
obj[F("ts_max_ac_pwr")] = iv->tsMaxAcPower; obj[F("ts_max_ac_pwr")] = iv->tsMaxAcPower;
obj[F("ts_max_temp")] = iv->tsMaxTemp; obj[F("ts_max_temp")] = iv->tsMaxTemperature;
JsonArray ch = obj.createNestedArray("ch"); JsonArray ch = obj.createNestedArray("ch");
@ -908,6 +909,7 @@ class RestApi {
void getLive(AsyncWebServerRequest *request, JsonObject obj) { void getLive(AsyncWebServerRequest *request, JsonObject obj) {
getGeneric(request, obj.createNestedObject(F("generic"))); getGeneric(request, obj.createNestedObject(F("generic")));
obj[F("refresh")] = mConfig->inst.sendInterval; obj[F("refresh")] = mConfig->inst.sendInterval;
obj[F("max_total_pwr")] = ah::round3(mApp->getTotalMaxPower());
for (uint8_t fld = 0; fld < sizeof(acList); fld++) { for (uint8_t fld = 0; fld < sizeof(acList); fld++) {
obj[F("ch0_fld_units")][fld] = String(units[fieldUnits[acList[fld]]]); obj[F("ch0_fld_units")][fld] = String(units[fieldUnits[acList[fld]]]);
@ -928,42 +930,39 @@ class RestApi {
} }
} }
void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj) { #if defined(ENABLE_HISTORY)
void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj, HistoryStorageType type) {
getGeneric(request, obj.createNestedObject(F("generic"))); getGeneric(request, obj.createNestedObject(F("generic")));
#if defined(ENABLE_HISTORY) obj[F("refresh")] = mApp->getHistoryPeriod(static_cast<uint8_t>(type));
obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::POWER);
uint16_t max = 0; uint16_t max = 0;
for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) {
uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::POWER, fld); uint16_t value = mApp->getHistoryValue(static_cast<uint8_t>(type), fld);
obj[F("value")][fld] = value; obj[F("value")][fld] = value;
if (value > max) if (value > max)
max = value; max = value;
} }
obj[F("max")] = max; obj[F("max")] = max;
obj[F("lastValueTs")] = mApp->getHistoryLastValueTs((uint8_t)HistoryStorageType::POWER);
#endif /*ENABLE_HISTORY*/
}
void getPowerHistoryDay(AsyncWebServerRequest *request, JsonObject obj){ if(HistoryStorageType::POWER_DAY == type) {
//getGeneric(request, obj.createNestedObject(F("generic"))); float yldDay = 0;
#if defined(ENABLE_HISTORY) for (uint8_t i = 0; i < mSys->getNumInverters(); i++) {
obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::POWER_DAY); Inverter<> *iv = mSys->getInverterByPos(i);
uint16_t max = 0; if (iv == NULL)
for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { continue;
uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::POWER_DAY, fld); record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
obj[F("value")][fld] = value; yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec);
if (value > max) }
max = value; obj[F("yld")] = ah::round3(yldDay / 1000.0);
} }
obj[F("max")] = max;
obj[F("lastValueTs")] = mApp->getHistoryLastValueTs((uint8_t)HistoryStorageType::POWER_DAY); obj[F("lastValueTs")] = mApp->getHistoryLastValueTs(static_cast<uint8_t>(type));
#endif /*ENABLE_HISTORY*/
} }
#endif /*ENABLE_HISTORY*/
#if defined(ENABLE_HISTORY_YIELD_PER_DAY)
void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) { void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) {
//getGeneric(request, obj.createNestedObject(F("generic")));
#if defined(ENABLE_HISTORY) && defined(ENABLE_HISTORY_YIELD_PER_DAY)
obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::YIELD); obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::YIELD);
uint16_t max = 0; uint16_t max = 0;
for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) {
@ -973,8 +972,8 @@ class RestApi {
max = value; max = value;
} }
obj[F("max")] = max; obj[F("max")] = max;
#endif /*ENABLE_HISTORY*/
} }
#endif /*ENABLE_HISTORY_YIELD_PER_DAY*/
bool setCtrl(JsonObject jsonIn, JsonObject jsonOut, const char *clientIP) { bool setCtrl(JsonObject jsonIn, JsonObject jsonOut, const char *clientIP) {
if(jsonIn.containsKey(F("auth"))) { if(jsonIn.containsKey(F("auth"))) {
@ -1016,8 +1015,6 @@ class RestApi {
iv->powerLimit[1] = AbsolutNonPersistent; iv->powerLimit[1] = AbsolutNonPersistent;
accepted = iv->setDevControlRequest(ActivePowerContr); accepted = iv->setDevControlRequest(ActivePowerContr);
if(accepted)
mApp->triggerTickSend(iv->id);
} else if(F("dev") == jsonIn[F("cmd")]) { } else if(F("dev") == jsonIn[F("cmd")]) {
DPRINTLN(DBG_INFO, F("dev cmd")); DPRINTLN(DBG_INFO, F("dev cmd"));
iv->setDevCommand(jsonIn[F("val")].as<int>()); iv->setDevCommand(jsonIn[F("val")].as<int>());

2
src/web/html/about.html

@ -14,7 +14,7 @@
<div class="p-2">Used Libraries</div> <div class="p-2">Used Libraries</div>
</div> </div>
<div class="row"><a href="https://github.com/bertmelis/espMqttClient" target="_blank">bertmelis/espMqttClient</a></div> <div class="row"><a href="https://github.com/bertmelis/espMqttClient" target="_blank">bertmelis/espMqttClient</a></div>
<div class="row"><a href="https://github.com/yubox-node-org/ESPAsyncWebServer" target="_blank">yubox-node-org/ESPAsyncWebServer</a></div> <div class="row"><a href="https://github.com/esphome/ESPAsyncWebServer" target="_blank">esphome/ESPAsyncWebServer</a></div>
<div class="row"><a href="https://github.com/bblanchon/ArduinoJson" target="_blank">bblanchon/ArduinoJson</a></div> <div class="row"><a href="https://github.com/bblanchon/ArduinoJson" target="_blank">bblanchon/ArduinoJson</a></div>
<div class="row"><a href="https://github.com/nrf24/RF24" target="_blank">nrf24/RF24</a></div> <div class="row"><a href="https://github.com/nrf24/RF24" target="_blank">nrf24/RF24</a></div>
<div class="row"><a href="https://github.com/paulstoffregen/Time" target="_blank">paulstoffregen/Time</a></div> <div class="row"><a href="https://github.com/paulstoffregen/Time" target="_blank">paulstoffregen/Time</a></div>

37
src/web/html/history.html

@ -45,12 +45,11 @@
s.x_mul = 60 s.x_mul = 60
s.ts_start = obj.lastValueTs - (obj.refresh * obj.value.length) s.ts_start = obj.lastValueTs - (obj.refresh * obj.value.length)
s.ts_dur = obj.lastValueTs - s.ts_start s.ts_dur = obj.lastValueTs - s.ts_start
s.ts_pad = (s.ts_dur < 1800) ? s.ts_start % 300 : s.ts_start % 1800 s.ts_pad = (s.ts_dur < 1800) ? 0 : s.ts_start % 1800
s.ts_dur -= s.ts_pad s.ts_dur += s.ts_pad
while(s.x_mul * 10 <= s.ts_dur) while(s.x_mul * 10 <= s.ts_dur)
s.x_mul += (s.x_mul == 60) ? 240 : ((s.x_mul < 1800) ? 300 : 1800) s.x_mul += (s.x_mul == 60) ? 240 : ((s.x_mul < 1800) ? 300 : 1800)
s.x_step = Math.ceil(s.ts_dur / s.x_mul) s.x_step = Math.ceil(s.ts_dur / s.x_mul)
s.x_max = s.x_mul * s.x_step
s.y_mul = 10 s.y_mul = 10
while(s.y_mul * 10 <= obj.max) while(s.y_mul * 10 <= obj.max)
@ -79,7 +78,7 @@
...gridText(n*2, scale), ...gridText(n*2, scale),
mlNs("g", {transform: "translate(30, 5)"}, [ mlNs("g", {transform: "translate(30, 5)"}, [
...grid(n*2, scale), ...grid(n*2, scale),
...poly(obj, scale) ...poly(n*2, obj, scale)
]) ])
]) ])
} }
@ -90,10 +89,10 @@
for(let i = 0; i <= scale.y_max; i += scale.y_mul) { for(let i = 0; i <= scale.y_max; i += scale.y_mul) {
g.push(mlNs("text", {x: 0, y: height-(i*div)+9}, String(i))) g.push(mlNs("text", {x: 0, y: height-(i*div)+9}, String(i)))
} }
div = x2 / scale.x_max div = x2 / scale.ts_dur
for(let i = 0; i < scale.x_max; i++) { for(let i = 0; i < scale.ts_dur; i++) {
if((i + scale.ts_pad) % scale.x_mul == 0) { if(i % scale.x_mul == 0) {
let d = new Date((scale.ts_start + i) * 1000) let d = new Date((scale.ts_start - scale.ts_pad + i) * 1000)
g.push(mlNs("text", {x: (i*div)+17, y: height+20}, ("0"+d.getHours()).slice(-2) + ":" + ("0"+d.getMinutes()).slice(-2))) g.push(mlNs("text", {x: (i*div)+17, y: height+20}, ("0"+d.getHours()).slice(-2) + ":" + ("0"+d.getMinutes()).slice(-2)))
} }
} }
@ -106,39 +105,45 @@
for(let i = 0; i <= scale.y_max; i += scale.y_mul) { for(let i = 0; i <= scale.y_max; i += scale.y_mul) {
g.push(mlNs("line", {x1: 0, x2: x2, y1: height-i*div, y2: height-i*div, "stroke-width": 1, "stroke-dasharray": "1,3", stroke: "#aaa"})) g.push(mlNs("line", {x1: 0, x2: x2, y1: height-i*div, y2: height-i*div, "stroke-width": 1, "stroke-dasharray": "1,3", stroke: "#aaa"}))
} }
div = x2 / scale.x_max div = x2 / scale.ts_dur
for(let i = 0; i <= scale.x_max; i++) { for(let i = 0; i <= scale.ts_dur; i++) {
if((i + scale.ts_pad) % scale.x_mul == 0) { if(i % scale.x_mul == 0) {
g.push(mlNs("line", {x1: (i*div), x2: (i*div), y1: 0, y2: height, "stroke-width": 1, "stroke-dasharray": "1,3", stroke: "#aaa"})) g.push(mlNs("line", {x1: (i*div), x2: (i*div), y1: 0, y2: height, "stroke-width": 1, "stroke-dasharray": "1,3", stroke: "#aaa"}))
} }
} }
return g return g
} }
function poly(obj, scale) { function poly(x2, obj, scale) {
let pts = "" let pts = ""
let i = 0, first = -1, last = -1, lastVal = 0 let i = 0, first = -1, last = -1, lastVal = 0
let div = scale.y_max / height let div = scale.y_max / height
let xOff = x2 / scale.ts_dur * scale.ts_pad
if(div == 0) if(div == 0)
div = 1 div = 1
for (val of obj.value) { for (val of obj.value) {
if(val > 0) { if(val > 0) {
lastVal = val lastVal = val
pts += " " + String(i) + "," + String(height - val / div) pts += " " + String(i + xOff) + "," + String(height - val / div)
if(first < 0) if(first < 0)
first = i first = i + xOff
last = i last = i + xOff
} }
i += 2 i += 2
} }
let pts2 = pts + " " + String(last) + "," + String(height) let pts2 = pts + " " + String(last) + "," + String(height)
pts2 += " " + String(first) + "," + String(height) pts2 += " " + String(first) + "," + String(height)
return [ elm = [
mlNs("polyline", {stroke: "url(#gLine)", fill: "none", points: pts}), mlNs("polyline", {stroke: "url(#gLine)", fill: "none", points: pts}),
mlNs("polyline", {stroke: "none", fill: "url(#gFill)", points: pts2}), mlNs("polyline", {stroke: "none", fill: "url(#gFill)", points: pts2}),
mlNs("text", {x: i*.8, y: 10}, "{#MAXIMUM}: " + String(obj.max) + "W"), mlNs("text", {x: i*.8, y: 10}, "{#MAXIMUM}: " + String(obj.max) + "W"),
mlNs("text", {x: i*.8, y: 25}, "{#LAST_VALUE}: " + String(lastVal) + "W") mlNs("text", {x: i*.8, y: 25}, "{#LAST_VALUE}: " + String(lastVal) + "W")
] ]
if(undefined !== obj.yld)
elm.push(mlNs("text", {x: i*.8, y: 40}, "{#YIELD_DAY}: " + String(obj.yld) + "kWh"))
return elm;
} }

10
src/web/html/setup.html

@ -130,7 +130,11 @@
<div class="col-4"><input type="checkbox" name="invRstMid"/></div> <div class="col-4"><input type="checkbox" name="invRstMid"/></div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<div class="col-8 mb-2">{#INV_PAUSE_SUNSET}</div> <div class="col-8 mb-2">{#INV_RESET_SUNRISE}</div>
<div class="col-4"><input type="checkbox" name="invRstComStart"/></div>
</div>
<div class="row mb-3">
<div class="col-8 mb-2">{#INV_RESET_SUNSET}</div>
<div class="col-4"><input type="checkbox" name="invRstComStop"/></div> <div class="col-4"><input type="checkbox" name="invRstComStop"/></div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
@ -138,7 +142,7 @@
<div class="col-4"><input type="checkbox" name="invRstNotAvail"/></div> <div class="col-4"><input type="checkbox" name="invRstNotAvail"/></div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<div class="col-8">{#INV_RESET_MAX_MIDNIGHT}</div> <div class="col-8">{#INV_RESET_MAX_VALUES}</div>
<div class="col-4"><input type="checkbox" name="invRstMaxMid"/></div> <div class="col-4"><input type="checkbox" name="invRstMaxMid"/></div>
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
@ -674,7 +678,7 @@
function ivGlob(obj) { function ivGlob(obj) {
for(var i of [["invInterval", "interval"]]) for(var i of [["invInterval", "interval"]])
document.getElementsByName(i[0])[0].value = obj[i[1]]; document.getElementsByName(i[0])[0].value = obj[i[1]];
for(var i of ["Mid", "ComStop", "NotAvail", "MaxMid"]) for(var i of ["Mid", "ComStop", "ComStart", "NotAvail", "MaxMid"])
document.getElementsByName("invRst"+i)[0].checked = obj["rst" + i]; document.getElementsByName("invRst"+i)[0].checked = obj["rst" + i];
document.getElementsByName("strtWthtTm")[0].checked = obj["strtWthtTm"]; document.getElementsByName("strtWthtTm")[0].checked = obj["strtWthtTm"];
document.getElementsByName("rdGrid")[0].checked = obj["rdGrid"]; document.getElementsByName("rdGrid")[0].checked = obj["rdGrid"];

3
src/web/html/style.css

@ -687,7 +687,7 @@ div.hr {
border-radius: 3px; border-radius: 3px;
display: inline-block; display: inline-block;
position: absolute; position: absolute;
transform: translate(-50%,-100%); transform: translate(-50%,-50%);
margin:0 auto; margin:0 auto;
color: var(--fg2); color: var(--fg2);
min-width: 100px; min-width: 100px;
@ -758,6 +758,7 @@ div.hr {
font-family: inherit; font-family: inherit;
cursor: pointer; cursor: pointer;
padding: 0; padding: 0;
color: var(--fg);
} }
button.close { button.close {

32
src/web/html/visualization.html

@ -21,6 +21,7 @@
var mNum = 0; var mNum = 0;
var total = Array(6).fill(0); var total = Array(6).fill(0);
var tPwrAck; var tPwrAck;
var totalsRendered = false
function getErrStr(code) { function getErrStr(code) {
if("ERR_AUTH") return "{#ERR_AUTH}" if("ERR_AUTH") return "{#ERR_AUTH}"
@ -54,11 +55,11 @@
]); ]);
} }
function numMid(val, unit, des, opt={class: "fs-6"}) { function numMid(val, unit, des, opt={class: "row"}) {
return ml("div", {class: "col-6 col-sm-4 col-md-3 mb-2"}, [ return ml("div", {class: "col-6 col-sm-4 col-md-3 mb-2"}, [
ml("div", {class: "row"}, ml("div", opt,
ml("div", {class: "col"}, [ ml("div", {class: "col"}, [
ml("span", opt, String(Math.round(val * 100) / 100)), ml("span", {class: "fs-6"}, String(Math.round(val * 100) / 100)),
ml("span", {class: "fs-8 mx-1"}, unit) ml("span", {class: "fs-8 mx-1"}, unit)
]) ])
), ),
@ -74,6 +75,7 @@
for(var i = 0; i < 6; i++) { for(var i = 0; i < 6; i++) {
total[i] = Math.round(total[i] * 100) / 100; total[i] = Math.round(total[i] * 100) / 100;
} }
totalsRendered = true
return ml("div", {class: "row mt-3 mb-5"}, return ml("div", {class: "row mt-3 mb-5"},
ml("div", {class: "col"}, [ ml("div", {class: "col"}, [
@ -104,7 +106,6 @@
total[4] += obj.ch[0][8]; // P_DC total[4] += obj.ch[0][8]; // P_DC
total[5] += obj.ch[0][10]; // Q_AC total[5] += obj.ch[0][10]; // Q_AC
} }
total[3] += obj.ch[0][11]; // MAX P_AC
total[1] += obj.ch[0][7]; // YieldDay total[1] += obj.ch[0][7]; // YieldDay
total[2] += obj.ch[0][6]; // YieldTotal total[2] += obj.ch[0][6]; // YieldTotal
@ -119,8 +120,8 @@
pwrLimit += ", " + (obj.max_pwr * obj.power_limit_read / 100).toFixed(1) + "&nbsp;W"; pwrLimit += ", " + (obj.max_pwr * obj.power_limit_read / 100).toFixed(1) + "&nbsp;W";
} }
var maxAcPwr = toIsoDateStr(new Date(obj.ts_max_ac_pwr * 1000)); var maxAcPwrDate = toIsoDateStr(new Date(obj.ts_max_ac_pwr * 1000))
var maxTemp = toIsoDateStr(new Date(obj.ts_max_temp * 1000)); var maxTempDate = toIsoDateStr(new Date(obj.ts_max_temp * 1000))
return ml("div", {class: "row mt-2"}, return ml("div", {class: "row mt-2"},
ml("div", {class: "col"}, [ ml("div", {class: "col"}, [
ml("div", {class: "p-2 " + clh}, ml("div", {class: "p-2 " + clh},
@ -135,8 +136,7 @@
ml("div", {class: "col a-c"}, ml("span", { class: "pointer", onclick: function() { ml("div", {class: "col a-c"}, ml("span", { class: "pointer", onclick: function() {
getAjax("/api/inverter/alarm/" + obj.id, parseIvAlarm); getAjax("/api/inverter/alarm/" + obj.id, parseIvAlarm);
}}, ("{#ALARMS}: " + obj.alarm_cnt))), }}, ("{#ALARMS}: " + obj.alarm_cnt))),
ml("div", {class: "col a-r mx-2 mx-md-1 tooltip", data: maxTemp}, String(obj.ch[0][5].toFixed(1)) + t.innerText + "<br>" + ml("div", {class: "col a-r mx-2 mx-md-1 tooltip", data: (obj.ch[0][12] + t.innerText + "\n" + maxTempDate)}, String(obj.ch[0][5].toFixed(1)) + t.innerText)
"Max:" + String(obj.ch[0][12].toFixed(1)) + t.innerText)
]) ])
), ),
ml("div", {class: "p-2 " + clbg}, [ ml("div", {class: "p-2 " + clbg}, [
@ -147,7 +147,7 @@
]), ]),
ml("div", {class: "hr"}), ml("div", {class: "hr"}),
ml("div", {class: "row mt-2"},[ ml("div", {class: "row mt-2"},[
numMid(obj.ch[0][11], "W", "{#MAX_AC_POWER}", {class: "fs-6 tooltip", data: maxAcPwr}), numMid(obj.ch[0][11], "W", "{#MAX_AC_POWER}", {class: "row tooltip", data: maxAcPwrDate}),
numMid(obj.ch[0][8], "W", "{#DC_POWER}"), numMid(obj.ch[0][8], "W", "{#DC_POWER}"),
numMid(obj.ch[0][0], "V", "{#AC_VOLTAGE}"), numMid(obj.ch[0][0], "V", "{#AC_VOLTAGE}"),
numMid(obj.ch[0][1], "A", "{#AC_CURRENT}"), numMid(obj.ch[0][1], "A", "{#AC_CURRENT}"),
@ -242,20 +242,18 @@
]) ])
); );
var last = true;
for(var i = obj.id + 1; i < ivEn.length; i++) { for(var i = obj.id + 1; i < ivEn.length; i++) {
if((i != ivEn.length) && ivEn[i]) { if((i != ivEn.length) && ivEn[i]) {
last = false;
getAjax("/api/inverter/id/" + i, parseIv); getAjax("/api/inverter/id/" + i, parseIv);
break; return
} }
} }
if(last) {
if(mNum > 1) if(mNum > 1) {
if(!totalsRendered)
mIvHtml.unshift(totals()); mIvHtml.unshift(totals());
document.getElementById("live").replaceChildren(...mIvHtml);
} }
document.getElementById("live").replaceChildren(...mIvHtml);
} }
function parseIvAlarm(obj) { function parseIvAlarm(obj) {
@ -516,7 +514,9 @@
ivEn = Object.values(Object.assign({}, obj["iv"])); ivEn = Object.values(Object.assign({}, obj["iv"]));
mIvHtml = []; mIvHtml = [];
mNum = 0; mNum = 0;
totalsRendered = false
total.fill(0); total.fill(0);
total[3] = obj.max_total_pwr
for(var i = 0; i < obj.iv.length; i++) { for(var i = 0; i < obj.iv.length; i++) {
if(obj.iv[i]) { if(obj.iv[i]) {
getAjax("/api/inverter/id/" + i, parseIv); getAjax("/api/inverter/id/" + i, parseIv);

13
src/web/html/wizard.html

@ -232,7 +232,7 @@
ml("div", {class: "row my-4"}, ml("div", {class: "col a-r"}, ml("input", {type: "button", class:"btn hide", id: "btn", value: "{#BTN_FINISH}", onclick: () => {redirect()}}, null))), ml("div", {class: "row my-4"}, ml("div", {class: "col a-r"}, ml("input", {type: "button", class:"btn hide", id: "btn", value: "{#BTN_FINISH}", onclick: () => {redirect()}}, null))),
ml("div", {class: "row mt-5"}, ml("div", {class: "col a-c"}, ml("a", {onclick: () => {redirect()}}, "{#STOP_WIZARD}"))) ml("div", {class: "row mt-5"}, ml("div", {class: "col a-c"}, ml("a", {onclick: () => {redirect()}}, "{#STOP_WIZARD}")))
) )
v = setInterval(() => {getAjax('/api/setup/getip', printIp)}, 500); v = setInterval(() => {getAjax('/api/setup/getip', printIp)}, 1000);
} }
function redirect() { function redirect() {
@ -240,7 +240,7 @@
} }
function printIp(obj) { function printIp(obj) {
if("0.0.0.0" != obj["ip"]) { if("0.0.0.0" != obj.ip) {
clearInterval(v) clearInterval(v)
setHide("btn", false) setHide("btn", false)
document.getElementById("state").innerHTML = "{#NETWORK_SUCCESS}" + obj.ip document.getElementById("state").innerHTML = "{#NETWORK_SUCCESS}" + obj.ip
@ -272,12 +272,12 @@
getAjax("/api/setup", ((o) => c.append(step1(o.eth)))); getAjax("/api/setup", ((o) => c.append(step1(o.eth))));
/*ELSE*/ /*ELSE*/
function nets(obj) { function nets(obj) {
clearInterval(v)
v = setInterval(() => {getAjax('/api/setup/networks', nets)}, 4000)
if(!obj.success) if(!obj.success)
return; return;
clearInterval(v)
v = setInterval(() => {getAjax('/api/setup/networks', nets)}, 5000)
var e = document.getElementById("net"); var e = document.getElementById("net");
if(obj.networks.length > 0) { if(obj.networks.length > 0) {
var a = [] var a = []
@ -289,7 +289,8 @@
e.replaceChildren(...a) e.replaceChildren(...a)
} }
redirIp = obj.ip + "/index" if("0.0.0.0" != obj.ip)
redirIp = "http://" + obj.ip + "/index"
} }
c.append(step1()) c.append(step1())

18
src/web/lang.json

@ -324,19 +324,24 @@
"de": "Werte und Gesamtertrag um Mitternacht zur&uuml;cksetzen" "de": "Werte und Gesamtertrag um Mitternacht zur&uuml;cksetzen"
}, },
{ {
"token": "INV_PAUSE_SUNSET", "token": "INV_RESET_SUNSET",
"en": "Reset values at sunset", "en": "Reset values at sunset",
"de": "Werte bei Sonnenuntergang zur&uuml;cksetzen" "de": "Werte bei Sonnenuntergang zur&uuml;cksetzen"
}, },
{
"token": "INV_RESET_SUNRISE",
"en": "Reset values at sunrise",
"de": "Werte bei Sonnenaufgang zur&uuml;cksetzen"
},
{ {
"token": "INV_RESET_NOT_AVAIL", "token": "INV_RESET_NOT_AVAIL",
"en": "Reset values when inverter status is 'not available'", "en": "Reset values when inverter status is 'not available'",
"de": "Werte zur&uuml;cksetzen, sobald der Wechselrichter nicht erreichbar ist" "de": "Werte zur&uuml;cksetzen, sobald der Wechselrichter nicht erreichbar ist"
}, },
{ {
"token": "INV_RESET_MAX_MIDNIGHT", "token": "INV_RESET_MAX_VALUES",
"en": "Reset 'max' values at midnight", "en": "Include reset 'max' values",
"de": "Maximalwerte mitternachts zur&uuml;cksetzen" "de": "Maximalwerte auch zur&uuml;cksetzen"
}, },
{ {
"token": "INV_START_WITHOUT_TIME", "token": "INV_START_WITHOUT_TIME",
@ -1587,6 +1592,11 @@
"token": "LAST_VALUE", "token": "LAST_VALUE",
"en": "Last value", "en": "Last value",
"de": "Letzter Wert" "de": "Letzter Wert"
},
{
"token": "YIELD_DAY",
"en": "Yield day",
"de": "Tagesertrag"
} }
] ]
} }

5
src/web/web.h

@ -498,12 +498,13 @@ class Web {
if (request->arg("invInterval") != "") if (request->arg("invInterval") != "")
mConfig->inst.sendInterval = request->arg("invInterval").toInt(); mConfig->inst.sendInterval = request->arg("invInterval").toInt();
mConfig->inst.rstYieldMidNight = (request->arg("invRstMid") == "on"); mConfig->inst.rstValsAtMidNight = (request->arg("invRstMid") == "on");
mConfig->inst.rstValsCommStop = (request->arg("invRstComStop") == "on"); mConfig->inst.rstValsCommStop = (request->arg("invRstComStop") == "on");
mConfig->inst.rstValsCommStart = (request->arg("invRstComStart") == "on");
mConfig->inst.rstValsNotAvail = (request->arg("invRstNotAvail") == "on"); mConfig->inst.rstValsNotAvail = (request->arg("invRstNotAvail") == "on");
mConfig->inst.startWithoutTime = (request->arg("strtWthtTm") == "on"); mConfig->inst.startWithoutTime = (request->arg("strtWthtTm") == "on");
mConfig->inst.readGrid = (request->arg("rdGrid") == "on"); mConfig->inst.readGrid = (request->arg("rdGrid") == "on");
mConfig->inst.rstMaxValsMidNight = (request->arg("invRstMaxMid") == "on"); mConfig->inst.rstIncludeMaxVals = (request->arg("invRstMaxMid") == "on");
// pinout // pinout

Loading…
Cancel
Save