Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring and new user settings for dtu cycle and cloud pause #4

Merged
merged 15 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cmake.configureOnOpen": false
}
34 changes: 34 additions & 0 deletions include/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Config.h
#ifndef CONFIG_H
#define CONFIG_H

#include <EEPROM.h>
#include <Arduino.h>

#define EEPROM_INIT_PATTERN 0xAA

struct UserConfig
{
char dtuSsid[32];
char dtuPassword[32];
char wifiSsid[32];
char wifiPassword[32];
char dtuHostIp[16];
char openhabHostIp[16];
char openItemPrefix[32];
int dtuCloudPauseTime;
boolean dtuCloudPauseActive;
int dtuUpdateTime;
boolean wifiAPstart;
int selectedUpdateChannel; // 0 - release 1 - snapshot
byte eepromInitialized; // specific pattern to determine floating state in EEPROM from Factory
};

extern UserConfig userConfig;

void saveConfigToEEPROM();
void loadConfigFromEEPROM();
void initializeEEPROM();
void printEEPROMdata();

#endif // CONFIG_H
92 changes: 92 additions & 0 deletions include/dtuInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// dtuInterface.h
#ifndef DTUINTERFACE_H
#define DTUINTERFACE_H

#include <Arduino.h>
#include <UnixTime.h>
#include <ESP8266WiFi.h>

#include "pb_encode.h"
#include "pb_decode.h"
#include "AppGetHistPower.pb.h"
#include "RealtimeDataNew.pb.h"
#include "GetConfig.pb.h"
#include "CommandPB.pb.h"
#include "CRC16.h"

// DTU connect
const uint16_t dtuPort = 10081;

#define DTU_TIME_OFFSET 28800
#define DTU_CLOUD_UPLOAD_SECONDS 40

#define DTU_STATE_OFFLINE 0
#define DTU_STATE_CONNECTED 1
#define DTU_STATE_CLOUD_PAUSE 2
#define DTU_STATE_TRY_RECONNECT 3

#define DTU_ERROR_NO_ERROR 0
#define DTU_ERROR_NO_TIME 1
#define DTU_ERROR_TIME_DIFF 2
#define DTU_ERROR_DATA_NO_CHANGE 3
#define DTU_ERROR_LAST_SEND 4

struct connectionControl
{
boolean preventCloudErrors = true;
boolean dtuActiveOffToCloudUpdate = true;
uint8_t dtuConnectState = DTU_STATE_OFFLINE;
uint8_t dtuErrorState = DTU_ERROR_NO_ERROR;
};

extern connectionControl dtuConnection;


struct baseData
{
float current = 0;
float voltage = 0;
float power = 0;
float dailyEnergy = 0;
float totalEnergy = 0;
};

struct inverterData
{
baseData grid;
baseData pv0;
baseData pv1;
float gridFreq = 0;
float inverterTemp = 0;
uint8_t powerLimit = 0;
uint8_t powerLimitSet = 101; // init with not possible value for startup
uint32_t dtuRssi = 0;
uint32_t wifi_rssi_gateway = 0;
uint32_t respTimestamp = 1704063600; // init with start time stamp > 0
uint32_t lastRespTimestamp = 1704063600; // init with start time stamp > 0
boolean uptodate = false;
};

extern inverterData globalData;

extern CRC16 crc;

void initializeCRC();
float calcValue(int32_t value, int32_t divider = 10);
String getTimeStringByTimestamp(unsigned long timestamp);
boolean preventCloudErrorTask(unsigned long locTimeSec);

void readRespAppGetHistPower(WiFiClient *localDtuClient);
void writeReqAppGetHistPower(WiFiClient *localDtuClient, unsigned long locTimeSec);

void readRespRealDataNew(WiFiClient *localDtuClient);
void writeReqRealDataNew(WiFiClient *localDtuClient, unsigned long locTimeSec);

void readRespGetConfig(WiFiClient *localDtuClient);
void writeReqGetConfig(WiFiClient *localDtuClient, unsigned long locTimeSec);

void readRespCommand(WiFiClient *localDtuClient);
void writeReqCommand(WiFiClient *localDtuClient, uint8_t setPercent, unsigned long locTimeSec);


