Skip to content

Commit

Permalink
v3.1.0
Browse files Browse the repository at this point in the history
3.1.0 20161221
* Add Sonoff Pow measurement smoothing
* Fix serial command topic preamble error
* Fix 2.x to 3.x migration inconsistencies
  • Loading branch information
arendst committed Dec 21, 2016
1 parent c95ea3c commit 824054d
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Sonoff-MQTT-OTA-Arduino
Provide ESP8266 based itead Sonoff with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE.

Current version is **3.0.9** - See ```sonoff/_releasenotes.ino``` for change information.
Current version is **3.1.0** - See ```sonoff/_releasenotes.ino``` for change information.

Starting with version 2.0.0 the following hardware devices are supported:
- itead Sonoff
Expand Down
Binary file modified api/arduino/sonoff.ino.bin
Binary file not shown.
7 changes: 6 additions & 1 deletion sonoff/_releasenotes.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/* 3.0.9 20161218
/* 3.1.0 20161221
* Add Sonoff Pow measurement smoothing
* Fix serial command topic preamble error
* Fix 2.x to 3.x migration inconsistencies
*
* 3.0.9 20161218
* Add Sonoff Pow voltage reading when relay is on but no load present
*
* 3.0.8 20161218
Expand Down
183 changes: 106 additions & 77 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* ====================================================
*/

#define VERSION 0x03000900 // 3.0.9
#define VERSION 0x03010000 // 3.1.0

#define SONOFF 1 // Sonoff, Sonoff SV, Sonoff Dual, Sonoff TH 10A/16A, S20 Smart Socket, 4 Channel
#define SONOFF_POW 9 // Sonoff Pow
Expand Down Expand Up @@ -400,9 +400,8 @@ boolean udpConnected = false;

/********************************************************************************************/

void CFG_Default()
void CFG_DefaultSet()
{
addLog_P(LOG_LEVEL_NONE, PSTR("Config: Use default configuration"));
memset(&sysCfg, 0x00, sizeof(SYSCFG));

sysCfg.cfg_holder = CFG_HOLDER;
Expand Down Expand Up @@ -484,96 +483,123 @@ void CFG_Default()
sysCfg.hlw_msplw = SAFE_POWER_WINDOW;
sysCfg.hlw_mkwh = 0; // MaxEnergy
sysCfg.hlw_mkwhs = 0; // MaxEnergyStart
}

void CFG_Default()
{
addLog_P(LOG_LEVEL_NONE, PSTR("Config: Use default configuration"));
CFG_DefaultSet();
CFG_Save();
}

