-
Notifications
You must be signed in to change notification settings - Fork 2
/
Working sketch v1
331 lines (301 loc) · 14.6 KB
/
Working sketch v1
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Andrew's WIP sketch for capturing data from Ambient F007th Thermo-Hygrometer
Inspired by the many weather station hackers who have gone before,
Only possible thanks to the invaluable help and support on the Arduino forums.
With particular thanks to
Rob Ward (whose Manchester Encoding reading by delay rather than interrupt
is the basis of this code)
https://github.com/robwlakes/ArduinoWeatherOS
The work of 3zero8 capturing and analysing the data
http://forum.arduino.cc/index.php?topic=214436.0
The work of Volgy capturing and analysing the data
https://github.com/volgy/gr-ambient
This example code is in the public domain.
What this code does:
Reads Ambient F007th Thermo-Hygrometer data packets
Synchs the data correctly within byte boundaries
Distinguishes between F007th data packets and other 434Mhz signals with equivalent header
Interprets temperature and humidity data
Prints data to the serial port as it is received
What it does not yet do (and needs to)
Correctly identify negative temperatures
Error check repeated readings
Collect readings and then send them as a single databurst
Known problems
Currently prints all recieved data twice
About 3% of readings are corrupted (Nearly always a result of skipping a bit in byte 3)
F007th Ambient Thermo-Hygrometer
Sample Data:
0 1 2 3 4 5 6 7
FF 45 4F 04 4B 0B 52 0
0 1 2 3 4 5 6 7 8 9 A B C D E
11111101 01000101 01001111 00000100 01001011 00001011 01010010 0000
hhhhhhhh SSSSSSSS NRRRRRRR bCCCTTTT TTTTTTTT HHHHHHHH CCCCCCCC ????
Channel 1 F007th sensor displaying 21.1 Centigrade and 11% RH
hhhhhhhh = header with final 01 before data packet starts (note using this sketch the header 01 is omitted when the binary is displayed)
SSSSSSSS = sensor ID, F007th = Ox45
NRRRRRRR = Rolling Code Byte, the N bit is set to 1 for 64 cycles to indicate it is reset or new to the Rx box
b = battery indicator?
CCC = Channel identifier, channels 1 to 8 can be selected on the F007th unit using dipswitches. Channel 1 => 000, Channel 2 => 001, Channel 3 => 010 etc.
TTTT TTTTTTTT = 12 bit temperature data. MSB indicates + or - temperature.
To obtain F: convert decimal to binary, take away 400 and divide by 10 e.g. (using example above) 010001001011 => 1099
(1099-400)/10= 69.9F
To obtain C: convert decimal to binary, take away 720 and multiply by 0.0556 e.g.
0.0556*(1099-720)= 21.1C
HHHHHHHH = 8 bit humidity in binary. e.g. (using example above) 00001011 => 11
CCCCCCCC = checksum? NOte that this sketch only looks at the first 6 bytes and ignores the checksum
*/
//Interface Definitions
int RxPin = 8; //The number of signal from the Rx
int ledPin = 13; //The number of the onboard LED pin
// Variables for Manchester Receiver Logic:
word sDelay = 242; //Small Delay about 1/4 of bit duration
word lDelay = 484; //Long Delay about 1/2 of bit duration, 1/4 + 1/2 = 3/4
byte polarity = 1; //0 for lo->hi==1 or 1 for hi->lo==1 for Polarity, sets tempBit at start
byte tempBit = 1; //Reflects the required transition polarity
boolean firstZero = false;//flags when the first '0' is found.
boolean noErrors = true; //flags if signal does not follow Manchester conventions
//variables for Header detection
byte headerBits = 10; //The number of ones expected to make a valid header
byte headerHits = 0; //Counts the number of "1"s to determine a header
//Variables for Byte storage
boolean sync0In=true; //Expecting sync0 to be inside byte boundaries, set to false for sync0 outside bytes
byte dataByte = 0; //Accumulates the bit information
byte nosBits = 6; //Counts to 8 bits within a dataByte
byte maxBytes = 6; //Set the bytes collected after each header. NB if set too high, any end noise will cause an error
byte nosBytes = 0; //Counter stays within 0 -> maxBytes
//Variables for multiple packets
byte bank = 0; //Points to the array of 0 to 3 banks of results from up to 4 last data downloads
byte nosRepeats = 3; //Number of times the header/data is fetched at least once or up to 4 times
//Banks for multiple packets if required (at least one will be needed)
byte manchester[4][20]; //Stores 4 banks of manchester pattern decoded on the fly
// Variables to prepare recorded values (used to create CSV output) for Ambient
byte stnId = 0; //Identifies the channel number
int dataType = 0; //Identifies the Ambient Thermo-Hygrometer code
float Ch1temp = 0; // Stored value for channel 1 temperature
float Ch1hum = 0; // Stored value for channel 1 humidity
float Ch2temp = 0; // Stored value for channel 2 temperature
float Ch2hum = 0; // Stored value for channel 2 humidity
float Ch3temp = 0; // Stored value for channel 3 temperature
float Ch3hum = 0; // Stored value for channel 3 humidity
float Ch4temp = 0; // Stored value for channel 4 temperature
float Ch4hum = 0; // Stored value for channel 4 humidity
float Ch5temp = 0; // Stored value for channel 5 temperature
float Ch5hum = 0; // Stored value for channel 5 humidity
float Ch6temp = 0; // Stored value for channel 6 temperature
float Ch6hum = 0; // Stored value for channel 6 humidity
//float Ch5temp = 0; // Stored value for channel 7 temperature
//float Ch5hum = 0; // Stored value for channel 7 humidity
//float Ch6temp = 0; // Stored value for channel 8 temperature
//float Ch6hum = 0; // Stored value for channel 8 humidity
void setup() {
Serial.begin(115200);//make it fast so it dumps quick!
pinMode(RxPin, INPUT);
pinMode(ledPin, OUTPUT);
/*
// uncomment to display workings
Serial.println();
lDelay=2*sDelay;//just to make sure the 1:2 ratio is established. They can have some other ratio if required
Serial.print("Using a delay of 1/4 bitWaveform ");// +-15% and they still seem to work ok, pretty tolerant!
Serial.print(sDelay,DEC);
Serial.print(" uSecs 1/2 bitWaveform ");//these may not be exactly 1:2 ratio as processing also contributes to these delays.
Serial.print(lDelay,DEC);
Serial.println(" uSecs ");
if (polarity){
Serial.println("Negative Polarity hi->lo=1");
}
else{
Serial.println("Positive Polarity lo->hi=1");
}
Serial.print(headerBits,DEC);
Serial.println(" bits expected for a valid header");
if (sync0In){
Serial.println("Sync Zero inside Packet");
}
else{
Serial.println("Sync Zero outside Packet");
}
Serial.println("D 00 00001111 01 22223333 02 44445555 03 66667777 04 88889999 05 AAAABBBB");
//if packet is repeated then best to have non matching numbers in the array slots to begin with
//clear the array to different nos cause if all zeroes it might think that is a valid 3 packets ie all equal
*/
eraseManchester(); //clear the array to different nos cause if all zeroes it might think that is a valid 3 packets ie all equal
} //end of setup
// Main routines, find header, then sync in with it, get a packet, and decode data in it, plus report any errors.
void loop(){
tempBit=polarity; //these begin the same for a packet
noErrors=true;
firstZero=false;
headerHits=0;
nosBits=0;
nosBytes=0;
while (noErrors && (nosBytes<maxBytes)){
while(digitalRead(RxPin)!=tempBit){
//pause here until a transition is found
}//at Data transition, half way through bit pattern, this should be where RxPin==tempBit
delayMicroseconds(sDelay);//skip ahead to 3/4 of the bit pattern
// 3/4 the way through, if RxPin has changed it is definitely an error
digitalWrite(ledPin,0); //Flag LED off!
if (digitalRead(RxPin)!=tempBit){
noErrors=false;//something has gone wrong, polarity has changed too early, ie always an error
}//exit and retry
else{
delayMicroseconds(lDelay);
//now 1 quarter into the next bit pattern,
if(digitalRead(RxPin)==tempBit){ //if RxPin has not swapped, then bitWaveform is swapping
//If the header is done, then it means data change is occuring ie 1->0, or 0->1
//data transition detection must swap, so it loops for the opposite transitions
tempBit = tempBit^1;
}//end of detecting no transition at end of bit waveform, ie end of previous bit waveform same as start of next bitwaveform
//Now process the tempBit state and make data definite 0 or 1's, allow possibility of Pos or Neg Polarity
byte bitState = tempBit ^ polarity;//if polarity=1, invert the tempBit or if polarity=0, leave it alone.
if(bitState==1){ //1 data could be header or packet
if(!firstZero){
headerHits++;
if (headerHits==headerBits){
digitalWrite(ledPin,1); //valid header accepted, minimum required found
//Serial.print("H");
}
}
else{
add(bitState);//already seen first zero so add bit in
}
}//end of dealing with ones
else{ //bitState==0 could first error, first zero or packet
// if it is header there must be no "zeroes" or errors
if(headerHits<headerBits){
//Still in header checking phase, more header hits required
noErrors=false;//landing here means header is corrupted, so it is probably an error
}//end of detecting a "zero" inside a header
else{
//we have our header, chewed up any excess and here is a zero
if (!firstZero){ //if first zero, it has not been found previously
firstZero=true;
if(sync0In){
add(bitState);//Add zero to bytes
dataByte = B11111111;
nosBits = 7;
}
//Serial.print("!");
}//end of finding first zero
else{
add(bitState);
}//end of adding a zero bit
}//end of dealing with a first zero
}//end of dealing with zero's (in header, first or later zeroes)
}//end of first error check
}//end of while noErrors=true and getting packet of bytes
digitalWrite(ledPin,0); //data processing exited, look for another header
}//end of mainloop
//Read the binary data from the bank and apply conversions where necessary to scale and format data
void analyseData(){
}
void add(byte bitData){
dataByte=(dataByte<<1)|bitData;
nosBits++;
if (nosBits==8){
nosBits=0;
manchester[bank][nosBytes]=dataByte;
nosBytes++;
//Serial.print("B");
}
if(nosBytes==maxBytes){
// hexBinDump();//for debug purposes dump out in hex and bainary
analyseData();//later on develop your own analysis routines
//Subroutines to check, analyse and format data for Ambient
int stnId = ((manchester[0][3]&B01110000)/16)+1; //3 bits of data across range 0 to 7 for channels 1 to 8
dataType = manchester[0][1]; // Identifies byte 1 for a Ambient Thermo-Hygrometer code (0x45)
if ((dataType == 0x45) && (stnId == 1)){ //Confirms data is from Ambient Thermo-Hygrometer on channel 1
Ch1temp = (float(((manchester[0][3]&B00000111)*256)+ manchester[0][4])-720)*0.0556; //includes conversion to C
// Ch1temp = (float(((manchester[0][3]&B00000111)*256)+ manchester[0][4])-400)/10; //includes conversion to F
Ch1hum =(manchester [0][5]); //Looks in byte bank 5 for 8 bit humidity data
Serial.print("Channel 1 data "); //Indicates channel number
Serial.print(Ch1temp,1);
Serial.print("C ");
// Serial.print("F "); // Required for F reporting
Serial.print(Ch1hum,0);
Serial.println("%");
}
if ((dataType == 0x45) && (stnId == 2)){ //Confirms data is from Ambient Thermo-Hygrometer on channel 2
Ch2temp = (float((((manchester[0][3]&B00000111)*256)+ manchester[0][4])-720)*0.5556)/10; //includes conversion to C
Ch2hum =(manchester [0][5]); //Looks in byte bank 5 for 8 bit humidity data
Serial.print("Channel 2 data "); //Indicates channel number
Serial.print(Ch2temp,1);
Serial.print("C ");
Serial.print(Ch2hum,0);
Serial.println("%");
}
if ((dataType == 0x45) && (stnId == 3)){ //Confirms data is from Ambient Thermo-Hygrometer on channel 3
Ch3temp = (float((((manchester[0][3]&B00000111)*256)+ manchester[0][4])-720)*0.5556)/10; //includes conversion to C
Ch3hum =(manchester [0][5]); //Looks in byte bank 5 for 8 bit humidity data
Serial.print("Channel 3 data "); //Indicates channel number
Serial.print(Ch3temp,1);
Serial.print("C ");
Serial.print(Ch3hum,0);
Serial.println("%");
}
if ((dataType == 0x45) && (stnId == 4)){ //Confirms data is from Ambient Thermo-Hygrometer on channel 4
Ch4temp = (float((((manchester[0][3]&B00000111)*256)+ manchester[0][4])-720)*0.5556)/10; //includes conversion to C
Ch4hum =(manchester [0][5]); //Looks in byte bank 5 for 8 bit humidity data
Serial.print("Channel 4 data "); //Indicates channel number
Serial.print(Ch4temp,1);
Serial.print("C ");
Serial.print(Ch4hum,0);
Serial.println("%");
}
if ((dataType == 0x45) && (stnId == 5)){ //Confirms data is from Ambient Thermo-Hygrometer on channel 5
Ch5temp = (float((((manchester[0][3]&B00000111)*256)+ manchester[0][4])-720)*0.5556)/10; //includes conversion to C
Ch5hum =(manchester [0][5]); //Looks in byte bank 5 for 8 bit humidity data
Serial.print("Channel 5 data "); //Indicates channel number
Serial.print(Ch5temp,1);
Serial.print("C ");
Serial.print(Ch5hum,0);
Serial.println("%");
}
if ((dataType == 0x45) && (stnId == 6)){ //Confirms data is from Ambient Thermo-Hygrometer on channel 6
Ch6temp = (float((((manchester[0][3]&B00000111)*256)+ manchester[0][4])-720)*0.5556)/10; //includes conversion to C
Ch6hum =(manchester [0][5]); //Looks in byte bank 5 for 8 bit humidity data
Serial.print("Channel 6 data "); //Indicates channel number
Serial.print(Ch6temp,1);
Serial.print("C ");
Serial.print(Ch6hum,0);
Serial.println("%");
}
//add additional copies of above code here if using channels 7 and 8
}
}
/*
//decomment to print out recieved binary
void hexBinDump(){
//Print the fully aligned binary data in manchester[bank] array
Serial.print("D ");
for( int i=0; i < maxBytes; i++){
byte mask = B10000000;
if (manchester[bank][i]<16){
Serial.print("0"); //Pad single digit hex
}
Serial.print(manchester[bank][i],HEX);
Serial.print(" ");
for (int k=0; k<8; k++){
if (manchester[bank][i] & mask){
Serial.print("1");
}
else{
Serial.print("0");
}
mask = mask >> 1;
}
Serial.print(" ");
}
Serial.println();
}
*/
void eraseManchester(){
//Clear the memory to non matching numbers across the banks
//If there is only one packet, with no repeats this is not necessary.
for( int j=0; j < 4; j++){
for( int i=0; i < 20; i++){
manchester[j][i]=j+i;
}
}
}