Skip to content

Commit 33a158f

Browse files
authored
Merge pull request #4465 from chaosmaster/ps16dz
Add support for PS-16-DZ Dimmer
2 parents 2085c74 + 2811668 commit 33a158f

File tree

5 files changed

+256
-2
lines changed

5 files changed

+256
-2
lines changed

Diff for: sonoff/my_user_config.h

+1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@
361361
#define USE_TUYA_DIMMER // Add support for Tuya Serial Dimmer
362362
#define TUYA_DIMMER_ID 0 // Default dimmer Id
363363
#define USE_ARMTRONIX_DIMMERS // Add support for Armtronix Dimmers (+1k4 code)
364+
#define USE_PS_16_DZ // ADD support for PS-16-DZ Dimmer
364365

365366
// Power monitoring sensors -----------------------
366367
#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code)

Diff for: sonoff/sonoff_post.h

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void KNX_CB_Action(message_t const &msg, void *arg);
111111
#ifndef TUYA_DIMMER_ID
112112
#define TUYA_DIMMER_ID 0 // Default dimmer Id
113113
#endif
114+
#define USE_PS_16_DZ // Add support for PS-16-DZ Dimmer
114115
#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code)
115116
#define USE_PZEM_AC // Add support for PZEM014,016 Energy monitor (+1k1 code)
116117
#define USE_PZEM_DC // Add support for PZEM003,017 Energy monitor (+1k1 code)

Diff for: sonoff/sonoff_template.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ enum SupportedModules {
256256
GOSUND,
257257
ARMTRONIX_DIMMERS,
258258
SK03_TUYA,
259+
PS_16_DZ,
259260
MAXMODULE };
260261

261262
/********************************************************************************************/
@@ -481,8 +482,9 @@ const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = {
481482
NEO_COOLCAM, // Socket Relay Devices
482483
OBI,
483484
ESP_SWITCH, // Switch Devices
484-
TUYA_DIMMER, // Dimmer Devices
485+
TUYA_DIMMER, // Dimmer Devices
485486
ARMTRONIX_DIMMERS,
487+
PS_16_DZ,
486488
H801, // Light Devices
487489
MAGICHOME,
488490
ARILUX_LC01,
@@ -1273,6 +1275,22 @@ const mytmplt kModules[MAXMODULE] PROGMEM = {
12731275
GPIO_LED1_INV, // GPIO14 Blue Led (0 = On, 1 = Off)
12741276
GPIO_REL1, // GPIO15 Relay (0 = Off, 1 = On)
12751277
0, 0
1278+
},
1279+
{ "PS-16-DZ", // PS-16-DZ Dimmer (ESP8266 w/ separate Nuvoton MCU dimmer)
1280+
// https://www.aliexpress.com/item/SM-Smart-WIFI-Wall-Dimmer-Light-Switch-US-Ewelink-APP-Remote-Control-Wi-Fi-Wirele-Work/32871151902.html
1281+
GPIO_USER,
1282+
GPIO_TXD, // GPIO01 MCU serial control
1283+
GPIO_USER,
1284+
GPIO_RXD, // GPIO03 MCU serial control
1285+
GPIO_USER,
1286+
GPIO_USER,
1287+
0, 0, 0, 0, 0, 0, // Flash connection
1288+
GPIO_USER,
1289+
GPIO_LED1, // GPIO13 WiFi LED
1290+
GPIO_USER,
1291+
GPIO_USER,
1292+
GPIO_USER,
1293+
0
12761294
}
12771295
};
12781296

Diff for: sonoff/xdrv_04_light.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ void LightAnimate(void)
832832
LightMy92x1Duty(cur_col[0], cur_col[1], cur_col[2], cur_col[3], cur_col[4]);
833833
}
834834
#ifdef USE_TUYA_DIMMER
835-
if (light_type == LT_SERIAL1) {
835+
if (light_type == LT_SERIAL1 && Settings.module == TUYA_DIMMER ) {
836836
LightSerialDuty(cur_col[0]);
837837
}
838838
#endif // USE_TUYA_DIMMER
@@ -841,6 +841,11 @@ void LightAnimate(void)
841841
LightSerial2Duty(cur_col[0],cur_col[1]);
842842
}
843843
#endif // USE_ARMTRONIX_DIMMERS
844+
#ifdef USE_PS_16_DZ
845+
if (light_type == LT_SERIAL1 && Settings.module == PS_16_DZ) {
846+
PS16DZSerialDuty(cur_col[0]);
847+
}
848+
#endif // USE_PS_16_DZ
844849

