-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver.cpp
More file actions
176 lines (126 loc) · 3.13 KB
/
receiver.cpp
File metadata and controls
176 lines (126 loc) · 3.13 KB
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
#include "rcswitch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <wiringPi.h>
#include <iostream>
#include <string>
#include <fstream>
void checkInput();
void receiveMessageAndOutput(int pin, int massage);
RCSwitch mySwitch;
void checkInput(){
while(1)
{
std::string input;
std::cin >> input;
if(input == "exit"){
printf("Stoppe Programm");
exit(0);
}
}
}
void receiveMessageAndOutput(int pin, int massage){
pinMode (pin, OUTPUT);
while(1) {
delay(10);
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
} else if(value == massage) {
digitalWrite (pin, HIGH) ; // On
delay (500) ; // mS
digitalWrite (pin, LOW) ; // Off
}
mySwitch.resetAvailable();
}
}
}
void decode(int value){
static int sReturn[12];
int nReturnPos = 0;
for (int i = 0; i < 12; i++) {
sReturn[11 - i] = (value & (3 << (2 * i))) >> (2 * i);
}
/*
for (int i = 0; i < 12; i++) {
printf("%i", sReturn[i]);
}
printf("\n"); */
static char group[5];
for (int i = 0; i < 5; i++) {
group[i] = (sReturn[nReturnPos++] == 0) ? '1' : '0';
}
printf("group %s ", group);
int number = 0;
for (int i = 1; i < 6; i++) {
if(sReturn[nReturnPos++] == 0)
{
number = i;
}
}
printf("number %i ", number);
printf("state %s \n \n", sReturn[nReturnPos++] == 0 ? "1" : "0");
}
int main(int argc, char *argv[]) {
// This pin is not the first pin on the RPi GPIO header!
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
// for more information.
int PIN = 2;
if(wiringPiSetup() == -1) {
printf("wiringPiSetup failed, exiting...");
return 0;
}
mySwitch = RCSwitch();
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
if( argc > 2 )
{
if(strcmp(argv[1], "-l") == 0)
{
printf("Listen mode\n");
while(1)
{
delay(10);
if ( mySwitch.available() )
{
int value = mySwitch.getReceivedValue();
if (value == 0)
{
printf("Unknown encoding\n");
} else
{
printf( "Received message code:%i\n", mySwitch.getReceivedValue() );
decode( mySwitch.getReceivedValue() );
}
mySwitch.resetAvailable();
}
}
}
else if( strcmp(argv[1], "-s" ) == 0)
{
printf("service go \n");
std::ifstream infile("/home/pi/433Mhz-Receiver-Raspberry/init");
int outputPin = 1;
int code = 0;
std::string line;
std::getline(infile, line);
outputPin = atoi( line.c_str() );
std::getline(infile, line);
code = atoi( line.c_str() );
printf("Pin %i\n", outputPin);
printf("Code %i\n", code);
receiveMessageAndOutput(outputPin, code);
}
else if( argc > 3 )
{
int outputPin = 1;
outputPin = atoi(argv[1]);
int code = atoi(argv[2]);
receiveMessageAndOutput(outputPin, code);
}
}
else
{
printf( "receiver [-l|-s|<pin><code>]\n" );
}
exit(0);
}