Compare commits

...
Author SHA1 Message Date
ESPEasy release bot e060c83cf8 automatically updated release notes for mega-20180719 2018-07-19 04:00:14 +02:00
Gijs NoorlanderandGitHub 5480af2d5d Merge pull request #1587 from TD-er/feature/sunrise_sunset_offset
[sunrise/sunset] Allow offset from sunrise/sunset
2018-07-19 01:07:39 +02:00
TD-er ff28605d39 [sunrise/sunset] Allow offset from sunrise/sunset
`%sunrise%` and `%sunset%` were already available.
Now it is also possible to get some time before or after those times.
For example `%sunrise+10m%` or `%sunset-1h%`
The offset must be an integer (including + or -) and can have a postfix of "m" or "h".
Default is "s", for seconds.
2018-07-19 01:06:06 +02:00
Gijs NoorlanderandGitHub fe0ca9b9a3 Merge pull request #1584 from denisfrench/enable_clientid_is_name_without_unit
When clientid = %sysname%, only add %unit% if non-zero (#1155)
2018-07-18 21:17:06 +02:00
Gijs NoorlanderandGitHub 54ef70aee9 Merge pull request #1586 from beigenmann/mega
lib_archive already exists & Implicit dependency not found
2018-07-18 21:16:03 +02:00
Gijs NoorlanderandGitHub e24d077061 Merge pull request #1583 from clumsy-stefan/P077_fix
Fix for P077 crashes
2018-07-18 21:15:44 +02:00
Benno Eigenmann 7ebf29d857 lib_archive already exists & Implicit dependency not found 2018-07-18 14:02:01 +02:00
denisfrench 33c6925376 When clientid = %sysname%, only add %unit% if non-zero 2018-07-17 19:35:17 +10:00
stefan 0b383a1f3b Fix for P077 crashes
some code rearrangements and improvements:
- fix a rare buffer overflow
- only update variables when new (correct) values are received
- little speed-up
- correct code indentations...
2018-07-16 13:39:51 +02:00
stefan 70d1b0807c Merge remote-tracking branch 'origin/mega' into mega 2018-07-16 07:22:31 +02:00
stefan 76f7dc82fc Merge branch 'bugfix/wifi_reset_empty_IP' into mega 2018-07-15 11:07:47 +02:00
7 changed files with 396 additions and 264 deletions
+19
View File
@@ -1,3 +1,22 @@
-------------------------------------------------
Changes in release mega-20180719 (since mega-20180716)
-------------------------------------------------
Release date: Thu Jul 19 04:00:14 CEST 2018
Benno Eigenmann (1):
lib_archive already exists & Implicit dependency not found
TD-er (1):
[sunrise/sunset] Allow offset from sunrise/sunset
denisfrench (1):
When clientid = %sysname%, only add %unit% if non-zero
stefan (1):
Fix for P077 crashes
-------------------------------------------------
Changes in release mega-20180716 (since mega-20180714)
-------------------------------------------------
+16 -17
View File
@@ -79,7 +79,7 @@ monitor_speed = 115200
[common]
build_flags = -D BUILD_GIT='"${sysenv.TRAVIS_TAG}"' ; ${compiler_warnings.build_flags}
build_flags = -D BUILD_GIT='"${sysenv.TRAVIS_TAG}"'
-D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
-D NDEBUG
-D VTABLES_IN_FLASH
@@ -183,7 +183,6 @@ lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
lib_archive = ${common.lib_archive}
framework = ${common.framework}
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
@@ -416,7 +415,7 @@ build_flags = ${esp8266_1M.build_flags} -D PLUGIN_BUILD_DEV -D FLA
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_build.flash_mode = ${Sonoff.board_build.flash_mode}
;board = ${Sonoff.board}
;build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_BASIC
@@ -430,7 +429,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_build.flash_mode = ${Sonoff.board_build.flash_mode}
;board = ${Sonoff.board}
;build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_TH10
@@ -444,7 +443,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_build.flash_mode = ${Sonoff.board_build.flash_mode}
;board = ${Sonoff.board}
;build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_TH16
@@ -498,7 +497,7 @@ build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_POW_R2
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_build.flash_mode = ${Sonoff.board_build.flash_mode}
;board = ${Sonoff.board}
;build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_S20
@@ -512,7 +511,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_build.flash_mode = ${Sonoff.board_build.flash_mode}
;board = ${Sonoff.board}
;build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_4CH
@@ -526,7 +525,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_build.flash_mode = ${Sonoff.board_build.flash_mode}
;board = ${Sonoff.board}
;build_flags = ${Sonoff.build_flags} -D PLUGIN_SET_SONOFF_TOUCH
@@ -544,7 +543,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -559,7 +558,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -574,7 +573,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -596,7 +595,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -612,7 +611,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -630,7 +629,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -645,7 +644,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -660,7 +659,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
@@ -675,7 +674,7 @@ lib_archive = ${common.lib_archive}
;lib_deps = ${common.lib_deps}
;lib_ignore = ${common.lib_ignore}
;lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
;lib_archive = ${common.lib_archive}
;board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
;board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
;board = esp01_1m
+4 -2
View File
@@ -142,8 +142,10 @@ bool MQTTConnect(int controller_idx)
String clientid;
if(Settings.MQTTUseUnitNameAsClientId){
clientid = Settings.Name;
clientid += F("_");
clientid += Settings.Unit;
if (Settings.Unit != 0) { // only append non-zero unit number
clientid += F("_");
clientid += Settings.Unit;
}
}
else{
clientid = F("ESPClient_");
+30 -3
View File
@@ -1522,21 +1522,48 @@ boolean isInt(const String& tBuf) {
return isNumerical(tBuf, true);
}
String getNumerical(const String& tBuf, bool mustBeInteger) {
String result = "";
const unsigned int bufLength = tBuf.length();
if (bufLength == 0) return result;
boolean decPt = false;
int firstDec = 0;
char c = tBuf.charAt(0);
if(c == '+' || c == '-') {
result += c;
firstDec = 1;
}
for(unsigned int x=firstDec; x < bufLength; ++x) {
c = tBuf.charAt(x);
if(c == '.') {
if (mustBeInteger) return result;
// Only one decimal point allowed
if(decPt) return result;
else decPt = true;
}
else if(c < '0' || c > '9') return result;
result += c;
}
return result;
}
boolean isNumerical(const String& tBuf, bool mustBeInteger) {
const unsigned int bufLength = tBuf.length();
if (bufLength == 0) return false;
boolean decPt = false;
int firstDec = 0;
if(tBuf.charAt(0) == '+' || tBuf.charAt(0) == '-')
char c = tBuf.charAt(0);
if(c == '+' || c == '-')
firstDec = 1;
for(unsigned int x=firstDec; x < bufLength; ++x) {
if(tBuf.charAt(x) == '.') {
c = tBuf.charAt(x);
if(c == '.') {
if (mustBeInteger) return false;
// Only one decimal point allowed
if(decPt) return false;
else decPt = true;
}
else if(tBuf.charAt(x) < '0' || tBuf.charAt(x) > '9') return false;
else if(c < '0' || c > '9') return false;
}
return true;
}
+26 -2
View File
@@ -390,6 +390,7 @@ void parseSpecialCharacters(String& s, boolean useURLencode)
// Simple macro to create the replacement string only when needed.
#define SMART_REPL(T,S) if (s.indexOf(T) != -1) { repl((T), (S), s, useURLencode);}
#define SMART_REPL_T(T,S) if (s.indexOf(T) != -1) { (S((T), s, useURLencode));}
void parseSystemVariables(String& s, boolean useURLencode)
{
parseSpecialCharacters(s, useURLencode);
@@ -442,8 +443,8 @@ void parseSystemVariables(String& s, boolean useURLencode)
SMART_REPL(F("%lcltime_am%"), getDateTimeString_ampm('-',':',' '))
SMART_REPL(F("%uptime%"), String(wdcounter / 2))
SMART_REPL(F("%unixtime%"), String(getUnixTime()))
SMART_REPL(F("%sunrise%"), getSunriseTimeString(':'))
SMART_REPL(F("%sunset%"), getSunsetTimeString(':'))
SMART_REPL_T(F("%sunset"), replSunSetTimeString)
SMART_REPL_T(F("%sunrise"), replSunRiseTimeString)
repl(F("%tskname%"), ExtraTaskSettings.TaskDeviceName, s, useURLencode);
if (s.indexOf(F("%vname")) != -1) {
@@ -454,6 +455,28 @@ void parseSystemVariables(String& s, boolean useURLencode)
}
}
String getReplacementString(const String& format, String& s) {
int startpos = s.indexOf(format);
int endpos = s.indexOf('%', startpos + 1);
String R = s.substring(startpos, endpos + 1);
String log = F("ReplacementString SunTime: ");
log += R;
log += F(" offset: ");
log += getSecOffset(R);
addLog(LOG_LEVEL_DEBUG, log);
return R;
}
void replSunRiseTimeString(const String& format, String& s, boolean useURLencode) {
String R = getReplacementString(format, s);
repl(R, getSunriseTimeString(':', getSecOffset(R)), s, useURLencode);
}
void replSunSetTimeString(const String& format, String& s, boolean useURLencode) {
String R = getReplacementString(format, s);
repl(R, getSunsetTimeString(':', getSecOffset(R)), s, useURLencode);
}
void parseEventVariables(String& s, struct EventStruct *event, boolean useURLencode)
{
SMART_REPL(F("%id%"), String(event->idx))
@@ -468,6 +491,7 @@ void parseEventVariables(String& s, struct EventStruct *event, boolean useURLenc
}
}
}
#undef SMART_REPL_T
#undef SMART_REPL
bool getConvertArgument(const String& marker, const String& s, float& argument, int& startIndex, int& endIndex) {
+57 -1
View File
@@ -17,6 +17,7 @@ uint32_t syncInterval = 3600; // time sync will be attempted after this many se
uint32_t sysTime = 0;
uint32_t prevMillis = 0;
uint32_t nextSyncTime = 0;
timeStruct tsRise, tsSet;
timeStruct sunRise;
timeStruct sunSet;
@@ -62,7 +63,6 @@ void calcSunRiseAndSet() {
float da = diurnalArc(dec, Settings.Latitude);
float rise = 12 - da - eqt - Settings.Longitude / 15.0;
float set = 12 + da - eqt - Settings.Longitude / 15.0;
timeStruct tsRise, tsSet;
tsRise.Hour = (int)rise;
tsRise.Minute = (rise - (int)rise) * 60.0;
tsSet.Hour = (int)set;
@@ -74,6 +74,25 @@ void calcSunRiseAndSet() {
breakTime(toLocal(makeTime(tsSet)), sunSet);
}
timeStruct getSunRise(int secOffset) {
return addSeconds(tsRise, secOffset, true);
}
timeStruct getSunSet(int secOffset) {
return addSeconds(tsSet, secOffset, true);
}
timeStruct addSeconds(const timeStruct& ts, int seconds, bool toLocalTime) {
unsigned long time = makeTime(ts);
time += seconds;
if (toLocalTime) {
time = toLocal(time);
}
timeStruct result;
breakTime(time, result);
return result;
}
void breakTime(unsigned long timeInput, struct timeStruct &tm) {
uint8_t year;
uint8_t month, monthLength;
@@ -142,6 +161,30 @@ uint32_t getUnixTime() {
return sysTime;
}
int getSecOffset(const String& format) {
int position_minus = format.indexOf('-');
int position_plus = format.indexOf('+');
if (position_minus == -1 && position_plus == -1)
return 0;
int sign_position = _max(position_minus, position_plus);
int position_percent = format.indexOf('%', sign_position);
if (position_percent == -1) {
return 0;
}
String valueStr = getNumerical(format.substring(sign_position, position_percent), true);
if (!isInt(valueStr)) return 0;
int value = valueStr.toInt();
switch (format.charAt(position_percent - 1)) {
case 'm':
case 'M':
return value * 60;
case 'h':
case 'H':
return value * 3600;
}
return value;
}
String getSunriseTimeString(char delimiter) {
return getTimeString(sunRise, delimiter, false, false);
}
@@ -150,6 +193,19 @@ String getSunsetTimeString(char delimiter) {
return getTimeString(sunSet, delimiter, false, false);
}
String getSunriseTimeString(char delimiter, int secOffset) {
if (secOffset == 0)
return getSunriseTimeString(delimiter);
return getTimeString(getSunRise(secOffset), delimiter, false, false);
}
String getSunsetTimeString(char delimiter, int secOffset) {
if (secOffset == 0)
return getSunsetTimeString(delimiter);
return getTimeString(getSunSet(secOffset), delimiter, false, false);
}
unsigned long now() {
// calculate number of seconds passed since last call to now()
bool timeSynced = false;
+244 -239
View File
@@ -27,7 +27,7 @@ boolean Plugin_077_init = false;
unsigned long energy_power_calibration = HLW_PREF_PULSE;
unsigned long energy_voltage_calibration = HLW_UREF_PULSE;
unsigned long energy_current_calibration = HLW_IREF_PULSE;
*/
*/
uint8_t cse_receive_flag = 0;
@@ -48,272 +48,277 @@ float energy_power = 0; // 123.1 W
boolean Plugin_077(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
boolean success = false;
switch (function) {
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_077;
Device[deviceCount].VType = SENSOR_TYPE_QUAD;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 4;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_077);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_077));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_077));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_077));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_077));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
addFormNumericBox(F("U Ref"), F("plugin_077_URef"), Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
addUnit(F("uSec"));
addFormNumericBox(F("I Ref"), F("plugin_077_IRef"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addUnit(F("uSec"));
addFormNumericBox(F("P Ref"), F("plugin_077_PRef"), Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
addUnit(F("uSec"));
addFormNote(F("Use 0 to read values stored on chip / default values"));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_077_URef"));;
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_077_IRef"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("plugin_077_PRef"));
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_077_init = true;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 0) Settings.TaskDevicePluginConfig[event->TaskIndex][0] = HLW_UREF_PULSE;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][1] == 0) Settings.TaskDevicePluginConfig[event->TaskIndex][1] = HLW_IREF_PULSE;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][2] == 0) Settings.TaskDevicePluginConfig[event->TaskIndex][2] = HLW_PREF_PULSE;
Settings.UseSerial = true; // Enable Serial port
Settings.SerialLogLevel = 0; // disable logging on serial port (used for CSE7766 communication)
Settings.BaudRate = 4800; // set BaudRate for CSE7766
Serial.flush();
Serial.begin(Settings.BaudRate);
success = true;
break;
}
/* currently not needed!
case PLUGIN_TEN_PER_SECOND:
switch (function) {
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_077;
Device[deviceCount].VType = SENSOR_TYPE_QUAD;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 4;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
long cf_frequency = 0;
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_077);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_077));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_077));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_077));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_077));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
addFormNumericBox(F("U Ref"), F("plugin_077_URef"), Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
addUnit(F("uSec"));
addFormNumericBox(F("I Ref"), F("plugin_077_IRef"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addUnit(F("uSec"));
addFormNumericBox(F("P Ref"), F("plugin_077_PRef"), Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
addUnit(F("uSec"));
addFormNote(F("Use 0 to read values stored on chip / default values"));
if (CSE_PULSES_NOT_INITIALIZED == cf_pulses_last_time) {
cf_pulses_last_time = cf_pulses; // Init after restart
} else {
if (cf_pulses < cf_pulses_last_time) { // Rolled over after 65535 pulses
cf_frequency = (65536 - cf_pulses_last_time) + cf_pulses;
} else {
cf_frequency = cf_pulses - cf_pulses_last_time;
}
if (cf_frequency) {
cf_pulses_last_time = cf_pulses;
// energy_kWhtoday_delta += (cf_frequency * energy_power_calibration) / 36;
// EnergyUpdateToday();
}
}
success = true;
break;
}
*/
case PLUGIN_READ:
{
// We do not actually read as this is already done by reading the serial output
// Instead we just send the last known state stored in Uservar
addLog(LOG_LEVEL_DEBUG_DEV, F("CSE: plugin read"));
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_077_URef"));;
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_077_IRef"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("plugin_077_PRef"));
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_077_init = true;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 0) Settings.TaskDevicePluginConfig[event->TaskIndex][0] = HLW_UREF_PULSE;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][1] == 0) Settings.TaskDevicePluginConfig[event->TaskIndex][1] = HLW_IREF_PULSE;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][2] == 0) Settings.TaskDevicePluginConfig[event->TaskIndex][2] = HLW_PREF_PULSE;
Settings.UseSerial = true; // Enable Serial port
Settings.SerialLogLevel = 0; // disable logging on serial port (used for CSE7766 communication)
Settings.BaudRate = 4800; // set BaudRate for CSE7766
Serial.flush();
Serial.begin(Settings.BaudRate);
success = true;
break;
}
/* currently not needed!
case PLUGIN_TEN_PER_SECOND:
{
long cf_frequency = 0;
if (CSE_PULSES_NOT_INITIALIZED == cf_pulses_last_time) {
cf_pulses_last_time = cf_pulses; // Init after restart
} else {
if (cf_pulses < cf_pulses_last_time) { // Rolled over after 65535 pulses
cf_frequency = (65536 - cf_pulses_last_time) + cf_pulses;
} else {
cf_frequency = cf_pulses - cf_pulses_last_time;
}
if (cf_frequency) {
cf_pulses_last_time = cf_pulses;
// energy_kWhtoday_delta += (cf_frequency * energy_power_calibration) / 36;
// EnergyUpdateToday();
}
}
success = true;
break;
}
*/
case PLUGIN_READ:
{
addLog(LOG_LEVEL_DEBUG_DEV, F("CSE: plugin read"));
// sendData(event);
success = true;
break;
}
// Variables set in PLUGIN_SERIAL_IN as soon as there are new values!
// UserVar[event->BaseVarIndex] = energy_voltage;
// UserVar[event->BaseVarIndex + 1] = energy_power;
// UserVar[event->BaseVarIndex + 2] = energy_current;
// UserVar[event->BaseVarIndex + 3] = cf_pulses;
success = true;
break;
}
case PLUGIN_SERIAL_IN:
{
if (Plugin_077_init) {
if (Serial.available() > 0) {
serial_in_byte = Serial.read();
success = true;
}
case PLUGIN_SERIAL_IN:
{
if (Plugin_077_init) {
if (Serial.available() > 0) {
serial_in_byte = Serial.read();
success = true;
if (cse_receive_flag) {
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
if (24 == serial_in_byte_counter) {
addLog(LOG_LEVEL_DEBUG_DEV, F("CSE: packet received"));
CseReceived(event);
cse_receive_flag = 0;
break;
}
} else {
if (0x5A == serial_in_byte) { // 0x5A - Packet header 2
cse_receive_flag = 1;
addLog(LOG_LEVEL_DEBUG_DEV, F("CSE: Header received"));
} else {
serial_in_byte_counter = 0;
}
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
}
serial_in_byte = 0; // Discard
if (cse_receive_flag) {
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
if (24 == serial_in_byte_counter) {
addLog(LOG_LEVEL_DEBUG_DEV, F("CSE: packet received"));
CseReceived(event);
cse_receive_flag = 0;
serial_in_byte_counter = 0;
// new packet received, update values
UserVar[event->BaseVarIndex] = energy_voltage;
UserVar[event->BaseVarIndex + 1] = energy_power;
UserVar[event->BaseVarIndex + 2] = energy_current;
UserVar[event->BaseVarIndex + 3] = cf_pulses;
// break;
}
} else {
if (0x5A == serial_in_byte) { // 0x5A - Packet header 2
cse_receive_flag = 1;
addLog(LOG_LEVEL_DEBUG_DEV, F("CSE: Header received"));
} else {
serial_in_byte_counter = 0;
}
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
}
serial_in_byte = 0; // Discard
UserVar[event->BaseVarIndex] = energy_voltage;
UserVar[event->BaseVarIndex + 1] = energy_power;
UserVar[event->BaseVarIndex + 2] = energy_current;
UserVar[event->BaseVarIndex + 3] = cf_pulses;
// String log = F("Variable: Tag: ");
// log += key;
// addLog(LOG_LEVEL_INFO, log);
// success = true;
}
break;
}
}
return success;
// String log = F("Variable: Tag: ");
// log += key;
// addLog(LOG_LEVEL_INFO, log);
// success = true;
}
}
break;
}
}
return success;
}
void CseReceived(struct EventStruct *event)
{
String log;
uint8_t header = serial_in_buffer[0];
if ((header & 0xFC) == 0xFC) {
addLog(LOG_LEVEL_DEBUG, F("CSE: Abnormal hardware"));
return;
}
String log;
uint8_t header = serial_in_buffer[0];
if ((header & 0xFC) == 0xFC) {
addLog(LOG_LEVEL_DEBUG, F("CSE: Abnormal hardware"));
return;
}
// Calculate checksum
uint8_t checksum = 0;
for (byte i = 2; i < 23; i++) checksum += serial_in_buffer[i];
if (checksum != serial_in_buffer[23]) {
addLog(LOG_LEVEL_DEBUG, F("CSE: Checksum Failure"));
return;
}
// Calculate checksum
uint8_t checksum = 0;
for (byte i = 2; i < 23; i++) checksum += serial_in_buffer[i];
if (checksum != serial_in_buffer[23]) {
addLog(LOG_LEVEL_DEBUG, F("CSE: Checksum Failure"));
return;
}
// Get chip calibration data (coefficients) and use as initial defaults
if (HLW_UREF_PULSE == Settings.TaskDevicePluginConfig[event->TaskIndex][0]) {
long voltage_coefficient = 191200; // uSec
if (CSE_NOT_CALIBRATED != header) {
voltage_coefficient = serial_in_buffer[2] << 16 | serial_in_buffer[3] << 8 | serial_in_buffer[4];
}
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = voltage_coefficient / CSE_UREF;
}
if (HLW_IREF_PULSE == Settings.TaskDevicePluginConfig[event->TaskIndex][1]) {
long current_coefficient = 16140; // uSec
if (CSE_NOT_CALIBRATED != header) {
current_coefficient = serial_in_buffer[8] << 16 | serial_in_buffer[9] << 8 | serial_in_buffer[10];
}
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = current_coefficient;
}
if (HLW_PREF_PULSE == Settings.TaskDevicePluginConfig[event->TaskIndex][2]) {
long power_coefficient = 5364000; // uSec
if (CSE_NOT_CALIBRATED != header) {
power_coefficient = serial_in_buffer[14] << 16 | serial_in_buffer[15] << 8 | serial_in_buffer[16];
}
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = power_coefficient / CSE_PREF;
}
// Get chip calibration data (coefficients) and use as initial defaults
if (HLW_UREF_PULSE == Settings.TaskDevicePluginConfig[event->TaskIndex][0]) {
long voltage_coefficient = 191200; // uSec
if (CSE_NOT_CALIBRATED != header) {
voltage_coefficient = serial_in_buffer[2] << 16 | serial_in_buffer[3] << 8 | serial_in_buffer[4];
}
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = voltage_coefficient / CSE_UREF;
}
if (HLW_IREF_PULSE == Settings.TaskDevicePluginConfig[event->TaskIndex][1]) {
long current_coefficient = 16140; // uSec
if (CSE_NOT_CALIBRATED != header) {
current_coefficient = serial_in_buffer[8] << 16 | serial_in_buffer[9] << 8 | serial_in_buffer[10];
}
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = current_coefficient;
}
if (HLW_PREF_PULSE == Settings.TaskDevicePluginConfig[event->TaskIndex][2]) {
long power_coefficient = 5364000; // uSec
if (CSE_NOT_CALIBRATED != header) {
power_coefficient = serial_in_buffer[14] << 16 | serial_in_buffer[15] << 8 | serial_in_buffer[16];
}
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = power_coefficient / CSE_PREF;
}
uint8_t adjustement = serial_in_buffer[20];
voltage_cycle = serial_in_buffer[5] << 16 | serial_in_buffer[6] << 8 | serial_in_buffer[7];
current_cycle = serial_in_buffer[11] << 16 | serial_in_buffer[12] << 8 | serial_in_buffer[13];
power_cycle = serial_in_buffer[17] << 16 | serial_in_buffer[18] << 8 | serial_in_buffer[19];
cf_pulses = serial_in_buffer[21] << 8 | serial_in_buffer[22];
uint8_t adjustement = serial_in_buffer[20];
voltage_cycle = serial_in_buffer[5] << 16 | serial_in_buffer[6] << 8 | serial_in_buffer[7];
current_cycle = serial_in_buffer[11] << 16 | serial_in_buffer[12] << 8 | serial_in_buffer[13];
power_cycle = serial_in_buffer[17] << 16 | serial_in_buffer[18] << 8 | serial_in_buffer[19];
cf_pulses = serial_in_buffer[21] << 8 | serial_in_buffer[22];
log = F("CSE: adjustement ");
log += adjustement;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: voltage_cycle ");
log += voltage_cycle;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: current_cycle ");
log += current_cycle;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: power_cycle ");
log += power_cycle;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: cf_pulses ");
log += cf_pulses;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: adjustement ");
log += adjustement;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: voltage_cycle ");
log += voltage_cycle;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: current_cycle ");
log += current_cycle;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: power_cycle ");
log += power_cycle;
addLog(LOG_LEVEL_DEBUG_DEV, log);
log = F("CSE: cf_pulses ");
log += cf_pulses;
addLog(LOG_LEVEL_DEBUG_DEV, log);
// if (energy_power_on) { // Powered on
// if (energy_power_on) { // Powered on
if (adjustement & 0x40) { // Voltage valid
energy_voltage = (float)(Settings.TaskDevicePluginConfig[event->TaskIndex][0] * CSE_UREF) / (float)voltage_cycle;
}
if (adjustement & 0x10) { // Power valid
if ((header & 0xF2) == 0xF2) { // Power cycle exceeds range
energy_power = 0;
} else {
if (0 == power_cycle_first) power_cycle_first = power_cycle; // Skip first incomplete power_cycle
if (power_cycle_first != power_cycle) {
power_cycle_first = -1;
energy_power = (float)(Settings.TaskDevicePluginConfig[event->TaskIndex][2] * CSE_PREF) / (float)power_cycle;
} else {
energy_power = 0;
}
}
} else {
power_cycle_first = 0;
energy_power = 0; // Powered on but no load
}
if (adjustement & 0x20) { // Current valid
if (0 == energy_power) {
energy_current = 0;
} else {
energy_current = (float)Settings.TaskDevicePluginConfig[event->TaskIndex][1] / (float)current_cycle;
}
}
if (adjustement & 0x40) { // Voltage valid
energy_voltage = (float)(Settings.TaskDevicePluginConfig[event->TaskIndex][0] * CSE_UREF) / (float)voltage_cycle;
}
if (adjustement & 0x10) { // Power valid
if ((header & 0xF2) == 0xF2) { // Power cycle exceeds range
energy_power = 0;
} else {
if (0 == power_cycle_first) power_cycle_first = power_cycle; // Skip first incomplete power_cycle
if (power_cycle_first != power_cycle) {
power_cycle_first = -1;
energy_power = (float)(Settings.TaskDevicePluginConfig[event->TaskIndex][2] * CSE_PREF) / (float)power_cycle;
} else {
energy_power = 0;
}
}
} else {
power_cycle_first = 0;
energy_power = 0; // Powered on but no load
}
if (adjustement & 0x20) { // Current valid
if (0 == energy_power) {
energy_current = 0;
} else {
energy_current = (float)Settings.TaskDevicePluginConfig[event->TaskIndex][1] / (float)current_cycle;
}
}
// } else { // Powered off
// power_cycle_first = 0;
// energy_voltage = 0;
// energy_power = 0;
// energy_current = 0;
// }
// } else { // Powered off
// power_cycle_first = 0;
// energy_voltage = 0;
// energy_power = 0;
// energy_current = 0;
// }
log = F("CSE voltage: ");
log += energy_voltage;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE power: ");
log += energy_power;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE current: ");
log += energy_current;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE piulses: ");
log += cf_pulses;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE voltage: ");
log += energy_voltage;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE power: ");
log += energy_power;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE current: ");
log += energy_current;
addLog(LOG_LEVEL_DEBUG, log);
log = F("CSE piulses: ");
log += cf_pulses;
addLog(LOG_LEVEL_DEBUG, log);
}
#endif // USES_P077