-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduinobridgerf.ino
213 lines (176 loc) · 6.01 KB
/
arduinobridgerf.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
/**
* @author Rogier van den Berg / http://www.rogiervandenberg.nl
*
* This project is to send/receive RF-signals (KaKu, Elro, etc).
* Its purpose is to function as a proxy/bridge, so you can toggle lights or receive commands in your domotica solution.
*
* It is designed to work with combined with https://github.com/bslagter/hue-control
*
* I am using the interpretation of RF-signals from the RemoteSwitch Libraries by Randy Simons,
* as found at https://bitbucket.org/fuzzillogic/433mhzforarduino/wiki/Home
*
*
==How to use==
* A command is built with 5 comma separated values:
* - Direction of the message: O or I (outgoing, incoming);
* - Library that applies: 1, 2 or 3 (NewRemoteSwitch, RemoteSwitch or RemoteSensor)
* - Code: xxxx (any value)
* - Period: xxx (microseconds)
* - Additional details: 0000 (4 digits, containing: Type (0=Off, 1=On, 2=Dim), 2 digits for Unit (which light), Dim value (always 0 for now)
*
* E.g: O,1,2559398,254,1020 (outgoing, NewRemoteswitch, code, period, type 1 / unit 02 / dim 0) --> Turn light 2 of remote 2559398 ON.
*/
#include <RemoteReceiver.h>
#include <RemoteTransmitter.h>
#include <NewRemoteReceiver.h>
#include <NewRemoteTransmitter.h>
#include <InterruptChain.h>
#include <String.h>
String incomingPacketBuffer = ""; //buffer to hold incoming packet,
String outgoingPacketBuffer = ""; //buffer to hold outgoing packet,
char packetBuffer[50]; //buffer to hold incoming packet,
//Connections of the RF transmitter and LED to the Arduino board (receiver is 2)
const int led = 4;
const int transmitterPin = 8;
boolean sending = false;
boolean serialMessageToSend = false;
boolean stringComplete = false; // whether the string is complete
void setup()
{
pinMode(led, OUTPUT);
incomingPacketBuffer.reserve(50);
outgoingPacketBuffer.reserve(50);
// Open serial communications and wait for port to open:
Serial.begin(115200);
// Interrupt -1 to indicate you will call the interrupt handler with InterruptChain
RemoteReceiver::init(-1, 2, handleOldRfRemotes);
// Again, interrupt -1 to indicate you will call the interrupt handler with InterruptChain
NewRemoteReceiver::init(-1, 2, handleNewRfRemotes);
// On interrupt, call the interrupt handlers of remote and sensor receivers
InterruptChain::addInterruptCallback(0, RemoteReceiver::interruptHandler);
InterruptChain::addInterruptCallback(0, NewRemoteReceiver::interruptHandler);
}
void loop() {
if (stringComplete) {
//Serial.println(incomingPacketBuffer);
incomingPacketBuffer.toCharArray(packetBuffer, 50);
handleCommand(packetBuffer);
// clear the string:
incomingPacketBuffer = "";
stringComplete = false;
}
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
incomingPacketBuffer += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
//If we receive a command, parse it to do something with it
void handleCommand(char* receivedCommand) {
char *p = receivedCommand;
char *str;
int count = 0;
String commandValues[5];
unsigned long receivedCode = 0;
unsigned int period = 0;
while ((str = strtok_r(p, ",", &p)) != NULL) {
if (count == 2)
{
receivedCode = atol(str); //Convert to long integer
}
if (count == 3)
{
period = atoi(str); //Convert to integer
}
commandValues[count++] = str;
}
if (commandValues[0] == "O" && commandValues[1] == "2") {
/*
Serial.print("Sending out over RF: ");
Serial.print(receivedCode);
Serial.print(" / ");
Serial.println(period);
*/
RemoteReceiver::disable();
RemoteTransmitter::sendCode(8, receivedCode, period, 3);
RemoteReceiver::enable();
} else if (commandValues[0] == "O" && commandValues[1] == "1") {
Serial.println("new kakuding");
}
}
// shows the received code sent from an old-style remote switch
void handleOldRfRemotes(unsigned long receivedCode, unsigned int period) {
// Print the received code.
/*
Serial.print("Interupted! Code: ");
Serial.print(receivedCode);
Serial.print(", period: ");
Serial.print(period);
Serial.println("us.");
*/
String codeString(receivedCode);
String periodString(period);
String command = "I,2," + codeString + "," + periodString + ",0000";
Serial.println(command);
}
// shows the received code sent from an old-style remote switch
void handleNewRfRemotes(NewRemoteCode receivedCode) {
/*
// Print the received code.
Serial.print("Addr ");
Serial.print(receivedCode.address);
if (receivedCode.groupBit) {
Serial.print(" group");
} else {
Serial.print(" unit ");
Serial.print(receivedCode.unit);
}
switch (receivedCode.switchType) {
case 0:
Serial.print(" off");
break;
case 1:
Serial.print(" on");
break;
case 2:
Serial.print(" dim level");
Serial.print(receivedCode.dimLevel);
break;
}
Serial.print(", period: ");
Serial.print(receivedCode.period);
Serial.println("us.");
*/
String codeString(receivedCode.address);
String periodString(receivedCode.period);
String typeString(receivedCode.switchType);
String unitString(receivedCode.unit);
if (unitString.length() == 1) {
unitString = "0" + unitString;
}
String command = "I,1," + codeString + "," + periodString + "," + typeString + unitString + "0";
Serial.println(command);
}
void transmitCodeNewKaKu(NewRemoteCode receivedCode) {
NewRemoteReceiver::disable();
interrupts();
NewRemoteTransmitter transmitter(receivedCode.address, 11, receivedCode.period);
if (receivedCode.switchType == 2) {
transmitter.sendDim(receivedCode.unit, receivedCode.dimLevel);
}
else {
if (receivedCode.groupBit) {
transmitter.sendGroup(receivedCode.switchType);
}
else {
transmitter.sendUnit(receivedCode.unit, receivedCode.switchType);
}
}
NewRemoteReceiver::enable();
}