Skip to content

Commit 61fb1c8

Browse files
author
betzrhodes
authored
Merge pull request #1 from electricimp/develop
Develop
2 parents c61c5ea + f2ec15a commit 61fb1c8

14 files changed

+162
-533
lines changed

Diff for: README.md

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
#Thermistor Class
1+
#Thermistor
22

33
This class makes it simple for an imp to read an NTC (“Negative Temperature Coefficient”) thermistor and determine the temperature. Thermistors are essentially temperature-dependent resistors. To use as a thermometer, a thermistor is used as half of a resistive divider, where the voltage across the full divider is known. The Imp then reads the voltage at the center of the divider to determine the ratio of resistance of the thermistor and the bias resistor (also the nominal resistance of the thermistor), [from which the temperature can be derived](http://en.wikipedia.org/wiki/Thermistor).
44

5-
**To add this library to your project, add** `#require "Thermistor.class.nut:1.0.0"` **to the top of your device code**
5+
**To add this library to your project, add** `#require "Thermistor.class.nut:2.0.0"` **to the top of your device code**
66

7-
## Usage
8-
9-
### Hardware
7+
## Hardware
108

119
A resistive divider can be formed with the thermistor on the top or the bottom; this class allows for either configuration. The top of the divider should be connected to the same rail as an imp’s V<sub>DDA</sub> pin (or V<sub>DD</sub> pin, in the case of the imp001, as V<sub>DD</sub> and V<sub>DDA</sub> are internally connected). The bottom of the divider should be connected to ground.
1210

1311
The resistance of the bias resistor in the voltage divider should be equal to the nominal resistance of the thermistor (the resistance at T0). This simplifies the temperature calculation and allows the largest dynamic range.
1412

1513
The center of the divider must be connected to a pin capable of analog input. On the imp001, any pin can be used as an analog input. On the imp002 or imp003, only some pins can be configured this way, so check the [Imp Pin Mux chart](http://electricimp.com/docs/hardware/imp/pinmux/).
1614

17-
### Software
15+
## Class Usage
16+
17+
### Constructor: Thermistor(pin, b_therm, t0_therm, [points], [high_side_therm])
1818

1919
The thermistor class takes three to five parameters (three required, two optional):
2020

@@ -33,29 +33,34 @@ The ß and T0 parameters are all available on the thermistor datasheet:
3333
| ß | Characteristic of the thermistor. Most thermistors have many ß values listed, for various temperature ranges. Choose the value for the temperature range you will be operating in. |
3434
| T0 | Temperature at which the nominal resistance (R) of the thermistor is measured. Typically room temperature (~25ºC) |
3535

36-
### Constructor
37-
3836
```squirrel
39-
const b_therm = 3988
40-
const t0_therm = 298.15
37+
const b_therm = 3988;
38+
const t0_therm = 298.15;
4139
42-
pin <- hardware.pin9
40+
pin <- hardware.pinJ;
4341
4442
// Thermistor on bottom of divider
45-
46-
myThermistor <- Thermistor(pin, b_therm, t0_therm, 10, false)
43+
therm <- Thermistor(pin, b_therm, t0_therm, 10, false)
4744
```
4845

49-
### Class Methods
46+
## Class Methods
47+
48+
### read()
49+
50+
The *read()* method returns the temperature in Celsius.
51+
52+
```squirrel
53+
local celsius = therm.read();
54+
server.log(celsius);
55+
```
5056

51-
### Reading a thermistor
57+
### readK()
5258

53-
The thermistor object provides three methods for reading the temperature: *readK()*, *readC()* and *readF()*. These return the temperature in Kelvin, Celsius and Fahrenheit, respectively.
59+
The *read()* method returns the temperature in Kelvin.
5460

5561
```squirrel
56-
local kelvin = therm.readK()
57-
local celsius = therm.readC()
58-
local fahrenheit = therm.readF()
62+
local kelvin = therm.readK();
63+
server.log(kelvin);
5964
```
6065

6166
## License

Diff for: Thermistor.class.nut

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
// Copyright (c) 2015 Electric Imp
1+
// Copyright (c) 2017 Electric Imp
22
// This file is licensed under the MIT License
33
// http://opensource.org/licenses/MIT
44

55
class Thermistor {
6+
7+
static VERSION = "2.0.0";
8+
69
_beta = null;
710
_t0 = null;
811

@@ -14,11 +17,22 @@ class Thermistor {
1417
constructor(pin, b, t0, points = 10, highSide = true) {
1518
_pin = pin;
1619
_pin.configure(ANALOG_IN);
17-
_highSide = highSide;
20+
21+
if (typeof points == "boolean") {
22+
_pointsPerRead = 10.0;
23+
_highSide = points;
24+
} else {
25+
_pointsPerRead = points * 1.0;
26+
_highSide = highSide;
27+
}
1828

1929
_beta = b * 1.0;
2030
_t0 = t0 * 1.0;
21-
_pointsPerRead = points * 1.0;
31+
}
32+
33+
// read thermistor in Celsius
34+
function read() {
35+
return (readK() - 273.15);
2236
}
2337

2438
// read thermistor in Kelvin
@@ -39,14 +53,4 @@ class Thermistor {
3953

4054
return (_t0 * _beta) / (_beta - _t0 * ln_therm);
4155
}
42-
43-
// read thermistor in Celsius
44-
function readC() {
45-
return this.readK() - 273.15;
46-
}
47-
48-
// read thermistor in Fahrenheit
49-
function readF() {
50-
return ((this.readK() - 273.15) * 9.0 / 5.0 + 32.0);
51-
}
5256
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#Tempbug Battery Example
2-
This example shows how to read a simple NTC thermistor and post the data to [Xively](https://xively.com/) every 15 minutes to be graphed.
2+
This example shows how to read a simple NTC thermistor and post the data to the agent every 15 minutes.
33

4-
In this example, the resistive divider is between 3.3V and Imp Pin8, which is configured as an open-drain output. This allows the Imp to stop current from flowing through the divider by driving Pin8 high, which allows for very long operation on battery.
4+
In this example, the resistive divider is between 3.3V and Imp Pin8, which is configured as an open-drain output. This allows the Imp to stop current from flowing through the divider by driving Pin8 high, which allows for very long operation on battery.
55

66
##Hardware Configuration
77

88
Form a resistive divider from the 3.3V pin on the Electric Imp Breakout to Ground with a 100kΩ resistor and a 100kΩ NTC thermistor. The example code assumes the NTC thermistor is on the bottom half of the divider, connected between the analog input and Imp Pin8. Imp Pin9 should be connected to the middle of the divider.
99

1010
<img class="img-rounded" src="tempbug_battery_100k.png" width="600">
1111

12-
For more information on building a battery-powered temperature sensor with Electric Imp and a Thermistor, see the [Tempbug Instructable.](http://www.instructables.com/id/TempBug-internet-connected-thermometer/).
12+
For more information on building a battery-powered temperature sensor with Electric Imp and a Thermistor, see the [Tempbug Instructable](http://www.instructables.com/id/TempBug-internet-connected-thermometer/).
1313

1414

1515
##Software Operation
16-
Unlike the simple example, this example does not reschedule readings with imp.wakeup. Instead, the Imp is programmed to take a single reading, then go to deep sleep with a timed wakeup. When the Imp wakes from deep sleep, the entire device firmware is reloaded and run from the beginning, so the loop is implicit.
16+
Unlike the simple example, this example does not reschedule readings with imp.wakeup. Instead, the Imp is programmed to take a single reading, then go to deep sleep with a timed wakeup. When the Imp wakes from deep sleep, the entire device firmware is reloaded and run from the beginning, so the loop is implicit.

Diff for: examples/Battery_Thermistor/tempbug.agent.nut

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2015 Electric Imp
2+
// This file is licensed under the MIT License
3+
// http://opensource.org/licenses/MIT
4+
5+
/* REGISTER DEVICE CALLBACKS ------------------------------------------------*/
6+
7+
device.on("data", function(datapoint) {
8+
// Log our datapoint
9+
server.log(http.jsonencode(datapoint));
10+
// Add code here to upload data to a web service
11+
});

Diff for: examples/Battery_Thermistor/tempbug.device.nut

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2015 Electric Imp
2+
// This file is licensed under the MIT License
3+
// http://opensource.org/licenses/MIT
4+
5+
// Include the Thermistor Library
6+
#require "Thermistor.class.nut:2.0.0"
7+
8+
/* GLOBALS and CONSTANTS -----------------------------------------------------*/
9+
10+
// all calculations are done in Kelvin
11+
// these are constants for this particular thermistor; if using a different one,
12+
// check your datasheet
13+
const b_therm = 3988;
14+
const t0_therm = 298.15;
15+
const WAKEINTERVAL_MIN = 15; // interval between wake-and-reads in minutes
16+
17+
18+
/* RUNTIME BEGINS HERE -------------------------------------------------------*/
19+
20+
// Configure Pins
21+
// pin 8 is driven high to turn off temp monitor (saves power) or low to read
22+
therm_en_l <- hardware.pin8;
23+
therm_en_l.configure(DIGITAL_OUT);
24+
therm_en_l.write(1);
25+
// pin 9 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature
26+
temp_sns <- hardware.pin9;
27+
28+
// instantiate our thermistor class
29+
myThermistor <- Thermistor(temp_sns, b_therm, t0_therm, 10, false);
30+
31+
therm_en_l.write(0);
32+
imp.sleep(0.001);
33+
local id = hardware.getdeviceid();
34+
local datapoint = {
35+
"id" : id,
36+
"temp" : format("%.2f",myThermistor.read())
37+
}
38+
agent.send("data",datapoint);
39+
therm_en_l.write(1);
40+
41+
//Sleep for 15 minutes and 1 second, minus the time past the 0:15
42+
//so we wake up near each 15 minute mark (prevents drifting on slow DHCP)
43+
imp.onidle( function() {
44+
server.sleepfor(1 + WAKEINTERVAL_MIN*60 - (time() % (WAKEINTERVAL_MIN*60)));
45+
});
46+
47+
// full firmware is reloaded and run from the top on each wake cycle, so no need to construct a loop

Diff for: examples/tempbug-simple/README.md renamed to examples/Simple_Thermistor/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Tempbug Simple Example
2-
This example shows how to read a simple NTC thermistor and post the data to [Xively](https://xively.com/) every 15 minutes to be graphed.
2+
This example shows how to read a simple NTC thermistor and post the data to the agent every 15 minutes.
33

4-
No consideration is made in this example for battery life. Please see tempbug-battery.
4+
No consideration is made in this example for battery life. Please see Battery-Thermistor example for power saving example.
55

66
##Hardware Configuration
77

Diff for: examples/Simple_Thermistor/tempbug.agent.nut

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2015 Electric Imp
2+
// This file is licensed under the MIT License
3+
// http://opensource.org/licenses/MIT
4+
5+
/* REGISTER DEVICE CALLBACKS ------------------------------------------------*/
6+
7+
device.on("data", function(datapoint) {
8+
// Log our datapoint
9+
server.log(http.jsonencode(datapoint));
10+
// Add code here to upload data to a web service
11+
});

Diff for: examples/Simple_Thermistor/tempbug.device.nut

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2015 Electric Imp
2+
// This file is licensed under the MIT License
3+
// http://opensource.org/licenses/MIT
4+
5+
// Include the Thermistor Library
6+
#require "Thermistor.class.nut:2.0.0"
7+
8+
/* GLOBALS and CONSTANTS -----------------------------------------------------*/
9+
10+
// all calculations are done in Kelvin
11+
// these are constants for this particular thermistor; if using a different one,
12+
// check your datasheet
13+
const b_therm = 3988;
14+
const t0_therm = 298.15;
15+
const INTERVAL = 900; // interval between wake-and-reads in seconds (15 minutes)
16+
17+
function getTemp() {
18+
// schedule the next temperature reading
19+
imp.wakeup(INTERVAL, getTemp);
20+
21+
// hardware id is used to separate feeds on Xively, so provide it with the data
22+
local id = hardware.getdeviceid();
23+
24+
// tempreature can also be returned in Kelvin or Celsius
25+
local datapoint = {
26+
"id" : id,
27+
"temp" : format("%.2f",myThermistor.read())
28+
}
29+
30+
server.log("Temp: " + datapoint.temp);
31+
agent.send("data", datapoint);
32+
}
33+
34+
/* RUNTIME BEGINS HERE -------------------------------------------------------*/
35+
36+
// Configure Pins
37+
// Pin 7 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature
38+
temp_sns <- hardware.pin7;
39+
40+
// Instantiate our thermistor class
41+
// This shows the thermistor on the bottom of the divider
42+
myThermistor <- Thermistor(temp_sns, b_therm, t0_therm, 10, false);
43+
44+
// This function will schedule itself to re-run after it is first called
45+
// Just call it once to start the loop.
46+
getTemp();

0 commit comments

Comments
 (0)