#endif // DTUINTERFACE_H
103 changes: 95 additions & 8 deletions include/index_html.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ const char INDEX_HTML[] PROGMEM = R"=====(
<input type="text" id="openhabIP" class="ipv4Input" name="ipv4" placeholder="xxx.xxx.xxx.xxx">
</div>
<div>
openHab item for PV0 - Power
openHab item prefix for U,I,P,dE,TE per channel:
</div>
<div>
<input type="text" id="oH_item1" maxlength="32">
<input type="text" id="ohItemPrefix" maxlength="32">
</div>
<div style="text-align: center;">
<b onclick="changeOpenhabData()" id="btnSaveWifiSettings" class="form-button btn">save</b>
Expand All @@ -82,7 +82,20 @@ const char INDEX_HTML[] PROGMEM = R"=====(
</div>
<hr>
<div>
dtu local wifi:
dtu request cycle in seconds (data update):
</div>
<div>
<input type="number" id="dtuDataCycle" min="1" max="60" placeholder="31">
</div>
<div>
dtu cloud update pause (no cycle update every full 15 min):
</div>
<div>
<input type="checkbox" id="dtuCloudPause">
</div>
<hr>
<div>
dtu local wireless access point:
</div>
<div>
<input type="text" id="dtuSsid" value="please type in" required maxlength="32">
Expand Down Expand Up @@ -294,7 +307,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
<b id="gwNTPtime" class="panelValueSmall">00:00:00</b>
</div>
<div class="panelValueBoxDetail">
<small class="panelHead">DTU connect</small>
<small class="panelHead">DTU</small>
<b id="dtu_connect_state" class="panelValueSmall valueText"> offline </b>
</div>
<div class="panelValueBoxDetail">
Expand Down Expand Up @@ -395,6 +408,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
if (id == '#changeSettings') {
getWIFIdata();
getDTUdata();
getOHdata();
}
}

Expand Down Expand Up @@ -514,7 +528,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(

var wifiGWPercent = Math.round(data.wifiConnection.rssiGW);
$('#rssitext_local').html(wifiGWPercent + '%');
var wifiDTUPercent = Math.round(data.dtuConnection.rssiDtu);
var wifiDTUPercent = Math.round(data.dtuConnection.dtuRssi);
$('#rssitext_dtu').html(wifiDTUPercent + '%');

$('#firmware').html("fw version: " + data.firmware.version);
Expand Down Expand Up @@ -559,11 +573,30 @@ const char INDEX_HTML[] PROGMEM = R"=====(

// get networkdata
$('#dtuHostIp').val(dtuData.dtuHostIp);
$('#dtuDataCycle').val(dtuData.dtuDataCycle);
if(dtuData.dtuCloudPause) {
$('#dtuCloudPause').prop("checked", true);
} else {
$('#dtuCloudPause').prop("checked", false);
}

$('#dtuSsid').val(dtuData.dtuSsid);
$('#dtuPassword').val(dtuData.dtuPassword);

}

function getOHdata() {
//
$('#btnSaveDtuSettings').css('opacity', '1.0');
$('#btnSaveDtuSettings').attr('onclick', "changeDtuData();")

ohData = cacheInfoData.openHabConnection;

// get networkdata
$('#openhabIP').val(ohData.ohHostIp);
$('#ohItemPrefix').val(ohData.ohItemPrefix);
}

