socket_switcher 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.utilsrc +26 -0
- data/Gemfile +8 -0
- data/README.md +0 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/arduino-receiver/receiver/receiver.ino +36 -0
- data/arduino/lib/.holder +0 -0
- data/arduino/lib/RCSwitch/RCSwitch.cpp +823 -0
- data/arduino/lib/RCSwitch/RCSwitch.h +144 -0
- data/arduino/lib/RCSwitch/examples/ReceiveDemo_Advanced/ReceiveDemo_Advanced.pde +24 -0
- data/arduino/lib/RCSwitch/examples/ReceiveDemo_Advanced/helperfunctions.ino +20 -0
- data/arduino/lib/RCSwitch/examples/ReceiveDemo_Advanced/output.ino +52 -0
- data/arduino/lib/RCSwitch/examples/ReceiveDemo_Simple/ReceiveDemo_Simple.pde +35 -0
- data/arduino/lib/RCSwitch/examples/SendDemo/SendDemo.pde +57 -0
- data/arduino/lib/RCSwitch/examples/TypeA_WithDIPSwitches/TypeA_WithDIPSwitches.pde +40 -0
- data/arduino/lib/RCSwitch/examples/TypeA_WithDIPSwitches_Lightweight/TypeA_WithDIPSwitches_Lightweight.ino +43 -0
- data/arduino/lib/RCSwitch/examples/TypeB_WithRotaryOrSlidingSwitches/TypeB_WithRotaryOrSlidingSwitches.pde +40 -0
- data/arduino/lib/RCSwitch/examples/TypeC_Intertechno/TypeC_Intertechno.pde +40 -0
- data/arduino/lib/RCSwitch/examples/TypeD_REV/TypeD_REV.ino +41 -0
- data/arduino/lib/RCSwitch/examples/Webserver/Webserver.pde +154 -0
- data/arduino/lib/RCSwitch/keywords.txt +57 -0
- data/arduino/src/switcher/switcher.ino.erb +79 -0
- data/config/default.yml +11 -0
- data/config/traffic_lights.yml +8 -0
- data/examples/blinkenlights.rb +25 -0
- data/lib/socket_switcher.rb +7 -0
- data/lib/socket_switcher/device.rb +24 -0
- data/lib/socket_switcher/errors.rb +26 -0
- data/lib/socket_switcher/port.rb +134 -0
- data/lib/socket_switcher/version.rb +8 -0
- data/socket_switcher.gemspec +49 -0
- data/spec/socket_switcher/device_spec.rb +37 -0
- data/spec/socket_switcher/port_spec.rb +216 -0
- data/spec/spec_helper.rb +9 -0
- metadata +176 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
/*
|
2
|
+
Example for Intertechno outlets
|
3
|
+
|
4
|
+
http://code.google.com/p/rc-switch/
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <RCSwitch.h>
|
8
|
+
|
9
|
+
RCSwitch mySwitch = RCSwitch();
|
10
|
+
|
11
|
+
void setup() {
|
12
|
+
|
13
|
+
// Transmitter is connected to Arduino Pin #10
|
14
|
+
mySwitch.enableTransmit(10);
|
15
|
+
|
16
|
+
// Optional set pulse length.
|
17
|
+
// mySwitch.setPulseLength(320);
|
18
|
+
|
19
|
+
}
|
20
|
+
|
21
|
+
void loop() {
|
22
|
+
|
23
|
+
// Switch on:
|
24
|
+
// The first parameter represents the familycode (a, b, c, ... f)
|
25
|
+
// The second parameter represents the group number
|
26
|
+
// The third parameter represents the device number
|
27
|
+
//
|
28
|
+
// In this example it's family 'b', group #3, device #2
|
29
|
+
mySwitch.switchOn('b', 3, 2);
|
30
|
+
|
31
|
+
// Wait a second
|
32
|
+
delay(1000);
|
33
|
+
|
34
|
+
// Switch off
|
35
|
+
mySwitch.switchOff('b', 3, 2);
|
36
|
+
|
37
|
+
// Wait another second
|
38
|
+
delay(1000);
|
39
|
+
|
40
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/*
|
2
|
+
Example for REV outlets (e.g. 8342L)
|
3
|
+
|
4
|
+
http://code.google.com/p/rc-switch/
|
5
|
+
|
6
|
+
Need help? http://forum.ardumote.com
|
7
|
+
*/
|
8
|
+
|
9
|
+
#include <RCSwitch.h>
|
10
|
+
|
11
|
+
RCSwitch mySwitch = RCSwitch();
|
12
|
+
|
13
|
+
void setup() {
|
14
|
+
|
15
|
+
// Transmitter is connected to Arduino Pin #10
|
16
|
+
mySwitch.enableTransmit(10);
|
17
|
+
|
18
|
+
// set pulse length.
|
19
|
+
mySwitch.setPulseLength(360);
|
20
|
+
|
21
|
+
}
|
22
|
+
|
23
|
+
void loop() {
|
24
|
+
|
25
|
+
// Switch on:
|
26
|
+
// The first parameter represents the channel (a, b, c, d)
|
27
|
+
// The second parameter represents the device number
|
28
|
+
//
|
29
|
+
// In this example it's family 'd', device #2
|
30
|
+
mySwitch.switchOn('d', 2);
|
31
|
+
|
32
|
+
// Wait a second
|
33
|
+
delay(1000);
|
34
|
+
|
35
|
+
// Switch off
|
36
|
+
mySwitch.switchOff('d', 2);
|
37
|
+
|
38
|
+
// Wait another second
|
39
|
+
delay(1000);
|
40
|
+
|
41
|
+
}
|
@@ -0,0 +1,154 @@
|
|
1
|
+
/*
|
2
|
+
A simple RCSwitch/Ethernet/Webserver demo
|
3
|
+
|
4
|
+
http://code.google.com/p/rc-switch/
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <SPI.h>
|
8
|
+
#include <Ethernet.h>
|
9
|
+
#include <RCSwitch.h>
|
10
|
+
|
11
|
+
// Ethernet configuration
|
12
|
+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
|
13
|
+
byte ip[] = { 192,168,0, 2 }; // IP Address
|
14
|
+
EthernetServer server(80); // Server Port 80
|
15
|
+
|
16
|
+
// RCSwitch configuration
|
17
|
+
RCSwitch mySwitch = RCSwitch();
|
18
|
+
int RCTransmissionPin = 7;
|
19
|
+
|
20
|
+
// More to do...
|
21
|
+
// You should also modify the processCommand() and
|
22
|
+
// httpResponseHome() functions to fit your needs.
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Setup
|
28
|
+
*/
|
29
|
+
void setup() {
|
30
|
+
Ethernet.begin(mac, ip);
|
31
|
+
server.begin();
|
32
|
+
mySwitch.enableTransmit( RCTransmissionPin );
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Loop
|
37
|
+
*/
|
38
|
+
void loop() {
|
39
|
+
char* command = httpServer();
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Command dispatcher
|
44
|
+
*/
|
45
|
+
void processCommand(char* command) {
|
46
|
+
if (strcmp(command, "1-on") == 0) {
|
47
|
+
mySwitch.switchOn(1,1);
|
48
|
+
} else if (strcmp(command, "1-off") == 0) {
|
49
|
+
mySwitch.switchOff(1,1);
|
50
|
+
} else if (strcmp(command, "2-on") == 0) {
|
51
|
+
mySwitch.switchOn(1,2);
|
52
|
+
} else if (strcmp(command, "2-off") == 0) {
|
53
|
+
mySwitch.switchOff(1,2);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
/**
|
58
|
+
* HTTP Response with homepage
|
59
|
+
*/
|
60
|
+
void httpResponseHome(EthernetClient c) {
|
61
|
+
c.println("HTTP/1.1 200 OK");
|
62
|
+
c.println("Content-Type: text/html");
|
63
|
+
c.println();
|
64
|
+
c.println("<html>");
|
65
|
+
c.println("<head>");
|
66
|
+
c.println( "<title>RCSwitch Webserver Demo</title>");
|
67
|
+
c.println( "<style>");
|
68
|
+
c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
|
69
|
+
c.println( "</style>");
|
70
|
+
c.println("</head>");
|
71
|
+
c.println("<body>");
|
72
|
+
c.println( "<h1>RCSwitch Webserver Demo</h1>");
|
73
|
+
c.println( "<ul>");
|
74
|
+
c.println( "<li><a href=\"./?1-on\">Switch #1 on</a></li>");
|
75
|
+
c.println( "<li><a href=\"./?1-off\">Switch #1 off</a></li>");
|
76
|
+
c.println( "</ul>");
|
77
|
+
c.println( "<ul>");
|
78
|
+
c.println( "<li><a href=\"./?2-on\">Switch #2 on</a></li>");
|
79
|
+
c.println( "<li><a href=\"./?2-off\">Switch #2 off</a></li>");
|
80
|
+
c.println( "</ul>");
|
81
|
+
c.println( "<hr>");
|
82
|
+
c.println( "<a href=\"http://code.google.com/p/rc-switch/\">http://code.google.com/p/rc-switch/</a>");
|
83
|
+
c.println("</body>");
|
84
|
+
c.println("</html>");
|
85
|
+
}
|
86
|
+
|
87
|
+
/**
|
88
|
+
* HTTP Redirect to homepage
|
89
|
+
*/
|
90
|
+
void httpResponseRedirect(EthernetClient c) {
|
91
|
+
c.println("HTTP/1.1 301 Found");
|
92
|
+
c.println("Location: /");
|
93
|
+
c.println();
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* HTTP Response 414 error
|
98
|
+
* Command must not be longer than 30 characters
|
99
|
+
**/
|
100
|
+
void httpResponse414(EthernetClient c) {
|
101
|
+
c.println("HTTP/1.1 414 Request URI too long");
|
102
|
+
c.println("Content-Type: text/plain");
|
103
|
+
c.println();
|
104
|
+
c.println("414 Request URI too long");
|
105
|
+
}
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Process HTTP requests, parse first request header line and
|
109
|
+
* call processCommand with GET query string (everything after
|
110
|
+
* the ? question mark in the URL).
|
111
|
+
*/
|
112
|
+
char* httpServer() {
|
113
|
+
EthernetClient client = server.available();
|
114
|
+
if (client) {
|
115
|
+
char sReturnCommand[32];
|
116
|
+
int nCommandPos=-1;
|
117
|
+
sReturnCommand[0] = '\0';
|
118
|
+
while (client.connected()) {
|
119
|
+
if (client.available()) {
|
120
|
+
char c = client.read();
|
121
|
+
if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
|
122
|
+
sReturnCommand[nCommandPos] = '\0';
|
123
|
+
if (strcmp(sReturnCommand, "\0") == 0) {
|
124
|
+
httpResponseHome(client);
|
125
|
+
} else {
|
126
|
+
processCommand(sReturnCommand);
|
127
|
+
httpResponseRedirect(client);
|
128
|
+
}
|
129
|
+
break;
|
130
|
+
}
|
131
|
+
if (nCommandPos>-1) {
|
132
|
+
sReturnCommand[nCommandPos++] = c;
|
133
|
+
}
|
134
|
+
if (c == '?' && nCommandPos == -1) {
|
135
|
+
nCommandPos = 0;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
if (nCommandPos > 30) {
|
139
|
+
httpResponse414(client);
|
140
|
+
sReturnCommand[0] = '\0';
|
141
|
+
break;
|
142
|
+
}
|
143
|
+
}
|
144
|
+
if (nCommandPos!=-1) {
|
145
|
+
sReturnCommand[nCommandPos] = '\0';
|
146
|
+
}
|
147
|
+
// give the web browser time to receive the data
|
148
|
+
delay(1);
|
149
|
+
client.stop();
|
150
|
+
|
151
|
+
return sReturnCommand;
|
152
|
+
}
|
153
|
+
return '\0';
|
154
|
+
}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#######################################
|
2
|
+
# Syntax Coloring Map For RCSwitch
|
3
|
+
#######################################
|
4
|
+
|
5
|
+
#######################################
|
6
|
+
# Datatypes (KEYWORD1)
|
7
|
+
#######################################
|
8
|
+
|
9
|
+
RCSwitch KEYWORD1
|
10
|
+
|
11
|
+
#######################################
|
12
|
+
# Methods and Functions (KEYWORD2)
|
13
|
+
#######################################
|
14
|
+
|
15
|
+
##########
|
16
|
+
#SENDS Begin
|
17
|
+
##########
|
18
|
+
switchOn KEYWORD2
|
19
|
+
switchOff KEYWORD2
|
20
|
+
sendTriState KEYWORD2
|
21
|
+
send KEYWORD2
|
22
|
+
##########
|
23
|
+
#SENDS End
|
24
|
+
##########
|
25
|
+
|
26
|
+
##########
|
27
|
+
#RECEIVE Begin
|
28
|
+
##########
|
29
|
+
enableReceive KEYWORD2
|
30
|
+
disableReceive KEYWORD2
|
31
|
+
available KEYWORD2
|
32
|
+
resetAvailable KEYWORD2
|
33
|
+
setReceiveTolerance KEYWORD2
|
34
|
+
getReceivedValue KEYWORD2
|
35
|
+
getReceivedBitlength KEYWORD2
|
36
|
+
getReceivedDelay KEYWORD2
|
37
|
+
getReceivedProtocol KEYWORD2
|
38
|
+
getReceivedRawdata KEYWORD2
|
39
|
+
##########
|
40
|
+
#RECEIVE End
|
41
|
+
##########
|
42
|
+
|
43
|
+
##########
|
44
|
+
#OTHERS Begin
|
45
|
+
##########
|
46
|
+
enableTransmit KEYWORD2
|
47
|
+
disableTransmit KEYWORD2
|
48
|
+
setPulseLength KEYWORD2
|
49
|
+
setProtocol KEYWORD2
|
50
|
+
setRepeatTransmit KEYWORD2
|
51
|
+
##########
|
52
|
+
#OTHERS End
|
53
|
+
##########
|
54
|
+
|
55
|
+
#######################################
|
56
|
+
# Constants (LITERAL1)
|
57
|
+
#######################################
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#include <RCSwitch.h>
|
2
|
+
|
3
|
+
#define DEBUG 0
|
4
|
+
#define DEVICE_MIN 0
|
5
|
+
#define DEVICE_MAX <%= device_max %>
|
6
|
+
#define RDY "RDY 0 <%= device_max %>"
|
7
|
+
#define STATE_OFF 0
|
8
|
+
#define STATE_ON 1
|
9
|
+
#define NL 10
|
10
|
+
#define CR 13
|
11
|
+
|
12
|
+
static unsigned long deviceOn[] = { <%= device_on * ', ' %> };
|
13
|
+
static unsigned long deviceOff[] = { <%= device_off * ', ' %> };
|
14
|
+
|
15
|
+
RCSwitch mySwitch = RCSwitch();
|
16
|
+
|
17
|
+
void setup() {
|
18
|
+
#if DEBUG
|
19
|
+
for (int i = 8; i <= 11; i++) {
|
20
|
+
pinMode(i, OUTPUT);
|
21
|
+
}
|
22
|
+
#endif
|
23
|
+
Serial.begin(9600, SERIAL_8N1);
|
24
|
+
Serial.setTimeout(1000);
|
25
|
+
Serial.println(RDY);
|
26
|
+
|
27
|
+
mySwitch.enableTransmit(<%= transmit_pin %>);
|
28
|
+
//mySwitch.setPulseLength(330);
|
29
|
+
//mySwitch.setRepeatTransmit(15);
|
30
|
+
}
|
31
|
+
|
32
|
+
void setState(int device, int state) {
|
33
|
+
#if DEBUG
|
34
|
+
digitalWrite(device + 8, state ? HIGH : LOW);
|
35
|
+
#endif
|
36
|
+
|
37
|
+
mySwitch.send(state ? deviceOn[device] : deviceOff[device], 24);
|
38
|
+
}
|
39
|
+
|
40
|
+
void switchDevice(int device, int state) {
|
41
|
+
if (device < DEVICE_MIN || device > DEVICE_MAX) {
|
42
|
+
Serial.println("NAK device");
|
43
|
+
return;
|
44
|
+
}
|
45
|
+
if (state != STATE_OFF && state != STATE_ON) {
|
46
|
+
Serial.println("NAK state");
|
47
|
+
return;
|
48
|
+
}
|
49
|
+
setState(device, state);
|
50
|
+
Serial.println("ACK");
|
51
|
+
}
|
52
|
+
|
53
|
+
bool readEol() {
|
54
|
+
if (Serial.peek() == CR) {
|
55
|
+
Serial.read();
|
56
|
+
if (Serial.peek() == NL) {
|
57
|
+
Serial.read();
|
58
|
+
}
|
59
|
+
return true;
|
60
|
+
} else {
|
61
|
+
return false;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
void loop() {
|
66
|
+
while (Serial.available() > 0) {
|
67
|
+
if (readEol()) {
|
68
|
+
Serial.println(RDY);
|
69
|
+
break;
|
70
|
+
}
|
71
|
+
|
72
|
+
int device = Serial.parseInt();
|
73
|
+
int state = Serial.parseInt();
|
74
|
+
|
75
|
+
if (readEol()) {
|
76
|
+
switchDevice(device, state);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
}
|
data/config/default.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$: << 'lib'
|
4
|
+
require 'socket_switcher'
|
5
|
+
|
6
|
+
specifier = ARGV.shift or fail "need port specifier"
|
7
|
+
port = SocketSwitcher::Port.new(specifier, timeout: 2000, debug: true)
|
8
|
+
max = (ENV['MAX_DEVICE'] || 3).to_i
|
9
|
+
(0..max).map do |i|
|
10
|
+
Thread.new do
|
11
|
+
loop do
|
12
|
+
begin
|
13
|
+
d = port.device(i)
|
14
|
+
rand < 0.5 ? d.on : d.off
|
15
|
+
sleep rand
|
16
|
+
rescue SocketSwitcher::CommunicationError => e
|
17
|
+
warn "Caught #{e.class}: #{e} => exiting!"
|
18
|
+
exit
|
19
|
+
rescue SocketSwitcher::LigthSwitchError => e
|
20
|
+
warn "Caught #{e.class}: #{e}"
|
21
|
+
sleep rand
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end.each(&:join)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class SocketSwitcher::Device
|
2
|
+
def initialize(port, number)
|
3
|
+
@port = port
|
4
|
+
@number = number
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :port
|
8
|
+
|
9
|
+
attr_reader :number
|
10
|
+
|
11
|
+
def on
|
12
|
+
@port.__send__(:set_state, self, 1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def off
|
16
|
+
@port.__send__(:set_state, self, 0)
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
"#<#{self.class} number=#{number}>"
|
21
|
+
end
|
22
|
+
|
23
|
+
alias inspect to_s
|
24
|
+
end
|