Browse Source

Merge branch 'beegee3-develop03-devil' into development03

pull/658/head
lumapu 2 years ago
parent
commit
6ca215c929
  1. 69
      src/app.cpp
  2. 17
      src/app.h
  3. 1
      src/appInterface.h
  4. 3
      src/defines.h
  5. 161
      src/hm/CircularBuffer.h
  6. 313
      src/hm/hmRadio.h
  7. 16
      src/hm/hmSystem.h
  8. 20
      src/hm/payload.h
  9. 10
      src/main.cpp
  10. 40
      src/publisher/pubMqtt.h
  11. 27
      src/utils/ahoyTimer.h
  12. 7
      src/web/web.h
  13. 54
      src/wifi/ahoywifi.cpp
  14. 11
      src/wifi/ahoywifi.h

69
src/app.cpp

@ -82,40 +82,26 @@ void app::loop(void) {
void app::loopStandard(void) { void app::loopStandard(void) {
ah::Scheduler::loop(); ah::Scheduler::loop();
mSys->Radio.loop(); if (mSys->Radio.loop()) {
mPayload.loop(); while (!mSys->Radio.mBufCtrl.empty()) {
packet_t *p = &mSys->Radio.mBufCtrl.front();
yield();
if (ah::checkTicker(&mRxTicker, 4)) {
bool rxRdy = mSys->Radio.switchRxCh();
if (!mSys->BufCtrl.empty()) {
uint8_t len;
packet_t *p = mSys->BufCtrl.getBack();
if (mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) {
if (mConfig->serial.debug) { if (mConfig->serial.debug) {
DPRINT(DBG_INFO, "RX " + String(len) + "B Ch" + String(p->rxCh) + " | "); DPRINT(DBG_INFO, "RX " + String(p->len) + "B Ch" + String(p->ch) + " | ");
mSys->Radio.dumpBuf(NULL, p->packet, len); mSys->Radio.dumpBuf(p->packet, p->len);
} }
mStat.frmCnt++; mStat.frmCnt++;
if (0 != len) mPayload.add(p);
mPayload.add(p, len); mSys->Radio.mBufCtrl.pop();
}
mSys->BufCtrl.popBack();
}
yield(); yield();
}
if (rxRdy)
mPayload.process(true); mPayload.process(true);
} }
mPayload.loop();
#if !defined(AP_ONLY)
if(mMqttEnabled) if(mMqttEnabled)
mMqtt.loop(); mMqtt.loop();
#endif
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -131,13 +117,15 @@ void app::onWifi(bool gotIp) {
regularTickers(); // reinstall regular tickers regularTickers(); // reinstall regular tickers
if (gotIp) { if (gotIp) {
mInnerLoopCb = std::bind(&app::loopStandard, this); mInnerLoopCb = std::bind(&app::loopStandard, this);
mSendTickerId = every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend"); every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend");
mMqttReconnect = true; mMqttReconnect = true;
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2");
if(WIFI_AP == WiFi.getMode()) if(WIFI_AP == WiFi.getMode()) {
mMqttEnabled = false;
everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL"); everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL");
} }
}
else { else {
mInnerLoopCb = std::bind(&app::loopWifi, this); mInnerLoopCb = std::bind(&app::loopWifi, this);
everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL"); everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL");
@ -232,6 +220,15 @@ void app::tickComm(void) {
once(std::bind(&app::tickComm, this), 1, "mqCom"); // MQTT not connected, retry once(std::bind(&app::tickComm, this), 1, "mqCom"); // MQTT not connected, retry
} }
//-----------------------------------------------------------------------------
void app::tickMidnight(void) {
// only used and enabled by MQTT (see setup())
uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight
onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2");
mMqtt.tickerMidnight();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::tickSend(void) { void app::tickSend(void) {
if(!mSys->Radio.isChipConnected()) { if(!mSys->Radio.isChipConnected()) {
@ -239,9 +236,9 @@ void app::tickSend(void) {
return; return;
} }
if (mIVCommunicationOn) { if (mIVCommunicationOn) {
if (!mSys->BufCtrl.empty()) { if (!mSys->Radio.mBufCtrl.empty()) {
if (mConfig->serial.debug) if (mConfig->serial.debug)
DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys->BufCtrl.getFill())); DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys->Radio.mBufCtrl.size()));
} }
int8_t maxLoop = MAX_NUM_INVERTERS; int8_t maxLoop = MAX_NUM_INVERTERS;
@ -264,21 +261,6 @@ void app::tickSend(void) {
updateLed(); updateLed();
} }
//-----------------------------------------------------------------------------
void app::tickMidnight(void) {
// only used and enabled by MQTT (see setup())
uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight
onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2");
mMqtt.tickerMidnight();
}
//-----------------------------------------------------------------------------
void app::handleIntr(void) {
DPRINTLN(DBG_VERBOSE, F("app::handleIntr"));
mSys->Radio.handleIntr();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::resetSystem(void) { void app::resetSystem(void) {
snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
@ -290,8 +272,7 @@ void app::resetSystem(void) {
mSunrise = 0; mSunrise = 0;
mSunset = 0; mSunset = 0;
mRxTicker = 0; mMqttEnabled = false;
mSendTickerId = 0xff; // invalid id
mSendLastIvId = 0; mSendLastIvId = 0;
mShowRebootRequest = false; mShowRebootRequest = false;

17
src/app.h

@ -18,10 +18,8 @@
#include "config/settings.h" #include "config/settings.h"
#include "defines.h" #include "defines.h"
#include "utils/crc.h" #include "utils/crc.h"
#include "utils/ahoyTimer.h"
#include "utils/scheduler.h" #include "utils/scheduler.h"
#include "hm/CircularBuffer.h"
#include "hm/hmSystem.h" #include "hm/hmSystem.h"
#include "hm/payload.h" #include "hm/payload.h"
#include "wifi/ahoywifi.h" #include "wifi/ahoywifi.h"
@ -61,10 +59,6 @@ class app : public IApp, public ah::Scheduler {
void loopWifi(void); void loopWifi(void);
void onWifi(bool gotIp); void onWifi(bool gotIp);
void regularTickers(void); void regularTickers(void);
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
bool getWifiApActive(void);
uint32_t getUptime() { uint32_t getUptime() {
return Scheduler::getUptime(); return Scheduler::getUptime();
@ -99,6 +93,10 @@ class app : public IApp, public ah::Scheduler {
mWifi.getAvailNetworks(obj); mWifi.getAvailNetworks(obj);
} }
void setOnUpdate() {
onWifi(false);
}
void setRebootFlag() { void setRebootFlag() {
once(std::bind(&app::tickReboot, this), 3, "rboot"); once(std::bind(&app::tickReboot, this), 3, "rboot");
} }
@ -206,6 +204,9 @@ class app : public IApp, public ah::Scheduler {
void tickReboot(void) { void tickReboot(void) {
DPRINTLN(DBG_INFO, F("Rebooting...")); DPRINTLN(DBG_INFO, F("Rebooting..."));
onWifi(false);
ah::Scheduler::resetTicker();
WiFi.disconnect();
ESP.restart(); ESP.restart();
} }
@ -247,13 +248,9 @@ class app : public IApp, public ah::Scheduler {
settings_t *mConfig; settings_t *mConfig;
uint8_t mSendLastIvId; uint8_t mSendLastIvId;
uint8_t mSendTickerId;
statistics_t mStat; statistics_t mStat;
// timer
uint32_t mRxTicker;
// mqtt // mqtt
PubMqttType mMqtt; PubMqttType mMqtt;
bool mMqttReconnect; bool mMqttReconnect;

1
src/appInterface.h

@ -17,6 +17,7 @@ class IApp {
virtual bool saveSettings() = 0; virtual bool saveSettings() = 0;
virtual bool readSettings(const char *path) = 0; virtual bool readSettings(const char *path) = 0;
virtual bool eraseSettings(bool eraseWifi) = 0; virtual bool eraseSettings(bool eraseWifi) = 0;
virtual void setOnUpdate() = 0;
virtual void setRebootFlag() = 0; virtual void setRebootFlag() = 0;
virtual const char *getVersion() = 0; virtual const char *getVersion() = 0;
virtual statistics_t *getStatistics() = 0; virtual statistics_t *getStatistics() = 0;

3
src/defines.h

@ -17,7 +17,8 @@
//------------------------------------- //-------------------------------------
typedef struct { typedef struct {
uint8_t rxCh; uint8_t ch;
uint8_t len;
uint8_t packet[MAX_RF_PAYLOAD_SIZE]; uint8_t packet[MAX_RF_PAYLOAD_SIZE];
} packet_t; } packet_t;

161
src/hm/CircularBuffer.h

@ -1,161 +0,0 @@
/*
CircularBuffer - An Arduino circular buffering library for arbitrary types.
Created by Ivo Pullens, Emmission, 2014 -- www.emmission.nl
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CircularBuffer_h
#define CircularBuffer_h
#if defined(ESP8266) || defined(ESP32)
#define DISABLE_IRQ noInterrupts()
#define RESTORE_IRQ interrupts()
#else
#define DISABLE_IRQ \
uint8_t sreg = SREG; \
cli();
#define RESTORE_IRQ \
SREG = sreg;
#endif
template <class BUFFERTYPE, uint8_t BUFFERSIZE>
class CircularBuffer {
typedef BUFFERTYPE BufferType;
BufferType Buffer[BUFFERSIZE];
public:
CircularBuffer() : m_buff(Buffer) {
m_size = BUFFERSIZE;
clear();
}
/** Clear all entries in the circular buffer. */
void clear(void)
{
m_front = 0;
m_fill = 0;
}
/** Test if the circular buffer is empty */
inline bool empty(void) const
{
return !m_fill;
}
/** Return the number of records stored in the buffer */
inline uint8_t available(void) const
{
return m_fill;
}
/** Test if the circular buffer is full */
inline bool full(void) const
{
return m_fill == m_size;
}
inline uint8_t getFill(void) const {
return m_fill;
}
/** Aquire record on front of the buffer, for writing.
* After filling the record, it has to be pushed to actually
* add it to the buffer.
* @return Pointer to record, or NULL when buffer is full.
*/
BUFFERTYPE* getFront(void) const
{
DISABLE_IRQ;
BUFFERTYPE* f = NULL;
if (!full())
f = get(m_front);
RESTORE_IRQ;
return f;
}
/** Push record to front of the buffer
* @param record Record to push. If record was aquired previously (using getFront) its
* data will not be copied as it is already present in the buffer.
* @return True, when record was pushed successfully.
*/
bool pushFront(BUFFERTYPE* record)
{
bool ok = false;
DISABLE_IRQ;
if (!full())
{
BUFFERTYPE* f = get(m_front);
if (f != record)
*f = *record;
m_front = (m_front+1) % m_size;
m_fill++;
ok = true;
}
RESTORE_IRQ;
return ok;
}
/** Aquire record on back of the buffer, for reading.
* After reading the record, it has to be pop'ed to actually
* remove it from the buffer.
* @return Pointer to record, or NULL when buffer is empty.
*/
BUFFERTYPE* getBack(void) const
{
BUFFERTYPE* b = NULL;
DISABLE_IRQ;
if (!empty())
b = get(back());
RESTORE_IRQ;
return b;
}
/** Remove record from back of the buffer.
* @return True, when record was pop'ed successfully.
*/
bool popBack(void)
{
bool ok = false;
DISABLE_IRQ;
if (!empty())
{
m_fill--;
ok = true;
}
RESTORE_IRQ;
return ok;
}
protected:
inline BUFFERTYPE * get(const uint8_t idx) const
{
return &(m_buff[idx]);
}
inline uint8_t back(void) const
{
return (m_front - m_fill + m_size) % m_size;
}
uint8_t m_size; // Total number of records that can be stored in the buffer.
BUFFERTYPE* const m_buff;
volatile uint8_t m_front; // Index of front element (not pushed yet).
volatile uint8_t m_fill; // Amount of records currently pushed.
};
#endif // CircularBuffer_h

313
src/hm/hmRadio.h

@ -9,28 +9,10 @@
#include "../utils/dbg.h" #include "../utils/dbg.h"
#include <RF24.h> #include <RF24.h>
#include "../utils/crc.h" #include "../utils/crc.h"
#ifndef DISABLE_IRQ
#if defined(ESP8266) || defined(ESP32)
#define DISABLE_IRQ noInterrupts()
#define RESTORE_IRQ interrupts()
#else
#define DISABLE_IRQ \
uint8_t sreg = SREG; \
cli();
#define RESTORE_IRQ \
SREG = sreg;
#endif
#endif
//#define CHANNEL_HOP // switch between channels or use static channel to send
#define DEFAULT_RECV_CHANNEL 3
#define SPI_SPEED 1000000 #define SPI_SPEED 1000000
#define DUMMY_RADIO_ID ((uint64_t)0xDEADBEEF01ULL)
#define RF_CHANNELS 5 #define RF_CHANNELS 5
#define RF_LOOP_CNT 300
#define TX_REQ_INFO 0x15 #define TX_REQ_INFO 0x15
#define TX_REQ_DEVCONTROL 0x51 #define TX_REQ_DEVCONTROL 0x51
@ -61,11 +43,12 @@ const char* const rf24AmpPowerNames[] = {"MIN", "LOW", "HIGH", "MAX"};
#define BIT_CNT(x) ((x)<<3) #define BIT_CNT(x) ((x)<<3)
static volatile bool mIrqRcvd;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// HM Radio class // HM Radio class
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <class BUFFER, uint8_t IRQ_PIN = DEF_IRQ_PIN, uint8_t CE_PIN = DEF_CE_PIN, uint8_t CS_PIN = DEF_CS_PIN, uint8_t AMP_PWR = RF24_PA_LOW> template <uint8_t IRQ_PIN = DEF_IRQ_PIN, uint8_t CE_PIN = DEF_CE_PIN, uint8_t CS_PIN = DEF_CS_PIN, uint8_t AMP_PWR = RF24_PA_LOW>
class HmRadio { class HmRadio {
public: public:
HmRadio() : mNrf24(CE_PIN, CS_PIN, SPI_SPEED) { HmRadio() : mNrf24(CE_PIN, CS_PIN, SPI_SPEED) {
@ -84,9 +67,9 @@ class HmRadio {
mRfChLst[3] = 61; mRfChLst[3] = 61;
mRfChLst[4] = 75; mRfChLst[4] = 75;
// default channels
mTxChIdx = 2; // Start TX with 40 mTxChIdx = 2; // Start TX with 40
mRxChIdx = 0; // Start RX with 03 mRxChIdx = 0; // Start RX with 03
mRxLoopCnt = RF_LOOP_CNT;
mSendCnt = 0; mSendCnt = 0;
mRetransmits = 0; mRetransmits = 0;
@ -96,11 +79,10 @@ class HmRadio {
} }
~HmRadio() {} ~HmRadio() {}
void setup(BUFFER *ctrl, uint8_t ampPwr = RF24_PA_LOW, uint8_t irq = IRQ_PIN, uint8_t ce = CE_PIN, uint8_t cs = CS_PIN) { void setup(uint8_t ampPwr = RF24_PA_LOW, uint8_t irq = IRQ_PIN, uint8_t ce = CE_PIN, uint8_t cs = CS_PIN) {
DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup")); DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup"));
pinMode(irq, INPUT_PULLUP); pinMode(irq, INPUT_PULLUP);
mBufCtrl = ctrl; attachInterrupt(digitalPinToInterrupt(irq), []()IRAM_ATTR{ mIrqRcvd = true; }, FALLING);
uint32_t dtuSn = 0x87654321; uint32_t dtuSn = 0x87654321;
uint32_t chipID = 0; // will be filled with last 3 bytes of MAC uint32_t chipID = 0; // will be filled with last 3 bytes of MAC
@ -121,27 +103,22 @@ class HmRadio {
DTU_RADIO_ID = ((uint64_t)(((dtuSn >> 24) & 0xFF) | ((dtuSn >> 8) & 0xFF00) | ((dtuSn << 8) & 0xFF0000) | ((dtuSn << 24) & 0xFF000000)) << 8) | 0x01; DTU_RADIO_ID = ((uint64_t)(((dtuSn >> 24) & 0xFF) | ((dtuSn >> 8) & 0xFF00) | ((dtuSn << 8) & 0xFF0000) | ((dtuSn << 24) & 0xFF000000)) << 8) | 0x01;
mNrf24.begin(ce, cs); mNrf24.begin(ce, cs);
mNrf24.setRetries(0, 0); mNrf24.setRetries(3, 15); // 3*250us + 250us and 15 loops -> 15ms
mNrf24.setChannel(DEFAULT_RECV_CHANNEL); mNrf24.setChannel(mRfChLst[mRxChIdx]);
mNrf24.setDataRate(RF24_250KBPS); mNrf24.setDataRate(RF24_250KBPS);
mNrf24.setAutoAck(true);
mNrf24.enableDynamicPayloads();
mNrf24.setCRCLength(RF24_CRC_16); mNrf24.setCRCLength(RF24_CRC_16);
mNrf24.setAutoAck(false);
mNrf24.setPayloadSize(MAX_RF_PAYLOAD_SIZE);
mNrf24.setAddressWidth(5); mNrf24.setAddressWidth(5);
mNrf24.openReadingPipe(1, DTU_RADIO_ID); mNrf24.openReadingPipe(1, DTU_RADIO_ID);
mNrf24.enableDynamicPayloads();
// enable only receiving interrupts // enable all receiving interrupts
mNrf24.maskIRQ(true, true, false); mNrf24.maskIRQ(false, false, false);
DPRINT(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_")); DPRINT(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_"));
DPRINTLN(DBG_INFO, String(rf24AmpPowerNames[ampPwr])); DPRINTLN(DBG_INFO, String(rf24AmpPowerNames[ampPwr]));
mNrf24.setPALevel(ampPwr & 0x03); mNrf24.setPALevel(ampPwr & 0x03);
mNrf24.startListening();
mTxCh = setDefaultChannels();
if(mNrf24.isChipConnected()) { if(mNrf24.isChipConnected()) {
DPRINTLN(DBG_INFO, F("Radio Config:")); DPRINTLN(DBG_INFO, F("Radio Config:"));
@ -151,77 +128,70 @@ class HmRadio {
DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring")); DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));
} }
void loop(void) { bool loop(void) {
if(mIrqRcvd) { if (!mIrqRcvd)
DISABLE_IRQ; return false; // nothing to do
mIrqRcvd = false; mIrqRcvd = false;
bool tx_ok, tx_fail, rx_ready; bool tx_ok, tx_fail, rx_ready;
mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH
RESTORE_IRQ; mNrf24.flush_tx(); // empty TX FIFO
uint8_t pipe, len; //DBGPRINTLN("TX whatHappened Ch" + String(mRfChLst[mTxChIdx]) + " " + String(tx_ok) + String(tx_fail) + String(rx_ready));
packet_t *p;
while(mNrf24.available(&pipe)) { // start listening on the default RX channel
if(!mBufCtrl->full()) { mRxChIdx = 0;
p = mBufCtrl->getFront(); mNrf24.setChannel(mRfChLst[mRxChIdx]);
p->rxCh = mRfChLst[mRxChIdx]; mNrf24.startListening();
len = mNrf24.getPayloadSize();
if(len > MAX_RF_PAYLOAD_SIZE) //uint32_t debug_ms = millis();
len = MAX_RF_PAYLOAD_SIZE; uint16_t cnt = 300; // that is 60 times 5 channels
while (0 < cnt--) {
mNrf24.read(p->packet, len); uint32_t startMillis = millis();
mBufCtrl->pushFront(p); while (millis()-startMillis < 4) { // listen 4ms to each channel
yield(); if (mIrqRcvd) {
mIrqRcvd = false;
if (getReceived()) { // everything received
//DBGPRINTLN("RX finished Cnt: " + String(300-cnt) + " time used: " + String(millis()-debug_ms)+ " ms");
mNrf24.stopListening();
return true;
} }
else
break;
} }
mNrf24.flush_rx(); // drop the packet yield();
RESTORE_IRQ;
} }
switchRxCh(); // switch to next RX channel
yield();
} }
// not finished but time is over
void enableDebug() { //DBGPRINTLN("RX not finished: 300 time used: " + String(millis()-debug_ms)+ " ms");
mSerialDebug = true; mNrf24.stopListening();
return true;
} }
void handleIntr(void) { bool isChipConnected(void) {
mIrqRcvd = true; //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected"));
return mNrf24.isChipConnected();
} }
void enableDebug() {
uint8_t setDefaultChannels(void) { mSerialDebug = true;
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setDefaultChannels"));
mTxChIdx = 2; // Start TX with 40
mRxChIdx = 0; // Start RX with 03
return mRfChLst[mTxChIdx];
} }
void sendControlPacket(uint64_t invId, uint8_t cmd, uint16_t *data, bool isRetransmit) { void sendControlPacket(uint64_t invId, uint8_t cmd, uint16_t *data, bool isRetransmit) {
DPRINTLN(DBG_INFO, F("sendControlPacket cmd: 0x") + String(cmd, HEX)); DPRINTLN(DBG_INFO, F("sendControlPacket cmd: 0x") + String(cmd, HEX));
sendCmdPacket(invId, TX_REQ_DEVCONTROL, SINGLE_FRAME, false); initPacket(invId, TX_REQ_DEVCONTROL, SINGLE_FRAME);
uint8_t cnt = 0; uint8_t cnt = 10;
mTxBuf[10 + cnt++] = cmd; // cmd -> 0 on, 1 off, 2 restart, 11 active power, 12 reactive power, 13 power factor mTxBuf[cnt++] = cmd; // cmd -> 0 on, 1 off, 2 restart, 11 active power, 12 reactive power, 13 power factor
mTxBuf[10 + cnt++] = 0x00; mTxBuf[cnt++] = 0x00;
if(cmd >= ActivePowerContr && cmd <= PFSet) { // ActivePowerContr, ReactivePowerContr, PFSet if(cmd >= ActivePowerContr && cmd <= PFSet) { // ActivePowerContr, ReactivePowerContr, PFSet
mTxBuf[10 + cnt++] = ((data[0] * 10) >> 8) & 0xff; // power limit mTxBuf[cnt++] = ((data[0] * 10) >> 8) & 0xff; // power limit
mTxBuf[10 + cnt++] = ((data[0] * 10) ) & 0xff; // power limit mTxBuf[cnt++] = ((data[0] * 10) ) & 0xff; // power limit
mTxBuf[10 + cnt++] = ((data[1] ) >> 8) & 0xff; // setting for persistens handlings mTxBuf[cnt++] = ((data[1] ) >> 8) & 0xff; // setting for persistens handlings
mTxBuf[10 + cnt++] = ((data[1] ) ) & 0xff; // setting for persistens handling mTxBuf[cnt++] = ((data[1] ) ) & 0xff; // setting for persistens handling
} }
sendPacket(invId, cnt, isRetransmit, true);
// crc control data
uint16_t crc = ah::crc16(&mTxBuf[10], cnt);
mTxBuf[10 + cnt++] = (crc >> 8) & 0xff;
mTxBuf[10 + cnt++] = (crc ) & 0xff;
// crc over all
mTxBuf[10 + cnt] = ah::crc8(mTxBuf, 10 + cnt);
sendPacket(invId, mTxBuf, 10 + cnt + 1, isRetransmit, true);
} }
void sendTimePacket(uint64_t invId, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit) { void sendTimePacket(uint64_t invId, uint8_t cmd, uint32_t ts, uint16_t alarmMesId, bool isRetransmit) {
DPRINTLN(DBG_DEBUG, F("sendTimePacket 0x") + String(cmd, HEX)); DPRINTLN(DBG_DEBUG, F("sendTimePacket 0x") + String(cmd, HEX));
sendCmdPacket(invId, TX_REQ_INFO, ALL_FRAMES, isRetransmit, false); initPacket(invId, TX_REQ_INFO, ALL_FRAMES);
mTxBuf[10] = cmd; // cid mTxBuf[10] = cmd; // cid
mTxBuf[11] = 0x00; mTxBuf[11] = 0x00;
CP_U32_LittleEndian(&mTxBuf[12], ts); CP_U32_LittleEndian(&mTxBuf[12], ts);
@ -229,61 +199,16 @@ class HmRadio {
mTxBuf[18] = (alarmMesId >> 8) & 0xff; mTxBuf[18] = (alarmMesId >> 8) & 0xff;
mTxBuf[19] = (alarmMesId ) & 0xff; mTxBuf[19] = (alarmMesId ) & 0xff;
} }
uint16_t crc = ah::crc16(&mTxBuf[10], 14); sendPacket(invId, 24, isRetransmit, true);
mTxBuf[24] = (crc >> 8) & 0xff;
mTxBuf[25] = (crc ) & 0xff;
mTxBuf[26] = ah::crc8(mTxBuf, 26);
sendPacket(invId, mTxBuf, 27, isRetransmit, true);
}
void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool isRetransmit, bool calcCrc = true) {
DPRINTLN(DBG_VERBOSE, F("sendCmdPacket, mid: ") + String(mid, HEX) + F(" pid: ") + String(pid, HEX));
memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE);
mTxBuf[0] = mid; // message id
CP_U32_BigEndian(&mTxBuf[1], (invId >> 8));
CP_U32_BigEndian(&mTxBuf[5], (DTU_RADIO_ID >> 8));
mTxBuf[9] = pid;
if(calcCrc) {
mTxBuf[10] = ah::crc8(mTxBuf, 10);
sendPacket(invId, mTxBuf, 11, isRetransmit, false);
}
} }
bool checkPaketCrc(uint8_t buf[], uint8_t *len, uint8_t rxCh) { void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool isRetransmit) {
//DPRINTLN(DBG_INFO, F("hmRadio.h:checkPaketCrc")); initPacket(invId, mid, pid);
*len = (buf[0] >> 2); sendPacket(invId, 10, isRetransmit, false);
if(*len > (MAX_RF_PAYLOAD_SIZE - 2))
*len = MAX_RF_PAYLOAD_SIZE - 2;
for(uint8_t i = 1; i < (*len + 1); i++) {
buf[i-1] = (buf[i] << 1) | (buf[i+1] >> 7);
}
uint8_t crc = ah::crc8(buf, *len-1);
bool valid = (crc == buf[*len-1]);
return valid;
}
bool switchRxCh(uint16_t addLoop = 0) {
if(!mNrf24.isChipConnected())
return true;
mRxLoopCnt += addLoop;
if(mRxLoopCnt != 0) {
mRxLoopCnt--;
DISABLE_IRQ;
mNrf24.stopListening();
mNrf24.setChannel(getRxNxtChannel());
mNrf24.startListening();
RESTORE_IRQ;
}
return (0 == mRxLoopCnt); // receive finished
} }
void dumpBuf(const char *info, uint8_t buf[], uint8_t len) { void dumpBuf(uint8_t buf[], uint8_t len) {
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:dumpBuf")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:dumpBuf"));
if(NULL != info)
DBGPRINT(String(info));
for(uint8_t i = 0; i < len; i++) { for(uint8_t i = 0; i < len; i++) {
DHEX(buf[i]); DHEX(buf[i]);
DBGPRINT(" "); DBGPRINT(" ");
@ -291,11 +216,6 @@ class HmRadio {
DBGPRINTLN(""); DBGPRINTLN("");
} }
bool isChipConnected(void) {
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected"));
return mNrf24.isChipConnected();
}
uint8_t getDataRate(void) { uint8_t getDataRate(void) {
if(!mNrf24.isChipConnected()) if(!mNrf24.isChipConnected())
return 3; // unkown return 3; // unkown
@ -306,7 +226,7 @@ class HmRadio {
return mNrf24.isPVariant(); return mNrf24.isPVariant();
} }
std::queue<packet_t> mBufCtrl;
uint32_t mSendCnt; uint32_t mSendCnt;
uint32_t mRetransmits; uint32_t mRetransmits;
@ -314,78 +234,91 @@ class HmRadio {
bool mSerialDebug; bool mSerialDebug;
private: private:
void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len, bool isRetransmit, bool clear=false) { bool getReceived(void) {
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket")); bool tx_ok, tx_fail, rx_ready;
//DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt)); mNrf24.whatHappened(tx_ok, tx_fail, rx_ready); // resets the IRQ pin to HIGH
//dumpBuf("SEN ", buf, len); DBGPRINTLN("RX whatHappened Ch" + String(mRfChLst[mRxChIdx]) + " " + String(tx_ok) + String(tx_fail) + String(rx_ready));
if(mSerialDebug) {
DPRINT(DBG_INFO, "TX " + String(len) + "B Ch" + String(mRfChLst[mTxChIdx]) + " | "); bool isLastPackage = false;
dumpBuf(NULL, buf, len); while(mNrf24.available()) {
uint8_t len;
len = mNrf24.getDynamicPayloadSize(); // if payload size > 32, corrupt payload has been flushed
if (len > 0) {
packet_t p;
p.ch = mRfChLst[mRxChIdx];
p.len = len;
mNrf24.read(p.packet, len);
mBufCtrl.push(p);
if (p.packet[0] == (TX_REQ_INFO + ALL_FRAMES)) // response from get information command
isLastPackage = (p.packet[9] > 0x81); // > 0x81 indicates last packet received
else if (p.packet[0] != 0x00) // ignore fragment number zero
isLastPackage = true; // response from dev control command
yield();
}
}
return isLastPackage;
} }
DISABLE_IRQ; void switchRxCh() {
mNrf24.stopListening(); mNrf24.stopListening();
// get next channel index
if(clear) if(++mRxChIdx >= RF_CHANNELS)
mRxLoopCnt = RF_LOOP_CNT;
mNrf24.setChannel(mRfChLst[mTxChIdx]);
mTxCh = getTxNxtChannel(); // switch channel for next packet
mNrf24.openWritingPipe(invId); // TODO: deprecated
mNrf24.setCRCLength(RF24_CRC_16);
mNrf24.enableDynamicPayloads();
mNrf24.setAutoAck(true);
mNrf24.setRetries(3, 15); // 3*250us and 15 loops -> 11.25ms
mNrf24.write(buf, len);
// Try to avoid zero payload acks (has no effect)
mNrf24.openWritingPipe(DUMMY_RADIO_ID); // TODO: why dummy radio id?, deprecated
mRxChIdx = 0; mRxChIdx = 0;
mNrf24.setChannel(mRfChLst[mRxChIdx]); mNrf24.setChannel(mRfChLst[mRxChIdx]);
mNrf24.setAutoAck(false);
mNrf24.setRetries(0, 0);
mNrf24.disableDynamicPayloads();
mNrf24.setCRCLength(RF24_CRC_DISABLED);
mNrf24.startListening(); mNrf24.startListening();
}
RESTORE_IRQ; void initPacket(uint64_t invId, uint8_t mid, uint8_t pid) {
if(isRetransmit) DPRINTLN(DBG_VERBOSE, F("initPacket, mid: ") + String(mid, HEX) + F(" pid: ") + String(pid, HEX));
mRetransmits++; memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE);
else mTxBuf[0] = mid; // message id
mSendCnt++; CP_U32_BigEndian(&mTxBuf[1], (invId >> 8));
CP_U32_BigEndian(&mTxBuf[5], (DTU_RADIO_ID >> 8));
mTxBuf[9] = pid;
} }
uint8_t getTxNxtChannel(void) { void sendPacket(uint64_t invId, uint8_t len, bool isRetransmit, bool clear=false) {
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket"));
//DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt));
if(++mTxChIdx >= RF_CHANNELS) // append crc's
mTxChIdx = 0; if (len > 10) {
return mRfChLst[mTxChIdx]; // crc control data
uint16_t crc = ah::crc16(&mTxBuf[10], len - 10);
mTxBuf[len++] = (crc >> 8) & 0xff;
mTxBuf[len++] = (crc ) & 0xff;
} }
// crc over all
mTxBuf[len++] = ah::crc8(mTxBuf, len);
uint8_t getRxNxtChannel(void) { if(mSerialDebug) {
DPRINT(DBG_INFO, "TX " + String(len) + "B Ch" + String(mRfChLst[mTxChIdx]) + " | ");
dumpBuf(mTxBuf, len);
}
if(++mRxChIdx >= RF_CHANNELS) mNrf24.setChannel(mRfChLst[mTxChIdx]);
mRxChIdx = 0; mNrf24.openWritingPipe(reinterpret_cast<uint8_t*>(&invId));
return mRfChLst[mRxChIdx]; mNrf24.startWrite(mTxBuf, len, false); // false = request ACK response
// switch TX channel for next packet
if(++mTxChIdx >= RF_CHANNELS)
mTxChIdx = 0;
if(isRetransmit)
mRetransmits++;
else
mSendCnt++;
} }
uint64_t DTU_RADIO_ID; uint64_t DTU_RADIO_ID;
uint8_t mTxCh;
uint8_t mTxChIdx;
uint8_t mRfChLst[RF_CHANNELS]; uint8_t mRfChLst[RF_CHANNELS];
uint8_t mTxChIdx;
uint8_t mRxChIdx; uint8_t mRxChIdx;
uint16_t mRxLoopCnt;
RF24 mNrf24; RF24 mNrf24;
BUFFER *mBufCtrl;
uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE]; uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE];
DevControlCmdType DevControlCmd;
volatile bool mIrqRcvd;
}; };
#endif /*__RADIO_H__*/ #endif /*__RADIO_H__*/

16
src/hm/hmSystem.h

@ -8,19 +8,11 @@
#include "hmInverter.h" #include "hmInverter.h"
#include "hmRadio.h" #include "hmRadio.h"
#include "CircularBuffer.h"
typedef CircularBuffer<packet_t, PACKET_BUFFER_SIZE> BufferType; template <uint8_t MAX_INVERTER=3, class INVERTERTYPE=Inverter<float>>
typedef HmRadio<BufferType> RadioType;
template <uint8_t MAX_INVERTER=3, class RADIO = RadioType, class BUFFER = BufferType, class INVERTERTYPE=Inverter<float>>
class HmSystem { class HmSystem {
public: public:
typedef RADIO RadioType; HmRadio<> Radio;
RadioType Radio;
typedef BUFFER BufferType;
BufferType BufCtrl;
//DevControlCmdType DevControlCmd;
HmSystem() { HmSystem() {
mNumInv = 0; mNumInv = 0;
@ -30,11 +22,11 @@ class HmSystem {
} }
void setup() { void setup() {
Radio.setup(&BufCtrl); Radio.setup();
} }
void setup(uint8_t ampPwr, uint8_t irqPin, uint8_t cePin, uint8_t csPin) { void setup(uint8_t ampPwr, uint8_t irqPin, uint8_t cePin, uint8_t csPin) {
Radio.setup(&BufCtrl, ampPwr, irqPin, cePin, csPin); Radio.setup(ampPwr, irqPin, cePin, csPin);
} }
void addInverters(cfgInst_t *config) { void addInverters(cfgInst_t *config) {

20
src/hm/payload.h

@ -48,6 +48,7 @@ class Payload {
mSerialDebug = false; mSerialDebug = false;
mHighPrioIv = NULL; mHighPrioIv = NULL;
mCbAlarm = NULL; mCbAlarm = NULL;
mCbAlarm = NULL;
} }
void enableSerialDebug(bool enable) { void enableSerialDebug(bool enable) {
@ -118,7 +119,7 @@ class Payload {
} }
} }
void add(packet_t *p, uint8_t len) { void add(packet_t *p) {
Inverter<> *iv = mSys->findInverter(&p->packet[1]); Inverter<> *iv = mSys->findInverter(&p->packet[1]);
if(NULL == iv) if(NULL == iv)
@ -133,8 +134,8 @@ class Payload {
} else { } else {
DPRINTLN(DBG_DEBUG, "PID: 0x" + String(*pid, HEX)); DPRINTLN(DBG_DEBUG, "PID: 0x" + String(*pid, HEX));
if ((*pid & 0x7F) < MAX_PAYLOAD_ENTRIES) { if ((*pid & 0x7F) < MAX_PAYLOAD_ENTRIES) {
memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], len - 11); memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], p->len - 11);
mPayload[iv->id].len[(*pid & 0x7F) - 1] = len - 11; mPayload[iv->id].len[(*pid & 0x7F) - 1] = p->len - 11;
mPayload[iv->id].gotFragment = true; mPayload[iv->id].gotFragment = true;
} }
@ -195,21 +196,24 @@ class Payload {
if (mPayload[iv->id].retransmits < mMaxRetrans) { if (mPayload[iv->id].retransmits < mMaxRetrans) {
mPayload[iv->id].retransmits++; mPayload[iv->id].retransmits++;
if(false == mPayload[iv->id].gotFragment) { if(false == mPayload[iv->id].gotFragment) {
/*
DPRINTLN(DBG_WARN, F("nothing received: Request Complete Retransmit")); DPRINTLN(DBG_WARN, F("nothing received: Request Complete Retransmit"));
mPayload[iv->id].txCmd = iv->getQueuedCmd(); mPayload[iv->id].txCmd = iv->getQueuedCmd();
DPRINTLN(DBG_INFO, F("(#") + String(iv->id) + F(") sendTimePacket 0x") + String(mPayload[iv->id].txCmd, HEX)); DPRINTLN(DBG_INFO, F("(#") + String(iv->id) + F(") sendTimePacket 0x") + String(mPayload[iv->id].txCmd, HEX));
mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].txCmd, mPayload[iv->id].ts, iv->alarmMesIndex, true); mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].txCmd, mPayload[iv->id].ts, iv->alarmMesIndex, true);
*/
DPRINTLN(DBG_WARN, F("(#") + String(iv->id) + F(") nothing received"));
mPayload[iv->id].retransmits = mMaxRetrans;
} else { } else {
for (uint8_t i = 0; i < (mPayload[iv->id].maxPackId - 1); i++) { for (uint8_t i = 0; i < (mPayload[iv->id].maxPackId - 1); i++) {
if (mPayload[iv->id].len[i] == 0) { if (mPayload[iv->id].len[i] == 0) {
DPRINTLN(DBG_WARN, F("Frame ") + String(i + 1) + F(" missing: Request Retransmit")); DPRINTLN(DBG_WARN, F("Frame ") + String(i + 1) + F(" missing: Request Retransmit"));
mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, (SINGLE_FRAME + i), true, true); mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, (SINGLE_FRAME + i), true);
break; // only request retransmit one frame per loop break; // only request retransmit one frame per loop
} }
yield(); yield();
} }
} }
mSys->Radio.switchRxCh(100);
} }
} }
} }
@ -242,13 +246,13 @@ class Payload {
if (mSerialDebug) { if (mSerialDebug) {
DPRINT(DBG_INFO, F("Payload (") + String(payloadLen) + "): "); DPRINT(DBG_INFO, F("Payload (") + String(payloadLen) + "): ");
mSys->Radio.dumpBuf(NULL, payload, payloadLen); mSys->Radio.dumpBuf(payload, payloadLen);
} }
if (NULL == rec) { if (NULL == rec) {
DPRINTLN(DBG_ERROR, F("record is NULL!")); DPRINTLN(DBG_ERROR, F("record is NULL!"));
} else if ((rec->pyldLen == payloadLen) || (0 == rec->pyldLen)) { } else if ((rec->pyldLen == payloadLen) || (0 == rec->pyldLen)) {
if (mPayload[iv->id].txId == (TX_REQ_INFO + 0x80)) if (mPayload[iv->id].txId == (TX_REQ_INFO + ALL_FRAMES))
mStat->rxSuccess++; mStat->rxSuccess++;
rec->ts = mPayload[iv->id].ts; rec->ts = mPayload[iv->id].ts;
@ -280,9 +284,7 @@ class Payload {
iv->setQueuedCmdFinished(); iv->setQueuedCmdFinished();
} }
} }
yield(); yield();
} }
} }

10
src/main.cpp

@ -7,21 +7,11 @@
#include "app.h" #include "app.h"
#include "config/config.h" #include "config/config.h"
app myApp; app myApp;
//-----------------------------------------------------------------------------
IRAM_ATTR void handleIntr(void) {
myApp.handleIntr();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void setup() { void setup() {
myApp.setup(); myApp.setup();
// TODO: move to HmRadio
attachInterrupt(digitalPinToInterrupt(myApp.getIrqPin()), handleIntr, FALLING);
} }

40
src/publisher/pubMqtt.h

@ -15,7 +15,6 @@
#endif #endif
#include "../utils/dbg.h" #include "../utils/dbg.h"
#include "../utils/ahoyTimer.h"
#include "../config/config.h" #include "../config/config.h"
#include <espMqttClient.h> #include <espMqttClient.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -40,8 +39,7 @@ class PubMqtt {
mRxCnt = 0; mRxCnt = 0;
mTxCnt = 0; mTxCnt = 0;
mSubscriptionCb = NULL; mSubscriptionCb = NULL;
mIvAvail = true; memset(mLastIvState, MQTT_STATUS_NOT_AVAIL_NOT_PROD, MAX_NUM_INVERTERS);
memset(mLastIvState, 0xff, MAX_NUM_INVERTERS);
} }
~PubMqtt() { } ~PubMqtt() { }
@ -70,6 +68,7 @@ class PubMqtt {
void loop() { void loop() {
#if defined(ESP8266) #if defined(ESP8266)
mClient.loop(); mClient.loop();
yield();
#endif #endif
} }
@ -408,13 +407,12 @@ class PubMqtt {
bool processIvStatus() { bool processIvStatus() {
// returns true if all inverters are available // returns true if all inverters are available
bool allAvail = true; bool allAvail = true; // shows if all enabled inverters are available
bool first = true; bool anyAvail = false; // shows if at least one enabled inverter is available
bool changed = false; bool changed = false;
char topic[7 + MQTT_TOPIC_LEN], val[40]; char topic[7 + MQTT_TOPIC_LEN], val[40];
Inverter<> *iv; Inverter<> *iv;
record_t<> *rec; record_t<> *rec;
bool totalComplete = true;
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { for (uint8_t id = 0; id < mSys->getNumInverters(); id++) {
iv = mSys->getInverterByPos(id); iv = mSys->getInverterByPos(id);
@ -422,32 +420,21 @@ class PubMqtt {
continue; // skip to next inverter continue; // skip to next inverter
rec = iv->getRecordStruct(RealTimeRunData_Debug); rec = iv->getRecordStruct(RealTimeRunData_Debug);
if(first)
mIvAvail = false;
first = false;
// inverter status // inverter status
uint8_t status = MQTT_STATUS_AVAIL_PROD; uint8_t status = MQTT_STATUS_NOT_AVAIL_NOT_PROD;
if ((!iv->isAvailable(*mUtcTimestamp)) || (!iv->config->enabled)) { if (iv->config->enabled) {
status = MQTT_STATUS_NOT_AVAIL_NOT_PROD; if (iv->isAvailable(*mUtcTimestamp))
if(iv->config->enabled) { // only change all-avail if inverter is enabled! status = (iv->isProducing(*mUtcTimestamp)) ? MQTT_STATUS_AVAIL_PROD : MQTT_STATUS_AVAIL_NOT_PROD;
totalComplete = false; else // inverter is enabled but not available
allAvail = false; allAvail = false;
} }
}
else {
mIvAvail = true;
if (!iv->isProducing(*mUtcTimestamp)) {
if (MQTT_STATUS_AVAIL_PROD == status)
status = MQTT_STATUS_AVAIL_NOT_PROD;
}
}
if(mLastIvState[id] != status) { if(mLastIvState[id] != status) {
mLastIvState[id] = status; mLastIvState[id] = status;
changed = true; changed = true;
if(mCfgMqtt->rstValsNotAvail) if((MQTT_STATUS_NOT_AVAIL_NOT_PROD == status) && (mCfgMqtt->rstValsNotAvail))
zeroValues(iv); zeroValues(iv);
snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name); snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name);
@ -461,12 +448,12 @@ class PubMqtt {
} }
if(changed) { if(changed) {
snprintf(val, 32, "%d", ((allAvail) ? MQTT_STATUS_ONLINE : ((mIvAvail) ? MQTT_STATUS_PARTIAL : MQTT_STATUS_OFFLINE))); snprintf(val, 32, "%d", ((allAvail) ? MQTT_STATUS_ONLINE : ((anyAvail) ? MQTT_STATUS_PARTIAL : MQTT_STATUS_OFFLINE)));
publish("status", val, true); publish("status", val, true);
sendIvData(false); // false prevents loop of same function sendIvData(false); // false prevents loop of same function
} }
return totalComplete; return allAvail;
} }
void sendAlarmData() { void sendAlarmData() {
@ -494,7 +481,7 @@ class PubMqtt {
memset(total, 0, sizeof(float) * 4); memset(total, 0, sizeof(float) * 4);
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { for (uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id); Inverter<> *iv = mSys->getInverterByPos(id);
if (NULL == iv) if ((NULL == iv) || (MQTT_STATUS_NOT_AVAIL_NOT_PROD == mLastIvState[id]))
continue; // skip to next inverter continue; // skip to next inverter
record_t<> *rec = iv->getRecordStruct(mSendList.front()); record_t<> *rec = iv->getRecordStruct(mSendList.front());
@ -626,7 +613,6 @@ class PubMqtt {
std::queue<uint8_t> mSendList; std::queue<uint8_t> mSendList;
std::queue<alarm_t> mAlarmList; std::queue<alarm_t> mAlarmList;
subscriptionCb mSubscriptionCb; subscriptionCb mSubscriptionCb;
bool mIvAvail; // shows if at least one inverter is available
bool mReconnectRequest; bool mReconnectRequest;
uint8_t mLastIvState[MAX_NUM_INVERTERS]; uint8_t mLastIvState[MAX_NUM_INVERTERS];
uint16_t mIntervalTimeout; uint16_t mIntervalTimeout;

27
src/utils/ahoyTimer.h

@ -1,27 +0,0 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __AHOY_TIMER_H__
#define __AHOY_TIMER_H__
#include <Arduino.h>
namespace ah {
inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
uint32_t mil = millis();
if(mil >= *ticker) {
*ticker = mil + interval;
return true;
}
else if((mil + interval) < (*ticker)) {
*ticker = mil + interval;
return true;
}
return false;
}
}
#endif /*__AHOY_TIMER_H__*/

7
src/web/web.h

@ -18,7 +18,6 @@
#include "../appInterface.h" #include "../appInterface.h"
#include "../hm/hmSystem.h" #include "../hm/hmSystem.h"
#include "../utils/ahoyTimer.h"
#include "../utils/helper.h" #include "../utils/helper.h"
#include "html/h/index_html.h" #include "html/h/index_html.h"
@ -132,6 +131,8 @@ class Web {
} }
void showUpdate2(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { void showUpdate2(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
mApp->setOnUpdate();
if(!index) { if(!index) {
Serial.printf("Update Start: %s\n", filename.c_str()); Serial.printf("Update Start: %s\n", filename.c_str());
#ifndef ESP32 #ifndef ESP32
@ -246,7 +247,7 @@ class Web {
AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), html); AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), html);
response->addHeader("Connection", "close"); response->addHeader("Connection", "close");
request->send(response); request->send(response);
if(reboot) //if(reboot)
mApp->setRebootFlag(); mApp->setRebootFlag();
} }
@ -263,7 +264,7 @@ class Web {
AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), html); AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), html);
response->addHeader("Connection", "close"); response->addHeader("Connection", "close");
request->send(response); request->send(response);
if(reboot) //if(reboot)
mApp->setRebootFlag(); mApp->setRebootFlag();
} }

54
src/wifi/ahoywifi.cpp

@ -25,8 +25,6 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) {
mStaConn = DISCONNECTED; mStaConn = DISCONNECTED;
mCnt = 0; mCnt = 0;
mScanActive = false; mScanActive = false;
mLastApClients = 0;
mScanCnt = 0;
#if defined(ESP8266) #if defined(ESP8266)
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1)); wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
@ -66,44 +64,31 @@ void ahoywifi::tickWifiLoop() {
#if !defined(AP_ONLY) #if !defined(AP_ONLY)
if(mStaConn != GOT_IP) { if(mStaConn != GOT_IP) {
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
if(WIFI_AP_STA == WiFi.getMode()) { if(mStaConn != IN_AP_MODE) {
mStaConn = IN_AP_MODE;
// first time switch to AP Mode // first time switch to AP Mode
if(mScanActive && (mLastApClients != WiFi.softAPgetStationNum())) if (mScanActive) {
WiFi.scanDelete();
mScanActive = false; mScanActive = false;
// scan is finished
if(!mScanActive) {
WiFi.mode(WIFI_AP);
mDns.start(53, "*", mApIp);
} }
// only once a client connects to AP
if(mLastApClients != WiFi.softAPgetStationNum()) {
mLastApClients = WiFi.softAPgetStationNum();
WiFi.scanDelete();
mAppWifiCb(false);
DBGPRINTLN(F("AP client connected")); DBGPRINTLN(F("AP client connected"));
welcome(mApIp.toString()); welcome(mApIp.toString());
} WiFi.mode(WIFI_AP);
mDns.start(53, "*", mApIp);
mAppWifiCb(true);
} }
mDns.processNextRequest(); mDns.processNextRequest();
return; return;
} }
else if(WIFI_AP == WiFi.getMode()) { else if(mStaConn == IN_AP_MODE) {
mLastApClients = 0;
mCnt = 0; mCnt = 0;
DPRINTLN(DBG_INFO, "DNS stop");
mDns.stop(); mDns.stop();
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
mStaConn = DISCONNECTED;
} }
mCnt++; mCnt++;
uint8_t timeout = 10; // seconds if(!mScanActive && mBSSIDList.empty()) { // start scanning APs with the given SSID
if (mStaConn == CONNECTED) // connected but no ip
timeout = 20;
if(!mScanActive && mBSSIDList.empty() && ((mCnt % timeout) == 0)) { // start scanning APs with the given SSID
DBGPRINT(F("scanning APs with SSID ")); DBGPRINT(F("scanning APs with SSID "));
DBGPRINTLN(String(mConfig->sys.stationSsid)); DBGPRINTLN(String(mConfig->sys.stationSsid));
mScanCnt = 0; mScanCnt = 0;
@ -115,14 +100,20 @@ void ahoywifi::tickWifiLoop() {
#endif #endif
return; return;
} }
uint8_t timeout = 10; // seconds
if (mStaConn == CONNECTED) // connected but no ip
timeout = 20;
DBGPRINT(F("reconnect in ")); DBGPRINT(F("reconnect in "));
DBGPRINT(String(timeout-mCnt)); DBGPRINT(String(timeout-mCnt));
DBGPRINTLN(F(" seconds")); DBGPRINTLN(F(" seconds"));
if(mScanActive) { if(mScanActive) {
getBSSIDs(); getBSSIDs();
//if(!mScanActive) // scan completed if(!mScanActive) // scan completed
// if ((mCnt % timeout) < 8) if ((mCnt % timeout) < timeout - 2)
// mCnt = timeout - 2; mCnt = timeout - 2;
} }
if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection
if(mStaConn != CONNECTED) if(mStaConn != CONNECTED)
@ -166,8 +157,6 @@ void ahoywifi::setupAp(void) {
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(mApIp, mApIp, IPAddress(255, 255, 255, 0)); WiFi.softAPConfig(mApIp, mApIp, IPAddress(255, 255, 255, 0));
WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PWD); WiFi.softAP(WIFI_AP_SSID, WIFI_AP_PWD);
mDns.start(53, "*", mApIp);
} }
@ -266,7 +255,7 @@ void ahoywifi::sortRSSI(int *sort, int n) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ahoywifi::scanAvailNetworks(void) { void ahoywifi::scanAvailNetworks(void) {
if(-2 == WiFi.scanComplete()) { if(!mScanActive) {
mScanActive = true; mScanActive = true;
if(WIFI_AP == WiFi.getMode()) if(WIFI_AP == WiFi.getMode())
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
@ -291,6 +280,8 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
} }
mScanActive = false; mScanActive = false;
WiFi.scanDelete(); WiFi.scanDelete();
if(mStaConn == IN_AP_MODE)
WiFi.mode(WIFI_AP);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -336,7 +327,6 @@ void ahoywifi::connectionEvent(WiFiStatus_t status) {
mScanActive = false; mScanActive = false;
} }
welcome(WiFi.localIP().toString() + F(" (Station)")); welcome(WiFi.localIP().toString() + F(" (Station)"));
mDns.stop();
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
DBGPRINTLN(F("[WiFi] AP disabled")); DBGPRINTLN(F("[WiFi] AP disabled"));
mAppWifiCb(true); mAppWifiCb(true);

11
src/wifi/ahoywifi.h

@ -34,6 +34,7 @@ class ahoywifi {
DISCONNECTED = 0, DISCONNECTED = 0,
CONNECTING, CONNECTING,
CONNECTED, CONNECTED,
IN_AP_MODE,
GOT_IP GOT_IP
} WiFiStatus_t; } WiFiStatus_t;
@ -41,6 +42,8 @@ class ahoywifi {
void setupAp(void); void setupAp(void);
void setupStation(void); void setupStation(void);
void sendNTPpacket(IPAddress& address); void sendNTPpacket(IPAddress& address);
void sortRSSI(int *sort, int n);
void getBSSIDs(void);
void connectionEvent(WiFiStatus_t status); void connectionEvent(WiFiStatus_t status);
#if defined(ESP8266) #if defined(ESP8266)
void onConnect(const WiFiEventStationModeConnected& event); void onConnect(const WiFiEventStationModeConnected& event);
@ -50,8 +53,7 @@ class ahoywifi {
void onWiFiEvent(WiFiEvent_t event); void onWiFiEvent(WiFiEvent_t event);
#endif #endif
void welcome(String msg); void welcome(String msg);
void sortRSSI(int *sort, int n);
void getBSSIDs(void);
settings_t *mConfig; settings_t *mConfig;
appWifiCb mAppWifiCb; appWifiCb mAppWifiCb;
@ -67,11 +69,8 @@ class ahoywifi {
uint8_t mCnt; uint8_t mCnt;
uint32_t *mUtcTimestamp; uint32_t *mUtcTimestamp;
uint8_t mLoopCnt;
bool mScanActive;
uint8_t mLastApClients;
uint8_t mScanCnt; uint8_t mScanCnt;
bool mScanActive;
std::list<uint8_t> mBSSIDList; std::list<uint8_t> mBSSIDList;
}; };

Loading…
Cancel
Save