mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-24 22:36:10 +02:00
fixed MQTT crash during boot if no MQTT was set
added #318 status LED support
This commit is contained in:
parent
ca8142e781
commit
700c5c71e4
8 changed files with 99 additions and 28 deletions
|
@ -42,6 +42,8 @@ void app::setup(uint32_t timeout) {
|
||||||
#endif
|
#endif
|
||||||
mSys->setup(mConfig.amplifierPower, mConfig.pinIrq, mConfig.pinCe, mConfig.pinCs);
|
mSys->setup(mConfig.amplifierPower, mConfig.pinIrq, mConfig.pinCe, mConfig.pinCs);
|
||||||
|
|
||||||
|
setupLed();
|
||||||
|
|
||||||
mWebInst = new web(this, &mSysConfig, &mConfig, &mStat, mVersion);
|
mWebInst = new web(this, &mSysConfig, &mConfig, &mStat, mVersion);
|
||||||
mWebInst->setup();
|
mWebInst->setup();
|
||||||
mWebInst->setProtection(strlen(mConfig.password) != 0);
|
mWebInst->setProtection(strlen(mConfig.password) != 0);
|
||||||
|
@ -263,6 +265,8 @@ void app::loop(void) {
|
||||||
} else if (mConfig.serialDebug)
|
} else if (mConfig.serialDebug)
|
||||||
DPRINTLN(DBG_WARN, F("Time not set or it is night time, therefore no communication to the inverter!"));
|
DPRINTLN(DBG_WARN, F("Time not set or it is night time, therefore no communication to the inverter!"));
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
|
updateLed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -761,6 +765,10 @@ void app::loadDefaultConfig(void) {
|
||||||
mConfig.pinIrq = DEF_IRQ_PIN;
|
mConfig.pinIrq = DEF_IRQ_PIN;
|
||||||
mConfig.amplifierPower = DEF_AMPLIFIERPOWER & 0x03;
|
mConfig.amplifierPower = DEF_AMPLIFIERPOWER & 0x03;
|
||||||
|
|
||||||
|
// status LED
|
||||||
|
mConfig.led.led0 = DEF_LED0_PIN;
|
||||||
|
mConfig.led.led1 = DEF_LED1_PIN;
|
||||||
|
|
||||||
// ntp
|
// ntp
|
||||||
snprintf(mConfig.ntpAddr, NTP_ADDR_LEN, "%s", DEF_NTP_SERVER_NAME);
|
snprintf(mConfig.ntpAddr, NTP_ADDR_LEN, "%s", DEF_NTP_SERVER_NAME);
|
||||||
mConfig.ntpPort = DEF_NTP_PORT;
|
mConfig.ntpPort = DEF_NTP_PORT;
|
||||||
|
@ -788,7 +796,7 @@ void app::loadDefaultConfig(void) {
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void app::loadEEpconfig(void) {
|
void app::loadEEpconfig(void) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("app::loadEEpconfig"));
|
DPRINTLN(DBG_INFO, F("loadEEpconfig"));
|
||||||
|
|
||||||
if (mWifiSettingsValid)
|
if (mWifiSettingsValid)
|
||||||
mEep->read(ADDR_CFG_SYS, (uint8_t *)&mSysConfig, CFG_SYS_LEN);
|
mEep->read(ADDR_CFG_SYS, (uint8_t *)&mSysConfig, CFG_SYS_LEN);
|
||||||
|
@ -863,8 +871,10 @@ void app::setupMqtt(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
mMqttTicker = 0;
|
mMqttTicker = 0;
|
||||||
|
if(mMqttActive) {
|
||||||
mMqtt.setup(&mConfig.mqtt, mSysConfig.deviceName);
|
mMqtt.setup(&mConfig.mqtt, mSysConfig.deviceName);
|
||||||
mMqtt.setCallback(std::bind(&app::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
mMqtt.setCallback(std::bind(&app::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||||
|
}
|
||||||
|
|
||||||
if (mMqttActive) {
|
if (mMqttActive) {
|
||||||
mMqtt.sendMsg("version", mVersion);
|
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) {
|
void app::resetPayload(Inverter<> *iv) {
|
||||||
DPRINTLN(DBG_INFO, "resetPayload: id: " + String(iv->id));
|
DPRINTLN(DBG_INFO, "resetPayload: id: " + String(iv->id));
|
||||||
|
|
|
@ -175,11 +175,14 @@ class app {
|
||||||
void resetSystem(void);
|
void resetSystem(void);
|
||||||
void loadDefaultConfig(void);
|
void loadDefaultConfig(void);
|
||||||
void loadEEpconfig(void);
|
void loadEEpconfig(void);
|
||||||
void setupMqtt(void);
|
|
||||||
|
|
||||||
|
void setupMqtt(void);
|
||||||
void sendMqttDiscoveryConfig(void);
|
void sendMqttDiscoveryConfig(void);
|
||||||
void sendMqtt(void);
|
void sendMqtt(void);
|
||||||
|
|
||||||
|
void setupLed(void);
|
||||||
|
void updateLed(void);
|
||||||
|
|
||||||
bool buildPayload(uint8_t id);
|
bool buildPayload(uint8_t id);
|
||||||
void processPayload(bool retransmit);
|
void processPayload(bool retransmit);
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,8 @@
|
||||||
#define DEF_CS_PIN 15
|
#define DEF_CS_PIN 15
|
||||||
#define DEF_CE_PIN 2
|
#define DEF_CE_PIN 2
|
||||||
#define DEF_IRQ_PIN 0
|
#define DEF_IRQ_PIN 0
|
||||||
|
#define DEF_LED0_PIN 255 // off
|
||||||
|
#define DEF_LED1_PIN 255 // off
|
||||||
|
|
||||||
// default NRF24 power, possible values (0 - 3)
|
// default NRF24 power, possible values (0 - 3)
|
||||||
#define DEF_AMPLIFIERPOWER 1
|
#define DEF_AMPLIFIERPOWER 1
|
||||||
|
|
|
@ -81,6 +81,7 @@ typedef enum {
|
||||||
#define CRC_LEN 2 // uint16_t
|
#define CRC_LEN 2 // uint16_t
|
||||||
#define DISCLAIMER_LEN 1
|
#define DISCLAIMER_LEN 1
|
||||||
#define STATIC_IP_LEN 16 // 4x uint32_t
|
#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_ADDR_LEN MAX_NUM_INVERTERS * 8 // uint64_t
|
||||||
#define INV_NAME_LEN MAX_NUM_INVERTERS * MAX_NAME_LENGTH // char[]
|
#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(pop) // restore original alignment from stack
|
||||||
|
|
||||||
|
|
||||||
#pragma pack(push) // push current alignment to stack
|
#pragma pack(push)
|
||||||
#pragma pack(1) // set alignment to 1 byte boundary
|
#pragma pack(1)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char deviceName[DEVNAME_LEN];
|
char deviceName[DEVNAME_LEN];
|
||||||
|
|
||||||
|
@ -123,22 +124,30 @@ typedef struct {
|
||||||
char stationSsid[SSID_LEN];
|
char stationSsid[SSID_LEN];
|
||||||
char stationPwd[PWD_LEN];
|
char stationPwd[PWD_LEN];
|
||||||
} sysConfig_t;
|
} sysConfig_t;
|
||||||
#pragma pack(pop) // restore original alignment from stack
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
|
||||||
#pragma pack(push) // push current alignment to stack
|
#pragma pack(push)
|
||||||
#pragma pack(1) // set alignment to 1 byte boundary
|
#pragma pack(1)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t ip[4]; // ip address
|
uint8_t ip[4]; // ip address
|
||||||
uint8_t mask[4]; // sub mask
|
uint8_t mask[4]; // sub mask
|
||||||
uint8_t dns[4]; // dns
|
uint8_t dns[4]; // dns
|
||||||
uint8_t gateway[4]; // standard gateway
|
uint8_t gateway[4]; // standard gateway
|
||||||
} staticIp_t;
|
} 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(push)
|
||||||
#pragma pack(1) // set alignment to 1 byte boundary
|
#pragma pack(1)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// protection
|
// protection
|
||||||
char password[PWD_LEN];
|
char password[PWD_LEN];
|
||||||
|
@ -173,8 +182,12 @@ typedef struct {
|
||||||
|
|
||||||
// static ip
|
// static ip
|
||||||
staticIp_t staticIp;
|
staticIp_t staticIp;
|
||||||
|
|
||||||
|
// status LED(s)
|
||||||
|
statusLed_t led;
|
||||||
} config_t;
|
} config_t;
|
||||||
#pragma pack(pop) // restore original alignment from stack
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t rxFail;
|
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_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_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_START 0
|
||||||
#define ADDR_CFG_SYS ADDR_START
|
#define ADDR_CFG_SYS ADDR_START
|
||||||
|
|
|
@ -362,11 +362,12 @@
|
||||||
|
|
||||||
function parsePinout(obj, type) {
|
function parsePinout(obj, type) {
|
||||||
var e = document.getElementById("pinout");
|
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) {
|
for(p of pins) {
|
||||||
e.appendChild(lbl(p[1], p[0].toUpperCase()));
|
e.appendChild(lbl(p[1], p[0].toUpperCase()));
|
||||||
if("ESP8266" == type) {
|
if("ESP8266" == type) {
|
||||||
e.appendChild(sel(p[1], [
|
e.appendChild(sel(p[1], [
|
||||||
|
[255, "off"],
|
||||||
[0, "D3 (GPIO0)"],
|
[0, "D3 (GPIO0)"],
|
||||||
[1, "TX (GPIO1)"],
|
[1, "TX (GPIO1)"],
|
||||||
[2, "D4 (GPIO2)"],
|
[2, "D4 (GPIO2)"],
|
||||||
|
@ -388,6 +389,7 @@
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
e.appendChild(sel(p[1], [
|
e.appendChild(sel(p[1], [
|
||||||
|
[255, "off"],
|
||||||
[0, "GPIO0"],
|
[0, "GPIO0"],
|
||||||
[1, "TX (GPIO1)"],
|
[1, "TX (GPIO1)"],
|
||||||
[2, "GPIO2 (LED)"],
|
[2, "GPIO2 (LED)"],
|
||||||
|
|
|
@ -50,10 +50,12 @@ class mqtt {
|
||||||
|
|
||||||
void sendMsg(const char *topic, const char *msg) {
|
void sendMsg(const char *topic, const char *msg) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
|
||||||
|
if(mAddressSet) {
|
||||||
char top[64];
|
char top[64];
|
||||||
snprintf(top, 64, "%s/%s", mCfg->topic, topic);
|
snprintf(top, 64, "%s/%s", mCfg->topic, topic);
|
||||||
sendMsg2(top, msg, false);
|
sendMsg2(top, msg, false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void sendMsg2(const char *topic, const char *msg, boolean retained) {
|
void sendMsg2(const char *topic, const char *msg, boolean retained) {
|
||||||
if(mAddressSet) {
|
if(mAddressSet) {
|
||||||
|
@ -61,12 +63,14 @@ class mqtt {
|
||||||
reconnect();
|
reconnect();
|
||||||
if(mClient->connected())
|
if(mClient->connected())
|
||||||
mClient->publish(topic, msg, retained);
|
mClient->publish(topic, msg, retained);
|
||||||
}
|
|
||||||
mTxCnt++;
|
mTxCnt++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool isConnected(bool doRecon = false) {
|
bool isConnected(bool doRecon = false) {
|
||||||
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
|
||||||
|
if(!mAddressSet)
|
||||||
|
return false;
|
||||||
if(doRecon && !mClient->connected())
|
if(doRecon && !mClient->connected())
|
||||||
reconnect();
|
reconnect();
|
||||||
return mClient->connected();
|
return mClient->connected();
|
||||||
|
@ -74,10 +78,12 @@ class mqtt {
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
//DPRINT(F("m"));
|
//DPRINT(F("m"));
|
||||||
|
if(mAddressSet) {
|
||||||
if(!mClient->connected())
|
if(!mClient->connected())
|
||||||
reconnect();
|
reconnect();
|
||||||
mClient->loop();
|
mClient->loop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t getTxCnt(void) {
|
uint32_t getTxCnt(void) {
|
||||||
return mTxCnt;
|
return mTxCnt;
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
#include "html/h/serial_html.h"
|
#include "html/h/serial_html.h"
|
||||||
#include "html/h/system_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[]) {
|
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
|
// pinout
|
||||||
uint8_t pin;
|
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();
|
pin = request->arg(String(pinArgNames[i])).toInt();
|
||||||
switch(i) {
|
switch(i) {
|
||||||
default: mConfig->pinCs = pin; break;
|
default: mConfig->pinCs = pin; break;
|
||||||
case 1: mConfig->pinCe = pin; break;
|
case 1: mConfig->pinCe = pin; break;
|
||||||
case 2: mConfig->pinIrq = pin; break;
|
case 2: mConfig->pinIrq = pin; break;
|
||||||
|
case 3: mConfig->led.led0 = pin; break;
|
||||||
|
case 4: mConfig->led.led1 = pin; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -267,6 +267,8 @@ void webApi::getPinout(JsonObject obj) {
|
||||||
obj[F("cs")] = mConfig->pinCs;
|
obj[F("cs")] = mConfig->pinCs;
|
||||||
obj[F("ce")] = mConfig->pinCe;
|
obj[F("ce")] = mConfig->pinCe;
|
||||||
obj[F("irq")] = mConfig->pinIrq;
|
obj[F("irq")] = mConfig->pinIrq;
|
||||||
|
obj[F("led0")] = mConfig->led.led0;
|
||||||
|
obj[F("led1")] = mConfig->led.led1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue