-
Notifications
You must be signed in to change notification settings - Fork 2
/
tinytemp.ino
113 lines (87 loc) · 2.27 KB
/
tinytemp.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
/*
* TinyTemp - Using an ATtiny, read temperature from a DS18B20 and
* transmit it using a FS1000A 433 MHz tx module.
*
* Copyright (c) 2016 Björn Stenberg <[email protected]>
* License: MIT, see file LICENSE
*
*/
#include <avr/sleep.h>
#include <avr/wdt.h>
#include "OneWire.h"
#include "FineOffset.h"
#define TX_PIN 2
#define TEMP_PIN 0
#define LED_PIN 3
/* change this to be unique per device in your area */
#define DEVICE_ID 202
/* if you have calibrated the sensor, put the correction value here */
#define TEMP_CORRECTION -0.0000
#define EBADTEMP -999
OneWire ow(TEMP_PIN);
FineOffset tx(TX_PIN);
byte addr[8];
ISR(WDT_vect) {
//Don't do anything. This is just here so that we wake up.
}
void powerDown(int time) {
if (time > 9 )
time = 9; //Limit incoming amount to legal settings
MCUSR &= ~(1<<WDRF); // Clear watch dog reset flag
WDTCR |= (1<<WDCE) | (1<<WDE); // Set WD_change enable, set WD enable
WDTCR = (time & 7) | (time & 8) << 2;
WDTCR |= (1<<WDIE); // enable interrupt
sleep_mode();
}
void flash_led(int count) {
for (int i=0; i < count; i++) {
digitalWrite(LED_PIN, HIGH);
delay(150);
digitalWrite(LED_PIN, LOW);
delay(150);
}
}
void setup(void)
{
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
// find temp sensor
while (!ow.search(addr)) {
flash_led(3);
delay(750);
}
// setup power
ADCSRA &= ~(1<<ADEN); //Disable ADC, saves ~230uA
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
sleep_enable();
}
double read_temp(void)
{
ow.reset();
ow.select(addr);
ow.write(0x44, 1); // start conversion, with parasite power on at the end
digitalWrite(LED_PIN, LOW);
powerDown(WDTO_1S); // conversion takes 750ms
digitalWrite(LED_PIN, HIGH);
ow.reset();
ow.select(addr);
ow.write(0xBE); // read scratchpad
byte data[9];
ow.read_bytes(data, 9);
if (OneWire::crc8(data, 8) != data[8])
return EBADTEMP;
return (data[1] << 8 | data[0]) / 16.0 + TEMP_CORRECTION;
}
void loop(void)
{
digitalWrite(LED_PIN, HIGH);
double temp = read_temp();
if (temp == EBADTEMP)
flash_led(3);
else
tx.send(DEVICE_ID, temp);
digitalWrite(LED_PIN, LOW);
// sleep 8 seconds * 30 loops = 240 sec = 4 min
for (int i=0; i<30; i++)
powerDown(WDTO_8S);
}