Browse Source

fixed MQTT crash during boot if no MQTT was set

added #318 status LED support
pull/421/head
lumapu 2 years ago
parent
commit
700c5c71e4
  1. 47
      tools/esp8266/app.cpp
  2. 5
      tools/esp8266/app.h
  3. 2
      tools/esp8266/config.h
  4. 33
      tools/esp8266/defines.h
  5. 4
      tools/esp8266/html/setup.html
  6. 22
      tools/esp8266/mqtt.h
  7. 12
      tools/esp8266/web.cpp
  8. 2
      tools/esp8266/webApi.cpp

47
tools/esp8266/app.cpp

@ -42,6 +42,8 @@ void app::setup(uint32_t timeout) {
#endif
mSys->setup(mConfig.amplifierPower, mConfig.pinIrq, mConfig.pinCe, mConfig.pinCs);
setupLed();
mWebInst = new web(this, &mSysConfig, &mConfig, &mStat, mVersion);
mWebInst->setup();
mWebInst->setProtection(strlen(mConfig.password) != 0);
@ -263,6 +265,8 @@ void app::loop(void) {
} else if (mConfig.serialDebug)
DPRINTLN(DBG_WARN, F("Time not set or it is night time, therefore no communication to the inverter!"));
yield();
updateLed();
}
}
}
@ -761,6 +765,10 @@ void app::loadDefaultConfig(void) {
mConfig.pinIrq = DEF_IRQ_PIN;
mConfig.amplifierPower = DEF_AMPLIFIERPOWER & 0x03;
// status LED
mConfig.led.led0 = DEF_LED0_PIN;
mConfig.led.led1 = DEF_LED1_PIN;
// ntp
snprintf(mConfig.ntpAddr, NTP_ADDR_LEN, "%s", DEF_NTP_SERVER_NAME);
mConfig.ntpPort = DEF_NTP_PORT;
@ -788,7 +796,7 @@ void app::loadDefaultConfig(void) {
//-----------------------------------------------------------------------------
void app::loadEEpconfig(void) {
DPRINTLN(DBG_VERBOSE, F("app::loadEEpconfig"));
DPRINTLN(DBG_INFO, F("loadEEpconfig"));
if (mWifiSettingsValid)
mEep->read(ADDR_CFG_SYS, (uint8_t *)&mSysConfig, CFG_SYS_LEN);
@ -863,8 +871,10 @@ void app::setupMqtt(void) {
}
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.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);
@ -876,6 +886,37 @@ void app::setupMqtt(void) {
}
}
//-----------------------------------------------------------------------------
void app::setupLed(void) {
/** LED connection diagram
* \\
* PIN ---- |<----- 3.3V
*
* */
if(mConfig.led.led0 != 0xff) {
pinMode(mConfig.led.led0, OUTPUT);
digitalWrite(mConfig.led.led0, HIGH); // LED off
}
if(mConfig.led.led1 != 0xff) {
pinMode(mConfig.led.led1, OUTPUT);
digitalWrite(mConfig.led.led1, HIGH); // LED off
}
}
//-----------------------------------------------------------------------------
void app::updateLed(void) {
if(mConfig.led.led0 != 0xff) {
Inverter<> *iv = mSys->getInverterByPos(0);
if (NULL != iv) {
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
if(iv->isProducing(mUtcTimestamp, rec))
digitalWrite(mConfig.led.led0, LOW); // LED on
else
digitalWrite(mConfig.led.led0, HIGH); // LED off
}
}
}
//-----------------------------------------------------------------------------
void app::resetPayload(Inverter<> *iv) {
DPRINTLN(DBG_INFO, "resetPayload: id: " + String(iv->id));

5
tools/esp8266/app.h

@ -175,11 +175,14 @@ class app {
void resetSystem(void);
void loadDefaultConfig(void);
void loadEEpconfig(void);
void setupMqtt(void);
void setupMqtt(void);
void sendMqttDiscoveryConfig(void);
void sendMqtt(void);
void setupLed(void);
void updateLed(void);
bool buildPayload(uint8_t id);
void processPayload(bool retransmit);

2
tools/esp8266/config.h

@ -49,6 +49,8 @@
#define DEF_CS_PIN 15
#define DEF_CE_PIN 2
#define DEF_IRQ_PIN 0
#define DEF_LED0_PIN 255 // off
#define DEF_LED1_PIN 255 // off
// default NRF24 power, possible values (0 - 3)
#define DEF_AMPLIFIERPOWER 1

33
tools/esp8266/defines.h

@ -81,6 +81,7 @@ typedef enum {
#define CRC_LEN 2 // uint16_t
#define DISCLAIMER_LEN 1
#define STATIC_IP_LEN 16 // 4x uint32_t
#define STATUS_LED_LEN 4 // 4x uint8_t
#define INV_ADDR_LEN MAX_NUM_INVERTERS * 8 // uint64_t
#define INV_NAME_LEN MAX_NUM_INVERTERS * MAX_NAME_LENGTH // char[]
@ -114,8 +115,8 @@ typedef struct {
#pragma pack(pop) // restore original alignment from stack
#pragma pack(push) // push current alignment to stack
#pragma pack(1) // set alignment to 1 byte boundary
#pragma pack(push)
#pragma pack(1)
typedef struct {
char deviceName[DEVNAME_LEN];
@ -123,22 +124,30 @@ typedef struct {
char stationSsid[SSID_LEN];
char stationPwd[PWD_LEN];
} sysConfig_t;
#pragma pack(pop) // restore original alignment from stack
#pragma pack(pop)
#pragma pack(push) // push current alignment to stack
#pragma pack(1) // set alignment to 1 byte boundary
#pragma pack(push)
#pragma pack(1)
typedef struct {
uint8_t ip[4]; // ip address
uint8_t mask[4]; // sub mask
uint8_t dns[4]; // dns
uint8_t gateway[4]; // standard gateway
} staticIp_t;
#pragma pack(pop) // restore original alignment from stack
#pragma pack(pop)
#pragma pack(push)
#pragma pack(1)
typedef struct {
uint8_t led0; // first led pin
uint8_t led1; // second led pin
} statusLed_t;
#pragma pack(pop)
#pragma pack(push) // push current alignment to stack
#pragma pack(1) // set alignment to 1 byte boundary
#pragma pack(push)
#pragma pack(1)
typedef struct {
// protection
char password[PWD_LEN];
@ -173,8 +182,12 @@ typedef struct {
// static ip
staticIp_t staticIp;
// status LED(s)
statusLed_t led;
} config_t;
#pragma pack(pop) // restore original alignment from stack
#pragma pack(pop)
typedef struct {
uint32_t rxFail;
@ -186,7 +199,7 @@ typedef struct {
#define CFG_MQTT_LEN MQTT_ADDR_LEN + 2 + MQTT_USER_LEN + MQTT_PWD_LEN +MQTT_TOPIC_LEN
#define CFG_SYS_LEN DEVNAME_LEN + SSID_LEN + PWD_LEN
#define CFG_LEN PWD_LEN + 7 + DISCLAIMER_LEN + NTP_ADDR_LEN + 2 + CFG_MQTT_LEN + CFG_SUN_LEN + 4 + STATIC_IP_LEN
#define CFG_LEN PWD_LEN + 7 + DISCLAIMER_LEN + NTP_ADDR_LEN + 2 + CFG_MQTT_LEN + CFG_SUN_LEN + 4 + STATIC_IP_LEN + STATUS_LED_LEN
#define ADDR_START 0
#define ADDR_CFG_SYS ADDR_START

4
tools/esp8266/html/setup.html

@ -362,11 +362,12 @@
function parsePinout(obj, type) {
var e = document.getElementById("pinout");
pins = [['cs', 'pinCs'], ['ce', 'pinCe'], ['irq', 'pinIrq']];
pins = [['cs', 'pinCs'], ['ce', 'pinCe'], ['irq', 'pinIrq'], ['led0', 'pinLed0'], ['led1', 'pinLed1']];
for(p of pins) {
e.appendChild(lbl(p[1], p[0].toUpperCase()));
if("ESP8266" == type) {
e.appendChild(sel(p[1], [
[255, "off"],
[0, "D3 (GPIO0)"],
[1, "TX (GPIO1)"],
[2, "D4 (GPIO2)"],
@ -388,6 +389,7 @@
}
else {
e.appendChild(sel(p[1], [
[255, "off"],
[0, "GPIO0"],
[1, "TX (GPIO1)"],
[2, "GPIO2 (LED)"],

22
tools/esp8266/mqtt.h

@ -44,15 +44,17 @@ class mqtt {
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
}
void setCallback(MQTT_CALLBACK_SIGNATURE){
void setCallback(MQTT_CALLBACK_SIGNATURE) {
mClient->setCallback(callback);
}
void sendMsg(const char *topic, const char *msg) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
char top[64];
snprintf(top, 64, "%s/%s", mCfg->topic, topic);
sendMsg2(top, msg, false);
if(mAddressSet) {
char top[64];
snprintf(top, 64, "%s/%s", mCfg->topic, topic);
sendMsg2(top, msg, false);
}
}
void sendMsg2(const char *topic, const char *msg, boolean retained) {
@ -61,12 +63,14 @@ class mqtt {
reconnect();
if(mClient->connected())
mClient->publish(topic, msg, retained);
mTxCnt++;
}
mTxCnt++;
}
bool isConnected(bool doRecon = false) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
if(!mAddressSet)
return false;
if(doRecon && !mClient->connected())
reconnect();
return mClient->connected();
@ -74,9 +78,11 @@ class mqtt {
void loop() {
//DPRINT(F("m"));
if(!mClient->connected())
reconnect();
mClient->loop();
if(mAddressSet) {
if(!mClient->connected())
reconnect();
mClient->loop();
}
}
uint32_t getTxCnt(void) {

12
tools/esp8266/web.cpp

@ -21,7 +21,7 @@
#include "html/h/serial_html.h"
#include "html/h/system_html.h"
const char* const pinArgNames[] = {"pinCs", "pinCe", "pinIrq"};
const char* const pinArgNames[] = {"pinCs", "pinCe", "pinIrq", "pinLed0", "pinLed1"};
//-----------------------------------------------------------------------------
web::web(app *main, sysConfig_t *sysCfg, config_t *config, statistics_t *stat, char version[]) {
@ -390,12 +390,14 @@ void web::showSave(AsyncWebServerRequest *request) {
// pinout
uint8_t pin;
for(uint8_t i = 0; i < 3; i ++) {
for(uint8_t i = 0; i < 5; i ++) {
pin = request->arg(String(pinArgNames[i])).toInt();
switch(i) {
default: mConfig->pinCs = pin; break;
case 1: mConfig->pinCe = pin; break;
case 2: mConfig->pinIrq = pin; break;
default: mConfig->pinCs = pin; break;
case 1: mConfig->pinCe = pin; break;
case 2: mConfig->pinIrq = pin; break;
case 3: mConfig->led.led0 = pin; break;
case 4: mConfig->led.led1 = pin; break;
}
}

2
tools/esp8266/webApi.cpp

@ -267,6 +267,8 @@ void webApi::getPinout(JsonObject obj) {
obj[F("cs")] = mConfig->pinCs;
obj[F("ce")] = mConfig->pinCe;
obj[F("irq")] = mConfig->pinIrq;
obj[F("led0")] = mConfig->led.led0;
obj[F("led1")] = mConfig->led.led1;
}

Loading…
Cancel
Save