Still working on the code.
It is hobby and i am not a programmer guy, so it takes time.
But i am running in a challenge i can't find how to solve.
When i fire a 64-bits code, this is in the log (sync bit makes it 65 bit)
- 211038: RFcode: Protocol: 1, 65 bit
211038: RFcode: Data bits of pulse train duration: 34339
211040: RFcode: Proposed protocol: { 350, { 3067712, 142 }, { 1, 1 }, { 1, 1 }, false }
211041: RFcode: Binary: 00110000101111001101010110010011001100110011110011001100110011001
211042: RFcode: Decimal: 7023833275277351321
211043: RFcode: http://192.168.178.164/control?cmd=RC,P ... 0110011001
585088: EVENT: 433test#RF=7023833110349350761
Where the challenge is, that line 211042 is the correct value. And i want this to send to the
UserVar[event->BaseVarIndex]
But when looking at line 585088, this is the event, but the value is different. I can't find out why.
Tried different ways to archieve the same result in value, but without luck.
It works when max 26bits are send. Everything more then 26 bits gives a wrong value
What am i doing wrong?
On line 250 i write the
UserVar[event->BaseVarIndex]
On line 390 the correct value is set for
Decimal
This is the plugin code (for now) what i have
Code: Select all
#ifdef USES_P144
//#######################################################################################################
//#################################### Plugin 144: RC-Switch TX #########################################
//#######################################################################################################
// written by Jochen Krapf (jk@nerd2nerd.org)
// 21-09-2021: Forked by Paul Hermans
// 21-09-2021: Added receive 433mhz code
// 24-10-2021: Added 64 bits send/receive (senddec not possible anymore)
// List of commands:
// (1) RC,<param>
// (2) RC,<param>,<param>,<param>
// List of RC params:
// (a) SEND=<binary_code>
// Send binary code with any length ("RC,SEND=000000000001010100010001")
// (b) SEND=<tristate_code>
// Send tristate code with any length ("RC,SEND=00000FFF0F0F")
// (d) ON=<binary_code>
// Send binary code for simple 10 DIP switch devices ("RC,ON=1010100010")
// (e) ON=<1..4><1..4>
// Send switch position for simple 2 rotary switch devices ("RC,ON=42")
// (f) ON=<a..f><1..4><1..4>
// Send switch position for Intertechno devices ("RC,ON=a42")
// (f) OFF= as ON...
// (g) PROTOCOL=<number>
// Set protocoln for devices ("RC,PROTOCOL=2") default=1
// (h) PULSE=<number>
// Set pulse length ("RC,PULSE=320") default=320
// (i) REPEAT=<number>
// Set number of transmission repeats ("RC,REPEAT=15") default=?
//
// Combinations:
// e.g. "RC,PROTOCOL=2,PULSE=320,REPEAT=15,SEND=000000000001010100010001"
#include "_Plugin_Helper.h"
#include <RCSwitch64.h>
static RCSwitch64 Plugin_144_RC = RCSwitch64();
static char Str[24];
#define PLUGIN_144
#define PLUGIN_ID_144 144
#define PLUGIN_NAME_144 "RC-Switch TX"
#define PLUGIN_ValueNAME1_144 "RF"
boolean Plugin_144(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_144;
//Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].Ports = 0;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_DUAL;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = false;
Device[deviceCount].TimerOptional = false;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_144);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_ValueNAME1_144));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
addFormPinSelect(F("TX-GPIO"), F("taskdevicepin1"), CONFIG_PIN1);
addFormPinSelect(F("RX-GPIO"), F("taskdevicepin2"), CONFIG_PIN2);
// addFormCheckBox(F("Use transceiver"), F("transceiver"), PCONFIG(1)); //future work
addFormPinSelect(F("Enable-GPIO"), F("taskdevicepin3"), CONFIG_PIN3);
addFormPinSelect(F("TX_RX Toggle-GPIO"), F("taskdevicetxpin"), PCONFIG(2));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
// PCONFIG(1) = isFormItemChecked(F("transceiver")); //future work
PCONFIG(2) = getFormItemInt(F("taskdevicetxpin"));
success = true;
break;
}
case PLUGIN_INIT:
{
UserVar[event->BaseVarIndex] = 0;
if (CONFIG_PIN1 >= 0) //TX-pin
{
pinMode(CONFIG_PIN1, OUTPUT);
String log = F("RC-Sw: Send Pin ");
log += CONFIG_PIN1;
log += F(" ");
Plugin_144_RC.enableTransmit(CONFIG_PIN1);
addLog(LOG_LEVEL_INFO, log);
}
if (CONFIG_PIN2 >= 0) // RX-pin
{
pinMode(CONFIG_PIN2, INPUT_PULLUP);
String log = F("RC-Sw: Receive pin ");
log += CONFIG_PIN2;
log += F(" ");
Plugin_144_RC.enableReceive(CONFIG_PIN2);
addLog(LOG_LEVEL_INFO, log);
}
if (CONFIG_PIN3 >= 0) //enable pin
pinMode(CONFIG_PIN3, OUTPUT);
if (PCONFIG(2) >= 0) //TX_RX pin
pinMode(PCONFIG(2), OUTPUT);
//Prefix Enable and TX_RX pin to low
digitalWrite (CONFIG_PIN3, LOW);
digitalWrite (PCONFIG(2), LOW);
delayMicroseconds(500);
//Set transceiver to RX after powerdown
digitalWrite (CONFIG_PIN3, HIGH);
delayMicroseconds(20);
digitalWrite (PCONFIG(2), HIGH);
delayMicroseconds(200);
digitalWrite (PCONFIG(2), LOW);
delayMicroseconds(40);
digitalWrite (CONFIG_PIN3, LOW);
delayMicroseconds(20);
digitalWrite (CONFIG_PIN3, HIGH);
delayMicroseconds(200);
success = true;
break;
}
case PLUGIN_ONCE_A_SECOND:
{
if (CONFIG_PIN2 >= 0) {
if (UserVar[event->BaseVarIndex] != 0) {
UserVar[event->BaseVarIndex] = (0);
}
if (Plugin_144_RC.available())
{
Serial.print("RF recieved");
int valuerf = Plugin_144_RC.getReceivedValue();
if (valuerf == 0) {
String log = F("RFcode: ");
log += String(valuerf);
log += " =Unknown encoding";
addLog(LOG_LEVEL_INFO, log);
} else {
int databuffer[128]; // get a copy of the received timings before they are overwritten
int numberoftimings = 2 * Plugin_144_RC.getReceivedBitlength() + 2;
if(numberoftimings > 128) numberoftimings = 128;
for (int i = 0; i < numberoftimings; i++) {
databuffer[i] = Plugin_144_RC.getReceivedRawdata()[i];
}
String printEmpty = F("");
addLog(LOG_LEVEL_INFO, printEmpty);
String printSerial1 = F("RFcode: ");
printSerial1 += F("Protocol: ");
printSerial1 += ( Plugin_144_RC.getReceivedProtocol() );
printSerial1 += F(", ");
printSerial1 += ( Plugin_144_RC.getReceivedBitlength() );
printSerial1 += F(" bit");
addLog(LOG_LEVEL_INFO, printSerial1);
unsigned int databitsoffset = abs( (int)Plugin_144_RC.getReceivedLevelInFirstTiming() - (int)Plugin_144_RC.getReceivedInverted());
unsigned long dataduration = 0;
for (unsigned int i = 1 + databitsoffset; i < numberoftimings - 1 + databitsoffset; i++) {
dataduration += databuffer[i];
}
String printSerial2 = F("RFcode: ");
printSerial2 += F("Data bits of pulse train duration: ");
printSerial2 += ( dataduration );
addLog(LOG_LEVEL_INFO, printSerial2);
unsigned int averagebitduration = (int)(0.5 + ((double)dataduration)/Plugin_144_RC.getReceivedBitlength());
unsigned int protocolratio = (unsigned int)(0.5 + ((double)(averagebitduration - Plugin_144_RC.getReceivedDelay())) / (double)Plugin_144_RC.getReceivedDelay());
String printSerial3 = F("RFcode: ");
printSerial3 += F("Proposed protocol: { ");
printSerial3 += (Plugin_144_RC.getReceivedDelay());
printSerial3 += F(", { ");
printSerial3 += ( (databitsoffset==0) ?
(int) (0.5 + (double)databuffer[2*Plugin_144_RC.getReceivedBitlength()+1]/(double)Plugin_144_RC.getReceivedDelay())
:
(int) (0.5 + (double)databuffer[0]/(double)Plugin_144_RC.getReceivedDelay())
);
printSerial3 += F(", ");
printSerial3 += ( (databitsoffset==0) ?
(int) (0.5 + (double)databuffer[0]/(double)Plugin_144_RC.getReceivedDelay())
:
(int) (0.5 + (double)databuffer[1]/(double)Plugin_144_RC.getReceivedDelay())
);
printSerial3 += F(" }, { ");
printSerial3 += F("1");
printSerial3 += F(", ");
printSerial3 += (protocolratio);
printSerial3 += F(" }, { ");
printSerial3 += (protocolratio);
printSerial3 += F(", ");
printSerial3 += F("1");
printSerial3 += F(" }, ");
printSerial3 += ((Plugin_144_RC.getReceivedInverted()) ? "true" : "false" );
printSerial3 += F(" }");
addLog(LOG_LEVEL_INFO, printSerial3);
binair(Plugin_144_RC.getReceivedValue(), Plugin_144_RC.getReceivedBitlength());
//char* dec(uint64_t n);
//uint64_t cvttoull(const char *res);
//uint64_t val = cvttoull(Str);
//UserVar[event->BaseVarIndex] = (val);
String string_val = (Str);
float float_var;
float_var = string_val.toFloat();
UserVar[event->BaseVarIndex] = (float_var);
//UserVar[event->BaseVarIndex] = (valuerf);
sendData(event);
}
Plugin_144_RC.resetAvailable();
}
success = true;
break;
}
}
case PLUGIN_WRITE:
{
if (PCONFIG(2) >= 0) {
String command = parseString(string, 1);
if (command == F("rc")) {
String param;
byte paramIdx = 2;
string.replace(" ", " ");
string.replace(" =", "=");
string.replace("= ", "=");
param = parseString(string, paramIdx++);
while (param.length()) {
addLog(LOG_LEVEL_DEBUG_MORE, param);
int index = param.indexOf('=');
if (index > 0) {
String paramKey = param.substring(0, index);
String paramVal = param.substring(index+1);
paramKey.toUpperCase();
addLog(LOG_LEVEL_DEBUG_MORE, paramKey);
addLog(LOG_LEVEL_DEBUG_MORE, paramVal);
if (paramKey == F("SEND")) {
if (paramVal.indexOf("F") >= 0)
Plugin_144_RC.sendTriState(&(paramVal[0]));
else
Plugin_144_RC.send(&(paramVal[0]));
}
if (paramKey == F("ON")) {
if (paramVal.length()==10) //simple 10 DIP switch
Plugin_144_RC.switchOn(&(paramVal.substring(0, 5)[0]), &(paramVal.substring(5)[0]));
else if (paramVal.length()==2) //2x rotary switch 1..4
Plugin_144_RC.switchOn(paramVal[0]-'0', paramVal[1]-'0');
else if (paramVal.length()==3) //Intertechno outlets
Plugin_144_RC.switchOn(paramVal[0], paramVal[1]-'0', paramVal[2]-'0');
}
if (paramKey == F("OFF")) {
if (paramVal.length()==10) //simple 10 DIP switch
Plugin_144_RC.switchOff(&(paramVal.substring(0, 5)[0]), &(paramVal.substring(5)[0]));
else if (paramVal.length()==2) //2x rotary switch 1..4
Plugin_144_RC.switchOff(paramVal[0]-'0', paramVal[1]-'0');
else if (paramVal.length()==3) //Intertechno outlets
Plugin_144_RC.switchOn(paramVal[0], paramVal[1]-'0', paramVal[2]-'0');
}
if (paramKey == F("PROTOCOL")) {
Plugin_144_RC.setProtocol(paramVal.toInt());
}
if (paramKey == F("PULSE")) {
Plugin_144_RC.setPulseLength(paramVal.toInt());
}
if (paramKey == F("REPEAT")) {
Plugin_144_RC.setRepeatTransmit(paramVal.toInt());
}
}
param = parseString(string, paramIdx++);
}
success = true;
}
break;
}
}
case PLUGIN_READ:
{
//no values
success = true;
break;
}
}
return success;
}
/* extended logging, for in terminal monitor */
static char * dec2binWzerofill(unsigned long long Dec, unsigned int bitLength);
static char * dec(unsigned long long Dec);
void binair(unsigned long long decimal, unsigned int length) {
if (decimal == 0) {
Serial.print("Unknown encoding.");
} else {
const char* b = dec2binWzerofill(decimal, length);
String printBinary = F("RFcode: ");
printBinary += F("Binary: ");
printBinary += b;
addLog(LOG_LEVEL_INFO, printBinary);
String printDecimal = F("RFcode: ");
printDecimal += F("Decimal: ");
const char* d = dec(decimal);
printDecimal += d;
addLog(LOG_LEVEL_INFO, printDecimal);
String url = String(NetworkLocalIP().toString()) + "/control?cmd=RC";
String printString = F("RFcode: http://");
printString += url;
printString += F(",PROTOCOL=");
printString += ( Plugin_144_RC.getReceivedProtocol() );
printString += F(",SEND=");
printString += b;
printString += F("");
addLog(LOG_LEVEL_INFO, printString);
String printEmpty = F("");
addLog(LOG_LEVEL_INFO, printEmpty);
}
}
//uint64_t cvttoull(const char *res) {
// uint64_t result = 0;
// while (*res) {
// result *= 10;
// result += *res++ - '0';
// }
// return result;
//}
static char * dec (unsigned long long Dec) {
//static char Str[20];
sprintf(Str, "%lld", Dec);
return Str;
}
static char * dec2binWzerofill(unsigned long long Dec, unsigned int bitLength) {
static char bin[128];
unsigned int i=0;
while (Dec > 0) {
bin[64+i++] = ((Dec & 1) > 0) ? '1' : '0';
Dec = Dec >> 1;
}
for (unsigned int j = 0; j< bitLength; j++) {
if (j >= bitLength - i) {
bin[j] = bin[ 63 + i - (j - (bitLength - i)) ];
} else {
bin[j] = '0';
}
}
bin[bitLength] = '\0';
return bin;
}
#endif