$('.passcheck').click(function () {
console.log("passcheck stat: " + $(this).attr("value") + " - id: " + $(this).attr("id"))
if ($(this).attr("value") == 'invisible') {
Expand Down Expand Up @@ -623,14 +656,24 @@ const char INDEX_HTML[] PROGMEM = R"=====(

function changeDtuData() {
var dtuHostIpSend = $('#dtuHostIp').val();
var dtuDataCycleSend = $('#dtuDataCycle').val();
if($("#dtuCloudPause").is(':checked')) {
dtuCloudPauseSend = 1;
} else {
dtuCloudPauseSend = 0;
}

var dtuSsidSend = $('#dtuSsid').val();
var dtuPasswordSend = $('#dtuPassword').val();

var data = {};
data["dtuHostIpSend"] = dtuHostIpSend;
data["dtuDataCycleSend"] = dtuDataCycleSend;
data["dtuCloudPauseSend"] = dtuCloudPauseSend;
data["dtuSsidSend"] = dtuSsidSend;
data["dtuPasswordSend"] = dtuPasswordSend;

console.log("send to server: dtuHostIp: " + dtuHostIpSend + " - dtuSsid: " + dtuSsidSend + " - pass: " + dtuPasswordSend);
console.log("send to server: dtuHostIp: " + dtuHostIpSend + " dtuDataCycle: " + dtuDataCycleSend + " dtuCloudPause: " + dtuCloudPauseSend + " - dtuSsid: " + dtuSsidSend + " - pass: " + dtuPasswordSend);

const urlEncodedDataPairs = [];

Expand Down Expand Up @@ -674,6 +717,50 @@ const char INDEX_HTML[] PROGMEM = R"=====(
return;
}

function changeOpenhabData() {
var openhabHostIpSend = $('#openhabIP').val();
var data = {};
data["openhabHostIpSend"] = openhabHostIpSend;

console.log("send to server: openhabHostIpSend: " + openhabHostIpSend);

const urlEncodedDataPairs = [];

// Turn the data object into an array of URL-encoded key/value pairs.
for (const [name, value] of Object.entries(data)) {
urlEncodedDataPairs.push(
`${encodeURIComponent(name)}=${encodeURIComponent(value)}`,
);
console.log("push: " + name);
}

// Combine the pairs into a single string and replace all %-encoded spaces to
// the '+' character; matches the behavior of browser form submissions.
const urlEncodedData = urlEncodedDataPairs.join("&").replace(/%20/g, "+");


var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", "/updateOHSettings", false); // false for synchronous request
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

// Finally, send our data.
xmlHttp.send(urlEncodedData);

strResult = JSON.parse(xmlHttp.responseText);
console.log("got from server: " + strResult);
console.log("got from server - strResult.dtuHostIp: " + strResult.openhabHostIp + " - cmp with: " + openhabHostIp);

if (strResult.dtuHostIp == dtuHostIpSend && strResult.dtuSsid == dtuSsidSend && strResult.dtuPassword == dtuPasswordSend) {
console.log("check saved data - OK");
alert("openhab Settings change\n__________________________________\n\nYour settings were successfully changed.\n\nClient connection will be reconnected to the new IP.");
} else {
alert("openhab Settings change\n__________________________________\n\nSome error occured! Checking data from gateway are not as excpeted after sending to save.\n\nPlease try again!");
}

hide('#changeSettings');
return;
}

function changeReleaseChannel(channel) {
if (cacheInfoData.firmware.selectedUpdateChannel == channel) return;

Expand Down Expand Up @@ -787,7 +874,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
function startUpdate() {
hide('#updateMenu');
show('#updateProgress');
$('#newVersionProgress').html(cacheInfoData.versionServer);
$('#newVersionProgress').html(cacheInfoData.firmware.versionServer);

var timeoutStart = 50.0;
var timeout = timeoutStart;
Expand Down Expand Up @@ -852,7 +939,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
function getTime(unix_timestamp, dateTime = "time") {
var date = new Date(unix_timestamp * 1000);
var day = ("0" + date.getDate()).substr(-2);
var mon = ("0" + date.getMonth() + 1).substr(-2);
var mon = ("0" + (date.getMonth() + 1)).substr(-2);
var year = date.getFullYear();
var hours = ("0" + date.getHours()).substr(-2);
var minutes = ("0" + date.getMinutes()).substr(-2);
Expand Down
6 changes: 3 additions & 3 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define VERSION "1.1.6"
#define BUILDTIME "13.02.2024 - 10:31:24"
#define BUILDTIMESTAMP "1707816684"
#define VERSION "1.2.0"
#define BUILDTIME "08.03.2024 - 08:34:47"
#define BUILDTIMESTAMP "1709883287"
8 changes: 4 additions & 4 deletions include/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.1.6",
"versiondate": "13.02.2024 - 10:31:24",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.1.6.bin",
"link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.1.6.bin"
"version": "1.2.0",
"versiondate": "08.03.2024 - 08:34:47",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.2.0.bin",
"link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.2.0.bin"
}
Loading
Loading