forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P111_RF.ino
226 lines (195 loc) · 8.46 KB
/
_P111_RF.ino
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//#######################################################################################################
//#################################### Plugin 111: Input RF #############################################
//#######################################################################################################
/*
Version: 2.0
Description: use this script to recieve RF with a cheap MX-05V alike receiver
Author: S4nder
Copyright: (c) 2015-2016 Sander Pleijers (s4nder)
License: MIT
License URI: http://en.wikipedia.org/wiki/MIT_License
Status : "Proof of concept"
This program was developed independently and it is not supported in any way.
*/
#ifdef PLUGIN_BUILD_TESTING
#include <RCSwitch.h>
RCSwitch *rfReceiver;
#define PLUGIN_111
#define PLUGIN_ID_111 111
#define PLUGIN_NAME_111 "RF Receiver - MX-05V alike receiver"
#define PLUGIN_ValueNAME1_111 "RF"
#ifndef USES_P016
int irReceiver = 0; // make sure it has value even if plugin not found
#endif
boolean Plugin_111(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_111;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_LONG;
Device[deviceCount].Ports = 0;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = false;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_111);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_ValueNAME1_111));
break;
}
case PLUGIN_INIT:
{
int rfPin = Settings.TaskDevicePin1[event->TaskIndex];
//Serial.println("INIT: RF433 RX rfpin: ");
//Serial.print(String(rfPin));
if (irReceiver != 0) {
String log = F("BUG: Cannot use IR reciever and RF reciever at the same time!");
Serial.print(log);
addLog(LOG_LEVEL_INFO, log);
delete rfReceiver;
rfReceiver = 0;
} else {
if (rfPin != -1)
{
Serial.println("INIT: RF433 RX created!");
rfReceiver = new RCSwitch();
rfReceiver->enableReceive(rfPin);
}
if (rfReceiver != 0 && rfPin == -1)
{
Serial.println("INIT: RF433 RX removed!");
rfReceiver->resetAvailable();
delete rfReceiver;
rfReceiver = 0;
}
}
success = true;
break;
}
case PLUGIN_ONCE_A_SECOND:
{
if (irReceiver != 0) break;
if (rfReceiver->available())
{
Serial.print("RF recieved");
int valuerf = rfReceiver->getReceivedValue();
if (valuerf == 0) {
Serial.print("Unknown encoding");
String log = F("RF Code Recieved: ");
log += String(valuerf);
log += " =Unknown encoding";
addLog(LOG_LEVEL_INFO, log);
} else {
output(rfReceiver->getReceivedValue(), rfReceiver->getReceivedBitlength(), rfReceiver->getReceivedDelay(), rfReceiver->getReceivedRawdata(), rfReceiver->getReceivedProtocol());
UserVar[event->BaseVarIndex] = (valuerf & 0xFFFF);
UserVar[event->BaseVarIndex + 1] = ((valuerf >> 16) & 0xFFFF);
String log = F("RF Code Recieved: ");
log += String(valuerf);
addLog(LOG_LEVEL_INFO, log);
/*
Usage:
1=RFSEND
2=commando
3=repeat (if not set will use default settings)
4=bits (if not set will use default settings)
1 2 3 4
http://<ESP IP address>/control?cmd=RFSEND,blablacommando,10,24
*/
String url = String(Settings.Name) + "/control?cmd=RFSEND," + String(rfReceiver->getReceivedValue()) + ",1," + String(rfReceiver->getReceivedBitlength());
String printString = F("To send this command, ");
//addLog(LOG_LEVEL_INFO, printString);
printString += F("use this: <a href=\"http://");
printString += url;
printString += F("\">URL</a>");
addLog(LOG_LEVEL_INFO, printString);
sendData(event);
}
rfReceiver->resetAvailable();
}
success = true;
break;
}
}
return success;
}
/* extended logging, for in terminal monitor */
static const char* bin2tristate(const char* bin);
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);
void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
if (decimal == 0) {
Serial.print("Unknown encoding.");
} else {
const char* b = dec2binWzerofill(decimal, length);
Serial.print("Decimal: ");
Serial.print(decimal);
Serial.print(" (");
Serial.print( length );
Serial.print("Bit) Binary: ");
Serial.print( b );
Serial.print(" Tri-State: ");
Serial.print( bin2tristate( b) );
Serial.print(" PulseLength: ");
Serial.print(delay);
Serial.print(" microseconds");
Serial.print(" Protocol: ");
Serial.println(protocol);
}
Serial.print("Raw data: ");
for (unsigned int i=0; i<= length*2; i++) {
Serial.print(raw[i]);
Serial.print(",");
}
Serial.println();
Serial.println();
}
static const char* bin2tristate(const char* bin) {
static char returnValue[50];
int pos = 0;
int pos2 = 0;
while (bin[pos]!='\0' && bin[pos+1]!='\0') {
if (bin[pos]=='0' && bin[pos+1]=='0') {
returnValue[pos2] = '0';
} else if (bin[pos]=='1' && bin[pos+1]=='1') {
returnValue[pos2] = '1';
} else if (bin[pos]=='0' && bin[pos+1]=='1') {
returnValue[pos2] = 'F';
} else {
return "not applicable";
}
pos = pos+2;
pos2++;
}
returnValue[pos2] = '\0';
return returnValue;
}
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
static char bin[64];
unsigned int i=0;
while (Dec > 0) {
bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
Dec = Dec >> 1;
}
for (unsigned int j = 0; j< bitLength; j++) {
if (j >= bitLength - i) {
bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
} else {
bin[j] = '0';
}
}
bin[bitLength] = '\0';
return bin;
}
#endif