845850
}
846851
}

Diff for: sonoff/xdrv_19_ps16dz_dimmer.ino

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
xdrv_19_ps16dz_dimmer.ino - PS_16_DZ dimmer support for Sonoff-Tasmota
3+
4+
Copyright (C) 2018 Joel Stein and Theo Arends
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifdef USE_PS_16_DZ
21+
22+
#define XDRV_19 19
23+
24+
#define PS16DZ_BUFFER_SIZE 256
25+
26+
#include <TasmotaSerial.h>
27+
28+
TasmotaSerial *PS16DZSerial = nullptr;
29+
30+
boolean ps16dz_ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction
31+
32+
boolean ps16dz_power = false;
33+
uint8_t ps16dz_bright = 0;
34+
//uint64_t ps16dz_seq = 0;
35+
36+
char ps16dz_tx_buffer[PS16DZ_BUFFER_SIZE]; // Serial transmit buffer
37+
char ps16dz_rx_buffer[PS16DZ_BUFFER_SIZE]; // Serial receive buffer
38+
int ps16dz_byte_counter = 0;
39+
40+
/*********************************************************************************************\
41+
* Internal Functions
42+
\*********************************************************************************************/
43+
44+
void printTimestamp(void)
45+
{
46+
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s%d%03d"), ps16dz_tx_buffer, LocalTime(), millis()%1000);
47+
}
48+
49+
boolean PS16DZSetPower(void)
50+
{
51+
boolean status = false;
52+
53+
uint8_t rpower = XdrvMailbox.index;
54+
int16_t source = XdrvMailbox.payload;
55+
56+
if (source != SRC_SWITCH && PS16DZSerial) { // ignore to prevent loop from pushing state from faceplate interaction
57+
58+
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+UPDATE=\"sequence\":\""));
59+
printTimestamp();
60+
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"switch\":\"%s\""), ps16dz_tx_buffer, rpower?"on":"off");
61+
snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer );
62+
AddLog(LOG_LEVEL_DEBUG);
63+
64+
PS16DZSerial->print(ps16dz_tx_buffer);
65+
PS16DZSerial->write(0x1B);
66+
PS16DZSerial->flush();
67+
68+
status = true;
69+
}
70+
return status;
71+
}
72+
73+
void PS16DZSerialDuty(uint8_t duty)
74+
{
75+
if (duty > 0 && !ps16dz_ignore_dim && PS16DZSerial) {
76+
if (duty < 25) {
77+
duty = 25; // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself
78+
}
79+
80+
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+UPDATE=\"sequence\":\""));
81+
printTimestamp();
82+
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "%s\",\"bright\":%d"), ps16dz_tx_buffer, round(duty * (100. / 255.)));
83+
snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer );
84+
AddLog(LOG_LEVEL_DEBUG);
85+
86+
PS16DZSerial->print(ps16dz_tx_buffer);
87+
PS16DZSerial->write(0x1B);
88+
PS16DZSerial->flush();
89+
90+
} else {
91+
ps16dz_ignore_dim = false; // reset flag
92+
93+
snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send Dim Level skipped due to 0 or already set. Value=%d"), duty);
94+
AddLog(LOG_LEVEL_DEBUG);
95+
96+
}
97+
}
98+
99+
void PS16DZResetWifi(void)
100+
{
101+
if (!Settings.flag.button_restrict) {
102+
char scmnd[20];
103+
snprintf_P(scmnd, sizeof(scmnd), D_CMND_WIFICONFIG " %d", 2);
104+
ExecuteCommand(scmnd, SRC_BUTTON);
105+
}
106+
}
107+
108+
/*********************************************************************************************\
109+
* API Functions
110+
\*********************************************************************************************/
111+
112+
boolean PS16DZModuleSelected(void)
113+
{
114+
light_type = LT_SERIAL1;
115+
return true;
116+
}
117+
118+
void PS16DZInit(void)
119+
{
120+
PS16DZSerial = new TasmotaSerial(pin[GPIO_RXD], pin[GPIO_TXD], 2);
121+
if (PS16DZSerial->begin(19200)) {
122+
if (PS16DZSerial->hardwareSerial()) { ClaimSerial(); }
123+
}
124+
}
125+
126+
void PS16DZSerialInput(void)
127+
{
128+
char scmnd[20];
129+
while (PS16DZSerial->available()) {
130+
yield();
131+
byte serial_in_byte = PS16DZSerial->read();
132+
if (serial_in_byte != 0x1B){
133+
if (ps16dz_byte_counter || (!ps16dz_byte_counter && serial_in_byte == 'A'));
134+
ps16dz_rx_buffer[ps16dz_byte_counter++] = serial_in_byte;
135+
}
136+
else {
137+
ps16dz_rx_buffer[ps16dz_byte_counter++] = 0x00;
138+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: command received: %s"), ps16dz_rx_buffer);
139+
AddLog(LOG_LEVEL_DEBUG);
140+
if(!strncmp(ps16dz_rx_buffer+3, "UPDATE", 6) || !strncmp(ps16dz_rx_buffer+3, "RESULT", 6)) {
141+
char *end_str;
142+
char *string = ps16dz_rx_buffer+10;
143+
char* token = strtok_r(string, ",", &end_str);
144+
while (token != NULL) {
145+
char* end_token;
146+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: token = %s"), token);
147+
AddLog(LOG_LEVEL_DEBUG);
148+
char* token2 = strtok_r(token, ":", &end_token);
149+
char* token3 = strtok_r(NULL, ":", &end_token);
150+
if(!strncmp(token2, "\"switch\"", 8)){
151+
ps16dz_power = !strncmp(token3, "\"on\"", 4)?true:false;
152+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: power received: %s"), token3);
153+
AddLog(LOG_LEVEL_DEBUG);
154+
if((power || Settings.light_dimmer > 0) && (power !=ps16dz_power)) {
155+
ExecuteCommandPower(1, ps16dz_power, SRC_SWITCH); // send SRC_SWITCH? to use as flag to prevent loop from inbound states from faceplate interaction
156+
}
157+
}
158+
else if(!strncmp(token2, "\"bright\"", 8)){
159+
ps16dz_bright = atoi(token3);
160+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: brightness received: %d"), ps16dz_bright);
161+
AddLog(LOG_LEVEL_DEBUG);
162+
if(power && ps16dz_bright > 0) {
163+
164+
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), ps16dz_bright );
165+
166+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: Send CMND_DIMMER_STR=%s"), scmnd );
167+
AddLog(LOG_LEVEL_DEBUG);
168+
169+
ps16dz_ignore_dim = true;
170+
ExecuteCommand(scmnd, SRC_SWITCH);
171+
}
172+
}
173+
else if(!strncmp(token2, "\"sequence\"", 10)){
174+
//ps16dz_seq = strtoull(token3+1, NULL, 10);
175+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: sequence received: %s"), token3);
176+
AddLog(LOG_LEVEL_DEBUG);
177+
}
178+
token = strtok_r(NULL, ",", &end_str);
179+
}
180+
}
181+
else if(!strncmp(ps16dz_rx_buffer+3, "SETTING", 7)) {
182+
snprintf_P(log_data, sizeof(log_data), PSTR("PSZ: Reset"));
183+
AddLog(LOG_LEVEL_DEBUG);
184+
PS16DZResetWifi();
185+
}
186+
memset(ps16dz_rx_buffer, 0, sizeof(ps16dz_rx_buffer));
187+
ps16dz_byte_counter = 0;
188+
189+
snprintf_P(ps16dz_tx_buffer, sizeof(ps16dz_tx_buffer), PSTR( "AT+SEND=ok"));
190+
snprintf_P(log_data, sizeof(log_data), PSTR( "PSZ: Send serial command: %s"), ps16dz_tx_buffer );
191+
AddLog(LOG_LEVEL_DEBUG);
192+
193+
PS16DZSerial->print(ps16dz_tx_buffer);
194+
PS16DZSerial->write(0x1B);
195+
PS16DZSerial->flush();
196+
}
197+
}
198+
}
199+
200+
201+
202+
/*********************************************************************************************\
203+
* Interface
204+
\*********************************************************************************************/
205+
206+
boolean Xdrv19(byte function)
207+
{
208+
boolean result = false;
209+
210+
if (PS_16_DZ == Settings.module) {
211+
switch (function) {
212+
case FUNC_MODULE_INIT:
213+
result = PS16DZModuleSelected();
214+
break;
215+
case FUNC_INIT:
216+
PS16DZInit();
217+
break;
218+
case FUNC_LOOP:
219+
if (PS16DZSerial) { PS16DZSerialInput(); }
220+
break;
221+
case FUNC_SET_DEVICE_POWER:
222+
result = PS16DZSetPower();
223+
break;
224+
}
225+
}
226+
return result;
227+
}
228+
229+
#endif // USE_PS_16_DZ

0 commit comments

Comments
 (0)