Skip to content

Commit

Permalink
Merge branch 'refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
lumapu committed Aug 14, 2022
2 parents d5d6c74 + a5f975b commit 94796ae
Show file tree
Hide file tree
Showing 23 changed files with 1,287 additions and 1,354 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tools/esp8266/binaries
*.ipch
tools/esp8266/.vscode/extensions.json
.DS_Store
.vscode
796 changes: 246 additions & 550 deletions tools/esp8266/app.cpp

Large diffs are not rendered by default.

186 changes: 152 additions & 34 deletions tools/esp8266/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
#ifndef __APP_H__
#define __APP_H__

#include "main.h"
#include "dbg.h"
#include "Arduino.h"


#include <RF24.h>
#include <RF24_config.h>
#include <ArduinoJson.h>

#include "eep.h"
#include "defines.h"
#include "crc.h"

#include "CircularBuffer.h"
#include "hmSystem.h"
#include "mqtt.h"
#include "wifi.h"
#include "web.h"

// hier läst sich das Verhalten der app in Bezug auf MQTT
// durch PER-Conpiler defines anpassen
Expand All @@ -25,7 +32,7 @@
// #define __MQTT_NO_DISCOVERCONFIG__ // das versenden der MQTTDiscoveryConfig abschalten ( gehört eigentlich ins Setup )

typedef CircularBuffer<packet_t, PACKET_BUFFER_SIZE> BufferType;
typedef HmRadio<RF24_CE_PIN, RF24_CS_PIN, RF24_IRQ_PIN, BufferType> RadioType;
typedef HmRadio<DEF_RF24_CE_PIN, DEF_RF24_CS_PIN, BufferType> RadioType;
typedef Inverter<float> InverterType;
typedef HmSystem<RadioType, BufferType, MAX_NUM_INVERTERS, InverterType> HmSystemType;

Expand All @@ -49,43 +56,28 @@ typedef struct {
} invPayload_t;


class app : public Main {
class wifi;
class web;

class app {
public:
app();
~app();
~app() {}

void setup(uint32_t timeout);
void loop(void);
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
String getStatistics(void);
String getLiveData(void);
String getJson(void);
bool getWifiApActive(void);

uint8_t app_loops;
uint8_t getIrqPin(void) {
return mSys->Radio.pinIrq;
return mConfig.pinIrq;
}

private:
bool buildPayload(uint8_t id);
void processPayload(bool retransmit);

void showFavicon(void);
void showIndex(void);
void showSetup(void);
void showSave(void);
void showErase(void);
void showStatistics(void);
void showHoymiles(void);
void showLiveData(void);
void showJSON(void);
void webapi(void);


void saveValues(bool webSend);
void updateCrc(void);
void sendMqttDiscoveryConfig(void);
const char* getFieldDeviceClass(uint8_t fieldId);
const char* getFieldStateClass(uint8_t fieldId);

uint64_t Serial2u64(const char *val) {
char tmp[3] = {0};
uint64_t ret = 0ULL;
Expand All @@ -101,25 +93,152 @@ class app : public Main {
return ret;
}

bool mShowRebootRequest;
String getDateTimeStr(time_t t) {
char str[20] = {0};
if(0 == t)
sprintf(str, "n/a");
else
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
return String(str);
}

inline uint32_t getUptime(void) {
return mUptimeSecs;
}

inline uint32_t getTimestamp(void) {
return mTimestamp;
}

void eraseSettings(bool all = false) {
//DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
uint8_t buf[64] = {0};
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
uint16_t end;
do {
end = addr + 64;
if(end > (ADDR_SETTINGS_CRC + 2))
end = (ADDR_SETTINGS_CRC + 2);
DPRINTLN(DBG_DEBUG, F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX));
mEep->write(addr, buf, (end-addr));
addr = end;
} while(addr < (ADDR_SETTINGS_CRC + 2));
mEep->commit();
}

inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
uint32_t mil = millis();
if(mil >= *ticker) {
*ticker = mil + interval;
return true;
}
else if(mil < (*ticker - interval)) {
*ticker = mil + interval;
return true;
}

return false;
}

HmSystemType *mSys;

private:
void resetSystem(void);
void loadDefaultConfig(void);
void loadEEpconfig(void);
void setupMqtt(void);

bool buildPayload(uint8_t id);
void processPayload(bool retransmit);

void sendMqttDiscoveryConfig(void);
const char* getFieldDeviceClass(uint8_t fieldId);
const char* getFieldStateClass(uint8_t fieldId);

inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
uint8_t buf[32];
uint16_t crc = 0xffff;
uint8_t len;

while(length > 0) {
len = (length < 32) ? length : 32;
mEep->read(start, buf, len);
crc = crc16(buf, len, crc);
start += len;
length -= len;
}
return crc;
}

