diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 0c6c499e..63670a14 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -10,11 +10,14 @@ app::app() : Main() { mSendTicker = new Ticker(); mFlagSend = false; + mShowRebootRequest = false; + mMqttTicker = NULL; mMqttEvt = false; - memset(mCmds, 0, sizeof(uint32_t)); - memset(mChannelStat, 0, sizeof(uint32_t)); + + memset(mCmds, 0, sizeof(uint32_t)*DBG_CMD_LIST_LEN); + //memset(mChannelStat, 0, sizeof(uint32_t) * 4); mSys = new HmSystemType(); } @@ -30,13 +33,14 @@ app::~app(void) { void app::setup(const char *ssid, const char *pwd, uint32_t timeout) { Main::setup(ssid, pwd, timeout); - mWeb->on("/", std::bind(&app::showIndex, this)); - mWeb->on("/setup", std::bind(&app::showSetup, this)); - mWeb->on("/save", std::bind(&app::showSave, this)); - mWeb->on("/cmdstat", std::bind(&app::showCmdStatistics, this)); - mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this)); - mWeb->on("/livedata", std::bind(&app::showLiveData, this)); - mWeb->on("/mqttstate", std::bind(&app::showMqtt, this)); + mWeb->on("/", std::bind(&app::showIndex, this)); + mWeb->on("/setup", std::bind(&app::showSetup, this)); + mWeb->on("/save", std::bind(&app::showSave, this)); + mWeb->on("/erase", std::bind(&app::showErase, this)); + mWeb->on("/cmdstat", std::bind(&app::showStatistics, this)); + mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this)); + mWeb->on("/livedata", std::bind(&app::showLiveData, this)); + mWeb->on("/mqttstate", std::bind(&app::showMqtt, this)); if(mSettingsValid) { uint16_t interval; @@ -51,7 +55,7 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) { mEep->read(ADDR_INV_TYPE + i, &invType); if(0ULL != invSerial) { mSys->addInverter(invName, invSerial, invType); - Serial.println("add inverter: " + String(invName) + ", SN: " + String(invSerial, HEX) + ", type: " + String(invType)); + DPRINTLN("add inverter: " + String(invName) + ", SN: " + String(invSerial, HEX) + ", type: " + String(invType)); } } @@ -67,8 +71,12 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) { mEep->read(ADDR_PINOUT+2, &mSys->Radio.pinIrq); + // nrf24 amplifier power + mEep->read(ADDR_RF24_AMP_PWR, &mSys->Radio.AmplifierPower); + // mqtt uint8_t mqttAddr[MQTT_ADDR_LEN]; + uint16_t mqttPort; char mqttUser[MQTT_USER_LEN]; char mqttPwd[MQTT_PWD_LEN]; char mqttTopic[MQTT_TOPIC_LEN]; @@ -77,13 +85,14 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) { mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN); mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN); mEep->read(ADDR_MQTT_INTERVAL, &interval); + mEep->read(ADDR_MQTT_PORT, &mqttPort); char addr[16] = {0}; sprintf(addr, "%d.%d.%d.%d", mqttAddr[0], mqttAddr[1], mqttAddr[2], mqttAddr[3]); if(interval < 1000) interval = 1000; - mMqtt.setup(addr, mqttTopic, mqttUser, mqttPwd); + mMqtt.setup(addr, mqttTopic, mqttUser, mqttPwd, mqttPort); mMqttTicker = new Ticker(); mMqttTicker->attach_ms(interval, std::bind(&app::mqttTicker, this)); @@ -92,8 +101,16 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) { mSys->setup(); - if(!mSettingsValid) - Serial.println("Warn: your settings are not valid! check [IP]/setup"); + if(!mWifiSettingsValid) + DPRINTLN("Warn: your settings are not valid! check [IP]/setup"); + else { + DPRINTLN("\n\n----------------------------------------"); + DPRINTLN("Welcome to AHOY!"); + DPRINT("\npoint your browser to http://"); + DPRINTLN(WiFi.localIP()); + DPRINTLN("to configure your device"); + DPRINTLN("----------------------------------------\n"); + } } @@ -110,7 +127,7 @@ void app::loop(void) { // process buffer only on first occurrence if((0 != len) && (0 == rptCnt)) { uint8_t *cmd = &p->packet[11]; - //Serial.println("CMD " + String(*cmd, HEX)); + //DPRINTLN("CMD " + String(*cmd, HEX)); //mSys->Radio.dumpBuf("Payload ", p->packet, len); inverter_t *iv = mSys->findInverter(&p->packet[3]); @@ -125,13 +142,15 @@ void app::loop(void) { else if(*cmd == 0x02) mCmds[1]++; else if(*cmd == 0x03) mCmds[2]++; else if(*cmd == 0x81) mCmds[3]++; - else if(*cmd == 0x84) mCmds[4]++; - else mCmds[5]++; + else if(*cmd == 0x82) mCmds[4]++; + else if(*cmd == 0x83) mCmds[5]++; + else if(*cmd == 0x84) mCmds[6]++; + else mCmds[7]++; - if(p->sendCh == 23) mChannelStat[0]++; + /*if(p->sendCh == 23) mChannelStat[0]++; else if(p->sendCh == 40) mChannelStat[1]++; else if(p->sendCh == 61) mChannelStat[2]++; - else mChannelStat[3]++; + else mChannelStat[3]++;*/ } } mSys->BufCtrl.popBack(); @@ -144,7 +163,8 @@ void app::loop(void) { inv = mSys->getInverterByPos(i); if(NULL != inv) { mSys->Radio.sendTimePacket(inv->radioId.u64, mTimestamp); - delay(20); + yield(); + delay(100); } } } @@ -165,6 +185,7 @@ void app::loop(void) { snprintf(val, 10, "%.3f", mSys->getValue(iv, i)); mMqtt.sendMsg(topic, val); delay(20); + yield(); } } } @@ -179,8 +200,9 @@ void app::loop(void) { if(0.0f != mSys->getValue(iv, i)) { snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, mSys->getFieldName(iv, i)); snprintf(val, 10, "%.3f %s", mSys->getValue(iv, i), mSys->getUnit(iv, i)); - Serial.println(String(topic) + ": " + String(val)); + DPRINTLN(String(topic) + ": " + String(val)); } + yield(); } } } @@ -282,17 +304,31 @@ void app::showSetup(void) { html.replace("{PINOUT}", String(pinout)); + // nrf24l01+ + String rf24; + for(uint8_t i = 0; i <= 3; i++) { + rf24 += ""; + } + html.replace("{RF24}", String(rf24)); + + if(mSettingsValid) { mEep->read(ADDR_INV_INTERVAL, &interval); html.replace("{INV_INTERVAL}", String(interval)); uint8_t mqttAddr[MQTT_ADDR_LEN] = {0}; + uint16_t mqttPort; mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN); mEep->read(ADDR_MQTT_INTERVAL, &interval); + mEep->read(ADDR_MQTT_PORT, &mqttPort); char addr[16] = {0}; sprintf(addr, "%d.%d.%d.%d", mqttAddr[0], mqttAddr[1], mqttAddr[2], mqttAddr[3]); html.replace("{MQTT_ADDR}", String(addr)); + html.replace("{MQTT_PORT}", String(mqttPort)); html.replace("{MQTT_USER}", String(mMqtt.getUser())); html.replace("{MQTT_PWD}", String(mMqtt.getPwd())); html.replace("{MQTT_TOPIC}", String(mMqtt.getTopic())); @@ -302,6 +338,7 @@ void app::showSetup(void) { html.replace("{INV_INTERVAL}", "1000"); html.replace("{MQTT_ADDR}", ""); + html.replace("{MQTT_PORT}", "1883"); html.replace("{MQTT_USER}", ""); html.replace("{MQTT_PWD}", ""); html.replace("{MQTT_TOPIC}", "/inverter"); @@ -319,20 +356,32 @@ void app::showSave(void) { //----------------------------------------------------------------------------- -void app::showCmdStatistics(void) { +void app::showErase() { + eraseSettings(); + showReboot(); +} + + +//----------------------------------------------------------------------------- +void app::showStatistics(void) { String content = "CMDs:\n"; - content += String("0x01: ") + String(mCmds[0]) + String("\n"); - content += String("0x02: ") + String(mCmds[1]) + String("\n"); - content += String("0x03: ") + String(mCmds[2]) + String("\n"); - content += String("0x81: ") + String(mCmds[3]) + String("\n"); - content += String("0x84: ") + String(mCmds[4]) + String("\n"); - content += String("other: ") + String(mCmds[5]) + String("\n"); - - content += "\nCHANNELs:\n"; + for(uint8_t i = 0; i < DBG_CMD_LIST_LEN; i ++) { + content += String("0x") + String(dbgCmds[i], HEX) + String(": ") + String(mCmds[i]) + String("\n"); + } + content += String("other: ") + String(mCmds[DBG_CMD_LIST_LEN]) + String("\n\n"); + + /*content += "\nCHANNELs:\n"; content += String("23: ") + String(mChannelStat[0]) + String("\n"); content += String("40: ") + String(mChannelStat[1]) + String("\n"); content += String("61: ") + String(mChannelStat[2]) + String("\n"); - content += String("75: ") + String(mChannelStat[3]) + String("\n"); + content += String("75: ") + String(mChannelStat[3]) + String("\n");*/ + + if(!mSys->Radio.isChipConnected()) + content += "WARNING! your NRF24 module can't be reached, check the wiring\n"; + + if(mShowRebootRequest) + content += "INFO: reboot your ESP to apply all your configuration changes!\n"; + mWeb->send(200, "text/plain", content); } @@ -400,7 +449,7 @@ void app::showLiveData(void) { //----------------------------------------------------------------------------- void app::showMqtt(void) { String txt = "connected"; - if(mMqtt.isConnected()) + if(!mMqtt.isConnected()) txt = "not " + txt; mWeb->send(200, "text/plain", txt); } @@ -447,8 +496,13 @@ void app::saveValues(bool webSend = true) { } + // nrf24 amplifier power + mSys->Radio.AmplifierPower = mWeb->arg("rf24Power").toInt() & 0x03; + mEep->write(ADDR_RF24_AMP_PWR, mSys->Radio.AmplifierPower); + // mqtt uint8_t mqttAddr[MQTT_ADDR_LEN] = {0}; + uint16_t mqttPort; char mqttUser[MQTT_USER_LEN]; char mqttPwd[MQTT_PWD_LEN]; char mqttTopic[MQTT_TOPIC_LEN]; @@ -463,7 +517,9 @@ void app::saveValues(bool webSend = true) { mWeb->arg("mqttPwd").toCharArray(mqttPwd, MQTT_PWD_LEN); mWeb->arg("mqttTopic").toCharArray(mqttTopic, MQTT_TOPIC_LEN); interval = mWeb->arg("mqttInterval").toInt(); + mqttPort = mWeb->arg("mqttPort").toInt(); mEep->write(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN); + mEep->write(ADDR_MQTT_PORT, mqttPort); mEep->write(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN); mEep->write(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN); mEep->write(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN); @@ -473,6 +529,7 @@ void app::saveValues(bool webSend = true) { if((mWeb->arg("reboot") == "on")) showReboot(); else { + mShowRebootRequest = true; mWeb->send(200, "text/html", "
saved
"); } @@ -490,6 +547,6 @@ void app::updateCrc(void) { uint16_t crc; crc = buildEEpCrc(ADDR_START_SETTINGS, (ADDR_NEXT - ADDR_START_SETTINGS)); - //Serial.println("new CRC: " + String(crc, HEX)); + //DPRINTLN("new CRC: " + String(crc, HEX)); mEep->write(ADDR_SETTINGS_CRC, crc); } diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index c4cabcdd..2fdc130d 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -22,6 +22,9 @@ const char* const wemosPins[] = {"D3 (GPIO0)", "TX (GPIO1)", "D4 (GPIO2)", "RX ( const char* const pinNames[] = {"CS", "CE", "IRQ"}; const char* const pinArgNames[] = {"pinCs", "pinCe", "pinIrq"}; +const uint8_t dbgCmds[] = {0x01, 0x02, 0x03, 0x81, 0x82, 0x83, 0x84}; +#define DBG_CMD_LIST_LEN 7 + class app : public Main { public: app(); @@ -42,7 +45,8 @@ class app : public Main { void showIndex(void); void showSetup(void); void showSave(void); - void showCmdStatistics(void); + void showErase(void); + void showStatistics(void); void showHoymiles(void); void showLiveData(void); void showMqtt(void); @@ -65,16 +69,15 @@ class app : public Main { return ret; } - uint8_t mState; - bool mKeyPressed; + bool mShowRebootRequest; HmSystemType *mSys; Ticker *mSendTicker; bool mFlagSend; - uint32_t mCmds[6]; - uint32_t mChannelStat[4]; + uint32_t mCmds[DBG_CMD_LIST_LEN+1]; + //uint32_t mChannelStat[4]; uint32_t mRecCnt; // mqtt diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 765ba2b2..fa96dd02 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -25,7 +25,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 2 -#define VERSION_PATCH 4 +#define VERSION_PATCH 9 //------------------------------------- @@ -50,11 +50,14 @@ typedef struct { #define PINOUT_LEN 3 // 3 pins: CS, CE, IRQ +#define RF24_AMP_PWR_LEN 1 + #define MQTT_ADDR_LEN 4 // IP #define MQTT_USER_LEN 16 #define MQTT_PWD_LEN 32 #define MQTT_TOPIC_LEN 32 #define MQTT_INTERVAL_LEN 2 // uint16_t +#define MQTT_PORT_LEN 2 // uint16_t #define ADDR_START 0 @@ -66,7 +69,9 @@ typedef struct { #define ADDR_PINOUT ADDR_START_SETTINGS -#define ADDR_INV_ADDR ADDR_PINOUT + PINOUT_LEN +#define ADDR_RF24_AMP_PWR ADDR_PINOUT + PINOUT_LEN + +#define ADDR_INV_ADDR ADDR_RF24_AMP_PWR + RF24_AMP_PWR_LEN #define ADDR_INV_NAME ADDR_INV_ADDR + INV_ADDR_LEN #define ADDR_INV_TYPE ADDR_INV_NAME + INV_NAME_LEN #define ADDR_INV_INTERVAL ADDR_INV_TYPE + INV_TYPE_LEN @@ -77,7 +82,8 @@ typedef struct { #define ADDR_MQTT_TOPIC ADDR_MQTT_PWD + MQTT_PWD_LEN #define ADDR_MQTT_INTERVAL ADDR_MQTT_TOPIC + MQTT_TOPIC_LEN -#define ADDR_NEXT ADDR_MQTT_INTERVAL + MQTT_INTERVAL_LEN +#define ADDR_MQTT_PORT ADDR_MQTT_INTERVAL + MQTT_INTERVAL_LEN +#define ADDR_NEXT ADDR_MQTT_PORT + MQTT_PORT_LEN #define ADDR_SETTINGS_CRC 400 diff --git a/tools/esp8266/eep.h b/tools/esp8266/eep.h index da1450b0..a49cae67 100644 --- a/tools/esp8266/eep.h +++ b/tools/esp8266/eep.h @@ -37,7 +37,7 @@ class eep { } void read(uint32_t addr, uint8_t data[], uint16_t length) { - for(uint8_t i = 0; i < length; i ++) { + for(uint16_t i = 0; i < length; i ++) { *(data++) = EEPROM.read(addr++); } } @@ -78,7 +78,7 @@ class eep { } void write(uint32_t addr, uint8_t data[], uint16_t length) { - for(uint8_t i = 0; i < length; i ++) { + for(uint16_t i = 0; i < length; i ++) { EEPROM.write(addr++, data[i]); } EEPROM.commit(); diff --git a/tools/esp8266/hmInverters.h b/tools/esp8266/hmInverters.h index 1da3f62c..0226a8cf 100644 --- a/tools/esp8266/hmInverters.h +++ b/tools/esp8266/hmInverters.h @@ -21,7 +21,7 @@ enum {CH0 = 0, CH1, CH2, CH3, CH4}; enum {CMD01 = 0x01, CMD02, CMD03, CMD82 = 0x82, CMD83, CMD84}; enum {INV_TYPE_HM600 = 0, INV_TYPE_HM1200, INV_TYPE_HM400}; -const char* const invTypes[] = {"HM600", "HM1200", "HM400"}; +const char* const invTypes[] = {"HM600", "HM1200 / HM1500", "HM400"}; #define NUM_INVERTER_TYPES 3 typedef struct { @@ -60,16 +60,16 @@ typedef struct { // HM400 HM350?, HM300? //------------------------------------- const byteAssign_t hm400assignment[] = { - { FLD_UDC, UNIT_V, CH1, CMD01, 14, 2, 10 }, - { FLD_IDC, UNIT_A, CH1, CMD01, 16, 2, 100 }, - { FLD_PDC, UNIT_W, CH1, CMD01, 18, 2, 10 }, - { FLD_YT, UNIT_KWH, CH1, CMD01, 20, 4, 1000 }, - { FLD_YD, UNIT_WH, CH1, CMD01, 24, 2, 1000 }, - { FLD_UAC, UNIT_V, CH0, CMD01, 26, 2, 10 }, - { FLD_F, UNIT_HZ, CH0, CMD82, 12, 2, 100 }, - { FLD_PAC, UNIT_W, CH0, CMD82, 14, 2, 10 }, - { FLD_IAC, UNIT_A, CH0, CMD82, 18, 2, 100 }, - { FLD_T, UNIT_C, CH0, CMD82, 22, 2, 10 } + { FLD_UDC, UNIT_V, CH1, CMD01, 3, 2, 10 }, + { FLD_IDC, UNIT_A, CH1, CMD01, 5, 2, 100 }, + { FLD_PDC, UNIT_W, CH1, CMD01, 7, 2, 10 }, + { FLD_YT, UNIT_KWH, CH1, CMD01, 9, 4, 1000 }, + { FLD_YD, UNIT_WH, CH1, CMD01, 13, 2, 1000 }, + { FLD_UAC, UNIT_V, CH0, CMD01, 15, 2, 10 }, + { FLD_F, UNIT_HZ, CH0, CMD82, 1, 2, 100 }, + { FLD_PAC, UNIT_W, CH0, CMD82, 3, 2, 10 }, + { FLD_IAC, UNIT_A, CH0, CMD82, 7, 2, 100 }, + { FLD_T, UNIT_C, CH0, CMD82, 11, 2, 10 } }; #define HM400_LIST_LEN (sizeof(hm400assignment) / sizeof(byteAssign_t)) @@ -78,26 +78,26 @@ const byteAssign_t hm400assignment[] = { // HM600, HM700 //------------------------------------- const byteAssign_t hm600assignment[] = { - { FLD_UDC, UNIT_V, CH1, CMD01, 14, 2, 10 }, - { FLD_IDC, UNIT_A, CH1, CMD01, 16, 2, 100 }, - { FLD_PDC, UNIT_W, CH1, CMD01, 18, 2, 10 }, - { FLD_UDC, UNIT_V, CH2, CMD01, 20, 2, 10 }, - { FLD_IDC, UNIT_A, CH2, CMD01, 22, 2, 100 }, - { FLD_PDC, UNIT_W, CH2, CMD01, 24, 2, 10 }, - { FLD_YW, UNIT_WH, CH0, CMD02, 12, 2, 1 }, - { FLD_YT, UNIT_WH, CH0, CMD02, 14, 4, 1 }, - { FLD_YD, UNIT_WH, CH1, CMD02, 18, 2, 1 }, - { FLD_YD, UNIT_WH, CH2, CMD02, 20, 2, 1 }, - { FLD_UAC, UNIT_V, CH0, CMD02, 22, 2, 10 }, - { FLD_F, UNIT_HZ, CH0, CMD02, 24, 2, 100 }, - { FLD_IAC, UNIT_A, CH0, CMD02, 26, 2, 10 }, - { FLD_T, UNIT_C, CH0, CMD83, 18, 2, 10 } + { FLD_UDC, UNIT_V, CH1, CMD01, 3, 2, 10 }, + { FLD_IDC, UNIT_A, CH1, CMD01, 5, 2, 100 }, + { FLD_PDC, UNIT_W, CH1, CMD01, 7, 2, 10 }, + { FLD_UDC, UNIT_V, CH2, CMD01, 9, 2, 10 }, + { FLD_IDC, UNIT_A, CH2, CMD01, 11, 2, 100 }, + { FLD_PDC, UNIT_W, CH2, CMD01, 13, 2, 10 }, + { FLD_YW, UNIT_WH, CH0, CMD02, 1, 2, 1 }, + { FLD_YT, UNIT_KWH, CH0, CMD02, 3, 4, 1000 }, + { FLD_YD, UNIT_WH, CH1, CMD02, 7, 2, 1 }, + { FLD_YD, UNIT_WH, CH2, CMD02, 9, 2, 1 }, + { FLD_UAC, UNIT_V, CH0, CMD02, 11, 2, 10 }, + { FLD_F, UNIT_HZ, CH0, CMD02, 13, 2, 100 }, + { FLD_IAC, UNIT_A, CH0, CMD02, 15, 2, 10 }, + { FLD_T, UNIT_C, CH0, CMD83, 7, 2, 10 } }; #define HM600_LIST_LEN (sizeof(hm600assignment) / sizeof(byteAssign_t)) //------------------------------------- -// HM1200, HM1500? +// HM1200, HM1500 //------------------------------------- const byteAssign_t hm1200assignment[] = { { FLD_UDC, UNIT_V, CH1, CMD01, 3, 2, 10 }, diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 70e710e0..65a098a5 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -13,6 +13,10 @@ #define DUMMY_RADIO_ID ((uint64_t)0xDEADBEEF01ULL) +const char* const rf24AmpPower[] = {"MIN", "LOW", "HIGH", "MAX"}; + + + //----------------------------------------------------------------------------- // MACROS //----------------------------------------------------------------------------- @@ -54,12 +58,13 @@ class HmRadio { pinCe = CE_PIN; pinIrq = IRQ_PIN; - mSendCnt = 0; + AmplifierPower = 1; + mSendCnt = 0; } ~HmRadio() {} void setup(BUFFER *ctrl) { - //Serial.println("HmRadio::setup, pins: " + String(pinCs) + ", " + String(pinCe) + ", " + String(pinIrq)); + //DPRINTLN("HmRadio::setup, pins: " + String(pinCs) + ", " + String(pinCe) + ", " + String(pinIrq)); pinMode(pinIrq, INPUT_PULLUP); mBufCtrl = ctrl; @@ -79,14 +84,18 @@ class HmRadio { // enable only receiving interrupts mNrf24.maskIRQ(true, true, false); - // Use lo PA level, as a higher level will disturb CH340 serial usb adapter - mNrf24.setPALevel(RF24_PA_MAX); + DPRINTLN("RF24 Amp Pwr: RF24_PA_" + String(rf24AmpPower[AmplifierPower])); + mNrf24.setPALevel(AmplifierPower & 0x03); mNrf24.startListening(); - Serial.println("Radio Config:"); + DPRINTLN("Radio Config:"); mNrf24.printPrettyDetails(); mSendChannel = getDefaultChannel(); + + if(!mNrf24.isChipConnected()) { + DPRINTLN("WARNING! your NRF24 module can't be reached, check the wiring"); + } } void handleIntr(void) { @@ -177,13 +186,30 @@ class HmRadio { return valid; } + void dumpBuf(const char *info, uint8_t buf[], uint8_t len) { + DPRINT(String(info)); + for(uint8_t i = 0; i < len; i++) { + if(buf[i] < 10) + DPRINT("0"); + DHEX(buf[i]); + DPRINT(" "); + } + DPRINTLN(""); + } + + bool isChipConnected(void) { + return mNrf24.isChipConnected(); + } + uint8_t pinCs; uint8_t pinCe; uint8_t pinIrq; + uint8_t AmplifierPower; + private: void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len) { - //Serial.println("sent packet: #" + String(mSendCnt)); + //DPRINTLN("sent packet: #" + String(mSendCnt)); //dumpBuf("SEN ", buf, len); DISABLE_IRQ; @@ -195,7 +221,7 @@ class HmRadio { mSendChannel = getDefaultChannel(); #endif mNrf24.setChannel(mSendChannel); - //Serial.println("CH: " + String(mSendChannel)); + //DPRINTLN("CH: " + String(mSendChannel)); mNrf24.openWritingPipe(invId); // TODO: deprecated mNrf24.setCRCLength(RF24_CRC_16); @@ -230,15 +256,6 @@ class HmRadio { mDtuIdCrc = crc16nrf24(tmp, BIT_CNT(5)); } - void dumpBuf(const char *info, uint8_t buf[], uint8_t len) { - Serial.print(String(info)); - for(uint8_t i = 0; i < len; i++) { - Serial.print(buf[i], HEX); - Serial.print(" "); - } - Serial.println(); - } - uint8_t mChanOut[4]; uint8_t mChanIdx; uint16_t mDtuIdCrc; diff --git a/tools/esp8266/hmSystem.h b/tools/esp8266/hmSystem.h index 0d40803f..70afe6b7 100644 --- a/tools/esp8266/hmSystem.h +++ b/tools/esp8266/hmSystem.h @@ -138,7 +138,7 @@ class HmSystem { void getAssignment(inverter_t *p) { if(INV_TYPE_HM600 == p->type) { - p->listLen = (uint8_t)(HM1200_LIST_LEN); + p->listLen = (uint8_t)(HM600_LIST_LEN); p->assign = (byteAssign_t*)hm600assignment; } else if(INV_TYPE_HM1200 == p->type) { diff --git a/tools/esp8266/html/h/setup_html.h b/tools/esp8266/html/h/setup_html.h index f4e99a1a..95f1236f 100644 --- a/tools/esp8266/html/h/setup_html.h +++ b/tools/esp8266/html/h/setup_html.h @@ -1,4 +1,4 @@ #ifndef __SETUP_H__ #define __SETUP_H__ -const char setup_html[] PROGMEM = "Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.
Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.
Inverter
{INVERTERS}General
@@ -31,16 +34,22 @@Pinout (Wemos)
{PINOUT} +Radio (NRF24L01+)
+ + +MQTT
+ + - +diff --git a/tools/esp8266/html/style.css b/tools/esp8266/html/style.css index 5eb5f7dc..21d5340f 100644 --- a/tools/esp8266/html/style.css +++ b/tools/esp8266/html/style.css @@ -57,6 +57,15 @@ a:hover, a:focus { color: #f00; } +a.erase { + background-color: #006ec0; + color: #fff; + padding: 7px; + display: inline-block; + margin-top: 30px; + float: right; +} + #content { padding: 15px 15px 60px 15px; } @@ -103,6 +112,7 @@ input.btn { color: #fff; border: 0px; float: right; + margin: 10px 0 30px; text-transform: uppercase; } diff --git a/tools/esp8266/main.cpp b/tools/esp8266/main.cpp index 61b2075c..25b68c71 100644 --- a/tools/esp8266/main.cpp +++ b/tools/esp8266/main.cpp @@ -43,8 +43,6 @@ void Main::setup(const char *ssid, const char *pwd, uint32_t timeout) { mWeb->onNotFound (std::bind(&Main::showNotFound, this)); startAp = getConfig(); - if(String(mDeviceName) != "") - WiFi.hostname(mDeviceName); if(false == startAp) startAp = setupStation(timeout); @@ -90,9 +88,8 @@ bool Main::getConfig(void) { memset(mStationPwd, 0, PWD_LEN); memset(mDeviceName, 0, DEVNAME_LEN); - // erase eeprom - uint8_t buf[ADDR_NEXT-ADDR_START_SETTINGS] = {0}; - mEep->write(ADDR_START_SETTINGS, buf, (ADDR_NEXT-ADDR_START_SETTINGS)); + // erase application settings except wifi settings + eraseSettings(); } return mApActive; @@ -127,6 +124,8 @@ bool Main::setupStation(uint32_t timeout) { WiFi.mode(WIFI_STA); WiFi.begin(mStationSsid, mStationPwd); + if(String(mDeviceName) != "") + WiFi.hostname(mDeviceName); delay(5000); Serial.println("wait for network"); diff --git a/tools/esp8266/main.h b/tools/esp8266/main.h index 9014b6fb..6e096789 100644 --- a/tools/esp8266/main.h +++ b/tools/esp8266/main.h @@ -53,6 +53,17 @@ class Main { return (crcCheck == crcRd); } + void eraseSettings(void) { + uint8_t buf[64] = {0}; + uint16_t addr = ADDR_START_SETTINGS, end; + do { + end = addr += 64; + if(end > (ADDR_SETTINGS_CRC + 2)) + end = (ADDR_SETTINGS_CRC + 2 - addr); + mEep->write(ADDR_START_SETTINGS, buf, (ADDR_NEXT-ADDR_START_SETTINGS)); + } while(addr < ADDR_START_SETTINGS); + } + char mStationSsid[SSID_LEN]; char mStationPwd[PWD_LEN]; bool mWifiSettingsValid; diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index b3247d14..4f9bc4b7 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -20,9 +20,9 @@ class mqtt { delete mClient; } - void setup(const char *broker, const char *topic, const char *user, const char *pwd) { + void setup(const char *broker, const char *topic, const char *user, const char *pwd, uint16_t port) { mAddressSet = true; - mClient->setServer(broker, 1883); + mClient->setServer(broker, port); snprintf(mUser, MQTT_USER_LEN, "%s", user); snprintf(mPwd, MQTT_PWD_LEN, "%s", pwd);