void CFG_Migrate_Part2()
{
addLog_P(LOG_LEVEL_NONE, PSTR("Config: Migrating configuration"));
memset(&sysCfg, 0x00, sizeof(SYSCFG));

sysCfg.cfg_holder = CFG_HOLDER;
sysCfg.saveFlag = 0;
sysCfg.version = VERSION;
sysCfg.bootcount = sysCfg2.bootcount;
sysCfg.migflag = 0;
sysCfg.savedata = sysCfg2.savedata;
sysCfg.savestate = sysCfg2.savestate;
sysCfg.model = sysCfg2.model;
sysCfg.timezone = sysCfg2.timezone;
strlcpy(sysCfg.otaUrl, sysCfg2.otaUrl, sizeof(sysCfg.otaUrl));
strlcpy(sysCfg.friendlyname, sysCfg2.mqtt_client, sizeof(sysCfg.friendlyname));
CFG_DefaultSet();

sysCfg.seriallog_level = sysCfg2.seriallog_level;
sysCfg.sta_active = sysCfg2.sta_active;
sysCfg.syslog_level = sysCfg2.syslog_level;
strlcpy(sysCfg.syslog_host, sysCfg2.syslog_host, sizeof(sysCfg.syslog_host));
strlcpy(sysCfg.sta_ssid[0], sysCfg2.sta_ssid1, sizeof(sysCfg.sta_ssid[0]));
strlcpy(sysCfg.sta_pwd[0], sysCfg2.sta_pwd1, sizeof(sysCfg.sta_pwd[0]));
strlcpy(sysCfg.sta_ssid[1], sysCfg2.sta_ssid2, sizeof(sysCfg.sta_ssid[1]));
strlcpy(sysCfg.sta_pwd[1], sysCfg2.sta_pwd2, sizeof(sysCfg.sta_pwd[1]));
strlcpy(sysCfg.hostname, sysCfg2.hostname, sizeof(sysCfg.hostname));
sysCfg.sta_config = sysCfg2.sta_config;
strlcpy(sysCfg.syslog_host, sysCfg2.syslog_host, sizeof(sysCfg.syslog_host));
sysCfg.syslog_port = sysCfg2.syslog_port;
sysCfg.syslog_level = sysCfg2.syslog_level;
sysCfg.webserver = sysCfg2.webserver;
sysCfg.weblog_level = sysCfg2.weblog_level;

strlcpy(sysCfg.mqtt_fingerprint, sysCfg2.mqtt_fingerprint, sizeof(sysCfg.mqtt_fingerprint));
strlcpy(sysCfg.otaUrl, sysCfg2.otaUrl, sizeof(sysCfg.otaUrl));
strlcpy(sysCfg.mqtt_host, sysCfg2.mqtt_host, sizeof(sysCfg.mqtt_host));
sysCfg.mqtt_port = sysCfg2.mqtt_port;
strlcpy(sysCfg.mqtt_client, sysCfg2.mqtt_client, sizeof(sysCfg.mqtt_client));
strlcpy(sysCfg.mqtt_user, sysCfg2.mqtt_user, sizeof(sysCfg.mqtt_user));
strlcpy(sysCfg.mqtt_pwd, sysCfg2.mqtt_pwd, sizeof(sysCfg.mqtt_pwd));
strlcpy(sysCfg.mqtt_grptopic, sysCfg2.mqtt_grptopic, sizeof(sysCfg.mqtt_grptopic));
strlcpy(sysCfg.mqtt_topic, sysCfg2.mqtt_topic, sizeof(sysCfg.mqtt_topic));
strlcpy(sysCfg.mqtt_topic2, sysCfg2.mqtt_topic2, sizeof(sysCfg.mqtt_topic2));
strlcpy(sysCfg.mqtt_grptopic, sysCfg2.mqtt_grptopic, sizeof(sysCfg.mqtt_grptopic));
strlcpy(sysCfg.mqtt_subtopic, sysCfg2.mqtt_subtopic, sizeof(sysCfg.mqtt_subtopic));
sysCfg.mqtt_button_retain = sysCfg2.mqtt_retain;
sysCfg.mqtt_power_retain = MQTT_POWER_RETAIN;
sysCfg.mqtt_units = sysCfg2.mqtt_units;
sysCfg.message_format = sysCfg2.message_format;
sysCfg.tele_period = sysCfg2.tele_period;
if ((sysCfg.tele_period > 0) && (sysCfg.tele_period < 10)) sysCfg.tele_period = 10; // Do not allow periods < 10 seconds

sysCfg.timezone = sysCfg2.timezone;
sysCfg.power = sysCfg2.power;
sysCfg.pulsetime = APP_PULSETIME;
sysCfg.ledstate = sysCfg2.ledstate;
sysCfg.switchmode = sysCfg2.switchmode;

strlcpy(sysCfg.domoticz_in_topic, sysCfg2.domoticz_in_topic, sizeof(sysCfg.domoticz_in_topic));
strlcpy(sysCfg.domoticz_out_topic, sysCfg2.domoticz_out_topic, sizeof(sysCfg.domoticz_out_topic));
sysCfg.domoticz_update_timer = sysCfg2.domoticz_update_timer;
sysCfg.domoticz_relay_idx[0] = sysCfg2.domoticz_relay_idx[0];
sysCfg.domoticz_relay_idx[1] = sysCfg2.domoticz_relay_idx[1];
sysCfg.domoticz_relay_idx[2] = sysCfg2.domoticz_relay_idx[2];
sysCfg.domoticz_relay_idx[3] = sysCfg2.domoticz_relay_idx[3];
sysCfg.domoticz_key_idx[0] = sysCfg2.domoticz_key_idx[0];
sysCfg.domoticz_key_idx[1] = sysCfg2.domoticz_key_idx[1];
sysCfg.domoticz_key_idx[2] = sysCfg2.domoticz_key_idx[2];
sysCfg.domoticz_key_idx[3] = sysCfg2.domoticz_key_idx[3];

sysCfg.hlw_pcal = sysCfg2.hlw_pcal;
sysCfg.hlw_ucal = sysCfg2.hlw_ucal;
sysCfg.hlw_ical = sysCfg2.hlw_ical;
sysCfg.hlw_kWhtoday = sysCfg2.hlw_kWhtoday;
sysCfg.hlw_kWhyesterday = sysCfg2.hlw_kWhyesterday;
sysCfg.hlw_kWhdoy = sysCfg2.hlw_kWhdoy;
sysCfg.hlw_pmin = sysCfg2.hlw_pmin;
sysCfg.hlw_pmax = sysCfg2.hlw_pmax;
sysCfg.hlw_umin = sysCfg2.hlw_umin;
sysCfg.hlw_umax = sysCfg2.hlw_umax;
sysCfg.hlw_imin = sysCfg2.hlw_imin;
sysCfg.hlw_imax = sysCfg2.hlw_imax;
sysCfg.hlw_mpl = sysCfg2.hlw_mpl; // MaxPowerLimit
sysCfg.hlw_mplh = sysCfg2.hlw_mplh;
sysCfg.hlw_mplw = sysCfg2.hlw_mplw;
sysCfg.hlw_mspl = sysCfg2.hlw_mspl; // MaxSafePowerLimit
sysCfg.hlw_msplh = SAFE_POWER_HOLD;
sysCfg.hlw_msplw = sysCfg2.hlw_msplw;
sysCfg.hlw_mkwh = sysCfg.hlw_mkwh; // MaxEnergy
sysCfg.hlw_mkwhs = sysCfg.hlw_mkwhs; // MaxEnergyStart

if (sysCfg2.version >= 0x01000D00) { // 1.0.13
sysCfg.ledstate = sysCfg2.ledstate;
}
if (sysCfg2.version >= 0x01001600) { // 1.0.22
sysCfg.mqtt_port = sysCfg2.mqtt_port;
strlcpy(sysCfg.mqtt_client, sysCfg2.mqtt_client, sizeof(sysCfg.mqtt_client));
strlcpy(sysCfg.mqtt_user, sysCfg2.mqtt_user, sizeof(sysCfg.mqtt_user));
strlcpy(sysCfg.mqtt_pwd, sysCfg2.mqtt_pwd, sizeof(sysCfg.mqtt_pwd));
strlcpy(sysCfg.friendlyname, sysCfg2.mqtt_client, sizeof(sysCfg.friendlyname));
}
if (sysCfg2.version >= 0x01001700) { // 1.0.23
sysCfg.webserver = sysCfg2.webserver;
}
if (sysCfg2.version >= 0x01001A00) { // 1.0.26
sysCfg.bootcount = sysCfg2.bootcount;
strlcpy(sysCfg.hostname, sysCfg2.hostname, sizeof(sysCfg.hostname));
sysCfg.syslog_port = sysCfg2.syslog_port;
}
if (sysCfg2.version >= 0x01001B00) { // 1.0.27
sysCfg.weblog_level = sysCfg2.weblog_level;
}
if (sysCfg2.version >= 0x01001C00) { // 1.0.28
sysCfg.tele_period = sysCfg2.tele_period;
if ((sysCfg.tele_period > 0) && (sysCfg.tele_period < 10)) sysCfg.tele_period = 10; // Do not allow periods < 10 seconds
}
if (sysCfg2.version >= 0x01002000) { // 1.0.32
sysCfg.sta_config = sysCfg2.sta_config;
}
if (sysCfg2.version >= 0x01002300) { // 1.0.35
sysCfg.savedata = sysCfg2.savedata;
}
if (sysCfg2.version >= 0x02000000) { // 2.0.0
sysCfg.model = sysCfg2.model;
}
if (sysCfg2.version >= 0x02000300) { // 2.0.3
sysCfg.mqtt_button_retain = sysCfg2.mqtt_retain;
sysCfg.savestate = sysCfg2.savestate;
}
if (sysCfg2.version >= 0x02000500) { // 2.0.5
sysCfg.hlw_pcal = sysCfg2.hlw_pcal;
sysCfg.hlw_ucal = sysCfg2.hlw_ucal;
sysCfg.hlw_ical = sysCfg2.hlw_ical;
sysCfg.hlw_kWhyesterday = sysCfg2.hlw_kWhyesterday;
sysCfg.mqtt_units = sysCfg2.mqtt_units;
}
if (sysCfg2.version >= 0x02000600) { // 2.0.6
sysCfg.hlw_pmin = sysCfg2.hlw_pmin;
sysCfg.hlw_pmax = sysCfg2.hlw_pmax;
sysCfg.hlw_umin = sysCfg2.hlw_umin;
sysCfg.hlw_umax = sysCfg2.hlw_umax;
sysCfg.hlw_imin = sysCfg2.hlw_imin;
sysCfg.hlw_imax = sysCfg2.hlw_imax;
}
if (sysCfg2.version >= 0x02000700) { // 2.0.7
sysCfg.message_format = sysCfg2.message_format;
strlcpy(sysCfg.domoticz_in_topic, sysCfg2.domoticz_in_topic, sizeof(sysCfg.domoticz_in_topic));
strlcpy(sysCfg.domoticz_out_topic, sysCfg2.domoticz_out_topic, sizeof(sysCfg.domoticz_out_topic));
sysCfg.domoticz_update_timer = sysCfg2.domoticz_update_timer;
sysCfg.domoticz_relay_idx[0] = sysCfg2.domoticz_relay_idx[0];
sysCfg.domoticz_relay_idx[1] = sysCfg2.domoticz_relay_idx[1];
sysCfg.domoticz_relay_idx[2] = sysCfg2.domoticz_relay_idx[2];
sysCfg.domoticz_relay_idx[3] = sysCfg2.domoticz_relay_idx[3];
sysCfg.domoticz_key_idx[0] = sysCfg2.domoticz_key_idx[0];
sysCfg.domoticz_key_idx[1] = sysCfg2.domoticz_key_idx[1];
sysCfg.domoticz_key_idx[2] = sysCfg2.domoticz_key_idx[2];
sysCfg.domoticz_key_idx[3] = sysCfg2.domoticz_key_idx[3];
sysCfg.hlw_mpl = sysCfg2.hlw_mpl; // MaxPowerLimit
sysCfg.hlw_mplh = sysCfg2.hlw_mplh;
sysCfg.hlw_mplw = sysCfg2.hlw_mplw;
sysCfg.hlw_mspl = sysCfg2.hlw_mspl; // MaxSafePowerLimit
sysCfg.hlw_msplh = sysCfg2.hlw_msplh;
sysCfg.hlw_msplw = sysCfg2.hlw_msplw;
sysCfg.hlw_mkwh = sysCfg2.hlw_mkwh; // MaxEnergy
sysCfg.hlw_mkwhs = sysCfg2.hlw_mkwhs; // MaxEnergyStart
}
if (sysCfg2.version >= 0x02001000) { // 2.0.16
sysCfg.hlw_kWhtoday = sysCfg2.hlw_kWhtoday;
sysCfg.hlw_kWhdoy = sysCfg2.hlw_kWhdoy;
}
if (sysCfg2.version >= 0x02001200) { // 2.0.18
sysCfg.switchmode = sysCfg2.switchmode;
}
if (sysCfg2.version >= 0x02010000) { // 2.1.0
strlcpy(sysCfg.mqtt_fingerprint, sysCfg2.mqtt_fingerprint, sizeof(sysCfg.mqtt_fingerprint));
}
if (sysCfg2.version >= 0x02010200) { // 2.1.2
sysCfg.sta_active = sysCfg2.sta_active;
strlcpy(sysCfg.sta_ssid[1], sysCfg2.sta_ssid2, sizeof(sysCfg.sta_ssid[1]));
strlcpy(sysCfg.sta_pwd[1], sysCfg2.sta_pwd2, sizeof(sysCfg.sta_pwd[1]));
}
CFG_Save();
}

