mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-25 23:06:11 +02:00
Radio changes, MQTT fixes
* consolidated (?) proposals from beegee3 * timing now 410microsesonds
This commit is contained in:
parent
1f0f8b904a
commit
24bfb43b02
4 changed files with 50 additions and 49 deletions
|
@ -51,6 +51,7 @@ void app::setup() {
|
||||||
|
|
||||||
mMiPayload.setup(this, &mSys, &mStat, mConfig->nrf.maxRetransPerPyld, &mTimestamp);
|
mMiPayload.setup(this, &mSys, &mStat, mConfig->nrf.maxRetransPerPyld, &mTimestamp);
|
||||||
mMiPayload.enableSerialDebug(mConfig->serial.debug);
|
mMiPayload.enableSerialDebug(mConfig->serial.debug);
|
||||||
|
mMiPayload.addPayloadListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1));
|
||||||
|
|
||||||
// DBGPRINTLN("--- after payload");
|
// DBGPRINTLN("--- after payload");
|
||||||
// DBGPRINTLN(String(ESP.getFreeHeap()));
|
// DBGPRINTLN(String(ESP.getFreeHeap()));
|
||||||
|
@ -67,6 +68,7 @@ void app::setup() {
|
||||||
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, &mSys, &mTimestamp);
|
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, &mSys, &mTimestamp);
|
||||||
mMqtt.setSubscriptionCb(std::bind(&app::mqttSubRxCb, this, std::placeholders::_1));
|
mMqtt.setSubscriptionCb(std::bind(&app::mqttSubRxCb, this, std::placeholders::_1));
|
||||||
mPayload.addAlarmListener(std::bind(&PubMqttType::alarmEventListener, &mMqtt, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
mPayload.addAlarmListener(std::bind(&PubMqttType::alarmEventListener, &mMqtt, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||||
|
mMiPayload.addAlarmListener(std::bind(&PubMqttType::alarmEventListener, &mMqtt, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
setupLed();
|
setupLed();
|
||||||
|
|
|
@ -101,19 +101,20 @@ class HmRadio {
|
||||||
// change the byte order of the DTU serial number and append the required 0x01 at the end
|
// change the byte order of the DTU serial number and append the required 0x01 at the end
|
||||||
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;
|
||||||
|
|
||||||
|
SPIClass* spi;
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
|
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
|
||||||
mSpi = new SPIClass(FSPI);
|
spi = new SPIClass(FSPI);
|
||||||
#else
|
#else
|
||||||
mSpi = new SPIClass(VSPI);
|
spi = new SPIClass(VSPI);
|
||||||
#endif
|
#endif
|
||||||
mSpi->begin(sclk, miso, mosi, cs);
|
spi->begin(sclk, miso, mosi, cs);
|
||||||
#else
|
#else
|
||||||
//the old ESP82xx cannot freely place their SPI pins
|
//the old ESP82xx cannot freely place their SPI pins
|
||||||
mSpi = new SPIClass();
|
spi = new SPIClass();
|
||||||
mSpi->begin();
|
spi->begin();
|
||||||
#endif
|
#endif
|
||||||
mNrf24.begin(mSpi, ce, cs);
|
mNrf24.begin(spi, ce, cs);
|
||||||
mNrf24.setRetries(3, 15); // 3*250us + 250us and 15 loops -> 15ms
|
mNrf24.setRetries(3, 15); // 3*250us + 250us and 15 loops -> 15ms
|
||||||
|
|
||||||
mNrf24.setChannel(mRfChLst[mRxChIdx]);
|
mNrf24.setChannel(mRfChLst[mRxChIdx]);
|
||||||
|
@ -123,7 +124,7 @@ class HmRadio {
|
||||||
mNrf24.enableDynamicPayloads();
|
mNrf24.enableDynamicPayloads();
|
||||||
mNrf24.setCRCLength(RF24_CRC_16);
|
mNrf24.setCRCLength(RF24_CRC_16);
|
||||||
mNrf24.setAddressWidth(5);
|
mNrf24.setAddressWidth(5);
|
||||||
mNrf24.openReadingPipe(1, DTU_RADIO_ID);
|
mNrf24.openReadingPipe(1, reinterpret_cast<uint8_t*>(&DTU_RADIO_ID));
|
||||||
|
|
||||||
// enable all receiving interrupts
|
// enable all receiving interrupts
|
||||||
mNrf24.maskIRQ(false, false, false);
|
mNrf24.maskIRQ(false, false, false);
|
||||||
|
@ -147,32 +148,31 @@ class HmRadio {
|
||||||
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
|
||||||
mNrf24.flush_tx(); // empty TX FIFO
|
mNrf24.flush_tx(); // empty TX FIFO
|
||||||
//DBGPRINTLN("TX whatHappened Ch" + String(mRfChLst[mTxChIdx]) + " " + String(tx_ok) + String(tx_fail) + String(rx_ready));
|
|
||||||
|
|
||||||
// start listening on the default RX channel
|
// start listening
|
||||||
mRxChIdx = 0;
|
|
||||||
mNrf24.setChannel(mRfChLst[mRxChIdx]);
|
mNrf24.setChannel(mRfChLst[mRxChIdx]);
|
||||||
mNrf24.startListening();
|
mNrf24.startListening();
|
||||||
|
|
||||||
//uint32_t debug_ms = millis();
|
uint32_t startMicros = micros();
|
||||||
uint16_t cnt = 300; // that is 60 times 5 channels
|
uint32_t loopMillis = millis();
|
||||||
while (0 < cnt--) {
|
while (millis()-loopMillis < 410) {
|
||||||
uint32_t startMillis = millis();
|
while (micros()-startMicros < 5110) { // listen 5110us to each channel
|
||||||
while (millis()-startMillis < 4) { // listen 4ms to each channel
|
|
||||||
if (mIrqRcvd) {
|
if (mIrqRcvd) {
|
||||||
mIrqRcvd = false;
|
mIrqRcvd = false;
|
||||||
if (getReceived()) { // everything received
|
if (getReceived()) { // everything received
|
||||||
//DBGPRINTLN("RX finished Cnt: " + String(300-cnt) + " time used: " + String(millis()-debug_ms)+ " ms");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
switchRxCh(); // switch to next RX channel
|
// switch to next RX channel
|
||||||
|
startMicros = micros();
|
||||||
|
if(++mRxChIdx >= RF_CHANNELS)
|
||||||
|
mRxChIdx = 0;
|
||||||
|
mNrf24.setChannel(mRfChLst[mRxChIdx]);
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
// not finished but time is over
|
// not finished but time is over
|
||||||
//DBGPRINTLN("RX not finished: 300 time used: " + String(millis()-debug_ms)+ " ms");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,9 +240,9 @@ class HmRadio {
|
||||||
sendPacket(invId, 24, isRetransmit, true);
|
sendPacket(invId, 24, isRetransmit, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool isRetransmit, bool isMI=false) {
|
void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool isRetransmit, bool appendCrc16=true) {
|
||||||
initPacket(invId, mid, pid);
|
initPacket(invId, mid, pid);
|
||||||
sendPacket(invId, 10, isRetransmit, isMI);
|
sendPacket(invId, 10, isRetransmit, appendCrc16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dumpBuf(uint8_t buf[], uint8_t len) {
|
void dumpBuf(uint8_t buf[], uint8_t len) {
|
||||||
|
@ -275,7 +275,6 @@ class HmRadio {
|
||||||
bool getReceived(void) {
|
bool getReceived(void) {
|
||||||
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
|
||||||
//DBGPRINTLN("RX whatHappened Ch" + String(mRfChLst[mRxChIdx]) + " " + String(tx_ok) + String(tx_fail) + String(rx_ready));
|
|
||||||
|
|
||||||
bool isLastPackage = false;
|
bool isLastPackage = false;
|
||||||
while(mNrf24.available()) {
|
while(mNrf24.available()) {
|
||||||
|
@ -286,29 +285,21 @@ class HmRadio {
|
||||||
p.ch = mRfChLst[mRxChIdx];
|
p.ch = mRfChLst[mRxChIdx];
|
||||||
p.len = len;
|
p.len = len;
|
||||||
mNrf24.read(p.packet, len);
|
mNrf24.read(p.packet, len);
|
||||||
|
if (p.packet[0] != 0x00) {
|
||||||
mBufCtrl.push(p);
|
mBufCtrl.push(p);
|
||||||
if (p.packet[0] == (TX_REQ_INFO + ALL_FRAMES)) // response from get information command
|
if (p.packet[0] == (TX_REQ_INFO + ALL_FRAMES)) // response from get information command
|
||||||
isLastPackage = (p.packet[9] > 0x81); // > 0x81 indicates last packet received
|
isLastPackage = (p.packet[9] > ALL_FRAMES); // > ALL_FRAMES indicates last packet received
|
||||||
else if (p.packet[0] == ( 0x0f + ALL_FRAMES) ) // response from MI get information command
|
else if (p.packet[0] == ( 0x0f + ALL_FRAMES) ) // response from MI get information command
|
||||||
isLastPackage = (p.packet[9] > 0x11); // > 0x11 indicates last packet received
|
isLastPackage = (p.packet[9] > 0x10); // > 0x10 indicates last packet received
|
||||||
else if (p.packet[0] != 0x00 && p.packet[0] != 0x88 && p.packet[0] != 0x92)
|
else if ((p.packet[0] != 0x88) && (p.packet[0] != 0x92)) // ignore fragment number zero and MI status messages //#0 was p.packet[0] != 0x00 &&
|
||||||
// ignore fragment number zero and MI status messages
|
|
||||||
isLastPackage = true; // response from dev control command
|
isLastPackage = true; // response from dev control command
|
||||||
|
}
|
||||||
|
}
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return isLastPackage;
|
return isLastPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
void switchRxCh() {
|
|
||||||
mNrf24.stopListening();
|
|
||||||
// get next channel index
|
|
||||||
if(++mRxChIdx >= RF_CHANNELS)
|
|
||||||
mRxChIdx = 0;
|
|
||||||
mNrf24.setChannel(mRfChLst[mRxChIdx]);
|
|
||||||
mNrf24.startListening();
|
|
||||||
}
|
|
||||||
|
|
||||||
void initPacket(uint64_t invId, uint8_t mid, uint8_t pid) {
|
void initPacket(uint64_t invId, uint8_t mid, uint8_t pid) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("initPacket, mid: ") + String(mid, HEX) + F(" pid: ") + String(pid, HEX));
|
DPRINTLN(DBG_VERBOSE, F("initPacket, mid: ") + String(mid, HEX) + F(" pid: ") + String(pid, HEX));
|
||||||
memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE);
|
memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE);
|
||||||
|
@ -318,12 +309,12 @@ class HmRadio {
|
||||||
mTxBuf[9] = pid;
|
mTxBuf[9] = pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendPacket(uint64_t invId, uint8_t len, bool isRetransmit, bool appendCrc16=false) {
|
void sendPacket(uint64_t invId, uint8_t len, bool isRetransmit, bool appendCrc16=true) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket"));
|
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket"));
|
||||||
//DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt));
|
//DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt));
|
||||||
|
|
||||||
// append crc's
|
// append crc's
|
||||||
if (appendCrc16 && len > 10) {
|
if (appendCrc16 && (len > 10)) {
|
||||||
// crc control data
|
// crc control data
|
||||||
uint16_t crc = ah::crc16(&mTxBuf[10], len - 10);
|
uint16_t crc = ah::crc16(&mTxBuf[10], len - 10);
|
||||||
mTxBuf[len++] = (crc >> 8) & 0xff;
|
mTxBuf[len++] = (crc >> 8) & 0xff;
|
||||||
|
@ -333,6 +324,10 @@ class HmRadio {
|
||||||
mTxBuf[len] = ah::crc8(mTxBuf, len);
|
mTxBuf[len] = ah::crc8(mTxBuf, len);
|
||||||
len++;
|
len++;
|
||||||
|
|
||||||
|
// set TX and RX channels
|
||||||
|
mTxChIdx = (mTxChIdx + 1) % RF_CHANNELS;
|
||||||
|
mRxChIdx = (mTxChIdx + 2) % RF_CHANNELS;
|
||||||
|
|
||||||
if(mSerialDebug) {
|
if(mSerialDebug) {
|
||||||
DPRINT(DBG_INFO, F("TX "));
|
DPRINT(DBG_INFO, F("TX "));
|
||||||
DBGPRINT(String(len));
|
DBGPRINT(String(len));
|
||||||
|
@ -347,10 +342,6 @@ class HmRadio {
|
||||||
mNrf24.openWritingPipe(reinterpret_cast<uint8_t*>(&invId));
|
mNrf24.openWritingPipe(reinterpret_cast<uint8_t*>(&invId));
|
||||||
mNrf24.startWrite(mTxBuf, len, false); // false = request ACK response
|
mNrf24.startWrite(mTxBuf, len, false); // false = request ACK response
|
||||||
|
|
||||||
// switch TX channel for next packet
|
|
||||||
if(++mTxChIdx >= RF_CHANNELS)
|
|
||||||
mTxChIdx = 0;
|
|
||||||
|
|
||||||
if(isRetransmit)
|
if(isRetransmit)
|
||||||
mRetransmits++;
|
mRetransmits++;
|
||||||
else
|
else
|
||||||
|
@ -364,7 +355,6 @@ class HmRadio {
|
||||||
uint8_t mTxChIdx;
|
uint8_t mTxChIdx;
|
||||||
uint8_t mRxChIdx;
|
uint8_t mRxChIdx;
|
||||||
|
|
||||||
SPIClass* mSpi;
|
|
||||||
RF24 mNrf24;
|
RF24 mNrf24;
|
||||||
uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE];
|
uint8_t mTxBuf[MAX_RF_PAYLOAD_SIZE];
|
||||||
};
|
};
|
||||||
|
|
|
@ -149,7 +149,7 @@ class MiPayload {
|
||||||
if (cmd == 0x01 || cmd == SystemConfigPara ) { //0x1 and 0x05 for HM-types
|
if (cmd == 0x01 || cmd == SystemConfigPara ) { //0x1 and 0x05 for HM-types
|
||||||
cmd = 0x0f; // for MI, these seem to make part of the Polling the device software and hardware version number command
|
cmd = 0x0f; // for MI, these seem to make part of the Polling the device software and hardware version number command
|
||||||
cmd2 = cmd == SystemConfigPara ? 0x01 : 0x00; //perhaps we can only try to get second frame?
|
cmd2 = cmd == SystemConfigPara ? 0x01 : 0x00; //perhaps we can only try to get second frame?
|
||||||
mSys->Radio.sendCmdPacket(iv->radioId.u64, cmd, cmd2, false, true);
|
mSys->Radio.sendCmdPacket(iv->radioId.u64, cmd, cmd2, false, false);
|
||||||
} else {
|
} else {
|
||||||
mSys->Radio.prepareDevInformCmd(iv->radioId.u64, cmd2, mPayload[iv->id].ts, iv->alarmMesIndex, false, cmd);
|
mSys->Radio.prepareDevInformCmd(iv->radioId.u64, cmd2, mPayload[iv->id].ts, iv->alarmMesIndex, false, cmd);
|
||||||
};
|
};
|
||||||
|
@ -442,7 +442,7 @@ const byteAssign_t InfoAssignment[] = {
|
||||||
mPayload[iv->id].retransmits = mMaxRetrans;
|
mPayload[iv->id].retransmits = mMaxRetrans;
|
||||||
} else if ( cmd == 0x0f ) {
|
} else if ( cmd == 0x0f ) {
|
||||||
//hard/firmware request
|
//hard/firmware request
|
||||||
mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x0f, 0x00, true, true);
|
mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x0f, 0x00, true, false);
|
||||||
//iv->setQueuedCmdFinished();
|
//iv->setQueuedCmdFinished();
|
||||||
//cmd = iv->getQueuedCmd();
|
//cmd = iv->getQueuedCmd();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -49,6 +49,8 @@ class PubMqtt {
|
||||||
mTxCnt = 0;
|
mTxCnt = 0;
|
||||||
mSubscriptionCb = NULL;
|
mSubscriptionCb = NULL;
|
||||||
memset(mLastIvState, MQTT_STATUS_NOT_AVAIL_NOT_PROD, MAX_NUM_INVERTERS);
|
memset(mLastIvState, MQTT_STATUS_NOT_AVAIL_NOT_PROD, MAX_NUM_INVERTERS);
|
||||||
|
memset(mIvLastRTRpub, 0, MAX_NUM_INVERTERS * 4);
|
||||||
|
|
||||||
mLastAnyAvail = false;
|
mLastAnyAvail = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -522,7 +524,13 @@ class PubMqtt {
|
||||||
void sendData(Inverter<> *iv, uint8_t curInfoCmd) {
|
void sendData(Inverter<> *iv, uint8_t curInfoCmd) {
|
||||||
record_t<> *rec = iv->getRecordStruct(curInfoCmd);
|
record_t<> *rec = iv->getRecordStruct(curInfoCmd);
|
||||||
|
|
||||||
if (iv->getLastTs(rec) > 0) {
|
uint32_t lastTs = iv->getLastTs(rec);
|
||||||
|
bool pubData = (lastTs > 0);
|
||||||
|
if (curInfoCmd == RealTimeRunData_Debug)
|
||||||
|
pubData &= (lastTs != mIvLastRTRpub[iv->id]);
|
||||||
|
|
||||||
|
if (pubData) {
|
||||||
|
mIvLastRTRpub[iv->id] = lastTs;
|
||||||
for (uint8_t i = 0; i < rec->length; i++) {
|
for (uint8_t i = 0; i < rec->length; i++) {
|
||||||
bool retained = false;
|
bool retained = false;
|
||||||
if (curInfoCmd == RealTimeRunData_Debug) {
|
if (curInfoCmd == RealTimeRunData_Debug) {
|
||||||
|
@ -653,6 +661,7 @@ class PubMqtt {
|
||||||
subscriptionCb mSubscriptionCb;
|
subscriptionCb mSubscriptionCb;
|
||||||
bool mLastAnyAvail;
|
bool mLastAnyAvail;
|
||||||
uint8_t mLastIvState[MAX_NUM_INVERTERS];
|
uint8_t mLastIvState[MAX_NUM_INVERTERS];
|
||||||
|
uint32_t mIvLastRTRpub[MAX_NUM_INVERTERS];
|
||||||
uint16_t mIntervalTimeout;
|
uint16_t mIntervalTimeout;
|
||||||
|
|
||||||
// last will topic and payload must be available trough lifetime of 'espMqttClient'
|
// last will topic and payload must be available trough lifetime of 'espMqttClient'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue