Add command ``SspmPowerOnState<relay> 0|1|2``

Add command ``SspmPowerOnState<relay> 0|1|2`` to set Sonoff SPM 4Relay module v1.2.0 power on state overruling tasmota  global power on state. 0 = Off, 1 = On, 2 = Saved state (#13447)
This commit is contained in:
Theo Arends 2022-10-16 12:57:17 +02:00
parent 0bd57a3787
commit 480fae6804
3 changed files with 164 additions and 33 deletions

View File

@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file.
## [12.1.1.6]
### Added
- Command ``WcClock 10..200`` set webcam clock in MHz. Default is 20
- Automatically resize FS to max flash size at initial boot (ESP32x)
- ESP32 Automatically resize FS to max flash size at initial boot (#16838)
- Command ``SspmPowerOnState<relay> 0|1|2`` to set Sonoff SPM 4Relay module v1.2.0 power on state overruling tasmota global power on state. 0 = Off, 1 = On, 2 = Saved state (#13447)
### Changed

View File

@ -116,6 +116,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
- Command ``UrlFetch <url>`` to download a file to filesystem
- Command ``DspSpeed 2..127`` to control message rotation speed on display of POWR3xxD and THR3xxD
- Command ``DspLine<1|2> <index>,<unit>,<index>,<unit>,...`` to select message(s) on display of POWR3xxD and THR3xxD
- Command ``SspmPowerOnState<relay> 0|1|2`` to set Sonoff SPM 4Relay module v1.2.0 power on state overruling tasmota global power on state. 0 = Off, 1 = On, 2 = Saved state [#13447](https://github.com/arendst/Tasmota/issues/13447)
- Command ``Sunrise 0..3`` to select sunrise dawn angle between Normal, Civil, Nautical or Astronomical [#16795](https://github.com/arendst/Tasmota/issues/16795)
- Command ``WcClock 10..200`` set webcam clock in MHz. Default is 20
- Support for Shelly Plus 2PM
@ -133,6 +134,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
- Support for Ethernet in ESP32 safeboot firmware [#16388](https://github.com/arendst/Tasmota/issues/16388)
- ESP32-S3 support for internal temperature sensor
- ESP32-S2 and ESP32-S3 touch button support
- ESP32 Automatically resize FS to max flash size at initial boot [#16838](https://github.com/arendst/Tasmota/issues/16838)
- Berry has persistent MQTT subscriptions: auto-subscribe at (re)connection
- Berry automated solidification of code
- LVGL/HASPmota add tiny "pixel perfect" fonts for small screens [#16758](https://github.com/arendst/Tasmota/issues/16758)

View File

@ -146,7 +146,10 @@
#define SSPM_FUNC_RESET 28 // 0x1C - Remove device from eWelink and factory reset
#define SSPM_FUNC_UPLOAD_DATA 31 // 0x1F - SPI Upload incremental data blocks of max 512 bytes to ARM
#define SSPM_FUNC_UPLOAD_DONE 33 // 0x21 - SPI Finish upload
#define SSPM_FUNC_GET_NEW1 37 // 0x25
#define SSPM_FUNC_34 34 // 0x22 - v1.2.0
#define SSPM_FUNC_GET_OPS_DEFAULTS 35 // 0x23 - v1.2.0 - Get Overload protection defaults
#define SSPM_FUNC_SET_POS 36 // 0x24 - v1.2.0 - Save power on relay state
#define SSPM_FUNC_GET_POS 37 // 0x25 - v1.2.0 - Read power on relay state
// From ARM to ESP
#define SSPM_FUNC_ENERGY_RESULT 6 // 0x06
@ -166,17 +169,14 @@
#define SSPM_FUNC_23 23 // 0x17
#define SSPM_FUNC_29 29 // 0x1D
#define SSPM_FUNC_32 32 // 0x20
#define SSPM_FUNC_34 34 // 0x22
#define SSPM_FUNC_35 35 // 0x23
#define SSPM_FUNC_36 36 // 0x24
#define SSPM_GPIO_ARM_RESET 15
#define SSPM_GPIO_LED_ERROR 33
#define SSPM_MODULE_NAME_SIZE 12
#define SSPM_MAIN_V1_0_0 0x00010000
#define SSPM_MAIN_V1_2_0 0x00010200
#define SSPM_VERSION_1_0_0 0x00010000
#define SSPM_VERSION_1_2_0 0x00010200
/*********************************************************************************************/
@ -264,6 +264,7 @@ typedef struct {
uint32_t timeout;
uint32_t main_version;
uint32_t relay_version;
power_t old_power;
power_t power_on_state;
uint16_t last_totals;
@ -271,11 +272,12 @@ typedef struct {
uint16_t expected_bytes;
uint8_t module[SSPM_MAX_MODULES][SSPM_MODULE_NAME_SIZE];
uint8_t history_day[SSPM_MAX_MODULES][4];
uint8_t poweron_state[SSPM_MAX_MODULES][4];
#ifdef SSPM_SIMULATE
uint8_t simulate;
uint8_t simulated_module;
#endif
#endif // SSPM_SIMULATE
uint8_t allow_updates;
uint8_t get_energy_relay;
int8_t get_energy_relay_focus;
@ -406,7 +408,7 @@ uint32_t SSPMGetMappedModuleId(uint32_t module) {
module_nr = 0; // Emulate modules by 0
}
}
#endif
#endif // SSPM_SIMULATE
return (uint32_t)module_nr; // 0, 1, ...
}
@ -912,7 +914,68 @@ void SSPMSendGetEnergyPeriod(uint32_t relay) {
}
void SSPMSendGetNew1(uint32_t module) {
void SSPMSendFunc34(uint32_t module) {
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 00 22 00 00 f2 6a 7f
Marker |Module id |Ac|Cm|Size |Ix|Chksm|
*/
if (module >= Sspm->module_max) { return; }
SSPMInitSend();
memcpy(SspmBuffer +3, Sspm->module[SSPMGetMappedModuleId(module)], SSPM_MODULE_NAME_SIZE);
SspmBuffer[16] = SSPM_FUNC_34; // 0x22
Sspm->command_sequence++;
SspmBuffer[19] = Sspm->command_sequence;
SSPMSend(22);
}
void SSPMSendGetOPSDefaults(uint32_t module) {
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 00 23 00 00 f4 94 fe
Marker |Module id |Ac|Cm|Size |Ix|Chksm|
*/
if (module >= Sspm->module_max) { return; }
SSPMInitSend();
memcpy(SspmBuffer +3, Sspm->module[SSPMGetMappedModuleId(module)], SSPM_MODULE_NAME_SIZE);
SspmBuffer[16] = SSPM_FUNC_GET_OPS_DEFAULTS; // 0x23
Sspm->command_sequence++;
SspmBuffer[19] = Sspm->command_sequence;
SSPMSend(22);
}
void SSPMSendSetPowerOnState(uint32_t module) {
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 00 24 00 05 0f 00 01 02 01 00 f3 c2
Marker |Module id |Ac|Cm|Size |??|P1|P2|P3|P4|Ix|Chksm|
P1 - Relay1 power on state (0 = On, 1 = Off, 2 = Laststate)
P2 - Relay2 power on state (0 = On, 1 = Off, 2 = Laststate)
P3 - Relay3 power on state (0 = On, 1 = Off, 2 = Laststate)
P4 - Relay4 power on state (0 = On, 1 = Off, 2 = Laststate)
*/
if (module >= Sspm->module_max) { return; }
SSPMInitSend();
memcpy(SspmBuffer +3, Sspm->module[SSPMGetMappedModuleId(module)], SSPM_MODULE_NAME_SIZE);
SspmBuffer[16] = SSPM_FUNC_SET_POS; // 0x24
SspmBuffer[18] = 0x05;
SspmBuffer[19] = 0x0F;
SspmBuffer[20] = Sspm->poweron_state[module][0];
SspmBuffer[21] = Sspm->poweron_state[module][1];
SspmBuffer[22] = Sspm->poweron_state[module][2];
SspmBuffer[23] = Sspm->poweron_state[module][3];
Sspm->command_sequence++;
SspmBuffer[24] = Sspm->command_sequence;
SSPMSend(27);
}
void SSPMSendGetPowerOnState(uint32_t module) {
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
aa 55 01 6b 7e 32 37 39 37 34 13 4b 35 36 37 00 25 00 00 08 c0 0a
@ -922,7 +985,7 @@ void SSPMSendGetNew1(uint32_t module) {
SSPMInitSend();
memcpy(SspmBuffer +3, Sspm->module[SSPMGetMappedModuleId(module)], SSPM_MODULE_NAME_SIZE);
SspmBuffer[16] = SSPM_FUNC_GET_NEW1; // 0x25
SspmBuffer[16] = SSPM_FUNC_GET_POS; // 0x25
Sspm->command_sequence++;
SspmBuffer[19] = Sspm->command_sequence;
@ -955,6 +1018,11 @@ void SSPMAddModule(void) {
}
Sspm->map_change = true;
}
uint32_t relay_version = SspmBuffer[36] << 16 | SspmBuffer[37] << 8 | SspmBuffer[38]; // 0x00010000 or 0x00010200
if (relay_version < Sspm->relay_version) {
Sspm->relay_version = relay_version; // Lowest version will be supported
}
mapped++;
AddLog(LOG_LEVEL_INFO, PSTR("SPM: 4Relay %d (mapped to %d) type %d version %d.%d.%d found with id %12_H"),
Sspm->module_max +1, mapped, SspmBuffer[35], SspmBuffer[36], SspmBuffer[37], SspmBuffer[38], Sspm->module[Sspm->module_max]);
@ -1086,8 +1154,8 @@ void SSPMHandleReceivedData(void) {
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR("SSPMOverload"));
Sspm->overload_relay = 255;
} else {
if (Sspm->main_version > SSPM_MAIN_V1_0_0) {
SSPMSendGetNew1(Sspm->module_selected -1);
if (Sspm->main_version > SSPM_VERSION_1_0_0) {
SSPMSendGetPowerOnState(Sspm->module_selected -1);
} else {
Sspm->module_selected--;
if (Sspm->module_selected > 0) {
@ -1109,7 +1177,7 @@ void SSPMHandleReceivedData(void) {
uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[3] << 8 | SspmBuffer[4]);
#ifdef SSPM_SIMULATE
if (Sspm->Settings.simulate_count) { module = Sspm->simulated_module; }
#endif
#endif // SSPM_SIMULATE
power_t current_state = (SspmBuffer[20] >> 4) << (module * 4); // Relays state
power_t mask = 0x0000000F << (module * 4);
TasmotaGlobal.power &= (POWER_MASK ^ mask);
@ -1206,7 +1274,7 @@ void SSPMHandleReceivedData(void) {
uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[20] << 8 | SspmBuffer[21]);
#ifdef SSPM_SIMULATE
if (Sspm->Settings.simulate_count) { module = Sspm->simulated_module; }
#endif
#endif // SSPM_SIMULATE
if (Sspm->history_relay < 255) {
uint32_t history_module = Sspm->history_relay >> 2;
uint32_t history_channel = Sspm->history_relay & 0x03; // Channel relays are NOT bit masked this time
@ -1359,16 +1427,53 @@ void SSPMHandleReceivedData(void) {
case SSPM_FUNC_RESET:
/* 0x1C
AA 55 01 00 00 00 00 00 00 00 00 00 00 00 00 80 1c 00 01 00 0b f9 e3
Marker |Module id |Ac|Cm|Size |St|Ix|Chksm|
*/
// TasmotaGlobal.restart_flag = 2;
break;
case SSPM_FUNC_GET_NEW1:
/* 0x25 v1.2.0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21 22 23
AA 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 25 00 01 01 06 98 06
case SSPM_FUNC_34:
/* 0x22 v1.2.0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 22 00 02 00 00 f2 19 00
Marker |Module id |Ac|Cm|Size |St| |Ix|Chksm|
*/
break;
case SSPM_FUNC_GET_OPS_DEFAULTS:
/* 0x23 v1.2.0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 23 00 11 00 14 00 00 0a 01 08 00 00 5a 00 12 c0 00 00 00 0a f4 7f 4d
Marker |Module id |Ac|Cm|Size |St|Max I|Min I|Max U |Min U |Max P |Min P |Ix|Chksm|
|OK|20.0A|0.10A| 240.00V| 0.10V|4400.00W| 0.10W|
*/
break;
case SSPM_FUNC_SET_POS:
/* 0x24 v1.2.0
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 24 00 01 00 00 80 a8
Marker |Module id |Ac|Cm|Size |St|Ix|Chksm|
*/
break;
case SSPM_FUNC_GET_POS:
/* 0x25 v1.2.0 - Get Power On State
Response v1.0.0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
AA 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 25 00 01 01 06 98 06
Marker |Module id |Ac|Cm|Size |St|Ix|Chksm|
Response v1.2.0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
aa 55 01 8b 34 32 37 39 37 34 13 4b 35 36 37 80 25 00 05 00 00 01 02 01 07 b6 89
Marker |Module id |Ac|Cm|Size |St|P1|P2|P3|P4|Ix|Chksm|
P1 - Relay1 power on state (0 = On, 1 = Off, 2 = Laststate)
P2 - Relay2 power on state (0 = On, 1 = Off, 2 = Laststate)
P3 - Relay3 power on state (0 = On, 1 = Off, 2 = Laststate)
P4 - Relay4 power on state (0 = On, 1 = Off, 2 = Laststate)
*/
Sspm->module_selected--;
if (!status && (0x05 == expected_bytes)) {
for (uint32_t i = 0; i < 4; i++) {
Sspm->poweron_state[Sspm->module_selected][i] = SspmBuffer[20 +i];
}
}
if (Sspm->module_selected > 0) {
SSPMSendGetModuleState(Sspm->module_selected -1);
} else {
@ -1402,7 +1507,7 @@ void SSPMHandleReceivedData(void) {
uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[19] << 8 | SspmBuffer[20]);
#ifdef SSPM_SIMULATE
if (Sspm->Settings.simulate_count) { module = Sspm->simulated_module; }
#endif
#endif // SSPM_SIMULATE
Sspm->current[module][channel] = SspmBuffer[32] + (float)SspmBuffer[33] / 100; // x.xxA
Sspm->voltage[module][channel] = SSPMGetValue(&SspmBuffer[34]); // x.xxV
Sspm->active_power[module][channel] = SSPMGetValue(&SspmBuffer[37]); // x.xxW
@ -1428,7 +1533,7 @@ void SSPMHandleReceivedData(void) {
uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[19] << 8 | SspmBuffer[20]);
#ifdef SSPM_SIMULATE
// if (Sspm->Settings.simulate_count) { module = Sspm->simulated_module; } // Won't work as this is initiated from device
#endif
#endif // SSPM_SIMULATE
power_t relay = (SspmBuffer[31] & 0x0F) << (module * 4); // Relays active
power_t relay_state = (SspmBuffer[31] >> 4) << (module * 4); // Relays state
for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) {
@ -1524,7 +1629,7 @@ void SSPMHandleReceivedData(void) {
SspmBuffer[19] = current_idh;
SspmBuffer[20] = current_idl;
}
#endif
#endif // SSPM_SIMULATE
}
SSPMSendAck(command_sequence);
break;
@ -1818,6 +1923,7 @@ void SSPMInit(void) {
#endif
#endif
Sspm->relay_version = 0xFFFFFFFF; // Find lowest supported relay version
Sspm->overload_relay = 255; // Disable display overload settings
Sspm->history_relay = 255; // Disable display energy history
Sspm->log_relay = 255; // Disable display logging
@ -1916,15 +2022,18 @@ void SSPMEvery100ms(void) {
// Scan sequence finished
#ifdef SSPM_SIMULATE
if (!Sspm->Settings.simulate_count) {
#endif
if (Sspm->power_on_state) {
TasmotaGlobal.power = Sspm->power_on_state;
Sspm->power_on_state = 0; // Reset power on state solving re-scan
SetPowerOnState(); // Set power on state now that all relays have been detected
#endif // SSPM_SIMULATE
if (Sspm->relay_version < SSPM_VERSION_1_2_0) {
// Set relay power on state based on Tasmota global setting
if (Sspm->power_on_state) {
TasmotaGlobal.power = Sspm->power_on_state;
Sspm->power_on_state = 0; // Reset power on state solving re-scan
SetPowerOnState(); // Set power on state now that all relays have been detected
}
}
#ifdef SSPM_SIMULATE
}
#endif
#endif // SSPM_SIMULATE
TasmotaGlobal.discovery_counter = 1; // Force TasDiscovery()
Sspm->allow_updates = 1; // Enable requests from 100mSec loop
Sspm->get_energy_relay = 0;
@ -2201,19 +2310,19 @@ const char kSSPMCommands[] PROGMEM = "SSPM|" // Prefix
"Display|Dump|" // SetOptions
#ifdef SSPM_SIMULATE
"Simulate|"
#endif
#endif // SSPM_SIMULATE
"Log|Energy|History|Scan|IamHere|"
"Reset|Map|Overload|"
D_CMND_ENERGYTOTAL "|" D_CMND_ENERGYYESTERDAY "|Send";
D_CMND_ENERGYTOTAL "|" D_CMND_ENERGYYESTERDAY "|Send|" D_CMND_POWERONSTATE;
void (* const SSPMCommand[])(void) PROGMEM = {
&CmndSSPMDisplay, &CmndSSPMDump,
#ifdef SSPM_SIMULATE
&CmndSSPMSimulate,
#endif
#endif // SSPM_SIMULATE
&CmndSSPMLog, &CmndSSPMEnergy, &CmndSSPMHistory, &CmndSSPMScan, &CmndSSPMIamHere,
&CmndSSPMReset, &CmndSSPMMap, &CmndSSPMOverload,
&CmndSpmEnergyTotal, &CmndSpmEnergyYesterday, &CmndSSPMSend };
&CmndSpmEnergyTotal, &CmndSpmEnergyYesterday, &CmndSSPMSend, &CmndSSPMPowerOnState };
void CmndSSPMDisplay(void) {
// Select either all relays, only powered on relays or user selected relay module
@ -2244,7 +2353,7 @@ void CmndSSPMSimulate(void) {
}
ResponseCmndNumber(Sspm->Settings.simulate_count);
}
#endif
#endif // SSPM_SIMULATE
void CmndSpmEnergyTotal(void) {
// Reset Energy Total
@ -2471,6 +2580,25 @@ void CmndSSPMSend(void) {
}
}
void CmndSSPMPowerOnState(void) {
// SspmPowerOnState2 0|1|2 - Set relay2 power on state (0 = Off, 1 = On, 2 = Saved)
if (Sspm->relay_version > SSPM_VERSION_1_0_0) {
uint32_t max_index = (Sspm->module_max *4) +1;
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index < max_index)) {
uint32_t module = (XdrvMailbox.index -1) >>2;
uint32_t relay = (XdrvMailbox.index -1) &3;
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
if (XdrvMailbox.payload < 2) { XdrvMailbox.payload = !XdrvMailbox.payload; } // Swap Tasmota power off (0) with Sonoff (1)
Sspm->poweron_state[module][relay] = XdrvMailbox.payload;
SSPMSendSetPowerOnState(module);
}
uint32_t poweron_state = Sspm->poweron_state[module][relay];
if (poweron_state < 2) { poweron_state = !poweron_state; } // Swap Sonoff power off (1) with Tasmota (0)
ResponseCmndIdxNumber(poweron_state);
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/