void updateCrc(void) {
DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
uint16_t crc;

crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
DPRINTLN(DBG_DEBUG, F("new Wifi CRC: ") + String(crc, HEX));
mEep->write(ADDR_WIFI_CRC, crc);

crc = buildEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT) - (ADDR_START_SETTINGS)));
DPRINTLN(DBG_DEBUG, F("new Settings CRC: ") + String(crc, HEX));
mEep->write(ADDR_SETTINGS_CRC, crc);

mEep->commit();
}

bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) {
DPRINTLN(DBG_VERBOSE, F("main.h:checkEEpCrc"));
DPRINTLN(DBG_DEBUG, F("start: ") + String(start) + F(", length: ") + String(length));
uint16_t crcRd, crcCheck;
crcCheck = buildEEpCrc(start, length);
mEep->read(crcPos, &crcRd);
DPRINTLN(DBG_DEBUG, "CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX));
return (crcCheck == crcRd);
}

void stats(void) {
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
uint32_t free;
uint16_t max;
uint8_t frag;
ESP.getHeapStats(&free, &max, &frag);
DPRINT(DBG_VERBOSE, F("free: ") + String(free));
DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%");
DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag));
}


uint32_t mUptimeTicker;
uint16_t mUptimeInterval;
uint32_t mUptimeSecs;
uint8_t mHeapStatCnt;


bool mWifiSettingsValid;
bool mSettingsValid;

eep *mEep;
uint32_t mTimestamp;

bool mShowRebootRequest;

wifi *mWifi;
web *mWebInst;
sysConfig_t mSysConfig;
config_t mConfig;
char mVersion[12];

uint16_t mSendTicker;
uint16_t mSendInterval;
uint8_t mSendLastIvId;

invPayload_t mPayload[MAX_NUM_INVERTERS];
uint32_t mRxFailed;
uint32_t mRxSuccess;
uint32_t mFrameCnt;
uint8_t mLastPacketId;
uint8_t mMaxRetransPerPyld;

// timer
uint32_t mTicker;
bool mSerialValues;
bool mSerialDebug;

uint32_t mRxTicker;

Expand All @@ -132,7 +251,6 @@ class app : public Main {

// serial
uint16_t mSerialTicker;
uint16_t mSerialInterval;
};

#endif /*__APP_H__*/
39 changes: 34 additions & 5 deletions tools/esp8266/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@
// default device name
#define DEF_DEVICE_NAME "AHOY-DTU"

// default pinout
#define DEF_RF24_CS_PIN 15
#define DEF_RF24_CE_PIN 2
#define DEF_RF24_IRQ_PIN 0

// default NRF24 power, possible values (0 - 3)
#define DEF_AMPLIFIERPOWER 2

// number of packets hold in buffer
#define PACKET_BUFFER_SIZE 30

// number of configurable inverters
#define MAX_NUM_INVERTERS 3

// default serial interval
#define SERIAL_INTERVAL 5
#define SERIAL_INTERVAL 5

// default send interval
#define SEND_INTERVAL 30

// default mqtt interval
#define MQTT_INTERVAL 60
#define SEND_INTERVAL 30

// maximum human readable inverter name length
#define MAX_NAME_LENGTH 16
Expand All @@ -64,6 +69,30 @@
// threshold of minimum power on which the inverter is marked as inactive
#define INACT_PWR_THRESH 3

// default ntp server uri
#define DEF_NTP_SERVER_NAME "pool.ntp.org"

// default ntp server port
#define DEF_NTP_PORT 8888

// default mqtt interval
#define MQTT_INTERVAL 60

// default MQTT broker uri
#define DEF_MQTT_BROKER "\0"

// default MQTT port
#define DEF_MQTT_PORT 1883

// default MQTT user
#define DEF_MQTT_USER "\0"

// default MQTT pwd
#define DEF_MQTT_PWD "\0"

// default MQTT topic
#define DEF_MQTT_TOPIC "inverter"

// changes the style of "/setup" page, visualized = nicer
#define LIVEDATA_VISUALIZED

Expand Down
Loading

0 comments on commit 94796ae

Please sign in to comment.