Skip to content

Commit

Permalink
Merge remote-tracking branch 'tbnobody/OpenDTU/master' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Jul 24, 2023
2 parents 18c464e + 92c9544 commit 2440028
Show file tree
Hide file tree
Showing 36 changed files with 216 additions and 151 deletions.
12 changes: 10 additions & 2 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct CONFIG_T {
byte WiFi_Dns2[4];
bool WiFi_Dhcp;
char WiFi_Hostname[WIFI_MAX_HOSTNAME_STRLEN + 1];
uint WiFi_ApTimeout;

char Ntp_Server[NTP_MAX_SERVER_STRLEN + 1];
char Ntp_Timezone[NTP_MAX_TIMEZONE_STRLEN + 1];
Expand All @@ -92,6 +93,7 @@ struct CONFIG_T {
uint8_t Ntp_SunsetType;

bool Mqtt_Enabled;
char Mqtt_Hostname[MQTT_MAX_HOSTNAME_STRLEN + 1];
bool Mqtt_VerboseLogging;
uint Mqtt_Port;
char Mqtt_Username[MQTT_MAX_USERNAME_STRLEN + 1];
Expand All @@ -116,6 +118,8 @@ struct CONFIG_T {
bool Mqtt_Hass_Retain;
char Mqtt_Hass_Topic[MQTT_MAX_TOPIC_STRLEN + 1];
bool Mqtt_Hass_IndividualPanels;
bool Mqtt_Hass_Expire;

bool Mqtt_Tls;
char Mqtt_RootCaCert[MQTT_MAX_CERT_STRLEN + 1];
bool Mqtt_TlsCertLogin;
Expand All @@ -125,9 +129,13 @@ struct CONFIG_T {
bool Vedirect_Enabled;
bool Vedirect_UpdatesOnly;

char Mqtt_Hostname[MQTT_MAX_HOSTNAME_STRLEN + 1];
INVERTER_CONFIG_T Inverter[INV_MAX_COUNT];

bool Mqtt_Hass_Expire;
uint64_t Dtu_Serial;
uint32_t Dtu_PollInterval;
uint8_t Dtu_NrfPaLevel;
int8_t Dtu_CmtPaLevel;
uint32_t Dtu_CmtFrequency;

bool PowerMeter_Enabled;
bool PowerMeter_VerboseLogging;
Expand Down
1 change: 1 addition & 0 deletions include/NetworkSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class NetworkSettingsClass {
bool adminEnabled = true;
bool forceDisconnection = false;
int adminTimeoutCounter = 0;
int adminTimeoutCounterMax = 0;
int connectTimeoutTimer = 0;
int connectRedoTimer = 0;
uint32_t lastTimerCall = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include "WebApi_battery.h"
#include "WebApi_config.h"
#include "WebApi_device.h"
#include "WebApi_devinfo.h"
#include "WebApi_dtu.h"
#include "WebApi_eventlog.h"
#include "WebApi_firmware.h"
#include "WebApi_device.h"
#include "WebApi_inverter.h"
#include "WebApi_limit.h"
#include "WebApi_maintenance.h"
Expand Down
1 change: 1 addition & 0 deletions include/WebApi_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum WebApiError {
NetworkGatewayInvalid,
NetworkDns1Invalid,
NetworkDns2Invalid,
NetworkApTimeoutInvalid,

NtpBase = 9000,
NtpServerLength,
Expand Down
22 changes: 11 additions & 11 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

#define ACCESS_POINT_NAME "OpenDTU-"
#define ACCESS_POINT_PASSWORD "openDTU42"
#define ACCESS_POINT_TIMEOUT 3;
#define AUTH_USERNAME "admin"
#define SECURITY_ALLOW_READONLY true

#define ADMIN_TIMEOUT 180
#define WIFI_RECONNECT_TIMEOUT 15
#define WIFI_RECONNECT_REDO_TIMEOUT 600

Expand All @@ -25,11 +25,11 @@
#define NTP_TIMEZONEDESCR "Europe/Berlin"
#define NTP_LONGITUDE 10.4515f
#define NTP_LATITUDE 51.1657f
#define NTP_SUNSETTYPE 1
#define NTP_SUNSETTYPE 1U

#define MQTT_ENABLED false
#define MQTT_HOST ""
#define MQTT_PORT 1883
#define MQTT_PORT 1883U
#define MQTT_USER ""
#define MQTT_PASSWORD ""
#define MQTT_TOPIC "solar/"
Expand Down Expand Up @@ -73,13 +73,13 @@
#define MQTT_LWT_TOPIC "dtu/status"
#define MQTT_LWT_ONLINE "online"
#define MQTT_LWT_OFFLINE "offline"
#define MQTT_PUBLISH_INTERVAL 5
#define MQTT_PUBLISH_INTERVAL 5U

#define DTU_SERIAL 0x99978563412
#define DTU_POLL_INTERVAL 5
#define DTU_NRF_PA_LEVEL 0
#define DTU_SERIAL 0x99978563412U
#define DTU_POLL_INTERVAL 5U
#define DTU_NRF_PA_LEVEL 0U
#define DTU_CMT_PA_LEVEL 0
#define DTU_CMT_FREQUENCY 865000
#define DTU_CMT_FREQUENCY 865000U

#define MQTT_HASS_ENABLED false
#define MQTT_HASS_EXPIRE true
Expand All @@ -91,9 +91,9 @@

#define DISPLAY_POWERSAFE true
#define DISPLAY_SCREENSAVER true
#define DISPLAY_ROTATION 2
#define DISPLAY_CONTRAST 60
#define DISPLAY_LANGUAGE 0
#define DISPLAY_ROTATION 2U
#define DISPLAY_CONTRAST 60U
#define DISPLAY_LANGUAGE 0U

#define VEDIRECT_ENABLED false
#define VEDIRECT_UPDATESONLY true
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/Hoymiles.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include "HoymilesRadio_NRF.h"
#include "HoymilesRadio_CMT.h"
#include "HoymilesRadio_NRF.h"
#include "inverters/InverterAbstract.h"
#include "types.h"
#include <Print.h>
Expand Down
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/ActivePowerControlCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ void ActivePowerControlCommand::setActivePowerLimit(float limit, PowerLimitContr

// limit
_payload[12] = (l >> 8) & 0xff;
_payload[13] = (l) & 0xff;
_payload[13] = (l)&0xff;

// type
_payload[14] = (type >> 8) & 0xff;
_payload[15] = (type) & 0xff;
_payload[15] = (type)&0xff;

udpateCRC(CRC_SIZE);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/parser/AlarmLogParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "../Hoymiles.h"
#include <cstring>

const std::array<const AlarmMessage_t, ALARM_MSG_COUNT> AlarmLogParser::_alarmMessages = {{
const std::array<const AlarmMessage_t, ALARM_MSG_COUNT> AlarmLogParser::_alarmMessages = { {
{ AlarmMessageType_t::ALL, 1, "Inverter start" },
{ AlarmMessageType_t::ALL, 2, "DTU command failed" },
{ AlarmMessageType_t::ALL, 73, "Temperature >80°C" }, // https://github.com/tbnobody/OpenDTU/discussions/590#discussioncomment-6049750
Expand Down Expand Up @@ -84,7 +84,7 @@ const std::array<const AlarmMessage_t, ALARM_MSG_COUNT> AlarmLogParser::_alarmMe
{ AlarmMessageType_t::ALL, 5200, "Firmware error" },
{ AlarmMessageType_t::ALL, 8310, "Shut down" },
{ AlarmMessageType_t::ALL, 9000, "Microinverter is suspected of being stolen" },
}};
} };

void AlarmLogParser::clearBuffer()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/parser/AlarmLogParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#pragma once
#include "Parser.h"
#include <Arduino.h>
#include <cstdint>
#include <array>
#include <cstdint>

#define ALARM_LOG_ENTRY_COUNT 15
#define ALARM_LOG_ENTRY_SIZE 12
Expand Down
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lib_deps =
bblanchon/ArduinoJson @ ^6.21.2
https://github.com/bertmelis/espMqttClient.git#v1.4.4
nrf24/RF24 @ ^1.4.7
olikraus/U8g2 @ ^2.34.22
olikraus/U8g2 @ ^2.35.3
buelowp/sunset @ ^1.1.7
https://github.com/coryjfowler/MCP_CAN_lib
plerup/EspSoftwareSerial@^8.0.1
Expand Down Expand Up @@ -68,13 +68,15 @@ board = esp32-c3-devkitc-02
custom_patches = esp32c3
build_flags = ${env.build_flags}


[env:generic_esp32c3_usb]
board = esp32-c3-devkitc-02
custom_patches = esp32c3
build_flags = ${env.build_flags}
-DARDUINO_USB_MODE=1
-DARDUINO_USB_CDC_ON_BOOT=1


[env:generic_esp32s3]
board = esp32-s3-devkitc-1
build_flags = ${env.build_flags}
Expand Down
4 changes: 3 additions & 1 deletion src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bool ConfigurationClass::write()
wifi["dns2"] = IPAddress(config.WiFi_Dns2).toString();
wifi["dhcp"] = config.WiFi_Dhcp;
wifi["hostname"] = config.WiFi_Hostname;
wifi["aptimeout"] = config.WiFi_ApTimeout;

JsonObject ntp = doc.createNestedObject("ntp");
ntp["server"] = config.Ntp_Server;
Expand Down Expand Up @@ -252,6 +253,7 @@ bool ConfigurationClass::read()
config.WiFi_Dns2[3] = wifi_dns2[3];

config.WiFi_Dhcp = wifi["dhcp"] | WIFI_DHCP;
config.WiFi_ApTimeout = wifi["aptimeout"] | ACCESS_POINT_TIMEOUT;

JsonObject ntp = doc["ntp"];
strlcpy(config.Ntp_Server, ntp["server"] | NTP_SERVER, sizeof(config.Ntp_Server));
Expand Down Expand Up @@ -436,7 +438,7 @@ void ConfigurationClass::migrate()
config.Mqtt_PublishInterval = mqtt["publish_invterval"];
}

if (config.Cfg_Version < 0x00011900) {
if (config.Cfg_Version < 0x00011900) {
JsonObject dtu = doc["dtu"];
config.Dtu_NrfPaLevel = dtu["pa_level"];
}
Expand Down
2 changes: 1 addition & 1 deletion src/InverterSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// for all generations, this is equivalent to SPI3_HOST in the lower level driver
// For ESP32-C2, the only externally usable HW SPI controller is SPI2, its signal names
// being prefixed with FSPI.
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#define SPI_NRF HSPI
#elif CONFIG_IDF_TARGET_ESP32C3
#define SPI_NRF FSPI
Expand Down
2 changes: 1 addition & 1 deletion src/MqttSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Copyright (C) 2022 Thomas Basler and others
*/
#include "MqttSettings.h"
#include "MessageOutput.h"
#include "Configuration.h"
#include "MessageOutput.h"

MqttSettingsClass::MqttSettingsClass()
{
Expand Down
30 changes: 16 additions & 14 deletions src/NetworkSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ void NetworkSettingsClass::init()
{
using std::placeholders::_1;

WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);

WiFi.onEvent(std::bind(&NetworkSettingsClass::NetworkEvent, this, _1));
setupMode();
}
Expand Down Expand Up @@ -121,11 +124,7 @@ void NetworkSettingsClass::setupMode()
dnsServer->stop();
dnsServerStatus = false;
if (_networkMode == network_mode::WiFi) {
/* // TODO (helge) check if this makes problems to connect to the ap
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
*/
WiFi.mode(WIFI_STA);
WiFi.mode(WIFI_STA);
} else {
WiFi.mode(WIFI_MODE_NULL);
}
Expand All @@ -141,6 +140,7 @@ void NetworkSettingsClass::enableAdminMode()
{
adminEnabled = true;
adminTimeoutCounter = 0;
adminTimeoutCounterMax = Configuration.get().WiFi_ApTimeout * 60;
setupMode();
}

Expand All @@ -160,8 +160,7 @@ void NetworkSettingsClass::loop()
setStaticIp();
setHostname();
}
} else
if (_networkMode != network_mode::WiFi) {
} else if (_networkMode != network_mode::WiFi) {
// Do stuff when switching to Ethernet mode
MessageOutput.println("Switch to WiFi mode");
_networkMode = network_mode::WiFi;
Expand All @@ -170,7 +169,12 @@ void NetworkSettingsClass::loop()
}

if (millis() - lastTimerCall > 1000) {
adminTimeoutCounter++;
if (adminEnabled && adminTimeoutCounterMax > 0) {
adminTimeoutCounter++;
if (adminTimeoutCounter % 10 == 0) {
MessageOutput.printf("Admin AP remaining seconds: %d / %d\r\n", adminTimeoutCounter, adminTimeoutCounterMax);
}
}
connectTimeoutTimer++;
connectRedoTimer++;
lastTimerCall = millis();
Expand All @@ -180,9 +184,9 @@ void NetworkSettingsClass::loop()
if (!isConnected()) {
adminTimeoutCounter = 0;
}
// If WiFi is connected to AP for more than ADMIN_TIMEOUT
// If WiFi is connected to AP for more than adminTimeoutCounterMax
// seconds, disable the internal Access Point
if (adminTimeoutCounter > ADMIN_TIMEOUT) {
if (adminTimeoutCounter > adminTimeoutCounterMax) {
adminEnabled = false;
MessageOutput.println("Admin mode disabled");
setupMode();
Expand Down Expand Up @@ -250,8 +254,7 @@ void NetworkSettingsClass::setHostname()
WiFi.mode(WIFI_MODE_APSTA);
WiFi.mode(WIFI_MODE_STA);
setupMode();
}
else if (_networkMode == network_mode::Ethernet) {
} else if (_networkMode == network_mode::Ethernet) {
if (ETH.setHostname(getHostname().c_str())) {
MessageOutput.println("done");
} else {
Expand All @@ -277,8 +280,7 @@ void NetworkSettingsClass::setStaticIp()
IPAddress(Configuration.get().WiFi_Dns2));
MessageOutput.println("done");
}
}
else if (_networkMode == network_mode::Ethernet) {
} else if (_networkMode == network_mode::Ethernet) {
if (Configuration.get().WiFi_Dhcp) {
MessageOutput.print("Configuring Ethernet DHCP IP... ");
ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
Expand Down
10 changes: 5 additions & 5 deletions src/PinMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
#define JSON_BUFFER_SIZE 6144

#ifndef DISPLAY_TYPE
#define DISPLAY_TYPE 0
#define DISPLAY_TYPE 0U
#endif

#ifndef DISPLAY_DATA
#define DISPLAY_DATA 255
#define DISPLAY_DATA 255U
#endif

#ifndef DISPLAY_CLK
#define DISPLAY_CLK 255
#define DISPLAY_CLK 255U
#endif

#ifndef DISPLAY_CS
#define DISPLAY_CS 255
#define DISPLAY_CS 255U
#endif

#ifndef DISPLAY_RESET
#define DISPLAY_RESET 255
#define DISPLAY_RESET 255U
#endif

#ifndef LED0
Expand Down
3 changes: 2 additions & 1 deletion src/WebApi_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ void WebApiDeviceClass::onDeviceAdminPost(AsyncWebServerRequest* request)
return;
}

if (!(root.containsKey("curPin") || root.containsKey("display"))) {
if (!(root.containsKey("curPin")
|| root.containsKey("display"))) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
response->setLength();
Expand Down
12 changes: 6 additions & 6 deletions src/WebApi_dtu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
return;
}

if (!(root.containsKey("serial") &&
root.containsKey("pollinterval") &&
root.containsKey("verbose_logging") &&
root.containsKey("nrf_palevel") &&
root.containsKey("cmt_palevel") &&
root.containsKey("cmt_frequency"))) {
if (!(root.containsKey("serial")
&& root.containsKey("pollinterval")
&& root.containsKey("verbose_logging")
&& root.containsKey("nrf_palevel")
&& root.containsKey("cmt_palevel")
&& root.containsKey("cmt_frequency"))) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
response->setLength();
Expand Down
Loading

0 comments on commit 2440028

Please sign in to comment.