-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorTransmitter.cpp
More file actions
183 lines (145 loc) · 4.75 KB
/
SensorTransmitter.cpp
File metadata and controls
183 lines (145 loc) · 4.75 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
/*
* RemoteSensor library v1.0.2 (20130601) for Arduino 1.0
*
* This library encodes, encrypts en transmits data to
* remote weather stations made by Hideki Electronics..
*
* Copyright 2011-2013 by Randy Simons http://randysimons.nl/
*
* Parts of this code based on Oopsje's CrestaProtocol.pdf, for which
* I thank him very much!
*
* License: GPLv3. See license.txt
*/
#include <SensorTransmitter.h>
/*******************
* Sensor base class
******************/
SensorTransmitter::SensorTransmitter(byte transmitterPin, byte randomId) {
_transmitterPin = transmitterPin;
_randomId = randomId;
pinMode(_transmitterPin, OUTPUT);
}
/* Encrypt data byte to send to station */
byte SensorTransmitter::encryptByte(byte b) {
byte a;
for(a=0; b; b<<=1) {
a^=b;
}
return a;
}
/* The second checksum. Input is OldChecksum^NewByte */
byte SensorTransmitter::secondCheck(byte b) {
byte c;
if (b&0x80) {
b^=0x95;
}
c = b^(b>>1);
if (b&1) {
c^=0x5f;
}
if (c&1) {
b^=0x5f;
}
return b^(c>>1);
}
/* Example to encrypt a package for sending,
Input: Buffer holds the unencrypted data.
Returns the number of bytes to send,
Buffer now holds data ready for sending.
*/
byte SensorTransmitter::encryptAndAddCheck(byte *buffer) {
byte cs1,cs2,count,i;
count=(buffer[2]>>1) & 0x1f;
cs1=0;
cs2=0;
for(i=1; i<count+1; i++) {
buffer[i]=encryptByte(buffer[i]);
cs1^=buffer[i];
cs2 =secondCheck(buffer[i]^cs2);
}
buffer[count+1]=cs1;
buffer[count+2]=secondCheck(cs1^cs2);
return count+3;
}
/* Send one byte and keep the transmitter ready to send the next */
void SensorTransmitter::sendManchesterByte(byte transmitterPin, byte b) {
byte i;
// Send start-bit 0.
digitalWrite(transmitterPin, LOW);
delayMicroseconds(500);
digitalWrite(transmitterPin, HIGH);
delayMicroseconds(500);
for (i = 0; i < 16; i++) {
if (b&1) {
digitalWrite(transmitterPin, HIGH);
} else {
digitalWrite(transmitterPin, LOW);
}
delayMicroseconds(500); /* 500uS delay */
b=~b;
if (i&1) {
b>>=1;
}
}
}
/* Send bytes (prepared by “encryptAndAddCheck”) and pause at the end. */
void SensorTransmitter::sendManchesterPackage(byte transmitterPin, byte *data, byte cnt) {
byte i;
for (i=0; i<cnt; i++) {
sendManchesterByte(transmitterPin, data[i]);
}
digitalWrite(transmitterPin, LOW); /* Drop the transmitter line */
}
/**
* Encrypts, adds checksums and transmits the data. The value of byte 3 in the data is ignored.
*/
void SensorTransmitter::sendPackage(byte transmitterPin, byte *data) {
byte buffer[14], temp, count;
for (temp=0x40; temp>=0x40; temp+=0x40) { /* Sends 3 packages */
memcpy(buffer, data, ((data[2] >> 1) & 0x1f) + 1);
buffer[3] = (buffer[3] & 0x1f) + temp;
count = encryptAndAddCheck(buffer); /* Encrypt, add checksum bytes */
sendManchesterPackage(transmitterPin, buffer,count); /* Send the package */
delay(30);
}
}
/************************************
* Thermo / Hygro sensor transmitter
***********************************/
ThermoHygroTransmitter::ThermoHygroTransmitter(byte transmitterPin, byte randomId, byte channel) : SensorTransmitter(transmitterPin, randomId) {
_channel = channel;
}
void ThermoHygroTransmitter::sendTempHumi(int temperature, byte humidity) {
byte buffer[10];
// Note: temperature is 10x the actual temperature! So, 23.5 degrees is passed as 235.
buffer[0] = 0x75; /* Header byte */
buffer[1] = (_channel << 5) | _randomId ; /* Thermo-hygro at channel 1 (see table1)*/
buffer[2] = 0xce; /* Package size byte for th-sensor */
buffer[3] = 0x1e; /* Themo/Hygro */
if ( temperature < 0 ) {
buffer[5] = 0x4 << 4; // High nibble is 0x4 for sub zero temperatures...
temperature = -temperature; // Make temperature positive
} else {
buffer[5] = 0xc << 4; // ...0xc for positive
}
// Note: temperature is now always positive!
buffer[4] = (((temperature % 100) / 10 ) << 4) | // the "3" from 23.5
(temperature % 10); // the "5" from 23.5
buffer[5] |= (temperature / 100); // the "2" from 23.5
buffer[6] = ((humidity / 10) << 4) | (humidity % 10); // BCD encoded
buffer[7]=0xff; /* Comfort flag */
sendPackage(_transmitterPin, buffer);
}
void ThermoHygroTransmitter::sendRainlevel(unsigned int level){ // in 0.7mm
byte buffer[10];
// Note: temperature is 10x the actual temperature! So, 23.5 degrees is passed as 235.
buffer[0] = 0x75; /* Header byte */
buffer[1] = (_channel << 5) | _randomId ; /* Thermo-hygro at channel 1 (see table1)*/
buffer[2] = 0xcc; /* Package size byte for rain-sensor */
buffer[3] = 0x0e; /* Rain level */
// Note: temperature is now always positive!
buffer[4] = level & 0xff; // Low byte
buffer[5] = (level >>8) & 0xff; // High byte
sendPackage(_transmitterPin, buffer);
}