Expand Down Expand Up @@ -1675,9 +1701,12 @@ void do_cmnd_power(byte device, byte state)
void do_cmnd(char *cmnd)
{
char stopic[TOPSZ], svalue[128];
char *start;
char *token;

token = strtok(cmnd, " ");
start = strrchr(token, '/'); // Skip possible cmnd/sonoff/ preamble
if (start) token = start;
snprintf_P(stopic, sizeof(stopic), PSTR("%s/%s/%s"), SUB_PREFIX, sysCfg.mqtt_topic, token);
token = strtok(NULL, "");
snprintf_P(svalue, sizeof(svalue), PSTR("%s"), (token == NULL) ? "" : token);
Expand Down
2 changes: 1 addition & 1 deletion sonoff/support.ino
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void CFG_Migrate()
if (sysCfg2.saveFlag < _sysCfgH.saveFlag)
spi_flash_read((CFG_LOCATION2 + 1) * SPI_FLASH_SEC_SIZE, (uint32*)&sysCfg2, sizeof(SYSCFG2));
interrupts();
snprintf_P(log, sizeof(log), PSTR("Config: Loaded previous configuration from flash at %X and count %d"), CFG_LOCATION + (sysCfg2.saveFlag &1), sysCfg2.saveFlag);
snprintf_P(log, sizeof(log), PSTR("Config: Loaded previous configuration from flash at %X and count %d"), CFG_LOCATION2 + (sysCfg2.saveFlag &1), sysCfg2.saveFlag);
addLog(LOG_LEVEL_DEBUG, log);
}
}
Expand Down
64 changes: 39 additions & 25 deletions sonoff/xsns_hlw8012.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,36 @@ POSSIBILITY OF SUCH DAMAGE.
#define HLW_IREF 4545 // 4.545A

