temp sensor over 433Mhz

Dependencies:   FastIO

BBC MicroBit with RF 433Mhz receiver reading Oregon-Scientific wireless temperature sensor. Originally written for the Raspberry Pi but easily converted for the little microbit.

Committer:
daw9000
Date:
Tue Jul 26 13:57:34 2016 +0000
Revision:
3:fe79c130b25f
Parent:
2:7455dae4e624
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daw9000 0:13cb9cc98bca 1 /* Program Oregon Written by David Wright, Jan 2015. */
daw9000 0:13cb9cc98bca 2 /* thanks and plagarised from various internet sources on oregon sensors. Paul(DISK91.com), ALTelectronics for their document on */
daw9000 0:13cb9cc98bca 3 /* Oregon Scientific RF protocol v1.0, kevinmehall on github for rtldr-433m-sensor, Alexander Yerezeyev and more .... */
daw9000 0:13cb9cc98bca 4 /* */
daw9000 0:13cb9cc98bca 5 /* This Program reads a 433Mhz transmission from an Oregon version 1 Protocol Temperature Sensor */
daw9000 0:13cb9cc98bca 6 /* */
daw9000 0:13cb9cc98bca 7 /* Programming uses the logic of first detecting the preamble portion of transmission by counting the number of short 1 pulses */
daw9000 0:13cb9cc98bca 8 /* Next the progam monitors the 3 sync pulses and translates the last sync pulse to determine the first data message bit either 1 or 0 */
daw9000 0:13cb9cc98bca 9 /* From knowing the first message data bit the program determines the next 31 message data bits using the logic as follows:- */
daw9000 0:13cb9cc98bca 10 /* Two SHORT pulses means that the message data bit is the same as the previous message data bit (we know first so can do this) */
daw9000 0:13cb9cc98bca 11 /* One LONG pulse means that the message data bit is opposite (inverse) of the previous message data bit. */
daw9000 0:13cb9cc98bca 12 /* */
daw9000 0:13cb9cc98bca 13 /* The resulting 32 bit message is reversed to form 8 nibbles(4 bit). These nibbles are decoded to produce the data values. */
daw9000 0:13cb9cc98bca 14 /* */
daw9000 0:13cb9cc98bca 15 /* This program is quite basic and simplistic in that there is no error checking and only really gets temperature, channel and low bat */
daw9000 0:13cb9cc98bca 16 /* I wrote this program to learn C programming from being a visual basic and java programmer and as an aid to learning to code */
daw9000 0:13cb9cc98bca 17 /* hardware interfaces on my Raspberry Pi B+. I found plenty of code for various Oregon sensors but none I could easily understand */
daw9000 0:13cb9cc98bca 18 /* from the C or Python code. Hence this program does nothing clever using bitwise or memory facilities or uses no rising clock edges */
daw9000 0:13cb9cc98bca 19 /* , falling clock edges, clock ticks etc. Just simple time measurement and ONs and OFFs. Also I dont do any hex conversions. */
daw9000 0:13cb9cc98bca 20 /* There are no hex conversions because for channel, temperature and low battery all the hex and decimal values are the same i.e. */
daw9000 0:13cb9cc98bca 21 /* only numbers 0 to 9 are used for each part of the message. The temp minus sign and low battery are determined from the raw binary */
daw9000 0:13cb9cc98bca 22 /* in nibble 2 (third nibble as it starts nibble zero). */
daw9000 0:13cb9cc98bca 23 /* */
daw9000 0:13cb9cc98bca 24 /* If you wish this program can be improved on by adding error checking by using the checksum (hex conversion needed) and/or */
daw9000 0:13cb9cc98bca 25 /* by storing values and checking against the second transmission of the same message (all messages sent twice, I sleep through the */
daw9000 0:13cb9cc98bca 26 /* second transmission Zzzzz). */
daw9000 2:7455dae4e624 27 /**/
daw9000 0:13cb9cc98bca 28
daw9000 2:7455dae4e624 29 /*IMPORTANT NOTE : Most 433Mhz receivers are 5v so will not be powered from MicroBit. The data output is low voltage 3v so OK for MicroBit. */
daw9000 2:7455dae4e624 30 /* FastIO used because pin bashing was too slow. Maybe improvements could be made by using mbed interruptln */
daw9000 2:7455dae4e624 31
daw9000 2:7455dae4e624 32 /* Feel free to change, copy, or whatever ... */
daw9000 2:7455dae4e624 33
daw9000 2:7455dae4e624 34 /*
daw9000 2:7455dae4e624 35 The MIT License (MIT)
daw9000 2:7455dae4e624 36
daw9000 2:7455dae4e624 37 Copyright (c) 2016 British Broadcasting Corporation.
daw9000 2:7455dae4e624 38 This software is provided by Lancaster University by arrangement with the BBC.
daw9000 2:7455dae4e624 39
daw9000 2:7455dae4e624 40 Permission is hereby granted, free of charge, to any person obtaining a
daw9000 2:7455dae4e624 41 copy of this software and associated documentation files (the "Software"),
daw9000 2:7455dae4e624 42 to deal in the Software without restriction, including without limitation
daw9000 2:7455dae4e624 43 the rights to use, copy, modify, merge, publish, distribute, sublicense,
daw9000 2:7455dae4e624 44 and/or sell copies of the Software, and to permit persons to whom the
daw9000 2:7455dae4e624 45 Software is furnished to do so, subject to the following conditions:
daw9000 2:7455dae4e624 46
daw9000 2:7455dae4e624 47 The above copyright notice and this permission notice shall be included in
daw9000 2:7455dae4e624 48 all copies or substantial portions of the Software.
daw9000 2:7455dae4e624 49
daw9000 2:7455dae4e624 50 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
daw9000 2:7455dae4e624 51 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
daw9000 2:7455dae4e624 52 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
daw9000 2:7455dae4e624 53 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
daw9000 2:7455dae4e624 54 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
daw9000 2:7455dae4e624 55 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
daw9000 2:7455dae4e624 56 DEALINGS IN THE SOFTWARE.
daw9000 2:7455dae4e624 57
daw9000 2:7455dae4e624 58
daw9000 2:7455dae4e624 59 Using the BBC MicroBit. */
daw9000 2:7455dae4e624 60
daw9000 1:706c7b028278 61
daw9000 0:13cb9cc98bca 62 #include "MicroBit.h"
daw9000 1:706c7b028278 63 #include "FastIO.h"
daw9000 1:706c7b028278 64
daw9000 0:13cb9cc98bca 65 int thisPin;
daw9000 0:13cb9cc98bca 66 int lastPin;
daw9000 0:13cb9cc98bca 67 int preambleON;
daw9000 0:13cb9cc98bca 68 int onShortLo;
daw9000 0:13cb9cc98bca 69 int onShortHi;
daw9000 0:13cb9cc98bca 70 int offShortLo;
daw9000 0:13cb9cc98bca 71 int offShortHi;
daw9000 0:13cb9cc98bca 72 int onLongLo;
daw9000 0:13cb9cc98bca 73 int onLongHi;
daw9000 0:13cb9cc98bca 74 int offLongLo;
daw9000 0:13cb9cc98bca 75 int offLongHi;
daw9000 0:13cb9cc98bca 76 int syncEnd0Lo;
daw9000 0:13cb9cc98bca 77 int syncEnd0Hi;
daw9000 0:13cb9cc98bca 78 long timeDiff;
daw9000 0:13cb9cc98bca 79 long startedAt;
daw9000 0:13cb9cc98bca 80 long endedAt;
daw9000 0:13cb9cc98bca 81 int preambleFound;
daw9000 0:13cb9cc98bca 82 int dataBits[32];
daw9000 1:706c7b028278 83 //int myPin = 1; // MicroBit P1
daw9000 1:706c7b028278 84 MicroBitPin P1(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_ALL);
daw9000 0:13cb9cc98bca 85 MicroBit uBit;
daw9000 1:706c7b028278 86 FastInOut<MICROBIT_PIN_P1> (myPin);
daw9000 1:706c7b028278 87 Timer xTime;
daw9000 0:13cb9cc98bca 88 long getTime(long returnUsecs)
daw9000 0:13cb9cc98bca 89 {
daw9000 1:706c7b028278 90 returnUsecs = xTime.read_us();
daw9000 1:706c7b028278 91 /* struct timespec currentTime;
daw9000 0:13cb9cc98bca 92 long microSecs;
daw9000 0:13cb9cc98bca 93 long secs;
daw9000 1:706c7b028278 94 clock_gettime(CLOCK_MONOTONIC, &currentTime);
daw9000 0:13cb9cc98bca 95 microSecs = currentTime.tv_nsec * 0.001;
daw9000 0:13cb9cc98bca 96 secs = currentTime.tv_sec;
daw9000 0:13cb9cc98bca 97 returnUsecs = microSecs + secs * 1000; */
daw9000 1:706c7b028278 98
daw9000 1:706c7b028278 99 return(returnUsecs);
daw9000 0:13cb9cc98bca 100 }
daw9000 0:13cb9cc98bca 101 int getPinValue(int returnPinValue)
daw9000 0:13cb9cc98bca 102 {
daw9000 0:13cb9cc98bca 103 // returnPinValue = digitalRead(myPin);
daw9000 1:706c7b028278 104 returnPinValue = myPin.read();
daw9000 1:706c7b028278 105 return(returnPinValue);
daw9000 0:13cb9cc98bca 106 }
daw9000 0:13cb9cc98bca 107 int detectPreamble(int returnDetected)
daw9000 0:13cb9cc98bca 108 {
daw9000 1:706c7b028278 109
daw9000 1:706c7b028278 110 thisPin = getPinValue(thisPin);
daw9000 1:706c7b028278 111 if (!(thisPin == lastPin))
daw9000 1:706c7b028278 112 {
daw9000 1:706c7b028278 113 endedAt = getTime(endedAt); // set timer end for last pin
daw9000 1:706c7b028278 114 timeDiff = endedAt - startedAt; // reset time
daw9000 1:706c7b028278 115 if (lastPin == 1)
daw9000 1:706c7b028278 116 {
daw9000 1:706c7b028278 117 if ((timeDiff >= onShortLo) && (timeDiff <= onShortHi)) // error of margin on pulse length
daw9000 1:706c7b028278 118 {
daw9000 1:706c7b028278 119 preambleON++; // looking for 12 short ON pulses.
daw9000 1:706c7b028278 120 }
daw9000 1:706c7b028278 121 else // not preamble
daw9000 1:706c7b028278 122 {
daw9000 1:706c7b028278 123 preambleON = 0;
daw9000 1:706c7b028278 124 }
daw9000 1:706c7b028278 125 }
daw9000 1:706c7b028278 126 else // check is this preamble, lastPin off
daw9000 1:706c7b028278 127 {
daw9000 1:706c7b028278 128
daw9000 1:706c7b028278 129
daw9000 1:706c7b028278 130 if (preambleON < 11) // last preamble is special as next low not short(thisPin)
daw9000 1:706c7b028278 131 {
daw9000 1:706c7b028278 132 if ((timeDiff > offShortLo) && (timeDiff < offShortHi)) // off ok
daw9000 1:706c7b028278 133 {
daw9000 1:706c7b028278 134 }
daw9000 1:706c7b028278 135 else // not preamble
daw9000 1:706c7b028278 136 {
daw9000 1:706c7b028278 137 preambleON = 0;
daw9000 1:706c7b028278 138 }
daw9000 1:706c7b028278 139
daw9000 1:706c7b028278 140 }
daw9000 1:706c7b028278 141 }
daw9000 1:706c7b028278 142
daw9000 1:706c7b028278 143 startedAt = endedAt; // set timer start for this pin
daw9000 1:706c7b028278 144 lastPin = thisPin;
daw9000 1:706c7b028278 145 }
daw9000 1:706c7b028278 146 if (preambleON == 12)
daw9000 1:706c7b028278 147 {
daw9000 1:706c7b028278 148 returnDetected = 1;
daw9000 1:706c7b028278 149 }
daw9000 1:706c7b028278 150 else
daw9000 1:706c7b028278 151 {
daw9000 1:706c7b028278 152 returnDetected = 0;
daw9000 1:706c7b028278 153 }
daw9000 1:706c7b028278 154 return(returnDetected);
daw9000 0:13cb9cc98bca 155 }
daw9000 0:13cb9cc98bca 156 int getSync()
daw9000 0:13cb9cc98bca 157 {
daw9000 0:13cb9cc98bca 158 // sync is long OFF, long ON, long OFF
daw9000 1:706c7b028278 159
daw9000 0:13cb9cc98bca 160 int sCount;
daw9000 1:706c7b028278 161 sCount = 1;
daw9000 0:13cb9cc98bca 162 thisPin = getPinValue(thisPin);
daw9000 0:13cb9cc98bca 163 lastPin = thisPin; //looking for state changes
daw9000 0:13cb9cc98bca 164 while (sCount < 3) // 3 sync pulses
daw9000 0:13cb9cc98bca 165 {
daw9000 0:13cb9cc98bca 166 if (!(thisPin == lastPin))
daw9000 0:13cb9cc98bca 167 {
daw9000 1:706c7b028278 168 sCount ++;
daw9000 1:706c7b028278 169 if (sCount == 3)
daw9000 1:706c7b028278 170 {
daw9000 1:706c7b028278 171 startedAt = getTime(startedAt); // time this pulse to get first bit value
daw9000 1:706c7b028278 172 }
daw9000 1:706c7b028278 173 lastPin = thisPin;
daw9000 0:13cb9cc98bca 174 }
daw9000 0:13cb9cc98bca 175 thisPin = getPinValue(thisPin); // poll the pin state
daw9000 0:13cb9cc98bca 176 }
daw9000 0:13cb9cc98bca 177 while ((thisPin == lastPin))
daw9000 0:13cb9cc98bca 178 {
daw9000 0:13cb9cc98bca 179 thisPin = getPinValue(thisPin);
daw9000 0:13cb9cc98bca 180 }
daw9000 0:13cb9cc98bca 181 endedAt = getTime(endedAt);
daw9000 1:706c7b028278 182 timeDiff = endedAt - startedAt;
daw9000 1:706c7b028278 183 startedAt = endedAt; // start timer for next bit.
daw9000 1:706c7b028278 184 if (timeDiff > syncEnd0Lo && timeDiff < syncEnd0Hi)
daw9000 1:706c7b028278 185 {
daw9000 1:706c7b028278 186 dataBits[0] = 0;
daw9000 1:706c7b028278 187 }
daw9000 1:706c7b028278 188 else
daw9000 1:706c7b028278 189 {
daw9000 1:706c7b028278 190 dataBits[0] = 1;
daw9000 1:706c7b028278 191 }
daw9000 0:13cb9cc98bca 192 return;
daw9000 0:13cb9cc98bca 193 }
daw9000 1:706c7b028278 194
daw9000 0:13cb9cc98bca 195 int getData()
daw9000 0:13cb9cc98bca 196 {
daw9000 0:13cb9cc98bca 197 // get next 31 data bits, we determined bit 0 in SYNC
daw9000 0:13cb9cc98bca 198 int i;
daw9000 0:13cb9cc98bca 199 int l;
daw9000 0:13cb9cc98bca 200 int s;
daw9000 0:13cb9cc98bca 201 i = 1; //first bit[0] was derived in Sync
daw9000 1:706c7b028278 202 s = 0; // short pulse
daw9000 1:706c7b028278 203 l = 0; // long pulse
daw9000 1:706c7b028278 204
daw9000 0:13cb9cc98bca 205 while (i < 32)
daw9000 0:13cb9cc98bca 206 {
daw9000 0:13cb9cc98bca 207 if (!(thisPin == lastPin)) // lastPin and thisPin are from getSync.
daw9000 0:13cb9cc98bca 208 {
daw9000 0:13cb9cc98bca 209 endedAt = getTime(endedAt); // timer started in getSync
daw9000 0:13cb9cc98bca 210 timeDiff = endedAt - startedAt;
daw9000 1:706c7b028278 211 startedAt = endedAt; // next starts at this end
daw9000 1:706c7b028278 212 if (lastPin == 0) //lastPin was OFF
daw9000 1:706c7b028278 213 {
daw9000 1:706c7b028278 214 if ((timeDiff > offShortLo) && (timeDiff < offShortHi))
daw9000 1:706c7b028278 215 { // short off detected
daw9000 1:706c7b028278 216 s++;
daw9000 1:706c7b028278 217 l=0;
daw9000 1:706c7b028278 218 }
daw9000 1:706c7b028278 219 if ((timeDiff > offLongLo) && (timeDiff < offLongHi))
daw9000 1:706c7b028278 220 { // long off detected
daw9000 1:706c7b028278 221 l++;
daw9000 1:706c7b028278 222 s=0;
daw9000 1:706c7b028278 223 }
daw9000 1:706c7b028278 224 }
daw9000 1:706c7b028278 225 else // lastPin was ON
daw9000 1:706c7b028278 226 {
daw9000 1:706c7b028278 227 if ((timeDiff > onShortLo) && (timeDiff < onShortHi)) // half-time
daw9000 1:706c7b028278 228 { // short on detetcted
daw9000 1:706c7b028278 229 s++;
daw9000 1:706c7b028278 230 l=0;
daw9000 1:706c7b028278 231 }
daw9000 1:706c7b028278 232 if ((timeDiff > onLongLo) && (timeDiff < onLongHi)) // full-time
daw9000 1:706c7b028278 233 { // long on detected
daw9000 1:706c7b028278 234 l++;
daw9000 1:706c7b028278 235 s=0;
daw9000 1:706c7b028278 236 }
daw9000 1:706c7b028278 237 }
daw9000 1:706c7b028278 238 if (s == 2)
daw9000 1:706c7b028278 239 { // 2 short pulses this bit equals previous bit (we know 1st bit from sync)
daw9000 1:706c7b028278 240 dataBits[i] = dataBits[(i-1)];
daw9000 1:706c7b028278 241 i++;
daw9000 1:706c7b028278 242 s=0;
daw9000 1:706c7b028278 243 l=0;
daw9000 1:706c7b028278 244 }
daw9000 1:706c7b028278 245 if (l == 1)
daw9000 1:706c7b028278 246 { // 1 long pulse this bit is inverse of previous bit (we know 1st bit from sync)
daw9000 1:706c7b028278 247 if (dataBits[(i-1)] == 0)
daw9000 1:706c7b028278 248 {
daw9000 1:706c7b028278 249 dataBits[i] = 1;
daw9000 1:706c7b028278 250 }
daw9000 1:706c7b028278 251 else
daw9000 1:706c7b028278 252 {
daw9000 1:706c7b028278 253 dataBits[i] = 0;
daw9000 1:706c7b028278 254 }
daw9000 1:706c7b028278 255 l=0;
daw9000 1:706c7b028278 256 s=0;
daw9000 1:706c7b028278 257 i++;
daw9000 1:706c7b028278 258 }
daw9000 0:13cb9cc98bca 259 // update last pin to this pin value
daw9000 0:13cb9cc98bca 260 lastPin = thisPin;
daw9000 0:13cb9cc98bca 261 }
daw9000 0:13cb9cc98bca 262 thisPin = getPinValue(thisPin); // get pin value
daw9000 0:13cb9cc98bca 263 }
daw9000 1:706c7b028278 264 return;
daw9000 0:13cb9cc98bca 265 }
daw9000 1:706c7b028278 266
daw9000 0:13cb9cc98bca 267 int processData()
daw9000 0:13cb9cc98bca 268 {
daw9000 0:13cb9cc98bca 269 int x = 0;
daw9000 0:13cb9cc98bca 270 int y = 0;
daw9000 0:13cb9cc98bca 271 int z= 0;
daw9000 0:13cb9cc98bca 272 int nStart;
daw9000 0:13cb9cc98bca 273 int nFinish;
daw9000 0:13cb9cc98bca 274 int nibbleValue;
daw9000 0:13cb9cc98bca 275 int nibbleValues[8];
daw9000 0:13cb9cc98bca 276 int temp;
daw9000 0:13cb9cc98bca 277 int nib;
daw9000 0:13cb9cc98bca 278 int nibble[4];
daw9000 0:13cb9cc98bca 279 int lowBat=0;
daw9000 1:706c7b028278 280
daw9000 1:706c7b028278 281 for (x=0;x<8;x++)
daw9000 1:706c7b028278 282 {
daw9000 1:706c7b028278 283 for (y=0;y<4;y++){nibble[y]=0;} //initialise
daw9000 1:706c7b028278 284 nStart=(31-((x*4)+3)); // array index for nibble start
daw9000 1:706c7b028278 285 nFinish=nStart+4;
daw9000 1:706c7b028278 286 y = 3; // nibble index
daw9000 1:706c7b028278 287 // Reverse the bits in message data (dataBits) to create 8 nibbles of 4 bits.
daw9000 1:706c7b028278 288 for (z=nStart;z<nFinish;z++) // read 4 bits
daw9000 1:706c7b028278 289 {
daw9000 1:706c7b028278 290 if (y >= 0)
daw9000 1:706c7b028278 291 {
daw9000 1:706c7b028278 292 nibble[y]=dataBits[z];//reverse bits, y starts at 3 back to 0
daw9000 1:706c7b028278 293 y--;
daw9000 1:706c7b028278 294 }
daw9000 1:706c7b028278 295 }
daw9000 1:706c7b028278 296 nibbleValue=0;
daw9000 1:706c7b028278 297 nib=8;
daw9000 1:706c7b028278 298 temp=0;
daw9000 1:706c7b028278 299 for (z=0;z<4;z++) // convert this nibble to decimal from binary
daw9000 1:706c7b028278 300 {
daw9000 1:706c7b028278 301 temp=nibbleValue;
daw9000 1:706c7b028278 302 nibbleValue=(nib * nibble[z]) + temp;
daw9000 1:706c7b028278 303 temp=nib;
daw9000 1:706c7b028278 304 if (temp > 1) nib = (temp / 2);
daw9000 1:706c7b028278 305
daw9000 1:706c7b028278 306 }
daw9000 1:706c7b028278 307 nibbleValues[x] = nibbleValue; // store nibble decimal values
daw9000 1:706c7b028278 308 }
daw9000 1:706c7b028278 309
daw9000 1:706c7b028278 310 // Print out the converted decimal nibble values
daw9000 1:706c7b028278 311 for (x=0;x<8;x++)
daw9000 1:706c7b028278 312 {
daw9000 1:706c7b028278 313 if (x==6) //channel number conversion
daw9000 1:706c7b028278 314 {
daw9000 1:706c7b028278 315 temp=99;
daw9000 1:706c7b028278 316 if (nibbleValues[x]==0) temp=1;
daw9000 1:706c7b028278 317 if (nibbleValues[x]==4) temp=2;
daw9000 1:706c7b028278 318 if (nibbleValues[x]==8) temp=3;
daw9000 1:706c7b028278 319 // printf(" Channel : %d\n",temp);
daw9000 1:706c7b028278 320 uBit.display.scroll("Channel:");
daw9000 1:706c7b028278 321 uBit.display.scroll(temp);
daw9000 1:706c7b028278 322 }
daw9000 1:706c7b028278 323 if (x==2)
daw9000 1:706c7b028278 324 {
daw9000 1:706c7b028278 325 // printf(" Temperature:");
daw9000 1:706c7b028278 326 uBit.display.scroll("Temp :");
daw9000 1:706c7b028278 327 if ((nibbleValues[x]==10) || (nibbleValues[x]==2)) uBit.display.scroll("-"); // printf("-"); // 8 = low bat, 2=minus, 10=minus+low bat
daw9000 1:706c7b028278 328 if ((nibbleValues[x]==8) || (nibbleValues[x]==10)) lowBat=1; else lowBat=0;
daw9000 1:706c7b028278 329
daw9000 1:706c7b028278 330 }
daw9000 1:706c7b028278 331 if ((x==3) || (x==4)) uBit.display.scroll(nibbleValues[x]);// printf("%d",nibbleValues[x]);
daw9000 1:706c7b028278 332 if (x==5)
daw9000 1:706c7b028278 333 {
daw9000 1:706c7b028278 334 // printf(".");
daw9000 1:706c7b028278 335 // printf("%d degC.\n",nibbleValues[x]);
daw9000 1:706c7b028278 336 uBit.display.scroll(".");
daw9000 1:706c7b028278 337 uBit.display.scroll(nibbleValues[x]);
daw9000 1:706c7b028278 338 uBit.display.scroll("degC");
daw9000 1:706c7b028278 339
daw9000 1:706c7b028278 340 }
daw9000 1:706c7b028278 341 if (lowBat==1) uBit.display.scroll("Low Battery"); // printf("Low Battery. \n");
daw9000 1:706c7b028278 342 }
daw9000 1:706c7b028278 343 return;
daw9000 0:13cb9cc98bca 344 }
daw9000 0:13cb9cc98bca 345 int main()
daw9000 0:13cb9cc98bca 346 {
daw9000 0:13cb9cc98bca 347 int p;
daw9000 0:13cb9cc98bca 348 uBit.init();
daw9000 0:13cb9cc98bca 349 // on short 1720, on long 3180, off short 1219, off long 2680
daw9000 0:13cb9cc98bca 350 // sync 1 off 4200, sync 2 on 5700, sync 3 off 5200 (sync 3 off long 6680)
daw9000 0:13cb9cc98bca 351 onShortLo = 1500;
daw9000 0:13cb9cc98bca 352 onShortHi = 2400;
daw9000 0:13cb9cc98bca 353 offShortLo = 970;
daw9000 0:13cb9cc98bca 354 offShortHi = 1950;
daw9000 0:13cb9cc98bca 355 onLongLo = 2980;
daw9000 0:13cb9cc98bca 356 onLongHi = 3880;
daw9000 0:13cb9cc98bca 357 offLongLo = 1950;
daw9000 0:13cb9cc98bca 358 offLongHi = 2900;
daw9000 0:13cb9cc98bca 359 // syncBeginLo = 4000;
daw9000 0:13cb9cc98bca 360 // syncBeginHi = 4400;
daw9000 0:13cb9cc98bca 361 // syncLo = 5500;
daw9000 0:13cb9cc98bca 362 // syncHi = 5900;
daw9000 0:13cb9cc98bca 363 // syncEnd1Lo = 5000;
daw9000 0:13cb9cc98bca 364 // syncEnd1Hi = 5400;
daw9000 0:13cb9cc98bca 365 syncEnd0Lo = 6480;
daw9000 0:13cb9cc98bca 366 syncEnd0Hi = 6880;
daw9000 0:13cb9cc98bca 367 // wiringPiSetup();
daw9000 0:13cb9cc98bca 368 // pinMode(myPin, INPUT);
daw9000 0:13cb9cc98bca 369 uBit.io.pin[myPin].setDigitalValue(0);
daw9000 1:706c7b028278 370 preambleFound = 0;
daw9000 1:706c7b028278 371 preambleON = 0;
daw9000 1:706c7b028278 372 // printf("Waiting for transmission (approx. every 30s).\n");
daw9000 1:706c7b028278 373 // printf("Press Ctrl-C to exit.\n");
daw9000 1:706c7b028278 374 uBit.display.scroll("Waiting...");
daw9000 1:706c7b028278 375 while (1)
daw9000 1:706c7b028278 376 { // loop forever or ctrl-c
daw9000 1:706c7b028278 377
daw9000 1:706c7b028278 378 thisPin = getPinValue(thisPin);
daw9000 1:706c7b028278 379 lastPin = thisPin;
daw9000 1:706c7b028278 380 xTime.start();
daw9000 1:706c7b028278 381 endedAt = getTime(endedAt); // set initial timer end
daw9000 1:706c7b028278 382 startedAt = endedAt;
daw9000 1:706c7b028278 383
daw9000 1:706c7b028278 384 while (preambleFound == 0) // constantly monitor for preamble
daw9000 1:706c7b028278 385 {
daw9000 1:706c7b028278 386 preambleFound = detectPreamble(preambleFound);
daw9000 1:706c7b028278 387 }
daw9000 1:706c7b028278 388 if (preambleFound == 1)
daw9000 1:706c7b028278 389 {
daw9000 1:706c7b028278 390 getSync();
daw9000 1:706c7b028278 391 getData();
daw9000 1:706c7b028278 392 processData();
daw9000 1:706c7b028278 393 preambleFound = 0;
daw9000 1:706c7b028278 394 preambleON = 0;
daw9000 1:706c7b028278 395 uBit.sleep(10); // avoid second xmission of same data
daw9000 1:706c7b028278 396 }
daw9000 1:706c7b028278 397 }
daw9000 1:706c7b028278 398 exit(0);
daw9000 1:706c7b028278 399 }