mirror of https://github.com/lumapu/ahoy.git
committed by
GitHub
94 changed files with 6009 additions and 2938 deletions
@ -0,0 +1 @@ |
|||
patches/GxEPD2_HAL.patch eol=lf |
@ -0,0 +1,56 @@ |
|||
# 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 passwords you need to add them again to the `json` file. Open the `json` file with a text editor and search for all the `"pwd":""` sections. 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 |
|||
... |
|||
``` |
|||
|
|||
## 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 or vice versa. |
|||
|
|||
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` |
|||
|
|||
The upload should be finished within one minute. |
|||
|
|||
## Testing |
|||
|
|||
Reboot your ESP an check if all your settings are present. |
|||
|
|||
## Get updated with 'Mainline' |
|||
|
|||
From time to time a new version of AhoyDTU will be published. To get the changes into your already prepared factory binary generation environment you have to do only a few steps: |
|||
|
|||
1. pull new changes from remote: `git pull` |
|||
2. check if the `data` folder is still there and contains the `settings.json` |
|||
3. build and upload |
|||
4. enjoy |
@ -1,26 +1,26 @@ |
|||
diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp
|
|||
index 12be5f8..cffeed7 100644
|
|||
index 6e88da9..09359c3 100644
|
|||
--- a/src/AsyncWebSocket.cpp
|
|||
+++ b/src/AsyncWebSocket.cpp
|
|||
@@ -737,7 +737,7 @@ void AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len)
|
|||
IPAddress AsyncWebSocketClient::remoteIP() const |
|||
{ |
|||
if (!_client) |
|||
- return IPAddress(0U);
|
|||
+ return IPAddress();
|
|||
@@ -827,7 +827,7 @@ void AsyncWebSocketClient::binary(AsyncWebSocketMessageBuffer * buffer)
|
|||
|
|||
IPAddress AsyncWebSocketClient::remoteIP() { |
|||
if(!_client) { |
|||
- return IPAddress((uint32_t)0);
|
|||
+ return IPAddress();
|
|||
} |
|||
return _client->remoteIP(); |
|||
} |
|||
diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp
|
|||
index 22a549f..e0b36b3 100644
|
|||
index a22e991..babef18 100644
|
|||
--- a/src/WebResponses.cpp
|
|||
+++ b/src/WebResponses.cpp
|
|||
@@ -318,7 +318,7 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
|
|||
@@ -317,7 +317,7 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
|
|||
free(buf); |
|||
return 0; |
|||
} |
|||
- outLen = sprintf_P((char*)buf+headLen, PSTR("%x"), readLen) + headLen;
|
|||
+ outLen = sprintf_P((char*)buf+headLen, PSTR("%04x"), readLen) + headLen;
|
|||
- outLen = sprintf((char*)buf+headLen, "%x", readLen) + headLen;
|
|||
+ outLen = sprintf((char*)buf+headLen, "%04x", readLen) + headLen;
|
|||
while(outLen < headLen + 4) buf[outLen++] = ' '; |
|||
buf[outLen++] = '\r'; |
|||
buf[outLen++] = '\n'; |
|||
|
@ -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) |
@ -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; |
|||
}; |
|||
|
@ -0,0 +1,79 @@ |
|||
import os |
|||
import subprocess |
|||
import shutil |
|||
from SCons.Script import DefaultEnvironment |
|||
Import("env") |
|||
|
|||
|
|||
def build_littlefs(): |
|||
if os.path.isfile('data/settings.json') == False: |
|||
return # nothing to do |
|||
|
|||
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(): |
|||
if os.path.isfile('data/settings.json') == False: |
|||
return # nothing to do |
|||
|
|||
BOOTLOADER_OFFSET = 0x0000 |
|||
PARTITIONS_OFFSET = 0x8000 |
|||
FIRMWARE_OFFSET = 0x10000 |
|||
|
|||
if env['PIOENV'][:13] == "esp32-wroom32": |
|||
BOOTLOADER_OFFSET = 0x1000 |
|||
|
|||
flash_size = int(env.BoardConfig().get("upload.maximum_size", "1310720")) # 0x140000 |
|||
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 == 0x330000: |
|||
littlefs_offset = 0x670000 |
|||
elif flash_size == 0x640000: |
|||
littlefs_offset = 0xc90000 |
|||
|
|||
# save current wd |
|||
start = os.getcwd() |
|||
os.chdir('.pio/build/' + env['PIOENV'] + '/') |
|||
|
|||
with open("bootloader.bin", "rb") as bootloader_file: |
|||
bootloader_data = bootloader_file.read() |
|||
|
|||
with open("partitions.bin", "rb") as partitions_file: |
|||
partitions_data = partitions_file.read() |
|||
|
|||
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: |
|||
merged_file.write(b'\xFF' * BOOTLOADER_OFFSET) |
|||
merged_file.write(bootloader_data) |
|||
|
|||
merged_file.write(b'\xFF' * (PARTITIONS_OFFSET - (BOOTLOADER_OFFSET + len(bootloader_data)))) |
|||
merged_file.write(partitions_data) |
|||
|
|||
merged_file.write(b'\xFF' * (FIRMWARE_OFFSET - (PARTITIONS_OFFSET + len(partitions_data)))) |
|||
merged_file.write(firmware_data) |
|||
|
|||
merged_file.write(b'\xFF' * (littlefs_offset - (FIRMWARE_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) |
@ -0,0 +1,40 @@ |
|||
import re |
|||
import os |
|||
import queue |
|||
|
|||
def error(msg): |
|||
print("ERROR: " + msg) |
|||
exit() |
|||
|
|||
def check(inp, lst, pattern): |
|||
q = queue.LifoQueue() |
|||
out = [] |
|||
keep = True |
|||
for line in inp: |
|||
x = re.findall(pattern, line) |
|||
if len(x) > 0: |
|||
if line.find("ENDIF_") != -1: |
|||
if not q.empty(): |
|||
e = q.get() |
|||
if e[0] == x[0]: |
|||
keep = e[1] |
|||
elif line.find("IF_") != -1: |
|||
q.put((x[0], keep)) |
|||
if keep is True: |
|||
keep = x[0] in lst |
|||
elif line.find("E") != -1: |
|||
if q.empty(): |
|||
error("(ELSE) missing open statement!") |
|||
e = q.get() |
|||
q.put(e) |
|||
if e[1] is True: |
|||
keep = not keep |
|||
else: |
|||
if keep is True: |
|||
out.append(line) |
|||
return out |
|||
|
|||
def conv(inp, lst): |
|||
#print(lst) |
|||
out = check(inp, lst, r'\/\*(?:IF_|ELS|ENDIF_)([A-Z0-9\-_]+)?\*\/') |
|||
return check(out, lst, r'\<\!\-\-(?:IF_|ELS|ENDIF_)([A-Z0-9\-_]+)?\-\-\>') |
@ -1,41 +1,54 @@ |
|||
Changelog v0.8.83 |
|||
Changelog v0.8.140 |
|||
|
|||
* added German translations for all variants |
|||
* added reading grid profile |
|||
* added decimal place for active power control (APC aka power limit) |
|||
* added information about working IRQ for NRF24 and CMT2300A to `/system` |
|||
* added loss rate to `/visualization` in the statistics window and MqTT |
|||
* added optional output to display whether it's night time or not. Can be reused as output to control battery system or mapped to a LED |
|||
* added timestamp for `max ac power` as tooltip |
|||
* added wizard for initial WiFi connection |
|||
* added history graph (still under development) |
|||
* added simulator (must be activated before compile, standard: off) |
|||
* added minimal version (without: MqTT, Display, History), WebUI is not changed! (not compiled automatically) |
|||
* added info about installed binary to `/update` |
|||
* added protection to prevent update to wrong firmware (environment check) |
|||
* added optional custom link to the menu |
|||
* added support for other regions (USA, Indonesia) |
|||
* added warning for WiFi channel 12-14 (ESP8266 only) |
|||
* added `max_power` to MqTT total values |
|||
* added API-Token authentification for external scripts |
|||
* improved MqTT by marking sent data and improved `last_success` resends |
|||
* improved communication for HM and MI inverters |
|||
* improved reading live data from inverter |
|||
* improved sending active power control command faster |
|||
* improved `/settings`: pinout has an own subgroup |
|||
* improved export by saving settings before they are exported (to have everything in JSON) |
|||
* improved code quality (cppcheck) |
|||
* seperated sunrise and sunset offset to two fields |
|||
* fix MqTT night communication |
|||
* fix missing favicon to html header |
|||
* fix build on Windows of `opendtufusion` environments (git: trailing whitespaces) |
|||
* fix generation of DTU-ID |
|||
* fix: protect commands from popup in `/live` if password is set |
|||
* fix: prevent sending commands to inverter which isn't active |
|||
* combined firmware and hardware version to JSON topics (MqTT) |
|||
* updated Prometheus with latest changes |
|||
* upgraded most libraries to newer versions |
|||
* beautified typography, added spaces between value and unit for `/visualization` |
|||
* removed add to total (MqTT) inverter setting |
|||
* added HMS-400-1T support (serial number 1125...) |
|||
* added further ESP8266 versions (-all, -minimal) because of small ressources on ESP8266 |
|||
* added some Gridprofiles |
|||
* added support for characters in serial number of inverter (A-F) |
|||
* added default coordinates on fresh install, needed for history graph on display and WebUI |
|||
* added option to reset values on communication start (sunrise) |
|||
* added max inverter temperature to WebUI |
|||
* added yield day to history graph |
|||
* added script and [instructions](../manual/factory_firmware.md) how to generate factory firmware which includes predefined settings |
|||
* added button for downloading coredump (ESP32 variants only) to `/system`. Once a crash happens the reason can be checked afterwards (even after a reboot) |
|||
* added support of HERF inverters, serial number is converted in Javascript |
|||
* added device name to HTML title |
|||
* added feature to restart Ahoy using MqTT |
|||
* added feature to publish MqTT messages as JSON as well (new setting) |
|||
* add timestamp to JSON output |
|||
* improved communication to inverter |
|||
* improved translation to German |
|||
* improved HTML pages, reduced in size by only including relevant contents depending by chip type |
|||
* improved history graph in WebUI |
|||
* improved network routines |
|||
* improved Wizard |
|||
* improved WebUI by disabling upload and import buttons when no file is selected |
|||
* improved queue, only add new object once they not exist in queue |
|||
* improved MqTT `OnMessage` (threadsafe) |
|||
* improved read of alarms, prevent duplicates, update alarm time if there is an update |
|||
* improved alarms are now sorted in ascending direction |
|||
* improved by prevent add inverter multiple times |
|||
* improved sending active power controll commands |
|||
* improved refresh routine of ePaper, full refresh each 12h |
|||
* redesigned WebUI on `/system` |
|||
* changed MqTT retained flags |
|||
* change MqTT return value of power limit acknowledge from `boolean` to `float`. The value returned is the same as it was set to confirm reception (not the read back value) |
|||
* converted ePaper and Ethernet to hal-SPI |
|||
* combined Ethernet and WiFi variants - Ethernet is now always included, but needs to be enabled if needed |
|||
* changed: Ethernet variants (W5500) now support WiFi as fall back / configuration |
|||
* switch AsyncWebserver library |
|||
* fixed autodiscovery for homeassistant |
|||
* fix reset values functionality |
|||
* fix read back of active power control value, now it has one decimal place |
|||
* fix NTP issues |
|||
* fixed MqTT discovery field `ALARM_MES_ID` |
|||
* fix close button color of modal windows in dark mode |
|||
* fixed calculation of max AC power |
|||
* fixed reset values at midnight if WiFi isn't available |
|||
* fixed HMT-1800-4T number of inputs |
|||
* fix crash if invalid serial number was set -> inverter will be disabled automatically |
|||
* fixed ESP8266, ESP32 static IP |
|||
* fixed ethernet MAC address read back |
|||
* update several libraries to more recent versions |
|||
* removed `yield efficiency` because the inverter already calculates correct |
|||
|
|||
full version log: [Development Log](https://github.com/lumapu/ahoy/blob/development03/src/CHANGES.md) |
|||
|
@ -1,261 +0,0 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2023 Ahoy, https://www.mikrocontroller.net/topic/525778
|
|||
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#if defined(ETHERNET) |
|||
|
|||
#if defined(ESP32) && defined(F) |
|||
#undef F |
|||
#define F(sl) (sl) |
|||
#endif |
|||
#include "ahoyeth.h" |
|||
#include <ESPmDNS.h> |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
ahoyeth::ahoyeth() |
|||
{ |
|||
// WiFi.onEvent(ESP32_W5500_event);
|
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoyeth::setup(settings_t *config, uint32_t *utcTimestamp, OnNetworkCB onNetworkCB, OnTimeCB onTimeCB) { |
|||
mConfig = config; |
|||
mUtcTimestamp = utcTimestamp; |
|||
mOnNetworkCB = onNetworkCB; |
|||
mOnTimeCB = onTimeCB; |
|||
|
|||
Serial.flush(); |
|||
WiFi.onEvent([this](WiFiEvent_t event, arduino_event_info_t info) -> void { this->onEthernetEvent(event, info); }); |
|||
|
|||
Serial.flush(); |
|||
#if defined(CONFIG_IDF_TARGET_ESP32S3) |
|||
mEthSpi.begin(DEF_ETH_MISO_PIN, DEF_ETH_MOSI_PIN, DEF_ETH_SCK_PIN, DEF_ETH_CS_PIN, DEF_ETH_IRQ_PIN, DEF_ETH_RST_PIN); |
|||
#else |
|||
ETH.begin(DEF_ETH_MISO_PIN, DEF_ETH_MOSI_PIN, DEF_ETH_SCK_PIN, DEF_ETH_CS_PIN, DEF_ETH_IRQ_PIN, ETH_SPI_CLOCK_MHZ, ETH_SPI_HOST); |
|||
#endif |
|||
|
|||
if(mConfig->sys.ip.ip[0] != 0) { |
|||
IPAddress ip(mConfig->sys.ip.ip); |
|||
IPAddress mask(mConfig->sys.ip.mask); |
|||
IPAddress dns1(mConfig->sys.ip.dns1); |
|||
IPAddress dns2(mConfig->sys.ip.dns2); |
|||
IPAddress gateway(mConfig->sys.ip.gateway); |
|||
if(!ETH.config(ip, gateway, mask, dns1, dns2)) |
|||
DPRINTLN(DBG_ERROR, F("failed to set static IP!")); |
|||
} |
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
bool ahoyeth::updateNtpTime(void) { |
|||
DPRINTLN(DBG_DEBUG, F(__FUNCTION__)); Serial.flush(); |
|||
Serial.printf("ETH.linkUp()=%s\n", ETH.linkUp() ? "up" : "down"); |
|||
Serial.print("ETH.localIP()="); |
|||
Serial.println(ETH.localIP()); |
|||
Serial.printf("Go on? %s\n", (!ETH.localIP()) ? "No..." : "Yes..."); |
|||
if (!ETH.localIP()) |
|||
return false; |
|||
|
|||
DPRINTLN(DBG_DEBUG, F("updateNtpTime: checking udp \"connection\"...")); Serial.flush(); |
|||
if (!mUdp.connected()) { |
|||
DPRINTLN(DBG_DEBUG, F("updateNtpTime: About to (re)connect...")); Serial.flush(); |
|||
IPAddress timeServer; |
|||
if (!WiFi.hostByName(mConfig->ntp.addr, timeServer)) |
|||
return false; |
|||
|
|||
if (!mUdp.connect(timeServer, mConfig->ntp.port)) |
|||
return false; |
|||
|
|||
DPRINTLN(DBG_DEBUG, F("updateNtpTime: Connected...")); Serial.flush(); |
|||
mUdp.onPacket([this](AsyncUDPPacket packet) { |
|||
DPRINTLN(DBG_DEBUG, F("updateNtpTime: about to handle ntp packet...")); Serial.flush(); |
|||
this->handleNTPPacket(packet); |
|||
}); |
|||
} |
|||
|
|||
DPRINTLN(DBG_DEBUG, F("updateNtpTime: prepare packet...")); Serial.flush(); |
|||
|
|||
// set all bytes in the buffer to 0
|
|||
memset(mUdpPacketBuffer, 0, NTP_PACKET_SIZE); |
|||
// Initialize values needed to form NTP request
|
|||
// (see URL above for details on the packets)
|
|||
|
|||
mUdpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
|
|||
mUdpPacketBuffer[1] = 0; // Stratum, or type of clock
|
|||
mUdpPacketBuffer[2] = 6; // Polling Interval
|
|||
mUdpPacketBuffer[3] = 0xEC; // Peer Clock Precision
|
|||
|
|||
// 8 bytes of zero for Root Delay & Root Dispersion
|
|||
mUdpPacketBuffer[12] = 49; |
|||
mUdpPacketBuffer[13] = 0x4E; |
|||
mUdpPacketBuffer[14] = 49; |
|||
mUdpPacketBuffer[15] = 52; |
|||
|
|||
//Send unicast
|
|||
DPRINTLN(DBG_DEBUG, F("updateNtpTime: send packet...")); Serial.flush(); |
|||
mUdp.write(mUdpPacketBuffer, sizeof(mUdpPacketBuffer)); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoyeth::handleNTPPacket(AsyncUDPPacket packet) { |
|||
char buf[80]; |
|||
|
|||
memcpy(buf, packet.data(), sizeof(buf)); |
|||
|
|||
unsigned long highWord = word(buf[40], buf[41]); |
|||
unsigned long lowWord = word(buf[42], buf[43]); |
|||
|
|||
// combine the four bytes (two words) into a long integer
|
|||
// this is NTP time (seconds since Jan 1 1900):
|
|||
unsigned long secsSince1900 = highWord << 16 | lowWord; |
|||
|
|||
*mUtcTimestamp = secsSince1900 - 2208988800UL; // UTC time
|
|||
DPRINTLN(DBG_INFO, "[NTP]: " + ah::getDateTimeStr(*mUtcTimestamp) + " UTC"); |
|||
mOnTimeCB(true); |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoyeth::welcome(String ip, String mode) { |
|||
DBGPRINTLN(F("\n\n--------------------------------")); |
|||
DBGPRINTLN(F("Welcome to AHOY!")); |
|||
DBGPRINT(F("\npoint your browser to http://")); |
|||
DBGPRINT(ip); |
|||
DBGPRINTLN(mode); |
|||
DBGPRINTLN(F("to configure your device")); |
|||
DBGPRINTLN(F("--------------------------------\n")); |
|||
} |
|||
|
|||
void ahoyeth::onEthernetEvent(WiFiEvent_t event, arduino_event_info_t info) { |
|||
AWS_LOG(F("[ETH]: Got event...")); |
|||
switch (event) { |
|||
#if ( ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) && ( ARDUINO_ESP32_GIT_VER != 0x46d5afb1 ) ) |
|||
// For breaking core v2.0.0
|
|||
// Why so strange to define a breaking enum arduino_event_id_t in WiFiGeneric.h
|
|||
// compared to the old system_event_id_t, now in tools/sdk/esp32/include/esp_event/include/esp_event_legacy.h
|
|||
// You can preserve the old enum order and just adding new items to do no harm
|
|||
case ARDUINO_EVENT_ETH_START: |
|||
AWS_LOG(F("\nETH Started")); |
|||
//set eth hostname here
|
|||
if(String(mConfig->sys.deviceName) != "") |
|||
ETH.setHostname(mConfig->sys.deviceName); |
|||
else |
|||
ETH.setHostname("ESP32_W5500"); |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_ETH_CONNECTED: |
|||
AWS_LOG(F("ETH Connected")); |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_ETH_GOT_IP: |
|||
if (!ESP32_W5500_eth_connected) { |
|||
#if defined (CONFIG_IDF_TARGET_ESP32S3) |
|||
AWS_LOG3(F("ETH MAC: "), mEthSpi.macAddress(), F(", IPv4: "), ETH.localIP()); |
|||
#else |
|||
AWS_LOG3(F("ETH MAC: "), ETH.macAddress(), F(", IPv4: "), ETH.localIP()); |
|||
#endif |
|||
|
|||
if (ETH.fullDuplex()) { |
|||
AWS_LOG0(F("FULL_DUPLEX, ")); |
|||
} else { |
|||
AWS_LOG0(F("HALF_DUPLEX, ")); |
|||
} |
|||
|
|||
AWS_LOG1(ETH.linkSpeed(), F("Mbps")); |
|||
|
|||
ESP32_W5500_eth_connected = true; |
|||
mOnNetworkCB(true); |
|||
} |
|||
if (!MDNS.begin(mConfig->sys.deviceName)) { |
|||
DPRINTLN(DBG_ERROR, F("Error setting up MDNS responder!")); |
|||
} else { |
|||
DBGPRINT(F("[WiFi] mDNS established: ")); |
|||
DBGPRINT(mConfig->sys.deviceName); |
|||
DBGPRINTLN(F(".local")); |
|||
} |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_ETH_DISCONNECTED: |
|||
AWS_LOG("ETH Disconnected"); |
|||
ESP32_W5500_eth_connected = false; |
|||
mUdp.close(); |
|||
mOnNetworkCB(false); |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_ETH_STOP: |
|||
AWS_LOG("\nETH Stopped"); |
|||
ESP32_W5500_eth_connected = false; |
|||
mUdp.close(); |
|||
mOnNetworkCB(false); |
|||
break; |
|||
|
|||
#else |
|||
|
|||
// For old core v1.0.6-
|
|||
// Core v2.0.0 defines a stupid enum arduino_event_id_t, breaking any code for ESP32_W5500 written for previous core
|
|||
// Why so strange to define a breaking enum arduino_event_id_t in WiFiGeneric.h
|
|||
// compared to the old system_event_id_t, now in tools/sdk/esp32/include/esp_event/include/esp_event_legacy.h
|
|||
// You can preserve the old enum order and just adding new items to do no harm
|
|||
case SYSTEM_EVENT_ETH_START: |
|||
AWS_LOG(F("\nETH Started")); |
|||
//set eth hostname here
|
|||
if(String(mConfig->sys.deviceName) != "") |
|||
ETH.setHostname(mConfig->sys.deviceName); |
|||
else |
|||
ETH.setHostname("ESP32_W5500"); |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_ETH_CONNECTED: |
|||
AWS_LOG(F("ETH Connected")); |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_ETH_GOT_IP: |
|||
if (!ESP32_W5500_eth_connected) { |
|||
AWS_LOG3(F("ETH MAC: "), ETH.macAddress(), F(", IPv4: "), ETH.localIP()); |
|||
|
|||
if (ETH.fullDuplex()) { |
|||
AWS_LOG0(F("FULL_DUPLEX, ")); |
|||
} else { |
|||
AWS_LOG0(F("HALF_DUPLEX, ")); |
|||
} |
|||
|
|||
AWS_LOG1(ETH.linkSpeed(), F("Mbps")); |
|||
|
|||
ESP32_W5500_eth_connected = true; |
|||
mOnNetworkCB(true); |
|||
} |
|||
if (!MDNS.begin(mConfig->sys.deviceName)) { |
|||
DPRINTLN(DBG_ERROR, F("Error setting up MDNS responder!")); |
|||
} else { |
|||
DBGPRINT(F("[WiFi] mDNS established: ")); |
|||
DBGPRINT(mConfig->sys.deviceName); |
|||
DBGPRINTLN(F(".local")); |
|||
} |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_ETH_DISCONNECTED: |
|||
AWS_LOG("ETH Disconnected"); |
|||
ESP32_W5500_eth_connected = false; |
|||
mUdp.close(); |
|||
mOnNetworkCB(false); |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_ETH_STOP: |
|||
AWS_LOG("\nETH Stopped"); |
|||
ESP32_W5500_eth_connected = false; |
|||
mUdp.close(); |
|||
mOnNetworkCB(false); |
|||
break; |
|||
#endif |
|||
|
|||
default: |
|||
|
|||
break; |
|||
} |
|||
|
|||
} |
|||
|
|||
#endif /* defined(ETHERNET) */ |
@ -1,64 +0,0 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://github.com/lumpapu/ahoy
|
|||
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#if defined(ETHERNET) |
|||
#ifndef __AHOYETH_H__ |
|||
#define __AHOYETH_H__ |
|||
|
|||
#include <functional> |
|||
|
|||
#include <Arduino.h> |
|||
#include <AsyncUDP.h> |
|||
#include <DNSServer.h> |
|||
|
|||
#include "ethSpi.h" |
|||
#include "../utils/dbg.h" |
|||
#include "../config/config.h" |
|||
#include "../config/settings.h" |
|||
|
|||
#include "AsyncWebServer_ESP32_W5500.h" |
|||
|
|||
|
|||
class app; |
|||
|
|||
#define NTP_PACKET_SIZE 48 |
|||
|
|||
class ahoyeth { |
|||
public: /* types */ |
|||
typedef std::function<void(bool)> OnNetworkCB; |
|||
typedef std::function<void(bool)> OnTimeCB; |
|||
|
|||
public: |
|||
ahoyeth(); |
|||
|
|||
void setup(settings_t *config, uint32_t *utcTimestamp, OnNetworkCB onNetworkCB, OnTimeCB onTimeCB); |
|||
bool updateNtpTime(void); |
|||
|
|||
private: |
|||
void setupEthernet(); |
|||
|
|||
void handleNTPPacket(AsyncUDPPacket packet); |
|||
|
|||
void welcome(String ip, String mode); |
|||
|
|||
void onEthernetEvent(WiFiEvent_t event, arduino_event_info_t info); |
|||
|
|||
private: |
|||
#if defined(CONFIG_IDF_TARGET_ESP32S3) |
|||
EthSpi mEthSpi; |
|||
#endif |
|||
settings_t *mConfig = nullptr; |
|||
|
|||
uint32_t *mUtcTimestamp; |
|||
AsyncUDP mUdp; // for time server
|
|||
byte mUdpPacketBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
|
|||
|
|||
OnNetworkCB mOnNetworkCB; |
|||
OnTimeCB mOnTimeCB; |
|||
|
|||
}; |
|||
|
|||
#endif /*__AHOYETH_H__*/ |
|||
#endif /* defined(ETHERNET) */ |
@ -0,0 +1,151 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __AHOY_ETHERNET_H__ |
|||
#define __AHOY_ETHERNET_H__ |
|||
|
|||
#if defined(ETHERNET) |
|||
#include <functional> |
|||
#include <AsyncUDP.h> |
|||
#include <ETH.h> |
|||
#include "AhoyEthernetSpi.h" |
|||
#include "AhoyNetwork.h" |
|||
#include "AhoyWifiEsp32.h" |
|||
|
|||
class AhoyEthernet : public AhoyWifi { |
|||
private: |
|||
enum class Mode { |
|||
WIRED, |
|||
WIRELESS |
|||
}; |
|||
|
|||
public: |
|||
AhoyEthernet() |
|||
: mMode (Mode::WIRELESS) {} |
|||
|
|||
virtual void begin() override { |
|||
mMode = Mode::WIRELESS; |
|||
mAp.enable(); |
|||
AhoyWifi::begin(); |
|||
|
|||
if(!mConfig->sys.eth.enabled) |
|||
return; |
|||
|
|||
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); |
|||
} |
|||
|
|||
virtual String getIp(void) override { |
|||
if(Mode::WIRELESS == mMode) |
|||
return AhoyWifi::getIp(); |
|||
else |
|||
return ETH.localIP().toString(); |
|||
} |
|||
|
|||
virtual String getMac(void) override { |
|||
if(Mode::WIRELESS == mMode) |
|||
return AhoyWifi::getMac(); |
|||
else |
|||
return mEthSpi.macAddress(); |
|||
} |
|||
|
|||
virtual bool isWiredConnection() override { |
|||
return (Mode::WIRED == mMode); |
|||
} |
|||
|
|||
private: |
|||
virtual void OnEvent(WiFiEvent_t event) override { |
|||
switch(event) { |
|||
case ARDUINO_EVENT_ETH_CONNECTED: |
|||
mMode = Mode::WIRED; // needed for static IP
|
|||
[[fallthrough]]; |
|||
case SYSTEM_EVENT_STA_CONNECTED: |
|||
mWifiConnecting = false; |
|||
if(NetworkState::CONNECTED != mStatus) { |
|||
if(ARDUINO_EVENT_ETH_CONNECTED == event) |
|||
WiFi.disconnect(); |
|||
|
|||
mStatus = NetworkState::CONNECTED; |
|||
DPRINTLN(DBG_INFO, F("Network connected")); |
|||
setStaticIp(); |
|||
} |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_STA_GOT_IP: |
|||
mStatus = NetworkState::GOT_IP; |
|||
if(mAp.isEnabled()) |
|||
mAp.disable(); |
|||
|
|||
mMode = Mode::WIRELESS; |
|||
if(!mConnected) { |
|||
mConnected = true; |
|||
ah::welcome(WiFi.localIP().toString(), F("Station WiFi")); |
|||
MDNS.begin(mConfig->sys.deviceName); |
|||
mOnNetworkCB(true); |
|||
} |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_ETH_GOT_IP: |
|||
mStatus = NetworkState::GOT_IP; |
|||
mMode = Mode::WIRED; |
|||
if(!mConnected) { |
|||
mAp.disable(); |
|||
mConnected = true; |
|||
ah::welcome(ETH.localIP().toString(), F("Station Ethernet")); |
|||
MDNS.begin(mConfig->sys.deviceName); |
|||
mOnNetworkCB(true); |
|||
WiFi.disconnect(); |
|||
} |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_ETH_STOP: |
|||
[[fallthrough]]; |
|||
case ARDUINO_EVENT_ETH_DISCONNECTED: |
|||
mStatus = NetworkState::DISCONNECTED; |
|||
if(mConnected) { |
|||
mMode = Mode::WIRELESS; |
|||
mConnected = false; |
|||
mOnNetworkCB(false); |
|||
MDNS.end(); |
|||
AhoyWifi::begin(); |
|||
} |
|||
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 && (Mode::WIRELESS == mMode)) { |
|||
mConnected = false; |
|||
mOnNetworkCB(false); |
|||
MDNS.end(); |
|||
AhoyWifi::begin(); |
|||
} |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
void setStaticIp() override { |
|||
setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { |
|||
if(Mode::WIRELESS == mMode) |
|||
return WiFi.config(ip, gateway, mask, dns1, dns2); |
|||
else |
|||
return ETH.config(ip, gateway, mask, dns1, dns2); |
|||
}); |
|||
} |
|||
|
|||
private: |
|||
AhoyEthernetSpi mEthSpi; |
|||
Mode mMode; |
|||
|
|||
}; |
|||
|
|||
#endif /*ETHERNET*/ |
|||
#endif /*__AHOY_ETHERNET_H__*/ |
@ -0,0 +1,254 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __AHOY_NETWORK_H__ |
|||
#define __AHOY_NETWORK_H__ |
|||
|
|||
#include "AhoyNetworkHelper.h" |
|||
#include "../config/settings.h" |
|||
#include "../utils/helper.h" |
|||
#include "AhoyWifiAp.h" |
|||
#include "AsyncJson.h" |
|||
|
|||
#define NTP_PACKET_SIZE 48 |
|||
|
|||
class AhoyNetwork { |
|||
public: |
|||
typedef std::function<void(bool)> OnNetworkCB; |
|||
typedef std::function<void(bool)> OnTimeCB; |
|||
|
|||
public: |
|||
void setup(settings_t *config, uint32_t *utcTimestamp, OnNetworkCB onNetworkCB, OnTimeCB onTimeCB) { |
|||
mConfig = config; |
|||
mUtcTimestamp = utcTimestamp; |
|||
mOnNetworkCB = onNetworkCB; |
|||
mOnTimeCB = onTimeCB; |
|||
|
|||
if('\0' == mConfig->sys.deviceName[0]) |
|||
snprintf(mConfig->sys.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME); |
|||
|
|||
mAp.setup(&mConfig->sys); |
|||
|
|||
#if defined(ESP32) |
|||
WiFi.onEvent([this](WiFiEvent_t event, arduino_event_info_t info) -> void { |
|||
OnEvent(event); |
|||
}); |
|||
#else |
|||
wifiConnectHandler = WiFi.onStationModeConnected( |
|||
[this](const WiFiEventStationModeConnected& event) -> void { |
|||
OnEvent((WiFiEvent_t)SYSTEM_EVENT_STA_CONNECTED); |
|||
}); |
|||
wifiGotIPHandler = WiFi.onStationModeGotIP( |
|||
[this](const WiFiEventStationModeGotIP& event) -> void { |
|||
OnEvent((WiFiEvent_t)SYSTEM_EVENT_STA_GOT_IP); |
|||
}); |
|||
wifiDisconnectHandler = WiFi.onStationModeDisconnected( |
|||
[this](const WiFiEventStationModeDisconnected& event) -> void { |
|||
OnEvent((WiFiEvent_t)SYSTEM_EVENT_STA_DISCONNECTED); |
|||
}); |
|||
#endif |
|||
} |
|||
|
|||
bool isConnected() const { |
|||
return (mStatus == NetworkState::CONNECTED); |
|||
} |
|||
|
|||
bool updateNtpTime(void) { |
|||
if(NetworkState::GOT_IP != mStatus) |
|||
return false; |
|||
|
|||
if (!mUdp.connected()) { |
|||
IPAddress timeServer; |
|||
if (!WiFi.hostByName(mConfig->ntp.addr, timeServer)) |
|||
return false; |
|||
if (!mUdp.connect(timeServer, mConfig->ntp.port)) |
|||
return false; |
|||
} |
|||
|
|||
mUdp.onPacket([this](AsyncUDPPacket packet) { |
|||
this->handleNTPPacket(packet); |
|||
}); |
|||
sendNTPpacket(); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
public: |
|||
virtual void begin() = 0; |
|||
virtual void tickNetworkLoop() = 0; |
|||
virtual String getIp(void) = 0; |
|||
virtual String getMac(void) = 0; |
|||
|
|||
virtual bool getWasInCh12to14() { |
|||
return false; |
|||
} |
|||
|
|||
virtual bool isWiredConnection() { |
|||
return false; |
|||
} |
|||
|
|||
bool isApActive() { |
|||
return mAp.isEnabled(); |
|||
} |
|||
|
|||
bool getAvailNetworks(JsonObject obj, IApp *app) { |
|||
if(!mScanActive) { |
|||
app->addOnce([this]() {scan();}, 1, "scan"); |
|||
return false; |
|||
} |
|||
|
|||
int n = WiFi.scanComplete(); |
|||
if (WIFI_SCAN_RUNNING == n) |
|||
return false; |
|||
|
|||
if(n > 0) { |
|||
JsonArray nets = obj.createNestedArray(F("networks")); |
|||
int sort[n]; |
|||
sortRSSI(&sort[0], n); |
|||
for (int i = 0; i < n; ++i) { |
|||
nets[i][F("ssid")] = WiFi.SSID(sort[i]); |
|||
nets[i][F("rssi")] = WiFi.RSSI(sort[i]); |
|||
} |
|||
} |
|||
mScanActive = false; |
|||
WiFi.scanDelete(); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
void scan(void) { |
|||
mScanActive = true; |
|||
if(mWifiConnecting) { |
|||
mWifiConnecting = false; |
|||
WiFi.disconnect(); |
|||
} |
|||
WiFi.scanNetworks(true, true); |
|||
} |
|||
|
|||
protected: |
|||
virtual void setStaticIp() = 0; |
|||
|
|||
void setupIp(std::function<bool(IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2)> cb) { |
|||
if(mConfig->sys.ip.ip[0] != 0) { |
|||
IPAddress ip(mConfig->sys.ip.ip); |
|||
IPAddress mask(mConfig->sys.ip.mask); |
|||
IPAddress dns1(mConfig->sys.ip.dns1); |
|||
IPAddress dns2(mConfig->sys.ip.dns2); |
|||
IPAddress gateway(mConfig->sys.ip.gateway); |
|||
if(cb(ip, gateway, mask, dns1, dns2)) |
|||
DPRINTLN(DBG_ERROR, F("failed to set static IP!")); |
|||
} |
|||
} |
|||
|
|||
virtual void OnEvent(WiFiEvent_t event) { |
|||
switch(event) { |
|||
case SYSTEM_EVENT_STA_CONNECTED: |
|||
[[fallthrough]]; |
|||
case ARDUINO_EVENT_ETH_CONNECTED: |
|||
if(NetworkState::CONNECTED != mStatus) { |
|||
mStatus = NetworkState::CONNECTED; |
|||
DPRINTLN(DBG_INFO, F("Network connected")); |
|||
} |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_STA_GOT_IP: |
|||
[[fallthrough]]; |
|||
case ARDUINO_EVENT_ETH_GOT_IP: |
|||
mStatus = NetworkState::GOT_IP; |
|||
break; |
|||
|
|||
case ARDUINO_EVENT_WIFI_STA_LOST_IP: |
|||
[[fallthrough]]; |
|||
case ARDUINO_EVENT_WIFI_STA_STOP: |
|||
[[fallthrough]]; |
|||
case SYSTEM_EVENT_STA_DISCONNECTED: |
|||
[[fallthrough]]; |
|||
case ARDUINO_EVENT_ETH_STOP: |
|||
[[fallthrough]]; |
|||
case ARDUINO_EVENT_ETH_DISCONNECTED: |
|||
mStatus = NetworkState::DISCONNECTED; |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
void sortRSSI(int *sort, int n) { |
|||
for (int i = 0; i < n; i++) |
|||
sort[i] = i; |
|||
for (int i = 0; i < n; i++) |
|||
for (int j = i + 1; j < n; j++) |
|||
if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) |
|||
std::swap(sort[i], sort[j]); |
|||
} |
|||
|
|||
private: |
|||
void sendNTPpacket(void) { |
|||
uint8_t buf[NTP_PACKET_SIZE]; |
|||
memset(buf, 0, NTP_PACKET_SIZE); |
|||
|
|||
buf[0] = 0b11100011; // LI, Version, Mode
|
|||
buf[1] = 0; // Stratum
|
|||
buf[2] = 6; // Max Interval between messages in seconds
|
|||
buf[3] = 0xEC; // Clock Precision
|
|||
// bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
|
|||
buf[12] = 49; // four-byte reference ID identifying
|
|||
buf[13] = 0x4E; |
|||
buf[14] = 49; |
|||
buf[15] = 52; |
|||
|
|||
mUdp.write(buf, NTP_PACKET_SIZE); |
|||
} |
|||
|
|||
void handleNTPPacket(AsyncUDPPacket packet) { |
|||
char buf[80]; |
|||
|
|||
memcpy(buf, packet.data(), sizeof(buf)); |
|||
|
|||
unsigned long highWord = word(buf[40], buf[41]); |
|||
unsigned long lowWord = word(buf[42], buf[43]); |
|||
|
|||
// combine the four bytes (two words) into a long integer
|
|||
// this is NTP time (seconds since Jan 1 1900):
|
|||
unsigned long secsSince1900 = highWord << 16 | lowWord; |
|||
|
|||
*mUtcTimestamp = secsSince1900 - 2208988800UL; // UTC time
|
|||
DPRINTLN(DBG_INFO, "[NTP]: " + ah::getDateTimeStr(*mUtcTimestamp) + " UTC"); |
|||
mOnTimeCB(true); |
|||
mUdp.close(); |
|||
} |
|||
|
|||
protected: |
|||
enum class NetworkState : uint8_t { |
|||
DISCONNECTED, |
|||
CONNECTED, |
|||
GOT_IP, |
|||
SCAN_READY, // ESP8266
|
|||
CONNECTING // ESP8266
|
|||
}; |
|||
|
|||
protected: |
|||
settings_t *mConfig = nullptr; |
|||
uint32_t *mUtcTimestamp = nullptr; |
|||
bool mConnected = false; |
|||
bool mScanActive = false; |
|||
bool mWifiConnecting = false; |
|||
|
|||
OnNetworkCB mOnNetworkCB; |
|||
OnTimeCB mOnTimeCB; |
|||
|
|||
NetworkState mStatus = NetworkState::DISCONNECTED; |
|||
|
|||
AhoyWifiAp mAp; |
|||
DNSServer mDns; |
|||
|
|||
AsyncUDP mUdp; // for time server
|
|||
#if defined(ESP8266) |
|||
WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler; |
|||
#endif |
|||
}; |
|||
|
|||
#endif /*__AHOY_NETWORK_H__*/ |
@ -0,0 +1,20 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#include "AhoyNetworkHelper.h" |
|||
|
|||
namespace ah { |
|||
void welcome(String ip, String info) { |
|||
DBGPRINTLN(F("\n\n-------------------")); |
|||
DBGPRINTLN(F("Welcome to AHOY!")); |
|||
DBGPRINT(F("\npoint your browser to http://")); |
|||
DBGPRINT(ip); |
|||
DBGPRINT(" ("); |
|||
DBGPRINT(info); |
|||
DBGPRINTLN(")"); |
|||
DBGPRINTLN(F("to configure your device")); |
|||
DBGPRINTLN(F("-------------------\n")); |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __AHOY_NETWORK_HELPER_H__ |
|||
#define __AHOY_NETWORK_HELPER_H__ |
|||
|
|||
#include "../utils/dbg.h" |
|||
#include <Arduino.h> |
|||
#if defined(ESP32) |
|||
#include "ESPAsyncWebServer.h" |
|||
#include <WiFiType.h> |
|||
#include <ESPmDNS.h> |
|||
#else |
|||
#include <ESP8266WiFi.h> |
|||
#include <ESP8266mDNS.h> |
|||
//#include <WiFiUdp.h>
|
|||
#include "ESPAsyncUDP.h" |
|||
|
|||
enum { |
|||
SYSTEM_EVENT_STA_CONNECTED = 1, |
|||
ARDUINO_EVENT_ETH_CONNECTED, |
|||
SYSTEM_EVENT_STA_GOT_IP, |
|||
ARDUINO_EVENT_ETH_GOT_IP, |
|||
ARDUINO_EVENT_WIFI_STA_LOST_IP, |
|||
ARDUINO_EVENT_WIFI_STA_STOP, |
|||
SYSTEM_EVENT_STA_DISCONNECTED, |
|||
ARDUINO_EVENT_ETH_STOP, |
|||
ARDUINO_EVENT_ETH_DISCONNECTED |
|||
}; |
|||
#endif |
|||
#include <DNSServer.h> |
|||
|
|||
namespace ah { |
|||
void welcome(String ip, String info); |
|||
} |
|||
|
|||
#endif /*__AHOY_NETWORK_HELPER_H__*/ |
@ -0,0 +1,76 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __AHOY_WIFI_AP_H__ |
|||
#define __AHOY_WIFI_AP_H__ |
|||
|
|||
#include "../utils/dbg.h" |
|||
#include <Arduino.h> |
|||
#include "../config/settings.h" |
|||
#include "AhoyNetworkHelper.h" |
|||
|
|||
class AhoyWifiAp { |
|||
public: |
|||
AhoyWifiAp() : mIp(192, 168, 4, 1) {} |
|||
|
|||
void setup(cfgSys_t *cfg) { |
|||
mCfg = cfg; |
|||
} |
|||
|
|||
void tickLoop() { |
|||
if(mEnabled) |
|||
mDns.processNextRequest(); |
|||
|
|||
if (WiFi.softAPgetStationNum() != mLast) { |
|||
mLast = WiFi.softAPgetStationNum(); |
|||
if(mLast > 0) |
|||
DBGPRINTLN(F("AP client connected")); |
|||
} |
|||
} |
|||
|
|||
void enable() { |
|||
if(mEnabled) |
|||
return; |
|||
|
|||
ah::welcome(mIp.toString(), String(F("Password: ") + String(mCfg->apPwd))); |
|||
|
|||
WiFi.mode(WIFI_AP_STA); |
|||
WiFi.softAPConfig(mIp, mIp, IPAddress(255, 255, 255, 0)); |
|||
WiFi.softAP(WIFI_AP_SSID, mCfg->apPwd); |
|||
|
|||
mDns.setErrorReplyCode(DNSReplyCode::NoError); |
|||
mDns.start(53, "*", mIp); |
|||
|
|||
mEnabled = true; |
|||
tickLoop(); |
|||
} |
|||
|
|||
void disable() { |
|||
if(!mEnabled) |
|||
return; |
|||
|
|||
if(WiFi.softAPgetStationNum() > 0) |
|||
return; |
|||
|
|||
mDns.stop(); |
|||
WiFi.softAPdisconnect(); |
|||
WiFi.mode(WIFI_STA); |
|||
|
|||
mEnabled = false; |
|||
} |
|||
|
|||
bool isEnabled() const { |
|||
return mEnabled; |
|||
} |
|||
|
|||
private: |
|||
cfgSys_t *mCfg = nullptr; |
|||
DNSServer mDns; |
|||
IPAddress mIp; |
|||
bool mEnabled = false; |
|||
uint8_t mLast = 0; |
|||
}; |
|||
|
|||
#endif /*__AHOY_WIFI_AP_H__*/ |
@ -0,0 +1,103 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __AHOY_WIFI_ESP32_H__ |
|||
#define __AHOY_WIFI_ESP32_H__ |
|||
|
|||
#if defined(ESP32) |
|||
#include <functional> |
|||
#include <AsyncUDP.h> |
|||
#include "AhoyNetwork.h" |
|||
#include "ESPAsyncWebServer.h" |
|||
|
|||
class AhoyWifi : public AhoyNetwork { |
|||
public: |
|||
virtual void begin() override { |
|||
mAp.enable(); |
|||
|
|||
if(strlen(mConfig->sys.stationSsid) == 0) |
|||
return; // no station wifi defined
|
|||
|
|||
|
|||
WiFi.disconnect(); // clean up
|
|||
WiFi.setHostname(mConfig->sys.deviceName); |
|||
#if !defined(AP_ONLY) |
|||
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); |
|||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); |
|||
setStaticIp(); |
|||
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, WIFI_ALL_CHANNEL_SCAN); |
|||
mWifiConnecting = true; |
|||
|
|||
DBGPRINT(F("connect to network '")); |
|||
DBGPRINT(mConfig->sys.stationSsid); |
|||
DBGPRINTLN(F("'")); |
|||
#endif |
|||
} |
|||
|
|||
void tickNetworkLoop() override { |
|||
if(mAp.isEnabled()) |
|||
mAp.tickLoop(); |
|||
} |
|||
|
|||
virtual String getIp(void) override { |
|||
return WiFi.localIP().toString(); |
|||
} |
|||
|
|||
virtual String getMac(void) override { |
|||
return WiFi.macAddress(); |
|||
} |
|||
|
|||
private: |
|||
virtual void OnEvent(WiFiEvent_t event) override { |
|||
switch(event) { |
|||
case SYSTEM_EVENT_STA_CONNECTED: |
|||
if(NetworkState::CONNECTED != mStatus) { |
|||
mStatus = NetworkState::CONNECTED; |
|||
mWifiConnecting = false; |
|||
DPRINTLN(DBG_INFO, F("Network connected")); |
|||
} |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_STA_GOT_IP: |
|||
mStatus = NetworkState::GOT_IP; |
|||
if(mAp.isEnabled()) |
|||
mAp.disable(); |
|||
|
|||
if(!mConnected) { |
|||
mConnected = true; |
|||
ah::welcome(WiFi.localIP().toString(), F("Station")); |
|||
MDNS.begin(mConfig->sys.deviceName); |
|||
mOnNetworkCB(true); |
|||
} |
|||
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; |
|||
} |
|||
} |
|||
|
|||
virtual 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*/ |
|||
#endif /*__AHOY_WIFI_ESP32_H__*/ |
@ -0,0 +1,170 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __AHOY_WIFI_ESP8266_H__ |
|||
#define __AHOY_WIFI_ESP8266_H__ |
|||
|
|||
#if defined(ESP8266) |
|||
#include <functional> |
|||
#include <list> |
|||
#include <WiFiUdp.h> |
|||
#include "AhoyNetwork.h" |
|||
#include "ESPAsyncWebServer.h" |
|||
|
|||
class AhoyWifi : public AhoyNetwork { |
|||
public: |
|||
void begin() override { |
|||
mAp.enable(); |
|||
|
|||
WiFi.setHostname(mConfig->sys.deviceName); |
|||
mBSSIDList.clear(); |
|||
} |
|||
|
|||
void tickNetworkLoop() override { |
|||
if(mAp.isEnabled()) |
|||
mAp.tickLoop(); |
|||
|
|||
mCnt++; |
|||
|
|||
switch(mStatus) { |
|||
case NetworkState::DISCONNECTED: |
|||
if(mConnected) { |
|||
mConnected = false; |
|||
mWifiConnecting = false; |
|||
mOnNetworkCB(false); |
|||
mAp.enable(); |
|||
MDNS.end(); |
|||
} |
|||
|
|||
if (WiFi.softAPgetStationNum() > 0) { |
|||
DBGPRINTLN(F("AP client connected")); |
|||
} |
|||
#if !defined(AP_ONLY) |
|||
else if (!mScanActive) { |
|||
DBGPRINT(F("scanning APs with SSID ")); |
|||
DBGPRINTLN(String(mConfig->sys.stationSsid)); |
|||
mScanCnt = 0; |
|||
mCnt = 0; |
|||
mScanActive = true; |
|||
WiFi.scanNetworks(true, true, 0U, ([this]() { |
|||
if (mConfig->sys.isHidden) |
|||
return (uint8_t*)NULL; |
|||
return (uint8_t*)(mConfig->sys.stationSsid); |
|||
})()); |
|||
} else if(getBSSIDs()) { |
|||
mStatus = NetworkState::SCAN_READY; |
|||
DBGPRINT(F("connect to network '")); Serial.flush(); |
|||
DBGPRINTLN(mConfig->sys.stationSsid); |
|||
} |
|||
#endif |
|||
break; |
|||
|
|||
case NetworkState::SCAN_READY: |
|||
mStatus = NetworkState::CONNECTING; |
|||
DBGPRINT(F("try to connect to BSSID:")); |
|||
uint8_t bssid[6]; |
|||
for (int j = 0; j < 6; j++) { |
|||
bssid[j] = mBSSIDList.front(); |
|||
mBSSIDList.pop_front(); |
|||
DBGPRINT(" " + String(bssid[j], HEX)); |
|||
} |
|||
DBGPRINTLN(""); |
|||
setStaticIp(); |
|||
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]); |
|||
mWifiConnecting = true; |
|||
break; |
|||
|
|||
case NetworkState::CONNECTING: |
|||
if (isTimeout(TIMEOUT)) { |
|||
WiFi.disconnect(); |
|||
mWifiConnecting = false; |
|||
mStatus = mBSSIDList.empty() ? NetworkState::DISCONNECTED : NetworkState::SCAN_READY; |
|||
} |
|||
break; |
|||
|
|||
case NetworkState::CONNECTED: |
|||
break; |
|||
|
|||
case NetworkState::GOT_IP: |
|||
if(!mConnected) { |
|||
mAp.disable(); |
|||
mConnected = true; |
|||
ah::welcome(WiFi.localIP().toString(), F("Station")); |
|||
MDNS.begin(mConfig->sys.deviceName); |
|||
MDNSResponder::hMDNSService hRes = MDNS.addService(NULL, "http", "tcp", 80); |
|||
MDNS.addServiceTxt(hRes, "path", "/"); |
|||
MDNS.announce(); |
|||
mOnNetworkCB(true); |
|||
} |
|||
|
|||
MDNS.update(); |
|||
|
|||
if(WiFi.channel() > 11) |
|||
mWasInCh12to14 = true; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
String getIp(void) override { |
|||
return WiFi.localIP().toString(); |
|||
} |
|||
|
|||
String getMac(void) override { |
|||
return WiFi.macAddress(); |
|||
} |
|||
|
|||
bool getWasInCh12to14() override { |
|||
return mWasInCh12to14; |
|||
} |
|||
|
|||
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 result = false; |
|||
int n = WiFi.scanComplete(); |
|||
if (n < 0) { |
|||
if (++mScanCnt < 20) |
|||
return false; |
|||
} |
|||
if(n > 0) { |
|||
mBSSIDList.clear(); |
|||
int sort[n]; |
|||
sortRSSI(&sort[0], n); |
|||
for (int i = 0; i < n; i++) { |
|||
DBGPRINT("BSSID " + String(i) + ":"); |
|||
uint8_t *bssid = WiFi.BSSID(sort[i]); |
|||
for (int j = 0; j < 6; j++){ |
|||
DBGPRINT(" " + String(bssid[j], HEX)); |
|||
mBSSIDList.push_back(bssid[j]); |
|||
} |
|||
DBGPRINTLN(""); |
|||
} |
|||
result = true; |
|||
} |
|||
mScanActive = false; |
|||
WiFi.scanDelete(); |
|||
return result; |
|||
} |
|||
|
|||
bool isTimeout(uint8_t timeout) { |
|||
return ((mCnt % timeout) == 0); |
|||
} |
|||
|
|||
private: |
|||
uint8_t mCnt = 0; |
|||
uint8_t mScanCnt = 0; |
|||
std::list<uint8_t> mBSSIDList; |
|||
bool mWasInCh12to14 = false; |
|||
static constexpr uint8_t TIMEOUT = 20; |
|||
static constexpr uint8_t SCAN_TIMEOUT = 10; |
|||
}; |
|||
|
|||
#endif /*ESP8266*/ |
|||
#endif /*__AHOY_WIFI_ESP8266_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__*/ |
@ -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) |
|||
break; // 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 |
@ -0,0 +1,44 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#ifndef __PLUGIN_LANG_H__ |
|||
#define __PLUGIN_LANG_H__ |
|||
|
|||
#ifdef LANG_DE |
|||
#define STR_MONTHNAME_3_CHAR_LIST "ErrJanFebMrzAprMaiJunJulAugSepOktNovDez" |
|||
#define STR_DAYNAME_3_CHAR_LIST "ErrSonMonDieMitDonFreSam" |
|||
#define STR_OFFLINE "aus" |
|||
#define STR_ONLINE "aktiv" |
|||
#define STR_NO_INVERTER "kein inverter" |
|||
#define STR_NO_WIFI "WLAN nicht verbunden" |
|||
#define STR_VERSION "Version" |
|||
#define STR_ACTIVE_INVERTERS "aktive WR" |
|||
#define STR_TODAY "heute" |
|||
#define STR_TOTAL "Gesamt" |
|||
#elif LANG_FR |
|||
#define STR_MONTHNAME_3_CHAR_LIST "ErrJanFevMarAvrMaiJunJulAouSepOctNovDec" |
|||
#define STR_DAYNAME_3_CHAR_LIST "ErrDimLunMarMerJeuVenSam" |
|||
#define STR_OFFLINE "eteint" |
|||
#define STR_ONLINE "online" |
|||
#define STR_NO_INVERTER "pas d'onduleur" |
|||
#define STR_NO_WIFI "WiFi not connected" |
|||
#define STR_VERSION "Version" |
|||
#define STR_ACTIVE_INVERTERS "active Inv" |
|||
#define STR_TODAY "today" |
|||
#define STR_TOTAL "total" |
|||
#else |
|||
#define STR_MONTHNAME_3_CHAR_LIST "ErrJanFebMarAprMayJunJulAugSepOctNovDec" |
|||
#define STR_DAYNAME_3_CHAR_LIST "ErrSunMonTueWedThuFriSat" |
|||
#define STR_OFFLINE "offline" |
|||
#define STR_ONLINE "online" |
|||
#define STR_NO_INVERTER "no inverter" |
|||
#define STR_NO_WIFI "WiFi not connected" |
|||
#define STR_VERSION "Version" |
|||
#define STR_ACTIVE_INVERTERS "active Inv" |
|||
#define STR_TODAY "today" |
|||
#define STR_TOTAL "total" |
|||
#endif |
|||
|
|||
#endif /*__PLUGIN_LANG_H__*/ |
@ -1,488 +0,0 @@ |
|||
//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://ahoydtu.de
|
|||
// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#if !defined(ETHERNET) |
|||
#if defined(ESP32) && defined(F) |
|||
#undef F |
|||
#define F(sl) (sl) |
|||
#endif |
|||
#include "ahoywifi.h" |
|||
|
|||
#if defined(ESP32) |
|||
#include <ESPmDNS.h> |
|||
#else |
|||
#include <ESP8266mDNS.h> |
|||
#endif |
|||
|
|||
// NTP CONFIG
|
|||
#define NTP_PACKET_SIZE 48 |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
ahoywifi::ahoywifi() : mApIp(192, 168, 4, 1) {} |
|||
|
|||
|
|||
/**
|
|||
* TODO: ESP32 has native strongest AP support! |
|||
* WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); |
|||
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); |
|||
*/ |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) { |
|||
mConfig = config; |
|||
mUtcTimestamp = utcTimestamp; |
|||
mAppWifiCb = cb; |
|||
|
|||
mGotDisconnect = false; |
|||
mStaConn = DISCONNECTED; |
|||
mCnt = 0; |
|||
mScanActive = false; |
|||
mScanCnt = 0; |
|||
mStopApAllowed = true; |
|||
|
|||
#if defined(ESP8266) |
|||
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1)); |
|||
wifiGotIPHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onGotIP, this, std::placeholders::_1)); |
|||
wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1)); |
|||
#else |
|||
WiFi.onEvent(std::bind(&ahoywifi::onWiFiEvent, this, std::placeholders::_1)); |
|||
#endif |
|||
|
|||
setupWifi(true); |
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::setupWifi(bool startAP = false) { |
|||
#if !defined(FB_WIFI_OVERRIDDEN) |
|||
if(startAP) { |
|||
setupAp(); |
|||
delay(1000); |
|||
} |
|||
#endif |
|||
#if !defined(AP_ONLY) |
|||
#if defined(FB_WIFI_OVERRIDDEN) |
|||
snprintf(mConfig->sys.stationSsid, SSID_LEN, "%s", FB_WIFI_SSID); |
|||
snprintf(mConfig->sys.stationPwd, PWD_LEN, "%s", FB_WIFI_PWD); |
|||
setupStation(); |
|||
#else |
|||
if(mConfig->valid) { |
|||
if(strncmp(mConfig->sys.stationSsid, FB_WIFI_SSID, 14) != 0) |
|||
setupStation(); |
|||
} |
|||
#endif |
|||
#endif |
|||
} |
|||
|
|||
|
|||
void ahoywifi::tickWifiLoop() { |
|||
static const uint8_t TIMEOUT = 20; |
|||
static const uint8_t SCAN_TIMEOUT = 10; |
|||
#if !defined(AP_ONLY) |
|||
|
|||
mCnt++; |
|||
|
|||
switch (mStaConn) { |
|||
case IN_STA_MODE: |
|||
// Nothing to do
|
|||
if (mGotDisconnect) { |
|||
mStaConn = RESET; |
|||
} |
|||
#if !defined(ESP32) |
|||
MDNS.update(); |
|||
if(WiFi.channel() > 11) |
|||
mWasInCh12to14 = true; |
|||
#endif |
|||
return; |
|||
case IN_AP_MODE: |
|||
if ((WiFi.softAPgetStationNum() == 0) || (!mStopApAllowed)) { |
|||
mCnt = 0; |
|||
mDns.stop(); |
|||
WiFi.mode(WIFI_AP_STA); |
|||
mStaConn = DISCONNECTED; |
|||
} else { |
|||
mDns.processNextRequest(); |
|||
return; |
|||
} |
|||
break; |
|||
case DISCONNECTED: |
|||
if ((WiFi.softAPgetStationNum() > 0) && (mStopApAllowed)) { |
|||
mStaConn = IN_AP_MODE; |
|||
// first time switch to AP Mode
|
|||
if (mScanActive) { |
|||
WiFi.scanDelete(); |
|||
mScanActive = false; |
|||
} |
|||
DBGPRINTLN(F("AP client connected")); |
|||
welcome(mApIp.toString(), ""); |
|||
WiFi.mode(WIFI_AP); |
|||
mDns.start(53, "*", mApIp); |
|||
mAppWifiCb(true); |
|||
mDns.processNextRequest(); |
|||
return; |
|||
} else if (!mScanActive) { |
|||
DBGPRINT(F("scanning APs with SSID ")); |
|||
DBGPRINTLN(String(mConfig->sys.stationSsid)); |
|||
mScanCnt = 0; |
|||
mCnt = 0; |
|||
mScanActive = true; |
|||
#if defined(ESP8266) |
|||
WiFi.scanNetworks(true, true, 0U, ([this]() { |
|||
if (mConfig->sys.isHidden) |
|||
return (uint8_t*)NULL; |
|||
return (uint8_t*)(mConfig->sys.stationSsid); |
|||
})()); |
|||
#else |
|||
WiFi.scanNetworks(true, true, false, 300U, 0U, ([this]() { |
|||
if (mConfig->sys.isHidden) |
|||
return (char*)NULL; |
|||
return (mConfig->sys.stationSsid); |
|||
})()); |
|||
#endif |
|||
return; |
|||
} else if(getBSSIDs()) { |
|||
// Scan ready
|
|||
mStaConn = SCAN_READY; |
|||
} else { |
|||
// In case of a timeout, what do we do?
|
|||
// For now we start scanning again as the original code did.
|
|||
// Would be better to into PA mode
|
|||
|
|||
if (isTimeout(SCAN_TIMEOUT)) { |
|||
WiFi.scanDelete(); |
|||
mScanActive = false; |
|||
} |
|||
} |
|||
break; |
|||
case SCAN_READY: |
|||
mStaConn = CONNECTING; |
|||
mCnt = 0; |
|||
DBGPRINT(F("try to connect to AP with BSSID:")); |
|||
uint8_t bssid[6]; |
|||
for (int j = 0; j < 6; j++) { |
|||
bssid[j] = mBSSIDList.front(); |
|||
mBSSIDList.pop_front(); |
|||
DBGPRINT(" " + String(bssid[j], HEX)); |
|||
} |
|||
DBGPRINTLN(""); |
|||
mGotDisconnect = false; |
|||
WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]); |
|||
|
|||
break; |
|||
case CONNECTING: |
|||
if (isTimeout(TIMEOUT)) { |
|||
WiFi.disconnect(); |
|||
mStaConn = mBSSIDList.empty() ? DISCONNECTED : SCAN_READY; |
|||
} |
|||
break; |
|||
case CONNECTED: |
|||
// Connection but no IP yet
|
|||
if (isTimeout(TIMEOUT) || mGotDisconnect) { |
|||
mStaConn = RESET; |
|||
} |
|||
break; |
|||
case GOT_IP: |
|||
welcome(WiFi.localIP().toString(), F(" (Station)")); |
|||
if(mStopApAllowed) { |
|||
WiFi.softAPdisconnect(); |
|||
WiFi.mode(WIFI_STA); |
|||
DBGPRINTLN(F("[WiFi] AP disabled")); |
|||
delay(100); |
|||
} |
|||
mAppWifiCb(true); |
|||
mGotDisconnect = false; |
|||
mStaConn = IN_STA_MODE; |
|||
|
|||
if (!MDNS.begin(mConfig->sys.deviceName)) { |
|||
DPRINTLN(DBG_ERROR, F("Error setting up MDNS responder!")); |
|||
} else { |
|||
DBGPRINT(F("[WiFi] mDNS established: ")); |
|||
DBGPRINT(mConfig->sys.deviceName); |
|||
DBGPRINTLN(F(".local")); |
|||
} |
|||
|
|||
break; |
|||
case RESET: |
|||
mGotDisconnect = false; |
|||
mStaConn = DISCONNECTED; |
|||
mCnt = 5; // try to reconnect in 5 sec
|
|||
setupWifi(); // reconnect with AP / Station setup
|
|||
mAppWifiCb(false); |
|||
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); |
|||
break; |
|||
default: |
|||
DBGPRINTLN(F("Unhandled status")); |
|||
break; |
|||
} |
|||
|
|||
#endif |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::setupAp(void) { |
|||
DPRINTLN(DBG_VERBOSE, F("wifi::setupAp")); |
|||
|
|||
DBGPRINTLN(F("\n---------\nAhoyDTU Info:")); |
|||
DBGPRINT(F("Version: ")); |
|||
DBGPRINT(String(VERSION_MAJOR)); |
|||
DBGPRINT(F(".")); |
|||
DBGPRINT(String(VERSION_MINOR)); |
|||
DBGPRINT(F(".")); |
|||
DBGPRINTLN(String(VERSION_PATCH)); |
|||
DBGPRINT(F("Github Hash: ")); |
|||
DBGPRINTLN(String(AUTO_GIT_HASH)); |
|||
|
|||
DBGPRINT(F("\n---------\nAP MODE\nSSID: ")); |
|||
DBGPRINTLN(WIFI_AP_SSID); |
|||
DBGPRINT(F("PWD: ")); |
|||
DBGPRINTLN(mConfig->sys.apPwd); |
|||
DBGPRINT(F("IP Address: http://")); |
|||
DBGPRINTLN(mApIp.toString()); |
|||
DBGPRINTLN(F("---------\n")); |
|||
|
|||
if(String(mConfig->sys.deviceName) != "") |
|||
WiFi.hostname(mConfig->sys.deviceName); |
|||
|
|||
WiFi.mode(WIFI_AP_STA); |
|||
WiFi.softAPConfig(mApIp, mApIp, IPAddress(255, 255, 255, 0)); |
|||
WiFi.softAP(WIFI_AP_SSID, mConfig->sys.apPwd); |
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::setupStation(void) { |
|||
DPRINTLN(DBG_VERBOSE, F("wifi::setupStation")); |
|||
if(mConfig->sys.ip.ip[0] != 0) { |
|||
IPAddress ip(mConfig->sys.ip.ip); |
|||
IPAddress mask(mConfig->sys.ip.mask); |
|||
IPAddress dns1(mConfig->sys.ip.dns1); |
|||
IPAddress dns2(mConfig->sys.ip.dns2); |
|||
IPAddress gateway(mConfig->sys.ip.gateway); |
|||
if(!WiFi.config(ip, gateway, mask, dns1, dns2)) |
|||
DPRINTLN(DBG_ERROR, F("failed to set static IP!")); |
|||
} |
|||
mBSSIDList.clear(); |
|||
if(String(mConfig->sys.deviceName) != "") |
|||
WiFi.hostname(mConfig->sys.deviceName); |
|||
WiFi.mode(WIFI_AP_STA); |
|||
|
|||
DBGPRINT(F("connect to network '")); |
|||
DBGPRINT(mConfig->sys.stationSsid); |
|||
DBGPRINTLN(F("' ...")); |
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
bool ahoywifi::getNtpTime(void) { |
|||
if(IN_STA_MODE != mStaConn) |
|||
return false; |
|||
|
|||
IPAddress timeServer; |
|||
uint8_t buf[NTP_PACKET_SIZE]; |
|||
uint8_t retry = 0; |
|||
|
|||
if (WiFi.hostByName(mConfig->ntp.addr, timeServer) != 1) |
|||
return false; |
|||
|
|||
mUdp.begin(mConfig->ntp.port); |
|||
sendNTPpacket(timeServer); |
|||
|
|||
while(retry++ < 5) { |
|||
int wait = 150; |
|||
while(--wait) { |
|||
if(NTP_PACKET_SIZE <= mUdp.parsePacket()) { |
|||
uint64_t secsSince1900; |
|||
mUdp.read(buf, NTP_PACKET_SIZE); |
|||
secsSince1900 = ((uint64_t)buf[40] << 24); |
|||
secsSince1900 |= (buf[41] << 16); |
|||
secsSince1900 |= (buf[42] << 8); |
|||
secsSince1900 |= (buf[43] ); |
|||
|
|||
*mUtcTimestamp = secsSince1900 - 2208988800UL; // UTC time
|
|||
DPRINTLN(DBG_INFO, "[NTP]: " + ah::getDateTimeStr(*mUtcTimestamp) + " UTC"); |
|||
return true; |
|||
} else |
|||
delay(10); |
|||
} |
|||
} |
|||
|
|||
DPRINTLN(DBG_INFO, F("[NTP]: getNtpTime failed")); |
|||
return false; |
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::sendNTPpacket(IPAddress& address) { |
|||
//DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket"));
|
|||
uint8_t buf[NTP_PACKET_SIZE] = {0}; |
|||
|
|||
buf[0] = B11100011; // LI, Version, Mode
|
|||
buf[1] = 0; // Stratum
|
|||
buf[2] = 6; // Max Interval between messages in seconds
|
|||
buf[3] = 0xEC; // Clock Precision
|
|||
// bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
|
|||
buf[12] = 49; // four-byte reference ID identifying
|
|||
buf[13] = 0x4E; |
|||
buf[14] = 49; |
|||
buf[15] = 52; |
|||
|
|||
mUdp.beginPacket(address, 123); // NTP request, port 123
|
|||
mUdp.write(buf, NTP_PACKET_SIZE); |
|||
mUdp.endPacket(); |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::sortRSSI(int *sort, int n) { |
|||
for (int i = 0; i < n; i++) |
|||
sort[i] = i; |
|||
for (int i = 0; i < n; i++) |
|||
for (int j = i + 1; j < n; j++) |
|||
if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) |
|||
std::swap(sort[i], sort[j]); |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::scanAvailNetworks(void) { |
|||
if(!mScanActive) { |
|||
mScanActive = true; |
|||
if(WIFI_AP == WiFi.getMode()) |
|||
WiFi.mode(WIFI_AP_STA); |
|||
WiFi.scanNetworks(true); |
|||
} |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
bool ahoywifi::getAvailNetworks(JsonObject obj) { |
|||
JsonArray nets = obj.createNestedArray("networks"); |
|||
|
|||
int n = WiFi.scanComplete(); |
|||
if (n < 0) |
|||
return false; |
|||
if(n > 0) { |
|||
int sort[n]; |
|||
sortRSSI(&sort[0], n); |
|||
for (int i = 0; i < n; ++i) { |
|||
nets[i]["ssid"] = WiFi.SSID(sort[i]); |
|||
nets[i]["rssi"] = WiFi.RSSI(sort[i]); |
|||
} |
|||
} |
|||
mScanActive = false; |
|||
WiFi.scanDelete(); |
|||
if(mStaConn == IN_AP_MODE) |
|||
WiFi.mode(WIFI_AP); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
bool ahoywifi::getBSSIDs() { |
|||
bool result = false; |
|||
int n = WiFi.scanComplete(); |
|||
if (n < 0) { |
|||
if (++mScanCnt < 20) |
|||
return false; |
|||
} |
|||
if(n > 0) { |
|||
mBSSIDList.clear(); |
|||
int sort[n]; |
|||
sortRSSI(&sort[0], n); |
|||
for (int i = 0; i < n; i++) { |
|||
DBGPRINT("BSSID " + String(i) + ":"); |
|||
uint8_t *bssid = WiFi.BSSID(sort[i]); |
|||
for (int j = 0; j < 6; j++){ |
|||
DBGPRINT(" " + String(bssid[j], HEX)); |
|||
mBSSIDList.push_back(bssid[j]); |
|||
} |
|||
DBGPRINTLN(""); |
|||
} |
|||
result = true; |
|||
} |
|||
mScanActive = false; |
|||
WiFi.scanDelete(); |
|||
return result; |
|||
} |
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::connectionEvent(WiFiStatus_t status) { |
|||
DPRINTLN(DBG_INFO, "connectionEvent"); |
|||
|
|||
switch(status) { |
|||
case CONNECTED: |
|||
if(mStaConn != CONNECTED) { |
|||
mStaConn = CONNECTED; |
|||
mGotDisconnect = false; |
|||
DBGPRINTLN(F("\n[WiFi] Connected")); |
|||
} |
|||
break; |
|||
|
|||
case GOT_IP: |
|||
mStaConn = GOT_IP; |
|||
break; |
|||
|
|||
case DISCONNECTED: |
|||
mGotDisconnect = true; |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
#if defined(ESP8266) |
|||
//-------------------------------------------------------------------------
|
|||
void ahoywifi::onConnect(const WiFiEventStationModeConnected& event) { |
|||
connectionEvent(CONNECTED); |
|||
} |
|||
|
|||
//-------------------------------------------------------------------------
|
|||
void ahoywifi::onGotIP(const WiFiEventStationModeGotIP& event) { |
|||
connectionEvent(GOT_IP); |
|||
} |
|||
|
|||
//-------------------------------------------------------------------------
|
|||
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) { |
|||
connectionEvent(DISCONNECTED); |
|||
} |
|||
|
|||
#else |
|||
//-------------------------------------------------------------------------
|
|||
void ahoywifi::onWiFiEvent(WiFiEvent_t event) { |
|||
DBGPRINT(F("Wifi event: ")); |
|||
DBGPRINTLN(String(event)); |
|||
|
|||
switch(event) { |
|||
case SYSTEM_EVENT_STA_CONNECTED: |
|||
connectionEvent(CONNECTED); |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_STA_GOT_IP: |
|||
connectionEvent(GOT_IP); |
|||
break; |
|||
|
|||
case SYSTEM_EVENT_STA_DISCONNECTED: |
|||
connectionEvent(DISCONNECTED); |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
#endif |
|||
|
|||
|
|||
//-----------------------------------------------------------------------------
|
|||
void ahoywifi::welcome(String ip, String mode) { |
|||
DBGPRINTLN(F("\n\n--------------------------------")); |
|||
DBGPRINTLN(F("Welcome to AHOY!")); |
|||
DBGPRINT(F("\npoint your browser to http://")); |
|||
DBGPRINT(ip); |
|||
DBGPRINTLN(mode); |
|||
DBGPRINTLN(F("to configure your device")); |
|||
DBGPRINTLN(F("--------------------------------\n")); |
|||
} |
|||
|
|||
#endif /* !defined(ETHERNET) */ |
@ -1,97 +0,0 @@ |
|||
//------------------------------------//-----------------------------------------------------------------------------
|
|||
// 2024 Ahoy, https://github.com/lumpapu/ahoy
|
|||
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed
|
|||
//-----------------------------------------------------------------------------
|
|||
|
|||
#if !defined(ETHERNET) |
|||
#ifndef __AHOYWIFI_H__ |
|||
#define __AHOYWIFI_H__ |
|||
|
|||
#include "../utils/dbg.h" |
|||
#include <Arduino.h> |
|||
#include <WiFiUdp.h> |
|||
#include <DNSServer.h> |
|||
#include "ESPAsyncWebServer.h" |
|||
|
|||
#include "../config/settings.h" |
|||
|
|||
class app; |
|||
|
|||
class ahoywifi { |
|||
public: |
|||
typedef std::function<void(bool)> appWifiCb; |
|||
|
|||
ahoywifi(); |
|||
|
|||
|
|||
void setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb); |
|||
void tickWifiLoop(void); |
|||
bool getNtpTime(void); |
|||
void scanAvailNetworks(void); |
|||
bool getAvailNetworks(JsonObject obj); |
|||
void setStopApAllowedMode(bool allowed) { |
|||
mStopApAllowed = allowed; |
|||
} |
|||
String getStationIp(void) { |
|||
return WiFi.localIP().toString(); |
|||
} |
|||
void setupStation(void); |
|||
|
|||
bool getWasInCh12to14() const { |
|||
return mWasInCh12to14; |
|||
} |
|||
|
|||
private: |
|||
typedef enum WiFiStatus { |
|||
DISCONNECTED = 0, |
|||
SCAN_READY, |
|||
CONNECTING, |
|||
CONNECTED, |
|||
IN_AP_MODE, |
|||
GOT_IP, |
|||
IN_STA_MODE, |
|||
RESET |
|||
} WiFiStatus_t; |
|||
|
|||
void setupWifi(bool startAP); |
|||
void setupAp(void); |
|||
void sendNTPpacket(IPAddress& address); |
|||
void sortRSSI(int *sort, int n); |
|||
bool getBSSIDs(void); |
|||
void connectionEvent(WiFiStatus_t status); |
|||
bool isTimeout(uint8_t timeout) { return (mCnt % timeout) == 0; } |
|||
|
|||
#if defined(ESP8266) |
|||
void onConnect(const WiFiEventStationModeConnected& event); |
|||
void onGotIP(const WiFiEventStationModeGotIP& event); |
|||
void onDisconnect(const WiFiEventStationModeDisconnected& event); |
|||
#else |
|||
void onWiFiEvent(WiFiEvent_t event); |
|||
#endif |
|||
void welcome(String ip, String mode); |
|||
|
|||
|
|||
settings_t *mConfig = nullptr; |
|||
appWifiCb mAppWifiCb; |
|||
|
|||
DNSServer mDns; |
|||
IPAddress mApIp; |
|||
WiFiUDP mUdp; // for time server
|
|||
#if defined(ESP8266) |
|||
WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler; |
|||
#endif |
|||
|
|||
WiFiStatus_t mStaConn = DISCONNECTED; |
|||
uint8_t mCnt = 0; |
|||
uint32_t *mUtcTimestamp = nullptr; |
|||
|
|||
uint8_t mScanCnt = 0; |
|||
bool mScanActive = false; |
|||
bool mGotDisconnect = false; |
|||
std::list<uint8_t> mBSSIDList; |
|||
bool mStopApAllowed = false; |
|||
bool mWasInCh12to14 = false; |
|||
}; |
|||
|
|||
#endif /*__AHOYWIFI_H__*/ |
|||
#endif /* !defined(ETHERNET) */ |
@ -0,0 +1,466 @@ |
|||
[ |
|||
{ |
|||
"id": "67bced2c4e728783", |
|||
"type": "mqtt in", |
|||
"z": "5de5756d190f9086", |
|||
"name": "", |
|||
"topic": "hoymiles/+", |
|||
"qos": "0", |
|||
"datatype": "auto-detect", |
|||
"broker": "319864a4e0fd913f", |
|||
"nl": false, |
|||
"rap": true, |
|||
"rh": 0, |
|||
"inputs": 0, |
|||
"x": 80, |
|||
"y": 2100, |
|||
"wires": [ |
|||
[ |
|||
"a55632ad0dff0b69" |
|||
] |
|||
] |
|||
}, |
|||
{ |
|||
"id": "a7f0d307d7cf77e2", |
|||
"type": "mqtt in", |
|||
"z": "5de5756d190f9086", |
|||
"name": "", |
|||
"topic": "hoymiles/X/#", |
|||
"qos": "0", |
|||
"datatype": "auto-detect", |
|||
"broker": "319864a4e0fd913f", |
|||
"nl": false, |
|||
"rap": true, |
|||
"rh": 0, |
|||
"inputs": 0, |
|||
"x": 90, |
|||
"y": 2260, |
|||
"wires": [ |
|||
[ |
|||
"7e17e5a3f4df3011", |
|||
"1a8cca488d53394a" |
|||
] |
|||
] |
|||
}, |
|||
{ |
|||
"id": "7e17e5a3f4df3011", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "Inverter X", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 340, |
|||
"y": 2260, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "fb7357db50501627", |
|||
"type": "change", |
|||
"z": "5de5756d190f9086", |
|||
"name": "Tags setzen", |
|||
"rules": [ |
|||
{ |
|||
"t": "set", |
|||
"p": "payload", |
|||
"pt": "msg", |
|||
"to": "(\t $a := $split(topic, '/');\t [\t payload,\t {\t \"device\":$a[0],\t \"name\":$a[1],\t \"channel\":$a[2]\t }\t ]\t)\t", |
|||
"tot": "jsonata" |
|||
}, |
|||
{ |
|||
"t": "delete", |
|||
"p": "topic", |
|||
"pt": "msg" |
|||
} |
|||
], |
|||
"action": "", |
|||
"property": "", |
|||
"from": "", |
|||
"to": "", |
|||
"reg": false, |
|||
"x": 610, |
|||
"y": 2360, |
|||
"wires": [ |
|||
[ |
|||
"91a4607dfda84b67" |
|||
] |
|||
] |
|||
}, |
|||
{ |
|||
"id": "670eb9fbb5c31b2c", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "InfluxDB", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 940, |
|||
"y": 2360, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "1a8cca488d53394a", |
|||
"type": "switch", |
|||
"z": "5de5756d190f9086", |
|||
"name": "", |
|||
"property": "$split(topic, '/')[2]", |
|||
"propertyType": "jsonata", |
|||
"rules": [ |
|||
{ |
|||
"t": "eq", |
|||
"v": "available", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "last_success", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "regex", |
|||
"v": "(ch[0-6])\\b", |
|||
"vt": "str", |
|||
"case": false |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "radio_stat", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "firmware", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "hardware", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "alarm", |
|||
"vt": "str" |
|||
} |
|||
], |
|||
"checkall": "true", |
|||
"repair": false, |
|||
"outputs": 7, |
|||
"x": 330, |
|||
"y": 2380, |
|||
"wires": [ |
|||
[ |
|||
"845aeb93e39092c5" |
|||
], |
|||
[ |
|||
"241a8e70e9fde93c" |
|||
], |
|||
[ |
|||
"fb7357db50501627" |
|||
], |
|||
[ |
|||
"9d38f021308664c1" |
|||
], |
|||
[ |
|||
"a508355f0cc87966" |
|||
], |
|||
[ |
|||
"d2c9aa1a8978aca6" |
|||
], |
|||
[ |
|||
"b27032beb597d5a7" |
|||
] |
|||
] |
|||
}, |
|||
{ |
|||
"id": "845aeb93e39092c5", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "available", |
|||
"active": true, |
|||
"tosidebar": false, |
|||
"console": false, |
|||
"tostatus": true, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "payload", |
|||
"statusType": "auto", |
|||
"x": 600, |
|||
"y": 2240, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "241a8e70e9fde93c", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "last_success", |
|||
"active": true, |
|||
"tosidebar": false, |
|||
"console": false, |
|||
"tostatus": true, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "payload", |
|||
"statusType": "auto", |
|||
"x": 610, |
|||
"y": 2300, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "9d38f021308664c1", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "radio_stat", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 600, |
|||
"y": 2400, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "a508355f0cc87966", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "firmware", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 600, |
|||
"y": 2440, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "d2c9aa1a8978aca6", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "hardware", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 600, |
|||
"y": 2480, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "b27032beb597d5a7", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "alarm", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 590, |
|||
"y": 2520, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "d814738cf55ad663", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "total", |
|||
"active": false, |
|||
"tosidebar": true, |
|||
"console": false, |
|||
"tostatus": false, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "", |
|||
"statusType": "auto", |
|||
"x": 590, |
|||
"y": 2160, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "a55632ad0dff0b69", |
|||
"type": "switch", |
|||
"z": "5de5756d190f9086", |
|||
"name": "", |
|||
"property": "$split(topic, '/')[1]", |
|||
"propertyType": "jsonata", |
|||
"rules": [ |
|||
{ |
|||
"t": "eq", |
|||
"v": "uptime", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "wifi_rssi", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "status", |
|||
"vt": "str" |
|||
}, |
|||
{ |
|||
"t": "eq", |
|||
"v": "total", |
|||
"vt": "str" |
|||
} |
|||
], |
|||
"checkall": "true", |
|||
"repair": false, |
|||
"outputs": 4, |
|||
"x": 330, |
|||
"y": 2100, |
|||
"wires": [ |
|||
[ |
|||
"1fbb0674d2576ee7" |
|||
], |
|||
[ |
|||
"e6be1c98ac55f511" |
|||
], |
|||
[ |
|||
"f9c2d3b30e34fdda" |
|||
], |
|||
[ |
|||
"d814738cf55ad663" |
|||
] |
|||
] |
|||
}, |
|||
{ |
|||
"id": "f9c2d3b30e34fdda", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "status", |
|||
"active": false, |
|||
"tosidebar": false, |
|||
"console": false, |
|||
"tostatus": true, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "payload", |
|||
"statusType": "auto", |
|||
"x": 590, |
|||
"y": 2100, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "e6be1c98ac55f511", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "wifi_rssi", |
|||
"active": false, |
|||
"tosidebar": false, |
|||
"console": false, |
|||
"tostatus": true, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "payload", |
|||
"statusType": "auto", |
|||
"x": 600, |
|||
"y": 2040, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "1fbb0674d2576ee7", |
|||
"type": "debug", |
|||
"z": "5de5756d190f9086", |
|||
"name": "uptime", |
|||
"active": false, |
|||
"tosidebar": false, |
|||
"console": false, |
|||
"tostatus": true, |
|||
"complete": "payload", |
|||
"targetType": "msg", |
|||
"statusVal": "payload", |
|||
"statusType": "auto", |
|||
"x": 590, |
|||
"y": 1980, |
|||
"wires": [] |
|||
}, |
|||
{ |
|||
"id": "91a4607dfda84b67", |
|||
"type": "change", |
|||
"z": "5de5756d190f9086", |
|||
"name": "Lösche", |
|||
"rules": [ |
|||
{ |
|||
"t": "delete", |
|||
"p": "payload[0].YieldDay", |
|||
"pt": "msg" |
|||
}, |
|||
{ |
|||
"t": "delete", |
|||
"p": "payload[0].MaxPower", |
|||
"pt": "msg" |
|||
}, |
|||
{ |
|||
"t": "delete", |
|||
"p": "payload[0].ALARM_MES_ID", |
|||
"pt": "msg" |
|||
} |
|||
], |
|||
"action": "", |
|||
"property": "", |
|||
"from": "", |
|||
"to": "", |
|||
"reg": false, |
|||
"x": 780, |
|||
"y": 2360, |
|||
"wires": [ |
|||
[ |
|||
"670eb9fbb5c31b2c" |
|||
] |
|||
] |
|||
}, |
|||
{ |
|||
"id": "319864a4e0fd913f", |
|||
"type": "mqtt-broker", |
|||
"name": "broker", |
|||
"broker": "localhost", |
|||
"port": "1883", |
|||
"clientid": "", |
|||
"autoConnect": true, |
|||
"usetls": false, |
|||
"protocolVersion": "4", |
|||
"keepalive": "60", |
|||
"cleansession": true, |
|||
"birthTopic": "", |
|||
"birthQos": "0", |
|||
"birthPayload": "", |
|||
"birthMsg": {}, |
|||
"closeTopic": "", |
|||
"closeQos": "0", |
|||
"closePayload": "", |
|||
"closeMsg": {}, |
|||
"willTopic": "", |
|||
"willQos": "0", |
|||
"willPayload": "", |
|||
"willMsg": {}, |
|||
"userProps": "", |
|||
"sessionExpiry": "" |
|||
} |
|||
] |
Loading…
Reference in new issue