Repository navigation
Expand file tree
/
Copy pathrscan.cpp
More file actions
executable file
·203 lines (184 loc) · 3.28 KB
/
Copy pathrscan.cpp
File metadata and controls
executable file
·203 lines (184 loc) · 3.28 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
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
/*
* Receiver for data of wireless weather sensors with RX868 and Raspberry Pi.
*
* Main program.
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <string.h>
using namespace std;
static volatile int keepRunning = 1;
// String to recognize the sensor
//char idst[] = "001100000101\0";
char idst[] = "001100101110\0";
char sign[200];
char sipo = 0;
int temp = 0;
int rainl = 0;
int rainb = 0;
int logging = 0;
float temp_in = 0;
string temp_in_file = "";
int newbit(char bit);
//Create file stream myfile
ofstream myfile;
ifstream tempfile;
/*
* Signal handler to catch Ctrl-C to terminate the program safely.
*/
void sigIntHandler(int dummy)
{
keepRunning = 0;
}
/*
* Thread to read the output of the receiver module.
*/
PI_THREAD(decoderThread)
{
piHiPri(50);
int x;
int len = 0;
int lo = 0;
int hi = 0;
int px = 0;
while (keepRunning)
{
x = digitalRead(2);
len++;
if (0 == x)
{
lo++;
}
else
{
hi++;
}
if (x != px && 0 == x)
{
// slope high->low
if (hi > 3 && hi < 12)
{
newbit('0');
}
else
{
newbit('1');
}
len = 1;
lo = 1;
hi = 0;
}
px = x;
delayMicroseconds(200);
}
}
// Not the best decoder, but working...
int newbit(char bit)
{
if ((strlen(idst) - 1 > sipo) && (bit != idst[sipo]))
{
// Not the sensor -> reset
sipo = 0;
temp = 0;
rainl = 0;
rainb = 0;
return 0;
}
sign[sipo] = bit;
if (sipo > 13 && sipo < 24)
{
temp *= 2;
if (bit == '1')
{
temp += 1;
}
}
if (sipo > 23 && sipo < 32)
{
rainl *= 2;
if (bit == '1')
{
rainl += 1;
}
}
if (sipo > 31 && sipo < 40)
{
rainb *= 2;
if (bit == '1')
{
rainb += 1;
}
}
if (sipo > 86)
{
// Print the signal
if (logging == 1)
{
printf("Signal found:\n%s\n . . . i i ?ttttttttttrrrrrrrrRRRRRRRR . . m m m m d d d d d d\n", sign);
float rtemp = (temp - 400) / 10.0;
printf("Temp: %.1f°C\n", rtemp);
float rrain = (rainb * 256 + rainl) * 0.3;
printf("Rain: %.1f mm\n", rrain);
}
sipo = 0;
temp = 0;
rainl = 0;
rainb = 0;
//Get temp from w1 device
try
{
tempfile.open("/sys/bus/w1/devices/28-00000a29b03b/w1_slave");
if (tempfile.is_open())
{
getline(tempfile, temp_in_file);
getline(tempfile, temp_in_file);
tempfile.close();
}
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
//Print raw signal to file
myfile.open("/home/pi/tempRainPi/rawdata.txt");
myfile << sign;
myfile << ";";
myfile << time(0);
myfile << ";";
myfile << temp_in_file.substr(29);
myfile.close();
return 0;
}
sipo++;
return 0;
}
/*
* MAIN
*/
int main()
{
wiringPiSetup();
/*
BCM GPIO 27: DATA (IN) == WiPin 2
*/
pinMode(2, INPUT);
pullUpDnControl(2, PUD_DOWN);
signal(SIGINT, sigIntHandler);
piThreadCreate(decoderThread);
while (keepRunning)
{
piLock(1);
piUnlock(1);
delay(200);
}
if (logging == 1)
{
printf("clean up and exit\n");
}
}