mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-24 19:26:37 +00:00
Add additional RFSend comand syntax
Add additional RFSend comand syntax
This commit is contained in:
parent
f2b04092f8
commit
c5cb7ffb78
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#ifdef USE_RC_SWITCH
|
#ifdef USE_RC_SWITCH
|
||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* RF send and receive using RCSwitch library
|
* RF send and receive using RCSwitch library https://github.com/sui77/rc-switch/
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
#define D_JSON_RF_PROTOCOL "Protocol"
|
#define D_JSON_RF_PROTOCOL "Protocol"
|
||||||
@ -34,39 +34,38 @@
|
|||||||
|
|
||||||
RCSwitch mySwitch = RCSwitch();
|
RCSwitch mySwitch = RCSwitch();
|
||||||
|
|
||||||
#define RF_TIME_AVOID_DUPLICATE 500 // Milliseconds
|
#define RF_TIME_AVOID_DUPLICATE 1000 // Milliseconds
|
||||||
|
|
||||||
unsigned long rf_lasttime = 0;
|
uint32_t rf_lasttime = 0;
|
||||||
|
|
||||||
void RfReceiveCheck()
|
void RfReceiveCheck()
|
||||||
{
|
{
|
||||||
if (mySwitch.available()) {
|
if (mySwitch.available()) {
|
||||||
|
|
||||||
unsigned long value = mySwitch.getReceivedValue();
|
unsigned long data = mySwitch.getReceivedValue();
|
||||||
unsigned int bit_length = mySwitch.getReceivedBitlength();
|
unsigned int bits = mySwitch.getReceivedBitlength();
|
||||||
unsigned int delay = mySwitch.getReceivedDelay();
|
int protocol = mySwitch.getReceivedProtocol();
|
||||||
unsigned int protocol = mySwitch.getReceivedProtocol();
|
int delay = mySwitch.getReceivedDelay();
|
||||||
|
|
||||||
snprintf_P(log_data, sizeof(log_data), PSTR("RFR: BitLen %d, Delay %d, Protocol %d, Value %lX (%u)"),
|
snprintf_P(log_data, sizeof(log_data), PSTR("RFR: Data %lX (%u), Bits %d, Protocol %d, Delay %d"), data, data, bits, protocol, delay);
|
||||||
bit_length, delay, protocol, value, value);
|
|
||||||
AddLog(LOG_LEVEL_DEBUG);
|
AddLog(LOG_LEVEL_DEBUG);
|
||||||
|
|
||||||
unsigned long now = millis();
|
uint32_t now = millis();
|
||||||
if ((now - rf_lasttime > RF_TIME_AVOID_DUPLICATE) && (value > 0)) {
|
if ((now - rf_lasttime > RF_TIME_AVOID_DUPLICATE) && (data > 0)) {
|
||||||
rf_lasttime = now;
|
rf_lasttime = now;
|
||||||
|
|
||||||
char stemp[16];
|
char stemp[16];
|
||||||
if (Settings.flag.rf_receive_decimal) {
|
if (Settings.flag.rf_receive_decimal) { // SetOption28 (0 = hexadecimal, 1 = decimal)
|
||||||
snprintf_P(stemp, sizeof(stemp), PSTR("%u"), (uint32_t)value);
|
snprintf_P(stemp, sizeof(stemp), PSTR("%u"), (uint32_t)data);
|
||||||
} else {
|
} else {
|
||||||
snprintf_P(stemp, sizeof(stemp), PSTR("\"%lX\""), (uint32_t)value);
|
snprintf_P(stemp, sizeof(stemp), PSTR("\"%lX\""), (uint32_t)data);
|
||||||
}
|
}
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_RFRECEIVED "\":{\"" D_JSON_RF_PROTOCOL "\":%d,\"" D_JSON_RF_BITS "\":%d,\"" D_JSON_RF_DATA "\":%s}}"),
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_JSON_RFRECEIVED "\":{\"" D_JSON_RF_DATA "\":%s,\"" D_JSON_RF_BITS "\":%d,\"" D_JSON_RF_PROTOCOL "\":%d}}"),
|
||||||
protocol, bit_length, stemp);
|
stemp, bits, protocol);
|
||||||
MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_RFRECEIVED));
|
MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_RFRECEIVED));
|
||||||
XdrvRulesProcess();
|
XdrvRulesProcess();
|
||||||
#ifdef USE_DOMOTICZ
|
#ifdef USE_DOMOTICZ
|
||||||
DomoticzSensor(DZ_COUNT, value); // Send value as Domoticz Counter value
|
DomoticzSensor(DZ_COUNT, data); // Send data as Domoticz Counter value
|
||||||
#endif // USE_DOMOTICZ
|
#endif // USE_DOMOTICZ
|
||||||
}
|
}
|
||||||
mySwitch.resetAvailable();
|
mySwitch.resetAvailable();
|
||||||
@ -87,63 +86,73 @@ void RfInit()
|
|||||||
* Commands
|
* Commands
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
/*
|
|
||||||
* ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(3) + 40 = 96
|
|
||||||
RFsend:
|
|
||||||
{ "protocol":1, "pulse":320, "repeat":15, "bits":24, "data":551502015 }
|
|
||||||
*/
|
|
||||||
|
|
||||||
boolean RfSendCommand()
|
boolean RfSendCommand()
|
||||||
{
|
{
|
||||||
boolean serviced = true;
|
boolean serviced = true;
|
||||||
|
|
||||||
boolean error = false;
|
boolean error = false;
|
||||||
char dataBufUc[XdrvMailbox.data_len];
|
|
||||||
|
|
||||||
uint32_t protocol = 0;
|
|
||||||
uint32_t pulse = 0;
|
|
||||||
uint32_t repeat = 0;
|
|
||||||
uint32_t bits = 0;
|
|
||||||
uint32_t data = 0;
|
|
||||||
|
|
||||||
UpperCase(dataBufUc, XdrvMailbox.data);
|
|
||||||
if (!strcasecmp_P(XdrvMailbox.topic, PSTR(D_CMND_RFSEND))) {
|
if (!strcasecmp_P(XdrvMailbox.topic, PSTR(D_CMND_RFSEND))) {
|
||||||
if (XdrvMailbox.data_len) {
|
if (XdrvMailbox.data_len) {
|
||||||
StaticJsonBuffer<128> jsonBuf;
|
unsigned long data = 0;
|
||||||
|
unsigned int bits = 24;
|
||||||
|
int protocol = 1;
|
||||||
|
int repeat = 10;
|
||||||
|
int pulse = 350;
|
||||||
|
|
||||||
|
char dataBufUc[XdrvMailbox.data_len];
|
||||||
|
UpperCase(dataBufUc, XdrvMailbox.data);
|
||||||
|
StaticJsonBuffer<150> jsonBuf; // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(5) + 40 = 134
|
||||||
JsonObject &root = jsonBuf.parseObject(dataBufUc);
|
JsonObject &root = jsonBuf.parseObject(dataBufUc);
|
||||||
if (!root.success()) {
|
if (root.success()) {
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_INVALID_JSON "\"}")); // JSON decode failed
|
// RFsend {"data":0x501014,"bits":24,"protocol":1,"repeat":10,"pulse":350}
|
||||||
}
|
|
||||||
else {
|
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_DONE "\"}"));
|
|
||||||
|
|
||||||
char parm_uc[10];
|
char parm_uc[10];
|
||||||
protocol = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PROTOCOL))];
|
data = strtoul(root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_DATA))], NULL, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input
|
||||||
pulse = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PULSE))];
|
|
||||||
repeat = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_REPEAT))];
|
|
||||||
bits = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_BITS))];
|
bits = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_BITS))];
|
||||||
data = strtoul(root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_DATA))], NULL, 0);
|
protocol = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PROTOCOL))];
|
||||||
|
repeat = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_REPEAT))];
|
||||||
if (!protocol) { protocol = 1; }
|
pulse = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PULSE))];
|
||||||
mySwitch.setProtocol(protocol);
|
} else {
|
||||||
if (!pulse) { pulse = 350; } // Default pulse length for protocol 1
|
// RFsend data, bits, protocol, repeat, pulse
|
||||||
mySwitch.setPulseLength(pulse);
|
char *p;
|
||||||
if (!repeat) { repeat = 10; } // Default at init
|
byte i = 0;
|
||||||
mySwitch.setRepeatTransmit(repeat);
|
for (char *str = strtok_r(XdrvMailbox.data, ", ", &p); str && i < 5; str = strtok_r(NULL, ", ", &p)) {
|
||||||
if (!bits) { bits = 24; } // Default 24 bits
|
switch (i++) {
|
||||||
if (data) {
|
case 0:
|
||||||
mySwitch.send(data, bits);
|
data = strtoul(str, NULL, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input
|
||||||
}
|
break;
|
||||||
else {
|
case 1:
|
||||||
error = true;
|
bits = atoi(str);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
protocol = atoi(str);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
repeat = atoi(str);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
pulse = atoi(str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
if (!protocol) { protocol = 1; }
|
||||||
|
mySwitch.setProtocol(protocol);
|
||||||
|
if (!pulse) { pulse = 350; } // Default pulse length for protocol 1
|
||||||
|
mySwitch.setPulseLength(pulse);
|
||||||
|
if (!repeat) { repeat = 10; } // Default at init
|
||||||
|
mySwitch.setRepeatTransmit(repeat);
|
||||||
|
if (!bits) { bits = 24; } // Default 24 bits
|
||||||
|
if (data) {
|
||||||
|
mySwitch.send(data, bits);
|
||||||
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_DONE "\"}"));
|
||||||
|
} else {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
if (error) {
|
if (error) {
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_NO " " D_JSON_RF_PROTOCOL ", " D_JSON_RF_PULSE ", " D_JSON_RF_REPEAT ", " D_JSON_RF_BITS " " D_JSON_OR " " D_JSON_RF_DATA "\"}"));
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_NO " " D_JSON_RF_DATA ", " D_JSON_RF_BITS ", " D_JSON_RF_PROTOCOL ", " D_JSON_RF_REPEAT " " D_JSON_OR " " D_JSON_RF_PULSE "\"}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else serviced = false; // Unknown command
|
else serviced = false; // Unknown command
|
||||||
|
Loading…
x
Reference in New Issue
Block a user