Skip to content

Commit

Permalink
6.2.0.2 - Energy monitoring
Browse files Browse the repository at this point in the history
6.2.0.2 20180904
 * Rewrite energy monitoring using energy sensor driver modules
 * Fix lost today and total energy value after power cycle (#3689)
  • Loading branch information
arendst committed Sep 4, 2018
1 parent 56ce45b commit 07dafe1
Show file tree
Hide file tree
Showing 14 changed files with 949 additions and 652 deletions.
6 changes: 5 additions & 1 deletion sonoff/_changelog.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* 6.2.0.1 20180902
/* 6.2.0.2 20180904
* Rewrite energy monitoring using energy sensor driver modules
* Fix lost today and total energy value after power cycle (#3689)
*
* 6.2.0.1 20180902
* Fix possible ambiguity on command parameters if StateText contains numbers only (#3656)
* Fix possible exception due to buffer overflow (#3659)
* Add Wifi channel number to state message (#3664)
Expand Down
10 changes: 7 additions & 3 deletions sonoff/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ struct SYSCFG {
// E00 - FFF free locations
} Settings;

struct RTCRBT {
uint16_t valid; // 000
uint8_t fast_reboot_count; // 002
uint8_t free_003[1]; // 003
} RtcReboot;

struct RTCMEM {
uint16_t valid; // 000
byte oswatch_blocked_loop; // 002
Expand All @@ -341,9 +347,7 @@ struct RTCMEM {
unsigned long energy_kWhtotal; // 008
unsigned long pulse_counter[MAX_COUNTERS]; // 00C
power_t power; // 01C
uint16_t extended_valid; // 020 Extended valid flag (v6.1.1.14)
uint8_t fast_reboot_count; // 022
uint8_t free_023[57]; // 023
uint8_t free_020[60]; // 020
// 05C next free location (64 (=core) + 100 (=tasmota offset) + 92 (=0x5C RTCMEM struct) = 256 bytes (max = 512))
} RtcSettings;

Expand Down
43 changes: 38 additions & 5 deletions sonoff/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ void RtcSettingsSave()
{
if (GetRtcSettingsCrc() != rtc_settings_crc) {
RtcSettings.valid = RTC_MEM_VALID;
RtcSettings.extended_valid = RTC_MEM_VALID;
ESP.rtcUserMemoryWrite(100, (uint32_t*)&RtcSettings, sizeof(RTCMEM));
rtc_settings_crc = GetRtcSettingsCrc();
#ifdef DEBUG_THEO
Expand All @@ -104,14 +103,12 @@ void RtcSettingsLoad()
if (RtcSettings.valid != RTC_MEM_VALID) {
memset(&RtcSettings, 0, sizeof(RTCMEM));
RtcSettings.valid = RTC_MEM_VALID;
RtcSettings.extended_valid = RTC_MEM_VALID;
RtcSettings.energy_kWhtoday = Settings.energy_kWhtoday;
RtcSettings.energy_kWhtotal = Settings.energy_kWhtotal;
for (byte i = 0; i < MAX_COUNTERS; i++) {
RtcSettings.pulse_counter[i] = Settings.pulse_counter[i];
}
RtcSettings.power = Settings.power;
// RtcSettings.fast_reboot_count = 0; // Explicit by memset
RtcSettingsSave();
}
rtc_settings_crc = GetRtcSettingsCrc();
Expand All @@ -122,9 +119,45 @@ boolean RtcSettingsValid()
return (RTC_MEM_VALID == RtcSettings.valid);
}

boolean RtcSettingsExtendedValid()
/********************************************************************************************/

uint32_t rtc_reboot_crc = 0;

uint32_t GetRtcRebootCrc()
{
uint32_t crc = 0;
uint8_t *bytes = (uint8_t*)&RtcReboot;

for (uint16_t i = 0; i < sizeof(RTCRBT); i++) {
crc += bytes[i]*(i+1);
}
return crc;
}

void RtcRebootSave()
{
if (GetRtcRebootCrc() != rtc_reboot_crc) {
RtcReboot.valid = RTC_MEM_VALID;
ESP.rtcUserMemoryWrite(100 - sizeof(RTCRBT), (uint32_t*)&RtcReboot, sizeof(RTCRBT));
rtc_reboot_crc = GetRtcRebootCrc();
}
}

void RtcRebootLoad()
{
ESP.rtcUserMemoryRead(100 - sizeof(RTCRBT), (uint32_t*)&RtcReboot, sizeof(RTCRBT));
if (RtcReboot.valid != RTC_MEM_VALID) {
memset(&RtcReboot, 0, sizeof(RTCRBT));
RtcReboot.valid = RTC_MEM_VALID;
// RtcReboot.fast_reboot_count = 0; // Explicit by memset
RtcRebootSave();
}
rtc_reboot_crc = GetRtcRebootCrc();
}

boolean RtcRebootValid()
{
return (RTC_MEM_VALID == RtcSettings.extended_valid);
return (RTC_MEM_VALID == RtcReboot.valid);
}

/*********************************************************************************************\
Expand Down
6 changes: 3 additions & 3 deletions sonoff/sonoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ enum LightTypes {LT_BASIC, LT_PWM1, LT_PWM2, LT_PWM3, LT_PWM4, LT_PWM5, LT_PWM6,
enum LichtSubtypes {LST_NONE, LST_SINGLE, LST_COLDWARM, LST_RGB, LST_RGBW, LST_RGBWC};
enum LichtSchemes {LS_POWER, LS_WAKEUP, LS_CYCLEUP, LS_CYCLEDN, LS_RANDOM, LS_MAX};

enum XsnsFunctions {FUNC_PRE_INIT, FUNC_INIT, FUNC_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_100_MSECOND, FUNC_EVERY_250_MSECOND, FUNC_EVERY_SECOND, FUNC_PREP_BEFORE_TELEPERIOD,
FUNC_JSON_APPEND, FUNC_WEB_APPEND, FUNC_SAVE_BEFORE_RESTART, FUNC_COMMAND, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, FUNC_SET_POWER, FUNC_SHOW_SENSOR,
FUNC_RULES_PROCESS, FUNC_FREE_MEM};
enum XsnsFunctions {FUNC_PRE_INIT, FUNC_INIT, FUNC_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_100_MSECOND, FUNC_EVERY_200_MSECOND, FUNC_EVERY_250_MSECOND, FUNC_EVERY_SECOND, FUNC_PREP_BEFORE_TELEPERIOD,
FUNC_JSON_APPEND, FUNC_WEB_APPEND, FUNC_SAVE_BEFORE_RESTART, FUNC_COMMAND, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, FUNC_SET_POWER, FUNC_SHOW_SENSOR,
FUNC_RULES_PROCESS, FUNC_SERIAL, FUNC_FREE_MEM};

const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };

Expand Down
51 changes: 18 additions & 33 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1
uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted)
uint8_t counter_no_pullup = 0; // Counter input pullup flag (1 = No pullup)
uint8_t dht_flg = 0; // DHT configured
uint8_t energy_flg = 1; // Energy monitor configured
uint8_t energy_flg = 0; // Energy monitor configured
uint8_t i2c_flg = 0; // I2C configured
uint8_t spi_flg = 0; // SPI configured
uint8_t light_type = 0; // Light types
Expand Down Expand Up @@ -1568,8 +1568,8 @@ void PerformEverySecond()
uptime++;

if (BOOT_LOOP_TIME == uptime) {
RtcSettings.fast_reboot_count = 0;
RtcSettingsSave();
RtcReboot.fast_reboot_count = 0;
RtcRebootSave();
}

if ((4 == uptime) && (SONOFF_IFAN02 == Settings.module)) { // Microcontroller needs 3 seconds before accepting commands
Expand Down Expand Up @@ -2193,29 +2193,14 @@ void SerialInput()
}
}

/*-------------------------------------------------------------------------------------------*\
* Sonoff bridge 19200 baud serial interface
\*-------------------------------------------------------------------------------------------*/
if (SONOFF_BRIDGE == Settings.module) {
if (SonoffBridgeSerialInput()) {
serial_in_byte_counter = 0;
Serial.flush();
return;
}
}
/*-------------------------------------------------------------------------------------------*/

#ifdef USE_ENERGY_SENSOR
/*-------------------------------------------------------------------------------------------*\
* Sonoff S31 and Sonoff Pow R2 4800 baud serial interface
\*-------------------------------------------------------------------------------------------*/
if ((SONOFF_S31 == Settings.module) || (SONOFF_POW_R2 == Settings.module)) {
if (CseSerialInput()) {
serial_in_byte_counter = 0;
Serial.flush();
return;
}
if (XdrvCall(FUNC_SERIAL)) {
serial_in_byte_counter = 0;
Serial.flush();
return;
}
#endif // USE_ENERGY_SENSOR

/*-------------------------------------------------------------------------------------------*/

if (serial_in_byte > 127 && !Settings.flag.mqtt_serial_raw) { // binary data...
Expand Down Expand Up @@ -2491,10 +2476,10 @@ void setup()
{
byte idx;

RtcSettingsLoad();
if (!RtcSettingsExtendedValid()) { RtcSettings.fast_reboot_count = 0; }
RtcSettings.fast_reboot_count++;
RtcSettingsSave();
RtcRebootLoad();
if (!RtcRebootValid()) { RtcReboot.fast_reboot_count = 0; }
RtcReboot.fast_reboot_count++;
RtcRebootSave();

Serial.begin(baudrate);
delay(10);
Expand Down Expand Up @@ -2528,26 +2513,26 @@ void setup()
sleep = Settings.sleep;

// Disable functionality as possible cause of fast restart within BOOT_LOOP_TIME seconds (Exception, WDT or restarts)
if (RtcSettings.fast_reboot_count > 1) { // Restart twice
if (RtcReboot.fast_reboot_count > 1) { // Restart twice
Settings.flag3.user_esp8285_enable = 0; // Disable ESP8285 Generic GPIOs interfering with flash SPI
if (RtcSettings.fast_reboot_count > 2) { // Restart 3 times
if (RtcReboot.fast_reboot_count > 2) { // Restart 3 times
for (byte i = 0; i < MAX_RULE_SETS; i++) {
if (bitRead(Settings.rule_stop, i)) {
bitWrite(Settings.rule_enabled, i, 0); // Disable rules causing boot loop
}
}
}
if (RtcSettings.fast_reboot_count > 3) { // Restarted 4 times
if (RtcReboot.fast_reboot_count > 3) { // Restarted 4 times
Settings.rule_enabled = 0; // Disable all rules
}
if (RtcSettings.fast_reboot_count > 4) { // Restarted 5 times
if (RtcReboot.fast_reboot_count > 4) { // Restarted 5 times
Settings.module = SONOFF_BASIC; // Reset module to Sonoff Basic
Settings.last_module = SONOFF_BASIC;
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
Settings.my_gp.io[i] = GPIO_NONE; // Reset user defined GPIO disabling sensors
}
}
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_LOG_SOME_SETTINGS_RESET " (%d)"), RtcSettings.fast_reboot_count);
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_LOG_SOME_SETTINGS_RESET " (%d)"), RtcReboot.fast_reboot_count);
AddLog(LOG_LEVEL_DEBUG);
}

Expand Down
2 changes: 2 additions & 0 deletions sonoff/sonoff_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void KNX_CB_Action(message_t const &msg, void *arg);

#define USE_DHT // Default DHT11 sensor needs no external library
#define USE_ENERGY_SENSOR // Use energy sensors
#define USE_HLW8012 // Use energy sensor for Sonoff Pow and WolfBlitz
#define USE_CSE7766 // Use energy sensor for Sonoff S31 and Pow R2

/*********************************************************************************************\
* [sonoff-sensors.bin]
Expand Down
2 changes: 1 addition & 1 deletion sonoff/sonoff_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef _SONOFF_VERSION_H_
#define _SONOFF_VERSION_H_

#define VERSION 0x06020001
#define VERSION 0x06020002

#define D_PROGRAMNAME "Sonoff-Tasmota"
#define D_AUTHOR "Theo Arends"
Expand Down
Loading

0 comments on commit 07dafe1

Please sign in to comment.