@ -4,236 +4,68 @@
//-----------------------------------------------------------------------------
# include "app.h"
# include "favicon.h"
# include "html/h/index_html.h"
# include "html/h/setup_html.h"
# include "html/h/hoymiles_html.h"
# include <ArduinoJson.h>
//-----------------------------------------------------------------------------
app : : app ( ) : Main ( ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::app():Main " ) ) ;
mSendTicker = 0xffff ;
mSendInterval = SEND_INTERVAL ;
mMqttTicker = 0xffff ;
mMqttInterval = MQTT_INTERVAL ;
mSerialTicker = 0xffff ;
mSerialInterval = SERIAL_INTERVAL ;
mMqttActive = false ;
mTicker = 0 ;
mRxTicker = 0 ;
app : : app ( ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::app " ) ) ;
mEep = new eep ( ) ;
Serial . begin ( 115200 ) ;
mSendLastIvId = 0 ;
mWifi = new wifi ( this , & mSysConfig , & mConfig ) ;
mShowRebootRequest = false ;
mWebInst = new web ( this , & mSysConfig , & mConfig , mVersion ) ;
mWebInst - > setup ( ) ;
mSerialValues = true ;
mSerialDebug = false ;
memset ( mPayload , 0 , ( MAX_NUM_INVERTERS * sizeof ( invPayload_t ) ) ) ;
mRxFailed = 0 ;
mRxSuccess = 0 ;
mFrameCnt = 0 ;
mLastPacketId = 0x00 ;
resetSystem ( ) ;
loadDefaultConfig ( ) ;
mSys = new HmSystemType ( ) ;
}
//-----------------------------------------------------------------------------
app : : ~ app ( void ) {
}
//-----------------------------------------------------------------------------
void app : : setup ( uint32_t timeout ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::setup " ) ) ;
Main : : setup ( timeout ) ;
mWeb - > on ( " / " , std : : bind ( & app : : showIndex , this ) ) ;
mWeb - > on ( " /favicon.ico " , std : : bind ( & app : : showFavicon , 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 ( " /json " , std : : bind ( & app : : showJSON , this ) ) ;
mWeb - > on ( " /api " , HTTP_POST , std : : bind ( & app : : webapi , this ) ) ;
if ( mSettingsValid ) {
mEep - > read ( ADDR_INV_INTERVAL , & mSendInterval ) ;
if ( mSendInterval < MIN_SEND_INTERVAL )
mSendInterval = MIN_SEND_INTERVAL ;
mSendTicker = mSendInterval ;
// inverter
uint64_t invSerial ;
char name [ MAX_NAME_LENGTH + 1 ] = { 0 } ;
uint16_t modPwr [ 4 ] ;
Inverter < > * iv ;
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
mEep - > read ( ADDR_INV_ADDR + ( i * 8 ) , & invSerial ) ;
mEep - > read ( ADDR_INV_NAME + ( i * MAX_NAME_LENGTH ) , name , MAX_NAME_LENGTH ) ;
mEep - > read ( ADDR_INV_CH_PWR + ( i * 2 * 4 ) , modPwr , 4 ) ;
if ( 0ULL ! = invSerial ) {
iv = mSys - > addInverter ( name , invSerial , modPwr ) ;
if ( NULL ! = iv ) {
mEep - > read ( ADDR_INV_PWR_LIM + ( i * 2 ) , ( uint16_t * ) & ( iv - > powerLimit [ 0 ] ) ) ;
if ( iv - > powerLimit [ 0 ] ! = 0xffff ) { // only set it, if it is changed by user. Default value in the html setup page is -1 = 0xffff
iv - > powerLimit [ 1 ] = 0x0001 ; // set the limit as persistent
iv - > devControlCmd = ActivePowerContr ; // set active power limit
iv - > devControlRequest = true ; // set to true to update the active power limit from setup html page
DPRINTLN ( DBG_INFO , F ( " add inverter: " ) + String ( name ) + " , SN: " + String ( invSerial , HEX ) + " , Power Limit: " + String ( iv - > powerLimit [ 0 ] ) ) ;
}
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
mEep - > read ( ADDR_INV_CH_NAME + ( i * 4 * MAX_NAME_LENGTH ) + j * MAX_NAME_LENGTH , iv - > chName [ j ] , MAX_NAME_LENGTH ) ;
}
}
mWifiSettingsValid = checkEEpCrc ( ADDR_START , ADDR_WIFI_CRC , ADDR_WIFI_CRC ) ;
mSettingsValid = checkEEpCrc ( ADDR_START_SETTINGS , ( ( ADDR_NEXT ) - ( ADDR_START_SETTINGS ) ) , ADDR_SETTINGS_CRC ) ;
loadEEpconfig ( ) ;
mMqttInterval + = mSendInterval ;
}
}
mEep - > read ( ADDR_INV_MAX_RTRY , & mMaxRetransPerPyld ) ;
if ( 0 = = mMaxRetransPerPyld )
mMaxRetransPerPyld = DEF_MAX_RETRANS_PER_PYLD ;
// pinout
mEep - > read ( ADDR_PINOUT , & mSys - > Radio . pinCs ) ;
mEep - > read ( ADDR_PINOUT + 1 , & mSys - > Radio . pinCe ) ;
mEep - > read ( ADDR_PINOUT + 2 , & mSys - > Radio . pinIrq ) ;
if ( mSys - > Radio . pinCs = = mSys - > Radio . pinCe ) {
mSys - > Radio . pinCs = RF24_CS_PIN ;
mSys - > Radio . pinCe = RF24_CE_PIN ;
mSys - > Radio . pinIrq = RF24_IRQ_PIN ;
}
mWifi - > setup ( timeout , mWifiSettingsValid ) ;
// nrf24 amplifier power
mEep - > read ( ADDR_RF24_AMP_PWR , & mSys - > Radio . AmplifierPower ) ;
// serial console
uint8_t tmp ;
mEep - > read ( ADDR_SER_INTERVAL , & mSerialInterval ) ;
if ( mSerialInterval < MIN_SERIAL_INTERVAL )
mSerialInterval = MIN_SERIAL_INTERVAL ;
mEep - > read ( ADDR_SER_ENABLE , & tmp ) ;
mSerialValues = ( tmp = = 0x01 ) ;
mEep - > read ( ADDR_SER_DEBUG , & tmp ) ;
mSerialDebug = ( tmp = = 0x01 ) ;
mSys - > Radio . mSerialDebug = mSerialDebug ;
// ntp
char ntpAddr [ NTP_ADDR_LEN ] ;
uint16_t ntpPort ;
mEep - > read ( ADDR_NTP_ADDR , ntpAddr , NTP_ADDR_LEN ) ;
mEep - > read ( ADDR_NTP_PORT , & ntpPort ) ;
// TODO set ntpAddr & ntpPort in main
// mqtt
uint16_t mqttPort ;
char mqttAddr [ MQTT_ADDR_LEN ] ;
char mqttUser [ MQTT_USER_LEN ] ;
char mqttPwd [ MQTT_PWD_LEN ] ;
char mqttTopic [ MQTT_TOPIC_LEN ] ;
char mqttDevName [ DEVNAME_LEN ] ;
mEep - > read ( ADDR_MQTT_ADDR , mqttAddr , MQTT_ADDR_LEN ) ;
mEep - > read ( ADDR_MQTT_USER , mqttUser , MQTT_USER_LEN ) ;
mEep - > read ( ADDR_MQTT_PWD , mqttPwd , MQTT_PWD_LEN ) ;
mEep - > read ( ADDR_MQTT_TOPIC , mqttTopic , MQTT_TOPIC_LEN ) ;
mEep - > read ( ADDR_DEVNAME , mqttDevName , DEVNAME_LEN ) ;
//mEep->read(ADDR_MQTT_INTERVAL, &mMqttInterval);
mEep - > read ( ADDR_MQTT_PORT , & mqttPort ) ;
if ( mqttAddr [ 0 ] > 0 ) {
mMqttActive = true ;
if ( mMqttInterval < MIN_MQTT_INTERVAL )
mMqttInterval = MIN_MQTT_INTERVAL ;
}
else
mMqttInterval = 0xffff ;
if ( 0 = = mqttPort )
mqttPort = 1883 ;
# ifndef AP_ONLY
setupMqtt ( ) ;
# endif
mSys - > setup ( & mConfig ) ;
}
mMqtt . setup ( mqttAddr , mqttTopic , mqttUser , mqttPwd , mqttDevName , mqttPort ) ;
mMqtt . mClient - > setCallback ( std : : bind ( & app : : cbMqtt , this , std : : placeholders : : _1 , std : : placeholders : : _2 , std : : placeholders : : _3 ) ) ;
mMqttTicker = 0 ;
//-----------------------------------------------------------------------------
void app : : loop ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::loop " ) ) ;
# ifdef __MQTT_TEST__
// für mqtt test
mMqttTicker = mMqttInterval - 10 ;
# endif
mSerialTicker = 0 ;
bool apActive = mWifi - > loop ( ) ;
mWebInst - > loop ( ) ;
if ( mqttAddr [ 0 ] > 0 ) {
char topic [ 30 ] ;
mMqtt . sendMsg ( " device " , mqttDevName ) ;
mMqtt . sendMsg ( " version " , mVersion ) ;
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
iv = mSys - > getInverterByPos ( i ) ;
if ( NULL ! = iv ) {
for ( uint8_t i = 0 ; i < 4 ; i + + ) {
if ( 0 ! = iv - > chName [ i ] [ 0 ] ) {
snprintf ( topic , 30 , " %s/ch%d/%s " , iv - > name , i + 1 , " name " ) ;
mMqtt . sendMsg ( topic , iv - > chName [ i ] ) ;
yield ( ) ;
}
}
}
if ( checkTicker ( & mUptimeTicker , mUptimeInterval ) ) {
mUptimeSecs + + ;
if ( 0 ! = mTimestamp )
mTimestamp + + ;
else {
if ( ! apActive ) {
mTimestamp = mWifi - > getNtpTime ( ) ;
DPRINTLN ( DBG_INFO , " [NTP]: " + getDateTimeStr ( mTimestamp ) ) ;
}
}
}
else {
DPRINTLN ( DBG_DEBUG , F ( " CRC pos: " ) + String ( ADDR_SETTINGS_CRC ) ) ;
DPRINTLN ( DBG_DEBUG , F ( " NXT pos: " ) + String ( ADDR_NEXT ) ) ;
DPRINTLN ( DBG_INFO , F ( " Settings not valid, erasing ... " ) ) ;
eraseSettings ( ) ;
saveValues ( false ) ;
delay ( 100 ) ;
DPRINTLN ( DBG_INFO , F ( " ... restarting ... " ) ) ;
delay ( 100 ) ;
ESP . restart ( ) ;
}
mSys - > setup ( ) ;
if ( ! mWifiSettingsValid )
DPRINTLN ( DBG_WARN , F ( " your settings are not valid! check [IP]/setup " ) ) ;
else {
DPRINTLN ( DBG_INFO , F ( " \n \n ---------------------------------------- " ) ) ;
DPRINTLN ( DBG_INFO , F ( " Welcome to AHOY! " ) ) ;
DPRINT ( DBG_INFO , F ( " \n point your browser to http:// " ) ) ;
if ( mApActive )
DBGPRINTLN ( F ( " 192.168.1.1 " ) ) ;
else
DBGPRINTLN ( WiFi . localIP ( ) ) ;
DPRINTLN ( DBG_INFO , F ( " to configure your device " ) ) ;
DPRINTLN ( DBG_INFO , F ( " ---------------------------------------- \n " ) ) ;
}
}
//-----------------------------------------------------------------------------
void app : : loop ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::loop " ) ) ;
Main : : loop ( ) ;
mSys - > Radio . loop ( ) ;
yield ( ) ;
if ( checkTicker ( & mRxTicker , 5 ) ) {
//DPRINTLN(DBG_VERBOSE, F("app_loops =") + String(app_loops));
app_loops = 0 ;
DPRINT ( DBG_VERBOSE , F ( " a " ) ) ;
bool rxRdy = mSys - > Radio . switchRxCh ( ) ;
if ( ! mSys - > BufCtrl . empty ( ) ) {
@ -242,7 +74,7 @@ void app::loop(void) {
if ( mSys - > Radio . checkPaketCrc ( p - > packet , & len , p - > rxCh ) ) {
// process buffer only on first occurrence
if ( mS erialDebug ) {
if ( mConfig . s erialDebug ) {
DPRINT ( DBG_INFO , " RX " + String ( len ) + " B Ch " + String ( p - > rxCh ) + " | " ) ;
mSys - > Radio . dumpBuf ( NULL , p - > packet , len ) ;
}
@ -349,7 +181,7 @@ void app::loop(void) {
mMqtt . loop ( ) ;
if ( checkTicker ( & mTicker , 1000 ) ) {
if ( ( + + mMqttTicker > = mMqttInterval ) & & ( mMqttInterval ! = 0xffff ) ) {
if ( ( + + mMqttTicker > = mMqttInterval ) & & ( mMqttInterval ! = 0xffff ) & & mMqttActive ) {
mMqttTicker = 0 ;
mMqtt . isConnected ( true ) ;
char topic [ 30 ] , val [ 10 ] ;
@ -380,8 +212,8 @@ void app::loop(void) {
# endif
}
if ( mSerialValues ) {
if ( + + mSerialTicker > = mS erialInterval ) {
if ( mConfig . serialShowIv ) {
if ( + + mSerialTicker > = mConfig . s erialInterval ) {
mSerialTicker = 0 ;
char topic [ 30 ] , val [ 10 ] ;
for ( uint8_t id = 0 ; id < mSys - > getNumInverters ( ) ; id + + ) {
@ -404,15 +236,15 @@ void app::loop(void) {
}
}
if ( + + mSendTicker > = mS endInterval ) {
if ( + + mSendTicker > = mConfig . s endInterval ) {
mSendTicker = 0 ;
if ( 0 ! = mTimestamp ) {
if ( mS erialDebug )
if ( mConfig . s erialDebug )
DPRINTLN ( DBG_DEBUG , F ( " Free heap: 0x " ) + String ( ESP . getFreeHeap ( ) , HEX ) ) ;
if ( ! mSys - > BufCtrl . empty ( ) ) {
if ( mS erialDebug )
if ( mConfig . s erialDebug )
DPRINTLN ( DBG_DEBUG , F ( " recbuf not empty! # " ) + String ( mSys - > BufCtrl . getFill ( ) ) ) ;
}
@ -431,7 +263,7 @@ void app::loop(void) {
if ( ! mPayload [ iv - > id ] . complete ) {
mRxFailed + + ;
if ( mS erialDebug ) {
if ( mConfig . s erialDebug ) {
DPRINT ( DBG_INFO , F ( " Inverter # " ) + String ( iv - > id ) + " " ) ;
DPRINTLN ( DBG_INFO , F ( " no Payload received! (retransmits: " ) + String ( mPayload [ iv - > id ] . retransmits ) + " ) " ) ;
}
@ -446,11 +278,11 @@ void app::loop(void) {
mPayload [ iv - > id ] . ts = mTimestamp ;
yield ( ) ;
if ( mS erialDebug )
if ( mConfig . s erialDebug )
DPRINTLN ( DBG_DEBUG , F ( " app:loop WiFi WiFi.status " ) + String ( WiFi . status ( ) ) ) ;
DPRINTLN ( DBG_INFO , F ( " Requesting Inverter SN " ) + String ( iv - > serial . u64 , HEX ) ) ;
if ( iv - > devControlRequest & & iv - > powerLimit [ 0 ] > 0 ) { // prevent to "switch off"
if ( mS erialDebug )
if ( mConfig . s erialDebug )
DPRINTLN ( DBG_INFO , F ( " Devcontrol request " ) + String ( iv - > devControlCmd ) + F ( " power limit " ) + String ( iv - > powerLimit [ 0 ] ) ) ;
mSys - > Radio . sendControlPacket ( iv - > radioId . u64 , iv - > devControlCmd , iv - > powerLimit ) ;
} else {
@ -459,7 +291,7 @@ void app::loop(void) {
}
}
}
else if ( mS erialDebug )
else if ( mConfig . s erialDebug )
DPRINTLN ( DBG_WARN , F ( " time not set, can't request inverter! " ) ) ;
yield ( ) ;
}
@ -514,12 +346,12 @@ void app::processPayload(bool retransmit) {
if ( ! buildPayload ( iv - > id ) ) {
if ( mPayload [ iv - > id ] . requested ) {
if ( retransmit ) {
if ( mPayload [ iv - > id ] . retransmits < mM axRetransPerPyld ) {
if ( mPayload [ iv - > id ] . retransmits < mConfig . m axRetransPerPyld ) {
mPayload [ iv - > id ] . retransmits + + ;
if ( mPayload [ iv - > id ] . maxPackId ! = 0 ) {
for ( uint8_t i = 0 ; i < ( mPayload [ iv - > id ] . maxPackId - 1 ) ; i + + ) {
if ( mPayload [ iv - > id ] . len [ i ] = = 0 ) {
if ( mS erialDebug )
if ( mConfig . s erialDebug )
DPRINTLN ( DBG_ERROR , F ( " while retrieving data: Frame " ) + String ( i + 1 ) + F ( " missing: Request Retransmit " ) ) ;
mSys - > Radio . sendCmdPacket ( iv - > radioId . u64 , TX_REQ_INFO , ( SINGLE_FRAME + i ) , true ) ;
break ; // only retransmit one frame per loop
@ -528,7 +360,7 @@ void app::processPayload(bool retransmit) {
}
}
else {
if ( mS erialDebug )
if ( mConfig . s erialDebug )
DPRINTLN ( DBG_ERROR , F ( " while retrieving data: last frame missing: Request Retransmit " ) ) ;
if ( 0x00 ! = mLastPacketId )
mSys - > Radio . sendCmdPacket ( iv - > radioId . u64 , TX_REQ_INFO , mLastPacketId , true ) ;
@ -551,7 +383,7 @@ void app::processPayload(bool retransmit) {
yield ( ) ;
}
offs - = 2 ;
if ( mS erialDebug ) {
if ( mConfig . s erialDebug ) {
DPRINT ( DBG_INFO , F ( " Payload ( " ) + String ( offs ) + " ): " ) ;
mSys - > Radio . dumpBuf ( NULL , payload , offs ) ;
}
@ -586,168 +418,6 @@ void app::processPayload(bool retransmit) {
}
//-----------------------------------------------------------------------------
void app : : showIndex ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showIndex " ) ) ;
String html = FPSTR ( index_html ) ;
html . replace ( F ( " {DEVICE} " ) , mDeviceName ) ;
html . replace ( F ( " {VERSION} " ) , mVersion ) ;
html . replace ( F ( " {TS} " ) , String ( mSendInterval ) + " " ) ;
html . replace ( F ( " {JS_TS} " ) , String ( mSendInterval * 1000 ) ) ;
html . replace ( F ( " {BUILD} " ) , String ( AUTO_GIT_HASH ) ) ;
mWeb - > send ( 200 , " text/html " , html ) ;
}
//-----------------------------------------------------------------------------
void app : : showSetup ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showSetup " ) ) ;
// overrides same method in main.cpp
String html = FPSTR ( setup_html ) ;
html . replace ( F ( " {SSID} " ) , mStationSsid ) ;
// PWD will be left at the default value (for protection)
// -> the PWD will only be changed if it does not match the placeholder "{PWD}"
html . replace ( F ( " {DEVICE} " ) , String ( mDeviceName ) ) ;
html . replace ( F ( " {VERSION} " ) , String ( mVersion ) ) ;
if ( mApActive )
html . replace ( F ( " {IP} " ) , String ( F ( " http://192.168.1.1 " ) ) ) ;
else
html . replace ( F ( " {IP} " ) , ( " http:// " + String ( WiFi . localIP ( ) . toString ( ) ) ) ) ;
String inv ;
uint64_t invSerial ;
char name [ MAX_NAME_LENGTH + 1 ] = { 0 } ;
uint16_t modPwr [ 4 ] ;
uint16_t invActivePowerLimit = - 1 ;
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
mEep - > read ( ADDR_INV_ADDR + ( i * 8 ) , & invSerial ) ;
mEep - > read ( ADDR_INV_NAME + ( i * MAX_NAME_LENGTH ) , name , MAX_NAME_LENGTH ) ;
mEep - > read ( ADDR_INV_CH_PWR + ( i * 2 * 4 ) , modPwr , 4 ) ;
mEep - > read ( ADDR_INV_PWR_LIM + ( i * 2 ) , ( uint16_t * ) & invActivePowerLimit ) ;
inv + = F ( " <p class= \" subdes \" >Inverter " ) + String ( i ) + " </p> " ;
inv + = F ( " <label for= \" inv " ) + String ( i ) + F ( " Addr \" >Address</label> " ) ;
inv + = F ( " <input type= \" text \" class= \" text \" name= \" inv " ) + String ( i ) + F ( " Addr \" value= \" " ) ;
if ( 0ULL ! = invSerial )
inv + = String ( invSerial , HEX ) ;
inv + = F ( " \" / maxlength= \" 12 \" onkeyup= \" checkSerial() \" > " ) ;
inv + = F ( " <label for= \" inv " ) + String ( i ) + F ( " Name \" >Name</label> " ) ;
inv + = F ( " <input type= \" text \" class= \" text \" name= \" inv " ) + String ( i ) + F ( " Name \" value= \" " ) ;
inv + = String ( name ) ;
inv + = F ( " \" / maxlength= \" " ) + String ( MAX_NAME_LENGTH ) + " \" > " ;
inv + = F ( " <label for= \" inv " ) + String ( i ) + F ( " ActivePowerLimit \" >Active Power Limit (W)</label> " ) ;
inv + = F ( " <input type= \" text \" class= \" text \" name= \" inv " ) + String ( i ) + F ( " ActivePowerLimit \" value= \" " ) ;
if ( name [ 0 ] = = 0 ) {
// If this value will be "saved" on next reboot the command to set the power limit will not be executed.
inv + = String ( 65535 ) ;
} else {
inv + = String ( invActivePowerLimit ) ;
}
inv + = F ( " \" / maxlength= \" " ) + String ( 6 ) + " \" > " ;
inv + = F ( " <label for= \" inv " ) + String ( i ) + F ( " ModPwr0 \" name= \" lbl " ) + String ( i ) ;
inv + = F ( " ModPwr \" >Max Module Power (Wp)</label> " ) ;
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
inv + = F ( " <input type= \" text \" class= \" text sh \" name= \" inv " ) + String ( i ) + F ( " ModPwr " ) + String ( j ) + F ( " \" value= \" " ) ;
inv + = String ( modPwr [ j ] ) ;
inv + = F ( " \" / maxlength= \" 4 \" > " ) ;
}
inv + = F ( " <br/><label for= \" inv " ) + String ( i ) + F ( " ModName0 \" name= \" lbl " ) + String ( i ) ;
inv + = F ( " ModName \" >Module Name</label> " ) ;
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
mEep - > read ( ADDR_INV_CH_NAME + ( i * 4 * MAX_NAME_LENGTH ) + j * MAX_NAME_LENGTH , name , MAX_NAME_LENGTH ) ;
inv + = F ( " <input type= \" text \" class= \" text sh \" name= \" inv " ) + String ( i ) + F ( " ModName " ) + String ( j ) + F ( " \" value= \" " ) ;
inv + = String ( name ) ;
inv + = F ( " \" / maxlength= \" " ) + String ( MAX_NAME_LENGTH ) + " \" > " ;
}
}
html . replace ( F ( " {INVERTERS} " ) , String ( inv ) ) ;
// pinout
String pinout ;
for ( uint8_t i = 0 ; i < 3 ; i + + ) {
pinout + = F ( " <label for= \" " ) + String ( pinArgNames [ i ] ) + " \" > " + String ( pinNames [ i ] ) + F ( " </label> " ) ;
pinout + = F ( " <select name= \" " ) + String ( pinArgNames [ i ] ) + " \" > " ;
for ( uint8_t j = 0 ; j < = 16 ; j + + ) {
pinout + = F ( " <option value= \" " ) + String ( j ) + " \" " ;
switch ( i ) {
default : if ( j = = mSys - > Radio . pinCs ) pinout + = F ( " selected " ) ; break ;
case 1 : if ( j = = mSys - > Radio . pinCe ) pinout + = F ( " selected " ) ; break ;
case 2 : if ( j = = mSys - > Radio . pinIrq ) pinout + = F ( " selected " ) ; break ;
}
pinout + = " > " + String ( wemosPins [ j ] ) + F ( " </option> " ) ;
}
pinout + = F ( " </select> " ) ;
}
html . replace ( F ( " {PINOUT} " ) , String ( pinout ) ) ;
// nrf24l01+
String rf24 ;
for ( uint8_t i = 0 ; i < = 3 ; i + + ) {
rf24 + = F ( " <option value= \" " ) + String ( i ) + " \" " ;
if ( i = = mSys - > Radio . AmplifierPower )
rf24 + = F ( " selected " ) ;
rf24 + = " > " + String ( rf24AmpPower [ i ] ) + F ( " </option> " ) ;
}
html . replace ( F ( " {RF24} " ) , String ( rf24 ) ) ;
if ( mSettingsValid ) {
html . replace ( F ( " {INV_INTVL} " ) , String ( mSendInterval ) ) ;
html . replace ( F ( " {INV_RETRIES} " ) , String ( mMaxRetransPerPyld ) ) ;
uint8_t tmp ;
mEep - > read ( ADDR_SER_ENABLE , & tmp ) ;
html . replace ( F ( " {SER_INTVL} " ) , String ( mSerialInterval ) ) ;
html . replace ( F ( " {SER_VAL_CB} " ) , ( tmp = = 0x01 ) ? " checked " : " " ) ;
mEep - > read ( ADDR_SER_DEBUG , & tmp ) ;
html . replace ( F ( " {SER_DBG_CB} " ) , ( tmp = = 0x01 ) ? " checked " : " " ) ;
char ntpAddr [ NTP_ADDR_LEN ] = { 0 } ;
uint16_t ntpPort ;
mEep - > read ( ADDR_NTP_ADDR , ntpAddr , NTP_ADDR_LEN ) ;
mEep - > read ( ADDR_NTP_PORT , & ntpPort ) ;
html . replace ( F ( " {NTP_ADDR} " ) , String ( ntpAddr ) ) ;
html . replace ( F ( " {NTP_PORT} " ) , String ( ntpPort ) ) ;
char mqttAddr [ MQTT_ADDR_LEN ] = { 0 } ;
uint16_t mqttPort ;
mEep - > read ( ADDR_MQTT_ADDR , mqttAddr , MQTT_ADDR_LEN ) ;
mEep - > read ( ADDR_MQTT_PORT , & mqttPort ) ;
html . replace ( F ( " {MQTT_ADDR} " ) , String ( mqttAddr ) ) ;
html . replace ( F ( " {MQTT_PORT} " ) , String ( mMqtt . getPort ( ) ) ) ;
html . replace ( F ( " {MQTT_USER} " ) , String ( mMqtt . getUser ( ) ) ) ;
html . replace ( F ( " {MQTT_PWD} " ) , String ( mMqtt . getPwd ( ) ) ) ;
html . replace ( F ( " {MQTT_TOPIC} " ) , String ( mMqtt . getTopic ( ) ) ) ;
html . replace ( F ( " {MQTT_INTVL} " ) , String ( mMqttInterval ) ) ;
}
mWeb - > send ( 200 , F ( " text/html " ) , html ) ;
}
//-----------------------------------------------------------------------------
void app : : showSave ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showSave " ) ) ;
saveValues ( true ) ;
}
//-----------------------------------------------------------------------------
void app : : showErase ( ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showErase " ) ) ;
eraseSettings ( ) ;
showReboot ( ) ;
}
//-----------------------------------------------------------------------------
void app : : cbMqtt ( char * topic , byte * payload , unsigned int length ) {
// callback handling on subscribed devcontrol topic
@ -820,10 +490,8 @@ void app::cbMqtt(char* topic, byte* payload, unsigned int length) {
}
//-----------------------------------------------------------------------------
void app : : showStatistics ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showStatistics " ) ) ;
String app : : getStatistics ( void ) {
String content = F ( " Receive success: " ) + String ( mRxSuccess ) + " \n " ;
content + = F ( " Receive fail: " ) + String ( mRxFailed ) + " \n " ;
content + = F ( " Frames received: " ) + String ( mFrameCnt ) + " \n " ;
@ -868,7 +536,7 @@ void app::showStatistics(void) {
content + = F ( " not " ) ;
content + = F ( " connected \n " ) ;
mWeb - > send ( 200 , F ( " text/plain " ) , content ) ;
return content ;
}
//-----------------------------------------------------------------------------
@ -885,22 +553,6 @@ void app::webapi(void) { // ToDo
mSys - > InfoCmd = payload [ " cmd " ] ;
DPRINTLN ( DBG_INFO , F ( " Will make tx-request 0x15 with subcmd " ) + String ( mSys - > InfoCmd ) ) ;
}
if ( payload [ " tx_request " ] = = ( uint8_t ) TX_REQ_DEVCONTROL ) {
if ( payload [ " cmd " ] = = ( uint8_t ) ActivePowerContr ) {
uint8_t iv_id = payload [ " inverter " ] ;
if ( iv_id > = 0 & & iv_id < = MAX_NUM_INVERTERS ) {
Inverter < > * iv = this - > mSys - > getInverterByPos ( iv_id ) ;
uint16_t webapiPayload = payload [ " payload " ] ;
if ( webapiPayload > 0 & & webapiPayload < 10000 ) {
iv - > devControlCmd = ActivePowerContr ;
iv - > powerLimit [ 0 ] = webapiPayload ;
iv - > powerLimit [ 1 ] = 0x0000 ; // if power limit is set via external interface --> set it temporay
DPRINTLN ( DBG_INFO , F ( " Power limit for inverter " ) + String ( iv - > id ) + F ( " set to " ) + String ( iv - > powerLimit [ 0 ] ) + F ( " W via REST API " ) ) ;
iv - > devControlRequest = true ;
}
}
}
}
mWeb - > send ( 200 , " text/json " , " {success:true} " ) ;
}
@ -998,13 +650,13 @@ void app::showLiveData(void) {
# endif
}
}
mWeb - > send ( 200 , F ( " text/html " ) , modHtml ) ;
return modHtml ;
}
//-----------------------------------------------------------------------------
void app : : showJSON ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showJSON " ) ) ;
String app : : getJson ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::showJson " ) ) ;
String modJson ;
modJson = F ( " { \n " ) ;
@ -1024,139 +676,17 @@ void app::showJSON(void) {
}
modJson + = F ( " \" json_ts \" : \" " ) + String ( getDateTimeStr ( mTimestamp ) ) + F ( " \" \n } \n " ) ;
// mWeb->send(200, F("text/json"), modJson);
mWeb - > send ( 200 , F ( " application/json " ) , modJson ) ; // the preferred content-type (https://stackoverflow.com/questions/22406077/what-is-the-exact-difference-between-content-type-text-json-and-application-jso)
return modJson ;
}
//-----------------------------------------------------------------------------
void app : : saveValues ( bool webSend = true ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::saveValues " ) ) ;
Main : : saveValues ( false ) ; // general configuration
if ( mWeb - > args ( ) > 0 ) {
char buf [ 20 ] = { 0 } ;
uint8_t i = 0 ;
uint16_t interval ;
uint16_t activepowerlimit = - 1 ;
// inverter
serial_u addr ;
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
// address
mWeb - > arg ( " inv " + String ( i ) + " Addr " ) . toCharArray ( buf , 20 ) ;
if ( strlen ( buf ) = = 0 )
memset ( buf , 0 , 20 ) ;
addr . u64 = Serial2u64 ( buf ) ;
mEep - > write ( ADDR_INV_ADDR + ( i * 8 ) , addr . u64 ) ;
// active power limit
activepowerlimit = mWeb - > arg ( " inv " + String ( i ) + " ActivePowerLimit " ) . toInt ( ) ;
if ( activepowerlimit ! = 0xffff & & activepowerlimit > 0 ) {
mEep - > write ( ADDR_INV_PWR_LIM + i * 2 , activepowerlimit ) ;
}
// name
mWeb - > arg ( " inv " + String ( i ) + " Name " ) . toCharArray ( buf , 20 ) ;
mEep - > write ( ADDR_INV_NAME + ( i * MAX_NAME_LENGTH ) , buf , MAX_NAME_LENGTH ) ;
// max channel power / name
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
uint16_t pwr = mWeb - > arg ( " inv " + String ( i ) + " ModPwr " + String ( j ) ) . toInt ( ) ;
mEep - > write ( ADDR_INV_CH_PWR + ( i * 2 * 4 ) + ( j * 2 ) , pwr ) ;
memset ( buf , 0 , 20 ) ;
mWeb - > arg ( " inv " + String ( i ) + " ModName " + String ( j ) ) . toCharArray ( buf , 20 ) ;
mEep - > write ( ADDR_INV_CH_NAME + ( i * 4 * MAX_NAME_LENGTH ) + j * MAX_NAME_LENGTH , buf , MAX_NAME_LENGTH ) ;
}
}
interval = mWeb - > arg ( " invInterval " ) . toInt ( ) ;
mEep - > write ( ADDR_INV_INTERVAL , interval ) ;
i = mWeb - > arg ( " invRetry " ) . toInt ( ) ;
mEep - > write ( ADDR_INV_MAX_RTRY , i ) ;
// pinout
for ( uint8_t i = 0 ; i < 3 ; i + + ) {
uint8_t pin = mWeb - > arg ( String ( pinArgNames [ i ] ) ) . toInt ( ) ;
mEep - > write ( ADDR_PINOUT + i , pin ) ;
}
// nrf24 amplifier power
mSys - > Radio . AmplifierPower = mWeb - > arg ( " rf24Power " ) . toInt ( ) & 0x03 ;
mEep - > write ( ADDR_RF24_AMP_PWR , mSys - > Radio . AmplifierPower ) ;
// ntp
char ntpAddr [ NTP_ADDR_LEN ] = { 0 } ;
uint16_t ntpPort ;
mWeb - > arg ( " ntpAddr " ) . toCharArray ( ntpAddr , NTP_ADDR_LEN ) ;
ntpPort = mWeb - > arg ( " ntpPort " ) . toInt ( ) ;
mEep - > write ( ADDR_NTP_ADDR , ntpAddr , NTP_ADDR_LEN ) ;
mEep - > write ( ADDR_NTP_PORT , ntpPort ) ;
// mqtt
char mqttAddr [ MQTT_ADDR_LEN ] = { 0 } ;
uint16_t mqttPort ;
char mqttUser [ MQTT_USER_LEN ] ;
char mqttPwd [ MQTT_PWD_LEN ] ;
char mqttTopic [ MQTT_TOPIC_LEN ] ;
mWeb - > arg ( " mqttAddr " ) . toCharArray ( mqttAddr , MQTT_ADDR_LEN ) ;
mWeb - > arg ( " mqttUser " ) . toCharArray ( mqttUser , MQTT_USER_LEN ) ;
mWeb - > arg ( " mqttPwd " ) . toCharArray ( mqttPwd , MQTT_PWD_LEN ) ;
mWeb - > arg ( " mqttTopic " ) . toCharArray ( mqttTopic , MQTT_TOPIC_LEN ) ;
//interval = mWeb->arg("mqttIntvl").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 ) ;
//mEep->write(ADDR_MQTT_INTERVAL, interval);
// serial console
bool tmp ;
interval = mWeb - > arg ( " serIntvl " ) . toInt ( ) ;
mEep - > write ( ADDR_SER_INTERVAL , interval ) ;
tmp = ( mWeb - > arg ( " serEn " ) = = " on " ) ;
mEep - > write ( ADDR_SER_ENABLE , ( uint8_t ) ( ( tmp ) ? 0x01 : 0x00 ) ) ;
mSerialDebug = ( mWeb - > arg ( " serDbg " ) = = " on " ) ;
mEep - > write ( ADDR_SER_DEBUG , ( uint8_t ) ( ( mSerialDebug ) ? 0x01 : 0x00 ) ) ;
DPRINT ( DBG_INFO , " Serial debug is " ) ;
if ( mSerialDebug ) DPRINTLN ( DBG_INFO , " on " ) ; else DPRINTLN ( DBG_INFO , " off " ) ;
mSys - > Radio . mSerialDebug = mSerialDebug ;
updateCrc ( ) ;
mEep - > commit ( ) ;
if ( ( mWeb - > arg ( " reboot " ) = = " on " ) )
showReboot ( ) ;
else {
mShowRebootRequest = true ;
mWeb - > send ( 200 , F ( " text/html " ) , F ( " <!doctype html><html><head><title>Setup saved</title><meta http-equiv= \" refresh \" content= \" 1; URL=/setup \" ></head><body> "
" <p>saved</p></body></html> " ) ) ;
}
}
else {
updateCrc ( ) ;
mEep - > commit ( ) ;
mWeb - > send ( 200 , F ( " text/html " ) , F ( " <!doctype html><html><head><title>Error</title><meta http-equiv= \" refresh \" content= \" 3; URL=/setup \" ></head><body> "
" <p>Error while saving</p></body></html> " ) ) ;
}
bool app : : getWifiApActive ( void ) {
return mWifi - > getApActive ( ) ;
}
//-----------------------------------------------------------------------------
void app : : updateCrc ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::updateCrc " ) ) ;
Main : : updateCrc ( ) ;
uint16_t crc ;
crc = buildEEpCrc ( ADDR_START_SETTINGS , ( ( ADDR_NEXT ) - ( ADDR_START_SETTINGS ) ) ) ;
DPRINTLN ( DBG_DEBUG , F ( " new CRC: " ) + String ( crc , HEX ) ) ;
mEep - > write ( ADDR_SETTINGS_CRC , crc ) ;
}
void app : : sendMqttDiscoveryConfig ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::sendMqttDiscoveryConfig " ) ) ;
@ -1178,7 +708,7 @@ void app::sendMqttDiscoveryConfig(void) {
} else {
snprintf ( name , 32 , " %s CH%d %s " , iv - > name , iv - > assign [ i ] . ch , iv - > getFieldName ( i ) ) ;
}
snprintf ( stateTopic , 64 , " %s/%s/ch%d/%s " , mMqtt . getTopic ( ) , iv - > name , iv - > assign [ i ] . ch , iv - > getFieldName ( i ) ) ;
snprintf ( stateTopic , 64 , " %s/%s/ch%d/%s " , mConfig . mqtt . topic , iv - > name , iv - > assign [ i ] . ch , iv - > getFieldName ( i ) ) ;
snprintf ( discoveryTopic , 64 , " %s/sensor/%s/ch%d_%s/config " , MQTT_DISCOVERY_PREFIX , iv - > name , iv - > assign [ i ] . ch , iv - > getFieldName ( i ) ) ;
snprintf ( uniq_id , 32 , " ch%d_%s " , iv - > assign [ i ] . ch , iv - > getFieldName ( i ) ) ;
const char * devCls = getFieldDeviceClass ( iv - > assign [ i ] . fieldId ) ;
@ -1210,6 +740,8 @@ void app::sendMqttDiscoveryConfig(void) {
}
}
//-----------------------------------------------------------------------------
const char * app : : getFieldDeviceClass ( uint8_t fieldId ) {
uint8_t pos = 0 ;
for ( ; pos < DEVICE_CLS_ASSIGN_LIST_LEN ; pos + + ) {
@ -1219,6 +751,8 @@ const char* app::getFieldDeviceClass(uint8_t fieldId) {
return ( pos > = DEVICE_CLS_ASSIGN_LIST_LEN ) ? NULL : deviceClasses [ deviceFieldAssignment [ pos ] . deviceClsId ] ;
}
//-----------------------------------------------------------------------------
const char * app : : getFieldStateClass ( uint8_t fieldId ) {
uint8_t pos = 0 ;
for ( ; pos < DEVICE_CLS_ASSIGN_LIST_LEN ; pos + + ) {
@ -1227,3 +761,187 @@ const char* app::getFieldStateClass(uint8_t fieldId) {
}
return ( pos > = DEVICE_CLS_ASSIGN_LIST_LEN ) ? NULL : stateClasses [ deviceFieldAssignment [ pos ] . stateClsId ] ;
}
//-----------------------------------------------------------------------------
void app : : resetSystem ( void ) {
mUptimeSecs = 0 ;
mUptimeTicker = 0xffffffff ;
mUptimeInterval = 1000 ;
# ifdef AP_ONLY
mTimestamp = 1 ;
# else
mTimestamp = 0 ;
# endif
mHeapStatCnt = 0 ;
mSendTicker = 0xffff ;
mMqttTicker = 0xffff ;
mMqttInterval = MQTT_INTERVAL ;
mSerialTicker = 0xffff ;
mMqttActive = false ;
mTicker = 0 ;
mRxTicker = 0 ;
mSendLastIvId = 0 ;
mShowRebootRequest = false ;
memset ( mPayload , 0 , ( MAX_NUM_INVERTERS * sizeof ( invPayload_t ) ) ) ;
mRxFailed = 0 ;
mRxSuccess = 0 ;
mFrameCnt = 0 ;
mLastPacketId = 0x00 ;
}
//-----------------------------------------------------------------------------
void app : : loadDefaultConfig ( void ) {
memset ( & mSysConfig , 0 , sizeof ( sysConfig_t ) ) ;
memset ( & mConfig , 0 , sizeof ( config_t ) ) ;
snprintf ( mVersion , 12 , " %d.%d.%d " , VERSION_MAJOR , VERSION_MINOR , VERSION_PATCH ) ;
snprintf ( mSysConfig . deviceName , DEVNAME_LEN , " %s " , DEF_DEVICE_NAME ) ;
// wifi
snprintf ( mSysConfig . stationSsid , SSID_LEN , " %s " , FB_WIFI_SSID ) ;
snprintf ( mSysConfig . stationPwd , PWD_LEN , " %s " , FB_WIFI_PWD ) ;
// nrf24
mConfig . sendInterval = SEND_INTERVAL ;
mConfig . maxRetransPerPyld = DEF_MAX_RETRANS_PER_PYLD ;
mConfig . pinCs = DEF_RF24_CS_PIN ;
mConfig . pinCe = DEF_RF24_CE_PIN ;
mConfig . pinIrq = DEF_RF24_IRQ_PIN ;
mConfig . amplifierPower = DEF_AMPLIFIERPOWER & 0x03 ;
// ntp
snprintf ( mConfig . ntpAddr , NTP_ADDR_LEN , " %s " , DEF_NTP_SERVER_NAME ) ;
mConfig . ntpPort = DEF_NTP_PORT ;
// mqtt
snprintf ( mConfig . mqtt . broker , MQTT_ADDR_LEN , " %s " , DEF_MQTT_BROKER ) ;
mConfig . mqtt . port = DEF_MQTT_PORT ;
snprintf ( mConfig . mqtt . user , MQTT_USER_LEN , " %s " , DEF_MQTT_USER ) ;
snprintf ( mConfig . mqtt . pwd , MQTT_PWD_LEN , " %s " , DEF_MQTT_PWD ) ;
snprintf ( mConfig . mqtt . topic , MQTT_TOPIC_LEN , " %s " , DEF_MQTT_TOPIC ) ;
// serial
mConfig . serialInterval = SERIAL_INTERVAL ;
mConfig . serialShowIv = false ;
mConfig . serialDebug = false ;
}
//-----------------------------------------------------------------------------
void app : : loadEEpconfig ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::loadEEpconfig " ) ) ;
if ( mWifiSettingsValid )
mEep - > read ( ADDR_CFG_SYS , ( uint8_t * ) & mSysConfig , CFG_SYS_LEN ) ;
if ( mSettingsValid ) {
mEep - > read ( ADDR_CFG , ( uint8_t * ) & mConfig , CFG_LEN ) ;
mSendTicker = mConfig . sendInterval ;
mSerialTicker = 0 ;
// inverter
uint64_t invSerial ;
char name [ MAX_NAME_LENGTH + 1 ] = { 0 } ;
uint16_t modPwr [ 4 ] ;
Inverter < > * iv ;
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
mEep - > read ( ADDR_INV_ADDR + ( i * 8 ) , & invSerial ) ;
mEep - > read ( ADDR_INV_NAME + ( i * MAX_NAME_LENGTH ) , name , MAX_NAME_LENGTH ) ;
mEep - > read ( ADDR_INV_CH_PWR + ( i * 2 * 4 ) , modPwr , 4 ) ;
if ( 0ULL ! = invSerial ) {
iv = mSys - > addInverter ( name , invSerial , modPwr ) ;
if ( NULL ! = iv ) {
mEep - > read ( ADDR_INV_PWR_LIM + ( i * 2 ) , ( uint16_t * ) & ( iv - > powerLimit [ 0 ] ) ) ;
if ( iv - > powerLimit [ 0 ] ! = 0xffff ) { // only set it, if it is changed by user. Default value in the html setup page is -1 = 0xffff
iv - > powerLimit [ 1 ] = 0x0001 ; // set the limit as persistent
iv - > devControlCmd = ActivePowerContr ; // set active power limit
iv - > devControlRequest = true ; // set to true to update the active power limit from setup html page
DPRINTLN ( DBG_INFO , F ( " add inverter: " ) + String ( name ) + " , SN: " + String ( invSerial , HEX ) + " , Power Limit: " + String ( iv - > powerLimit [ 0 ] ) ) ;
}
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
mEep - > read ( ADDR_INV_CH_NAME + ( i * 4 * MAX_NAME_LENGTH ) + j * MAX_NAME_LENGTH , iv - > chName [ j ] , MAX_NAME_LENGTH ) ;
}
}
// TODO: the original mqttinterval value is not needed any more
mMqttInterval + = mConfig . sendInterval ;
}
}
}
}
//-----------------------------------------------------------------------------
void app : : saveValues ( void ) {
DPRINTLN ( DBG_VERBOSE , F ( " app::saveValues " ) ) ;
mEep - > write ( ADDR_CFG_SYS , ( uint8_t * ) & mSysConfig , CFG_SYS_LEN ) ;
mEep - > write ( ADDR_CFG , ( uint8_t * ) & mConfig , CFG_LEN ) ;
Inverter < > * iv ;
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
iv = mSys - > getInverterByPos ( i ) ;
if ( NULL ! = iv ) {
mEep - > write ( ADDR_INV_ADDR + ( i * 8 ) , iv - > serial . u64 ) ;
mEep - > write ( ADDR_INV_PWR_LIM + i * 2 , iv - > powerLimit [ 0 ] ) ;
mEep - > write ( ADDR_INV_NAME + ( i * MAX_NAME_LENGTH ) , iv - > name , MAX_NAME_LENGTH ) ;
// max channel power / name
for ( uint8_t j = 0 ; j < 4 ; j + + ) {
mEep - > write ( ADDR_INV_CH_PWR + ( i * 2 * 4 ) + ( j * 2 ) , iv - > chMaxPwr [ j ] ) ;
mEep - > write ( ADDR_INV_CH_NAME + ( i * 4 * MAX_NAME_LENGTH ) + j * MAX_NAME_LENGTH , iv - > chName [ j ] , MAX_NAME_LENGTH ) ;
}
}
}
updateCrc ( ) ;
mEep - > commit ( ) ;
}
//-----------------------------------------------------------------------------
void app : : setupMqtt ( void ) {
if ( mSettingsValid ) {
if ( mConfig . mqtt . broker [ 0 ] > 0 ) {
mMqttActive = true ;
if ( mMqttInterval < MIN_MQTT_INTERVAL )
mMqttInterval = MIN_MQTT_INTERVAL ;
}
else
mMqttInterval = 0xffff ;
mMqttTicker = 0 ;
mMqtt . setup ( & mConfig . mqtt , mSysConfig . deviceName ) ;
mMqtt . setCallback ( std : : bind ( & app : : cbMqtt , this , std : : placeholders : : _1 , std : : placeholders : : _2 , std : : placeholders : : _3 ) ) ;
if ( mMqttActive ) {
mMqtt . sendMsg ( " version " , mVersion ) ;
if ( mMqtt . isConnected ( ) )
mMqtt . sendMsg ( " device " , mSysConfig . deviceName ) ;
/*char topic[30];
for ( uint8_t i = 0 ; i < MAX_NUM_INVERTERS ; i + + ) {
iv = mSys - > getInverterByPos ( i ) ;
if ( NULL ! = iv ) {
for ( uint8_t i = 0 ; i < 4 ; i + + ) {
if ( 0 ! = iv - > chName [ i ] [ 0 ] ) {
snprintf ( topic , 30 , " %s/ch%d/%s " , iv - > name , i + 1 , " name " ) ;
mMqtt . sendMsg ( topic , iv - > chName [ i ] ) ;
yield ( ) ;
}
}
}
} */
}
}
}