-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapture.cpp
233 lines (188 loc) · 6.24 KB
/
capture.cpp
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
227
228
229
230
231
232
233
/* Convert RF signal into bits (temperature sensor version)
* Written by : Ray Wang (Rayshobby LLC)
* http://rayshobby.net/?p=8827
* Update: adapted to RPi using WiringPi
*/
// ring buffer size has to be large enough to fit
// data between two successive sync signals
#include <string>
#include <time.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sqlite3.h>
#define RING_BUFFER_SIZE 256
#define SYNC_LENGTH 9000
#define SEP_LENGTH 500
#define BIT1_LENGTH 4000
#define BIT0_LENGTH 2000
#define DATA_PIN 2 // wiringPi GPIO 2 (P1.13)
unsigned long timings[RING_BUFFER_SIZE];
unsigned int syncIndex1 = 0;
unsigned int syncIndex2 = 0;
bool received = false;
const char *temperatureDatabase = "TemperatureReadings.db";
bool isSync(unsigned int idx) {
unsigned long t0 = timings[(idx + RING_BUFFER_SIZE - 1) % RING_BUFFER_SIZE];
unsigned long t1 = timings[idx];
if ((t0 > (SEP_LENGTH - 100)) && (t0 < (SEP_LENGTH + 100)) && (t1 > (SYNC_LENGTH - 1000)) && (t1 < (SYNC_LENGTH + 1000)) && (digitalRead(DATA_PIN) == HIGH)) {
return true;
}
return false;
}
int sqlCallback(void *NotUsed, int argc, char **argv, char **azColName) {
return 0;
}
void printTime()
{
time_t ltime = time(NULL); /* get current cal time */
printf("%s", asctime(localtime(<ime)));
}
void executesql(std::string sql) {
sqlite3 *db;
char *zErrMsg = 0;
int rc = sqlite3_open(temperatureDatabase, &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return;
} else {
fprintf(stdout, "Opened database successfully\n");
}
/* Execute SQL statement */
rc = sqlite3_exec(db, sql.c_str(), sqlCallback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Sql executed successfully\n");
}
sqlite3_close(db);
}
inline bool databaseExist() {
if (FILE *file = fopen(temperatureDatabase, "r")) {
fclose(file);
return true;
} else {
return false;
}
}
void createDatabase() {
if (!databaseExist()) {
std::string sql = "CREATE TABLE Temperatures(Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, SensorId REAL NOT NULL, Celsius INT NOT NULL, CreatedOn datetime default current_timestamp);";
executesql(sql);
std::string sql2 = "CREATE TABLE Sensors(Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, SensorId REAL NOT NULL, SensorName TEXT NOT NULL, CreatedOn datetime default current_timestamp);";
executesql(sql2);
}
}
void insertTemp(std::string sensor, int celsiusReading) {
std::string sql = "INSERT INTO Temperatures (SensorId, Celsius) VALUES (" + sensor + ", " + std::to_string(celsiusReading) + ");";
executesql(sql);
}
void handler() {
static unsigned long duration = 0;
static unsigned long lastTime = 0;
static unsigned int ringIndex = 0;
static unsigned int syncCount = 0;
if (received == true) {
return;
}
long time = micros();
duration = time - lastTime;
lastTime = time;
ringIndex = (ringIndex + 1) % RING_BUFFER_SIZE;
timings[ringIndex] = duration;
if (isSync(ringIndex)) {
syncCount++;
if (syncCount == 1) {
syncIndex1 = (ringIndex + 1) % RING_BUFFER_SIZE;
}
else if (syncCount == 2) {
syncCount = 0;
syncIndex2 = (ringIndex + 1) % RING_BUFFER_SIZE;
unsigned int changeCount = (syncIndex2 < syncIndex1) ? (syncIndex2 + RING_BUFFER_SIZE - syncIndex1) : (syncIndex2 - syncIndex1);
if (changeCount != 66) {
received = false;
syncIndex1 = 0;
syncIndex2 = 0;
}
else {
received = true;
}
}
}
}
int main(int argc, char *argv[]) {
createDatabase();
if (wiringPiSetup() == -1) {
printf("No wiring pi detected\n");
return 0;
}
wiringPiISR(DATA_PIN, INT_EDGE_BOTH, &handler);
while (true) {
if (received == true) {
std::string capturedBinary = "";
// system("/usr/local/bin/gpio edge 2 none");
for (unsigned int i = syncIndex1; i != syncIndex2; i = (i + 2) % RING_BUFFER_SIZE) {
unsigned long t0 = timings[i], t1 = timings[(i + 1) % RING_BUFFER_SIZE];
if ((t0 > (SEP_LENGTH - 200)) && (t0 < (SEP_LENGTH + 200))) {
if ((t1 > (BIT1_LENGTH - 1000)) && (t1 < (BIT1_LENGTH + 1000))) {
capturedBinary += "1";
printf("1");
} else if ((t1 > (BIT0_LENGTH - 1000)) && (t1 < (BIT0_LENGTH + 1000))) {
capturedBinary += "0";
printf("0");
} else {
printf("SYNC");
}
} else {
printf("?%d?", t0);
}
}
printf("\n");
unsigned long temp = 0;
bool negative = false;
bool fail = false;
for (unsigned int i = (syncIndex1 + 24) % RING_BUFFER_SIZE; i != (syncIndex1 + 48) % RING_BUFFER_SIZE; i = (i + 2) % RING_BUFFER_SIZE) {
unsigned long t0 = timings[i], t1 = timings[(i + 1) % RING_BUFFER_SIZE];
if ((t0 > (SEP_LENGTH - 200)) && (t0 < (SEP_LENGTH + 200))) {
if ((t1 > (BIT1_LENGTH - 1000)) && (t1 < (BIT1_LENGTH + 1000))) {
if (i == (syncIndex1 + 24) % RING_BUFFER_SIZE) {
negative = true;
}
temp = (temp << 1) + 1;
} else if ((t1 > (BIT0_LENGTH - 1000)) && (t1 < (BIT0_LENGTH + 1000))) {
temp = (temp << 1) + 0;
} else {
printf("not one or zero: %d\n", t1);
fail = true;
}
} else {
printf("wrong separation length: %d\n", t0);
fail = true;
}
}
if (!fail) {
if (negative) {
temp = 4096 - temp;
printf("-");
}
printTime();
int celsiusReading = (temp + 5) / 10;
// First bit in capturedBinary represents the sensor identifier
std::string sensor = capturedBinary.substr(0, 8);
printf("%d C %d F\n", celsiusReading, (temp * 9 / 5 + 325) / 10);
insertTemp(sensor, celsiusReading);
} else {
printf("Decoding Error.\n");
}
delay(1000);
received = false;
syncIndex1 = 0;
syncIndex2 = 0;
// wiringPiISR(DATA_PIN, INT_EDGE_BOTH, &handler);
printf("============================= \n \n \n");
}
}
exit(0);
}