byte hlw_SELflag, hlw_cf_timer, hlw_cf1_timer, hlw_fifth_second, hlw_startup;
unsigned long hlw_cf_plen, hlw_cf_last, hlw_cf_active;
unsigned long hlw_cf1_plen, hlw_cf1_last;
unsigned long hlw_cf1u_plen, hlw_cf1i_plen;
unsigned long hlw_cf_plen, hlw_cf_last;
unsigned long hlw_cf1_plen, hlw_cf1_last, hlw_cf1_ptot, hlw_cf1_pcnt, hlw_cf1u_plen, hlw_cf1i_plen;
unsigned long hlw_Ecntr, hlw_EDcntr, hlw_kWhtoday;
uint32_t hlw_lasttime;

unsigned long hlw_cf1u_pcntmax, hlw_cf1i_pcntmax;

Ticker tickerHLW;

void hlw_cf_interrupt() ICACHE_RAM_ATTR;
void hlw_cf1_interrupt() ICACHE_RAM_ATTR;

void hlw_cf_interrupt()
void hlw_cf_interrupt() // Service Power
{
hlw_cf_plen = micros() - hlw_cf_last;
hlw_cf_last = micros();
hlw_cf_active = 1;
if (hlw_cf_plen > 4000000) hlw_cf_plen = 0; // Just powered on
hlw_cf_timer = 15; // Support down to 4W which takes about 3 seconds
hlw_EDcntr++;
hlw_Ecntr++;
}

