-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathookDecoder.ino
178 lines (147 loc) · 4.17 KB
/
ookDecoder.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
#define VERSION "v0.9 20151228"
#include <util/atomic.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include "DecodeOOK.h"
#include "Blueline.h"
#include "Acurite5n1.h"
#include "Acurite592TX.h"
#define DPIN_OOK_RX 2
#define DPIN_LED 13
#define ARRAY_SIZE 200
#define REPORT_TIME 30000
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 0, 200 };
byte ip[] = { 192, 168, 0, 70};
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
Blueline blueline;
Acurite5n1 acurite5n1;
Acurite592TX acurite592tx;
volatile word pulse; //pulse duration
long previousMillis = 0;
char packet[100];
#if DPIN_OOK_RX >= 14
#define VECT PCINT1_vect
#elif DPIN_OOK_RX >= 8
#define VECT PCINT0_vect
#else
#define VECT PCINT2_vect
#endif
ISR(VECT) {
PinChange();
}
static void setupPinChangeInterrupt ()
{
pinMode(DPIN_OOK_RX, INPUT);
#if DPIN_OOK_RX >= 14
bitSet(PCMSK1, DPIN_OOK_RX - 14);
bitSet(PCICR, PCIE1);
#elif DPIN_OOK_RX >= 8
bitSet(PCMSK0, DPIN_OOK_RX - 8);
bitSet(PCICR, PCIE0);
#else
PCMSK2 = bit(DPIN_OOK_RX);
bitSet(PCICR, PCIE2);
#endif
}
void PinChange(void) {
static word last;
// determine the pulse length in microseconds, for either polarity
pulse = micros() - last;
last += pulse;
}
void reportSerial (const char* s, class DecodeOOK& decoder) {
byte pos;
const byte* data = decoder.getData(pos);
Serial.print("["); Serial.print(millis() / 1000); Serial.print("] ");
for (byte i = 0; i < pos; ++i) {
Serial.print(data[i] >> 4, HEX);
Serial.print(data[i] & 0x0F, HEX);
}
Serial.print(' ');
Serial.println(s);
decoder.resetDecoder();
}
void setup () {
delay(250); // delay so that W5100 Ethernet chip
// has enough time to reset
Serial.begin(38400);
pinMode(DPIN_LED,OUTPUT);
setupPinChangeInterrupt();
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("ookDecoder", "online");
client.publish("ookDecoder", VERSION);
Serial.println("ookDecoder started");
Serial.println(VERSION);
}
}
void loop () {
//may have issues with rollover
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > REPORT_TIME) {
previousMillis = currentMillis;
if (client.connect("arduinoClient")) {
Serial.println("connected to arduinoClient");
client.publish("ookDecoder","report");
blueline.MQTTreport(packet);
if (strlen(packet) > 0) {
client.publish("blueline",packet);
Serial.println(packet);
}
acurite5n1.MQTTreport(packet);
if (strlen(packet) > 0) {
client.publish("acurite5n1",packet);
Serial.println(packet);
}
acurite592tx.MQTTreport(packet);
if (strlen(packet) > 0) {
client.publish("acurite592tx",packet);
Serial.println(packet);
}
} else {
Serial.println("connection failed");
}
}
static int i = 0;
word p;
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
p = pulse;
pulse = 0;
}
if (p>150 && p<2000) {
if (blueline.nextPulse(p)) {
//turn on led
digitalWrite(DPIN_LED, HIGH);
//reportSerial("Blueline", blueline);
blueline.decodeRxPacket();
//blueline.PrintRaw();
blueline.resetDecoder();
digitalWrite(DPIN_LED,LOW);
}
if (acurite5n1.nextPulse(p)) {
//turn on led
digitalWrite(DPIN_LED, HIGH);
//reportSerial("Acurite5n1", acurite5n1);
acurite5n1.DecodePacket();
//acurite5n1.PrintRaw();
acurite5n1.resetDecoder();
digitalWrite(DPIN_LED,LOW);
}
if (acurite592tx.nextPulse(p)) {
//turn on led
digitalWrite(DPIN_LED, HIGH);
//reportSerial("Acurite592TX", acurite592tx);
acurite592tx.DecodePacket();
//acurite592tx.PrintRaw();
acurite592tx.resetDecoder();
digitalWrite(DPIN_LED,LOW);
}
}
}