void hlw_cf1_interrupt()
void hlw_cf1_interrupt() // Service Voltage and Current
{
hlw_cf1_plen = micros() - hlw_cf1_last;
hlw_cf1_last = micros();
if ((hlw_cf1_timer > 2) && (hlw_cf1_timer < 7)) {
if (hlw_SELflag) {
hlw_cf1i_plen = hlw_cf1_plen;
} else {
hlw_cf1u_plen = hlw_cf1_plen;
}
if ((hlw_cf1_timer > 2) && (hlw_cf1_timer < 8)) { // Allow for 300 mSec set-up time and measure for up to 1 second
hlw_cf1_ptot += hlw_cf1_plen;
hlw_cf1_pcnt++;
if (hlw_cf1_pcnt == 10) hlw_cf1_timer = 8; // We need up to ten samples within 1 second (low current could take up to 0.3 second)
}
}

Expand Down Expand Up @@ -92,18 +92,31 @@ void hlw_200mS()
}
}

hlw_cf_timer--;
if (!hlw_cf_timer) {
hlw_cf_timer = 15; // 4W = 3 Sec
if (!hlw_cf_active) hlw_cf_plen = 0; // No load for over three seconds
hlw_cf_active = 0;
if (hlw_cf_timer) {
hlw_cf_timer--;
if (!hlw_cf_timer) hlw_cf_plen = 0; // No load for over three seconds
}

hlw_cf1_timer--;
if (!hlw_cf1_timer) {
hlw_cf1_timer = 10;
hlw_cf1_timer++;
if (hlw_cf1_timer >= 8) {
hlw_cf1_timer = 0;
hlw_SELflag = (hlw_SELflag) ? 0 : 1;
digitalWrite(HLW_SEL, hlw_SELflag);

if (hlw_cf1_pcnt) {
hlw_cf1_plen = hlw_cf1_ptot / hlw_cf1_pcnt;
} else {
hlw_cf1_plen = 0;
}
if (hlw_SELflag) {
hlw_cf1u_plen = hlw_cf1_plen;
hlw_cf1u_pcntmax = hlw_cf1_pcnt;
} else {
hlw_cf1i_plen = hlw_cf1_plen;
hlw_cf1i_pcntmax = hlw_cf1_pcnt;
}
hlw_cf1_ptot = 0;
hlw_cf1_pcnt = 0;
}
}

Expand All @@ -119,7 +132,7 @@ boolean hlw_readEnergy(byte option, float &ed, uint16_t &e, uint16_t &w, uint16_
int hlw_period, hlw_interval;

//char log[LOGSZ];
//snprintf_P(log, sizeof(log), PSTR("HLW: CF %d, CF1U %d, CF1I %d"), hlw_cf_plen, hlw_cf1u_plen, hlw_cf1i_plen);
//snprintf_P(log, sizeof(log), PSTR("HLW: CF %d, CF1U %d (%d), CF1I %d (%d)"), hlw_cf_plen, hlw_cf1u_plen, hlw_cf1u_pcntmax, hlw_cf1i_plen, hlw_cf1i_pcntmax);
//addLog(LOG_LEVEL_DEBUG, log);

if (hlw_kWhtoday) {
Expand Down Expand Up @@ -191,6 +204,8 @@ void hlw_init()
hlw_cf1_last = 0;
hlw_cf1u_plen = 0;
hlw_cf1i_plen = 0;
hlw_cf1u_pcntmax = 0;
hlw_cf1i_pcntmax = 0;

hlw_Ecntr = 0;
hlw_EDcntr = 0;
Expand All @@ -208,10 +223,9 @@ void hlw_init()
hlw_startup = 1;
hlw_lasttime = 0;
hlw_fifth_second = 0;
hlw_cf_timer = 1;
hlw_cf_active = 0;
hlw_cf1_timer = 1;
hlw_cf_timer = 0;
hlw_cf1_timer = 0;
tickerHLW.attach_ms(200, hlw_200mS);
}

#endif // USE_POWERMONITOR

0 comments on commit 824054d

Please sign in to comment.