primer commit probando arduino, va el .ino de sensor de temperatura y humedad
This commit is contained in:
commit
9ddd47df78
|
@ -0,0 +1,9 @@
|
|||
#! /usr/bin/python3
|
||||
|
||||
# haha, "daemon" :D
|
||||
# aquí iré metiendo la lectura de datos y guardado en algún sitio
|
||||
|
||||
import serial
|
||||
ser = serial.Serial("/dev/ttyACM0", 9600) # 9600 bauds
|
||||
while True:
|
||||
print(ser.readline())
|
|
@ -0,0 +1,259 @@
|
|||
/* DHT library
|
||||
|
||||
MIT license
|
||||
written by Adafruit Industries
|
||||
*/
|
||||
|
||||
#include "DHT.h"
|
||||
|
||||
#define MIN_INTERVAL 2000
|
||||
|
||||
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
|
||||
_pin = pin;
|
||||
_type = type;
|
||||
#ifdef __AVR
|
||||
_bit = digitalPinToBitMask(pin);
|
||||
_port = digitalPinToPort(pin);
|
||||
#endif
|
||||
_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for
|
||||
// reading pulses from DHT sensor.
|
||||
// Note that count is now ignored as the DHT reading algorithm adjusts itself
|
||||
// basd on the speed of the processor.
|
||||
}
|
||||
|
||||
void DHT::begin(void) {
|
||||
// set up the pins!
|
||||
pinMode(_pin, INPUT_PULLUP);
|
||||
// Using this value makes sure that millis() - lastreadtime will be
|
||||
// >= MIN_INTERVAL right away. Note that this assignment wraps around,
|
||||
// but so will the subtraction.
|
||||
_lastreadtime = -MIN_INTERVAL;
|
||||
DEBUG_PRINT("Max clock cycles: "); DEBUG_PRINTLN(_maxcycles, DEC);
|
||||
}
|
||||
|
||||
//boolean S == Scale. True == Fahrenheit; False == Celcius
|
||||
float DHT::readTemperature(bool S, bool force) {
|
||||
float f = NAN;
|
||||
|
||||
if (read(force)) {
|
||||
switch (_type) {
|
||||
case DHT11:
|
||||
f = data[2];
|
||||
if(S) {
|
||||
f = convertCtoF(f);
|
||||
}
|
||||
break;
|
||||
case DHT22:
|
||||
case DHT21:
|
||||
f = data[2] & 0x7F;
|
||||
f *= 256;
|
||||
f += data[3];
|
||||
f *= 0.1;
|
||||
if (data[2] & 0x80) {
|
||||
f *= -1;
|
||||
}
|
||||
if(S) {
|
||||
f = convertCtoF(f);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
float DHT::convertCtoF(float c) {
|
||||
return c * 1.8 + 32;
|
||||
}
|
||||
|
||||
float DHT::convertFtoC(float f) {
|
||||
return (f - 32) * 0.55555;
|
||||
}
|
||||
|
||||
float DHT::readHumidity(bool force) {
|
||||
float f = NAN;
|
||||
if (read()) {
|
||||
switch (_type) {
|
||||
case DHT11:
|
||||
f = data[0];
|
||||
break;
|
||||
case DHT22:
|
||||
case DHT21:
|
||||
f = data[0];
|
||||
f *= 256;
|
||||
f += data[1];
|
||||
f *= 0.1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
|
||||
float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit) {
|
||||
// Using both Rothfusz and Steadman's equations
|
||||
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
|
||||
float hi;
|
||||
|
||||
if (!isFahrenheit)
|
||||
temperature = convertCtoF(temperature);
|
||||
|
||||
hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (percentHumidity * 0.094));
|
||||
|
||||
if (hi > 79) {
|
||||
hi = -42.379 +
|
||||
2.04901523 * temperature +
|
||||
10.14333127 * percentHumidity +
|
||||
-0.22475541 * temperature*percentHumidity +
|
||||
-0.00683783 * pow(temperature, 2) +
|
||||
-0.05481717 * pow(percentHumidity, 2) +
|
||||
0.00122874 * pow(temperature, 2) * percentHumidity +
|
||||
0.00085282 * temperature*pow(percentHumidity, 2) +
|
||||
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
|
||||
|
||||
if((percentHumidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
|
||||
hi -= ((13.0 - percentHumidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
|
||||
|
||||
else if((percentHumidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
|
||||
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
|
||||
}
|
||||
|
||||
return isFahrenheit ? hi : convertFtoC(hi);
|
||||
}
|
||||
|
||||
boolean DHT::read(bool force) {
|
||||
// Check if sensor was read less than two seconds ago and return early
|
||||
// to use last reading.
|
||||
uint32_t currenttime = millis();
|
||||
if (!force && ((currenttime - _lastreadtime) < 2000)) {
|
||||
return _lastresult; // return last correct measurement
|
||||
}
|
||||
_lastreadtime = currenttime;
|
||||
|
||||
// Reset 40 bits of received data to zero.
|
||||
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
|
||||
|
||||
// Send start signal. See DHT datasheet for full signal diagram:
|
||||
// http://www.adafruit.com/datasheets/Digital%20humidity%20and%20temperature%20sensor%20AM2302.pdf
|
||||
|
||||
// Go into high impedence state to let pull-up raise data line level and
|
||||
// start the reading process.
|
||||
digitalWrite(_pin, HIGH);
|
||||
delay(250);
|
||||
|
||||
// First set data line low for 20 milliseconds.
|
||||
pinMode(_pin, OUTPUT);
|
||||
digitalWrite(_pin, LOW);
|
||||
delay(20);
|
||||
|
||||
uint32_t cycles[80];
|
||||
{
|
||||
// Turn off interrupts temporarily because the next sections are timing critical
|
||||
// and we don't want any interruptions.
|
||||
InterruptLock lock;
|
||||
|
||||
// End the start signal by setting data line high for 40 microseconds.
|
||||
digitalWrite(_pin, HIGH);
|
||||
delayMicroseconds(40);
|
||||
|
||||
// Now start reading the data line to get the value from the DHT sensor.
|
||||
pinMode(_pin, INPUT_PULLUP);
|
||||
delayMicroseconds(10); // Delay a bit to let sensor pull data line low.
|
||||
|
||||
// First expect a low signal for ~80 microseconds followed by a high signal
|
||||
// for ~80 microseconds again.
|
||||
if (expectPulse(LOW) == 0) {
|
||||
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
if (expectPulse(HIGH) == 0) {
|
||||
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
|
||||
// Now read the 40 bits sent by the sensor. Each bit is sent as a 50
|
||||
// microsecond low pulse followed by a variable length high pulse. If the
|
||||
// high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
|
||||
// then it's a 1. We measure the cycle count of the initial 50us low pulse
|
||||
// and use that to compare to the cycle count of the high pulse to determine
|
||||
// if the bit is a 0 (high state cycle count < low state cycle count), or a
|
||||
// 1 (high state cycle count > low state cycle count). Note that for speed all
|
||||
// the pulses are read into a array and then examined in a later step.
|
||||
for (int i=0; i<80; i+=2) {
|
||||
cycles[i] = expectPulse(LOW);
|
||||
cycles[i+1] = expectPulse(HIGH);
|
||||
}
|
||||
} // Timing critical code is now complete.
|
||||
|
||||
// Inspect pulses and determine which ones are 0 (high state cycle count < low
|
||||
// state cycle count), or 1 (high state cycle count > low state cycle count).
|
||||
for (int i=0; i<40; ++i) {
|
||||
uint32_t lowCycles = cycles[2*i];
|
||||
uint32_t highCycles = cycles[2*i+1];
|
||||
if ((lowCycles == 0) || (highCycles == 0)) {
|
||||
DEBUG_PRINTLN(F("Timeout waiting for pulse."));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
data[i/8] <<= 1;
|
||||
// Now compare the low and high cycle times to see if the bit is a 0 or 1.
|
||||
if (highCycles > lowCycles) {
|
||||
// High cycles are greater than 50us low cycle count, must be a 1.
|
||||
data[i/8] |= 1;
|
||||
}
|
||||
// Else high cycles are less than (or equal to, a weird case) the 50us low
|
||||
// cycle count so this must be a zero. Nothing needs to be changed in the
|
||||
// stored data.
|
||||
}
|
||||
|
||||
DEBUG_PRINTLN(F("Received:"));
|
||||
DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(F(", "));
|
||||
DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(F(", "));
|
||||
DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(F(", "));
|
||||
DEBUG_PRINT(data[3], HEX); DEBUG_PRINT(F(", "));
|
||||
DEBUG_PRINT(data[4], HEX); DEBUG_PRINT(F(" =? "));
|
||||
DEBUG_PRINTLN((data[0] + data[1] + data[2] + data[3]) & 0xFF, HEX);
|
||||
|
||||
// Check we read 40 bits and that the checksum matches.
|
||||
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
|
||||
_lastresult = true;
|
||||
return _lastresult;
|
||||
}
|
||||
else {
|
||||
DEBUG_PRINTLN(F("Checksum failure!"));
|
||||
_lastresult = false;
|
||||
return _lastresult;
|
||||
}
|
||||
}
|
||||
|
||||
// Expect the signal line to be at the specified level for a period of time and
|
||||
// return a count of loop cycles spent at that level (this cycle count can be
|
||||
// used to compare the relative time of two pulses). If more than a millisecond
|
||||
// ellapses without the level changing then the call fails with a 0 response.
|
||||
// This is adapted from Arduino's pulseInLong function (which is only available
|
||||
// in the very latest IDE versions):
|
||||
// https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_pulse.c
|
||||
uint32_t DHT::expectPulse(bool level) {
|
||||
uint32_t count = 0;
|
||||
// On AVR platforms use direct GPIO port access as it's much faster and better
|
||||
// for catching pulses that are 10's of microseconds in length:
|
||||
#ifdef __AVR
|
||||
uint8_t portState = level ? _bit : 0;
|
||||
while ((*portInputRegister(_port) & _bit) == portState) {
|
||||
if (count++ >= _maxcycles) {
|
||||
return 0; // Exceeded timeout, fail.
|
||||
}
|
||||
}
|
||||
// Otherwise fall back to using digitalRead (this seems to be necessary on ESP8266
|
||||
// right now, perhaps bugs in direct port access functions?).
|
||||
#else
|
||||
while (digitalRead(_pin) == level) {
|
||||
if (count++ >= _maxcycles) {
|
||||
return 0; // Exceeded timeout, fail.
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return count;
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/* DHT library
|
||||
|
||||
MIT license
|
||||
written by Adafruit Industries
|
||||
*/
|
||||
#ifndef DHT_H
|
||||
#define DHT_H
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
|
||||
// Uncomment to enable printing out nice debug messages.
|
||||
//#define DHT_DEBUG
|
||||
|
||||
// Define where debug output will be printed.
|
||||
#define DEBUG_PRINTER Serial
|
||||
|
||||
// Setup debug printing macros.
|
||||
#ifdef DHT_DEBUG
|
||||
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
|
||||
#else
|
||||
#define DEBUG_PRINT(...) {}
|
||||
#define DEBUG_PRINTLN(...) {}
|
||||
#endif
|
||||
|
||||
// Define types of sensors.
|
||||
#define DHT11 11
|
||||
#define DHT22 22
|
||||
#define DHT21 21
|
||||
#define AM2301 21
|
||||
|
||||
|
||||
class DHT {
|
||||
public:
|
||||
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
|
||||
void begin(void);
|
||||
float readTemperature(bool S=false, bool force=false);
|
||||
float convertCtoF(float);
|
||||
float convertFtoC(float);
|
||||
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
|
||||
float readHumidity(bool force=false);
|
||||
boolean read(bool force=false);
|
||||
|
||||
private:
|
||||
uint8_t data[5];
|
||||
uint8_t _pin, _type;
|
||||
#ifdef __AVR
|
||||
// Use direct GPIO access on an 8-bit AVR so keep track of the port and bitmask
|
||||
// for the digital pin connected to the DHT. Other platforms will use digitalRead.
|
||||
uint8_t _bit, _port;
|
||||
#endif
|
||||
uint32_t _lastreadtime, _maxcycles;
|
||||
bool _lastresult;
|
||||
|
||||
uint32_t expectPulse(bool level);
|
||||
|
||||
};
|
||||
|
||||
class InterruptLock {
|
||||
public:
|
||||
InterruptLock() {
|
||||
noInterrupts();
|
||||
}
|
||||
~InterruptLock() {
|
||||
interrupts();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,5 @@
|
|||
This is an Arduino library for the DHT series of low cost temperature/humidity sensors.
|
||||
|
||||
Tutorial: https://learn.adafruit.com/dht
|
||||
|
||||
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder DHT. Check that the DHT folder contains DHT.cpp and DHT.h. Place the DHT library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
|
|
@ -0,0 +1,69 @@
|
|||
// Example testing sketch for various DHT humidity/temperature sensors
|
||||
// Written by ladyada, public domain
|
||||
|
||||
#include "DHT.h"
|
||||
|
||||
#define DHTPIN 2 // what digital pin we're connected to
|
||||
|
||||
// Uncomment whatever type you're using!
|
||||
//#define DHTTYPE DHT11 // DHT 11
|
||||
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
|
||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||
|
||||
// Connect pin 1 (on the left) of the sensor to +5V
|
||||
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
|
||||
// to 3.3V instead of 5V!
|
||||
// Connect pin 2 of the sensor to whatever your DHTPIN is
|
||||
// Connect pin 4 (on the right) of the sensor to GROUND
|
||||
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
|
||||
|
||||
// Initialize DHT sensor.
|
||||
// Note that older versions of this library took an optional third parameter to
|
||||
// tweak the timings for faster processors. This parameter is no longer needed
|
||||
// as the current DHT reading algorithm adjusts itself to work on faster procs.
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("DHTxx test!");
|
||||
|
||||
dht.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Wait a few seconds between measurements.
|
||||
delay(2000);
|
||||
|
||||
// Reading temperature or humidity takes about 250 milliseconds!
|
||||
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
float h = dht.readHumidity();
|
||||
// Read temperature as Celsius (the default)
|
||||
float t = dht.readTemperature();
|
||||
// Read temperature as Fahrenheit (isFahrenheit = true)
|
||||
float f = dht.readTemperature(true);
|
||||
|
||||
// Check if any reads failed and exit early (to try again).
|
||||
if (isnan(h) || isnan(t) || isnan(f)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute heat index in Fahrenheit (the default)
|
||||
float hif = dht.computeHeatIndex(f, h);
|
||||
// Compute heat index in Celsius (isFahreheit = false)
|
||||
float hic = dht.computeHeatIndex(t, h, false);
|
||||
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(h);
|
||||
Serial.print(" %\t");
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(t);
|
||||
Serial.print(" *C ");
|
||||
Serial.print(f);
|
||||
Serial.print(" *F\t");
|
||||
Serial.print("Heat index: ");
|
||||
Serial.print(hic);
|
||||
Serial.print(" *C ");
|
||||
Serial.print(hif);
|
||||
Serial.println(" *F");
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
###########################################
|
||||
# Syntax Coloring Map For DHT-sensor-library
|
||||
###########################################
|
||||
|
||||
###########################################
|
||||
# Datatypes (KEYWORD1)
|
||||
###########################################
|
||||
|
||||
DHT KEYWORD1
|
||||
|
||||
###########################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
###########################################
|
||||
|
||||
begin KEYWORD2
|
||||
readTemperature KEYWORD2
|
||||
convertCtoF KEYWORD2
|
||||
convertFtoC KEYWORD2
|
||||
computeHeatIndex KEYWORD2
|
||||
readHumidity KEYWORD2
|
||||
read KEYWORD2
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
name=DHT sensor library
|
||||
version=1.2.3
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
|
||||
paragraph=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
|
||||
category=Sensors
|
||||
url=https://github.com/adafruit/DHT-sensor-library
|
||||
architectures=*
|
|
@ -0,0 +1,16 @@
|
|||
.idea
|
||||
classes
|
||||
target
|
||||
out
|
||||
build
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
*.log
|
||||
*.war
|
||||
.idea
|
||||
.project
|
||||
.classpath
|
||||
.settings
|
||||
.gradle
|
||||
|
|
@ -0,0 +1,807 @@
|
|||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
// Version 3.7.2 modified on Dec 6, 2011 to support Arduino 1.0
|
||||
// See Includes...
|
||||
// Modified by Jordan Hochenbaum
|
||||
|
||||
#include "DallasTemperature.h"
|
||||
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
extern "C" {
|
||||
#include "WConstants.h"
|
||||
}
|
||||
#endif
|
||||
|
||||
DallasTemperature::DallasTemperature() {}
|
||||
DallasTemperature::DallasTemperature(OneWire* _oneWire)
|
||||
|
||||
#if REQUIRESALARMS
|
||||
: _AlarmHandler(&defaultAlarmHandler)
|
||||
#endif
|
||||
{
|
||||
setOneWire(_oneWire);
|
||||
}
|
||||
|
||||
bool DallasTemperature::validFamily(const uint8_t* deviceAddress){
|
||||
switch (deviceAddress[0]){
|
||||
case DS18S20MODEL:
|
||||
case DS18B20MODEL:
|
||||
case DS1822MODEL:
|
||||
case DS1825MODEL:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void DallasTemperature::setOneWire(OneWire* _oneWire){
|
||||
|
||||
_wire = _oneWire;
|
||||
devices = 0;
|
||||
parasite = false;
|
||||
bitResolution = 9;
|
||||
waitForConversion = true;
|
||||
checkForConversion = true;
|
||||
|
||||
}
|
||||
|
||||
// initialise the bus
|
||||
void DallasTemperature::begin(void){
|
||||
|
||||
DeviceAddress deviceAddress;
|
||||
|
||||
_wire->reset_search();
|
||||
devices = 0; // Reset the number of devices when we enumerate wire devices
|
||||
|
||||
while (_wire->search(deviceAddress)){
|
||||
|
||||
if (validAddress(deviceAddress)){
|
||||
|
||||
if (!parasite && readPowerSupply(deviceAddress)) parasite = true;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
|
||||
readScratchPad(deviceAddress, scratchPad);
|
||||
|
||||
bitResolution = max(bitResolution, getResolution(deviceAddress));
|
||||
|
||||
devices++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// returns the number of devices found on the bus
|
||||
uint8_t DallasTemperature::getDeviceCount(void){
|
||||
return devices;
|
||||
}
|
||||
|
||||
// returns true if address is valid
|
||||
bool DallasTemperature::validAddress(const uint8_t* deviceAddress){
|
||||
return (_wire->crc8(deviceAddress, 7) == deviceAddress[7]);
|
||||
}
|
||||
|
||||
// finds an address at a given index on the bus
|
||||
// returns true if the device was found
|
||||
bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index){
|
||||
|
||||
uint8_t depth = 0;
|
||||
|
||||
_wire->reset_search();
|
||||
|
||||
while (depth <= index && _wire->search(deviceAddress)) {
|
||||
if (depth == index && validAddress(deviceAddress)) return true;
|
||||
depth++;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
bool DallasTemperature::isConnected(const uint8_t* deviceAddress){
|
||||
|
||||
ScratchPad scratchPad;
|
||||
return isConnected(deviceAddress, scratchPad);
|
||||
|
||||
}
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
// also allows for updating the read scratchpad
|
||||
bool DallasTemperature::isConnected(const uint8_t* deviceAddress, uint8_t* scratchPad)
|
||||
{
|
||||
bool b = readScratchPad(deviceAddress, scratchPad);
|
||||
return b && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
|
||||
}
|
||||
|
||||
bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* scratchPad){
|
||||
|
||||
// send the reset command and fail fast
|
||||
int b = _wire->reset();
|
||||
if (b == 0) return false;
|
||||
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(READSCRATCH);
|
||||
|
||||
// Read all registers in a simple loop
|
||||
// byte 0: temperature LSB
|
||||
// byte 1: temperature MSB
|
||||
// byte 2: high alarm temp
|
||||
// byte 3: low alarm temp
|
||||
// byte 4: DS18S20: store for crc
|
||||
// DS18B20 & DS1822: configuration register
|
||||
// byte 5: internal use & crc
|
||||
// byte 6: DS18S20: COUNT_REMAIN
|
||||
// DS18B20 & DS1822: store for crc
|
||||
// byte 7: DS18S20: COUNT_PER_C
|
||||
// DS18B20 & DS1822: store for crc
|
||||
// byte 8: SCRATCHPAD_CRC
|
||||
for(uint8_t i = 0; i < 9; i++){
|
||||
scratchPad[i] = _wire->read();
|
||||
}
|
||||
|
||||
b = _wire->reset();
|
||||
return (b == 1);
|
||||
}
|
||||
|
||||
|
||||
void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, const uint8_t* scratchPad){
|
||||
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(WRITESCRATCH);
|
||||
_wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
|
||||
_wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp
|
||||
|
||||
// DS1820 and DS18S20 have no configuration register
|
||||
if (deviceAddress[0] != DS18S20MODEL) _wire->write(scratchPad[CONFIGURATION]);
|
||||
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
|
||||
// save the newly written values to eeprom
|
||||
_wire->write(COPYSCRATCH, parasite);
|
||||
delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
|
||||
|
||||
if (parasite) delay(10); // 10ms delay
|
||||
_wire->reset();
|
||||
|
||||
}
|
||||
|
||||
bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress){
|
||||
|
||||
bool ret = false;
|
||||
_wire->reset();
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(READPOWERSUPPLY);
|
||||
if (_wire->read_bit() == 0) ret = true;
|
||||
_wire->reset();
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// set resolution of all devices to 9, 10, 11, or 12 bits
|
||||
// if new resolution is out of range, it is constrained.
|
||||
void DallasTemperature::setResolution(uint8_t newResolution){
|
||||
|
||||
bitResolution = constrain(newResolution, 9, 12);
|
||||
DeviceAddress deviceAddress;
|
||||
for (int i=0; i<devices; i++)
|
||||
{
|
||||
getAddress(deviceAddress, i);
|
||||
setResolution(deviceAddress, bitResolution);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// set resolution of a device to 9, 10, 11, or 12 bits
|
||||
// if new resolution is out of range, 9 bits is used.
|
||||
bool DallasTemperature::setResolution(const uint8_t* deviceAddress, uint8_t newResolution){
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)){
|
||||
|
||||
// DS1820 and DS18S20 have no resolution configuration register
|
||||
if (deviceAddress[0] != DS18S20MODEL){
|
||||
|
||||
switch (newResolution){
|
||||
case 12:
|
||||
scratchPad[CONFIGURATION] = TEMP_12_BIT;
|
||||
break;
|
||||
case 11:
|
||||
scratchPad[CONFIGURATION] = TEMP_11_BIT;
|
||||
break;
|
||||
case 10:
|
||||
scratchPad[CONFIGURATION] = TEMP_10_BIT;
|
||||
break;
|
||||
case 9:
|
||||
default:
|
||||
scratchPad[CONFIGURATION] = TEMP_9_BIT;
|
||||
break;
|
||||
}
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
return true; // new value set
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// returns the global resolution
|
||||
uint8_t DallasTemperature::getResolution(){
|
||||
return bitResolution;
|
||||
}
|
||||
|
||||
// returns the current resolution of the device, 9-12
|
||||
// returns 0 if device not found
|
||||
uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress){
|
||||
|
||||
// DS1820 and DS18S20 have no resolution configuration register
|
||||
if (deviceAddress[0] == DS18S20MODEL) return 12;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
switch (scratchPad[CONFIGURATION])
|
||||
{
|
||||
case TEMP_12_BIT:
|
||||
return 12;
|
||||
|
||||
case TEMP_11_BIT:
|
||||
return 11;
|
||||
|
||||
case TEMP_10_BIT:
|
||||
return 10;
|
||||
|
||||
case TEMP_9_BIT:
|
||||
return 9;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// sets the value of the waitForConversion flag
|
||||
// TRUE : function requestTemperature() etc returns when conversion is ready
|
||||
// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
|
||||
// (1) programmer has to check if the needed delay has passed
|
||||
// (2) but the application can do meaningful things in that time
|
||||
void DallasTemperature::setWaitForConversion(bool flag){
|
||||
waitForConversion = flag;
|
||||
}
|
||||
|
||||
// gets the value of the waitForConversion flag
|
||||
bool DallasTemperature::getWaitForConversion(){
|
||||
return waitForConversion;
|
||||
}
|
||||
|
||||
|
||||
// sets the value of the checkForConversion flag
|
||||
// TRUE : function requestTemperature() etc will 'listen' to an IC to determine whether a conversion is complete
|
||||
// FALSE: function requestTemperature() etc will wait a set time (worst case scenario) for a conversion to complete
|
||||
void DallasTemperature::setCheckForConversion(bool flag){
|
||||
checkForConversion = flag;
|
||||
}
|
||||
|
||||
// gets the value of the waitForConversion flag
|
||||
bool DallasTemperature::getCheckForConversion(){
|
||||
return checkForConversion;
|
||||
}
|
||||
|
||||
bool DallasTemperature::isConversionAvailable(const uint8_t* deviceAddress){
|
||||
|
||||
// Check if the clock has been raised indicating the conversion is complete
|
||||
ScratchPad scratchPad;
|
||||
readScratchPad(deviceAddress, scratchPad);
|
||||
return scratchPad[0];
|
||||
|
||||
}
|
||||
|
||||
// sends command for all devices on the bus to perform a temperature conversion
|
||||
void DallasTemperature::requestTemperatures(){
|
||||
|
||||
_wire->reset();
|
||||
_wire->skip();
|
||||
_wire->write(STARTCONVO, parasite);
|
||||
|
||||
// ASYNC mode?
|
||||
if (!waitForConversion) return;
|
||||
blockTillConversionComplete(bitResolution, NULL);
|
||||
|
||||
}
|
||||
|
||||
// sends command for one device to perform a temperature by address
|
||||
// returns FALSE if device is disconnected
|
||||
// returns TRUE otherwise
|
||||
bool DallasTemperature::requestTemperaturesByAddress(const uint8_t* deviceAddress){
|
||||
|
||||
uint8_t bitResolution = getResolution(deviceAddress);
|
||||
if (bitResolution == 0){
|
||||
return false; //Device disconnected
|
||||
}
|
||||
|
||||
if (_wire->reset() == 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
_wire->select(deviceAddress);
|
||||
_wire->write(STARTCONVO, parasite);
|
||||
|
||||
|
||||
// ASYNC mode?
|
||||
if (!waitForConversion) return true;
|
||||
|
||||
blockTillConversionComplete(bitResolution, deviceAddress);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Continue to check if the IC has responded with a temperature
|
||||
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution, const uint8_t* deviceAddress){
|
||||
|
||||
int delms = millisToWaitForConversion(bitResolution);
|
||||
if (deviceAddress != NULL && checkForConversion && !parasite){
|
||||
unsigned long now = millis();
|
||||
while(!isConversionAvailable(deviceAddress) && (millis() - delms < now));
|
||||
} else {
|
||||
delay(delms);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
|
||||
int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution){
|
||||
|
||||
switch (bitResolution){
|
||||
case 9:
|
||||
return 94;
|
||||
case 10:
|
||||
return 188;
|
||||
case 11:
|
||||
return 375;
|
||||
default:
|
||||
return 750;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// sends command for one device to perform a temp conversion by index
|
||||
bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex){
|
||||
|
||||
DeviceAddress deviceAddress;
|
||||
getAddress(deviceAddress, deviceIndex);
|
||||
|
||||
return requestTemperaturesByAddress(deviceAddress);
|
||||
|
||||
}
|
||||
|
||||
// Fetch temperature for device index
|
||||
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex){
|
||||
|
||||
DeviceAddress deviceAddress;
|
||||
if (!getAddress(deviceAddress, deviceIndex)){
|
||||
return DEVICE_DISCONNECTED_C;
|
||||
}
|
||||
|
||||
return getTempC((uint8_t*)deviceAddress);
|
||||
|
||||
}
|
||||
|
||||
// Fetch temperature for device index
|
||||
float DallasTemperature::getTempFByIndex(uint8_t deviceIndex){
|
||||
|
||||
DeviceAddress deviceAddress;
|
||||
|
||||
if (!getAddress(deviceAddress, deviceIndex)){
|
||||
return DEVICE_DISCONNECTED_F;
|
||||
}
|
||||
|
||||
return getTempF((uint8_t*)deviceAddress);
|
||||
|
||||
}
|
||||
|
||||
// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
|
||||
int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, uint8_t* scratchPad){
|
||||
|
||||
int16_t fpTemperature =
|
||||
(((int16_t) scratchPad[TEMP_MSB]) << 11) |
|
||||
(((int16_t) scratchPad[TEMP_LSB]) << 3);
|
||||
|
||||
/*
|
||||
DS1820 and DS18S20 have a 9-bit temperature register.
|
||||
|
||||
Resolutions greater than 9-bit can be calculated using the data from
|
||||
the temperature, and COUNT REMAIN and COUNT PER °C registers in the
|
||||
scratchpad. The resolution of the calculation depends on the model.
|
||||
|
||||
While the COUNT PER °C register is hard-wired to 16 (10h) in a
|
||||
DS18S20, it changes with temperature in DS1820.
|
||||
|
||||
After reading the scratchpad, the TEMP_READ value is obtained by
|
||||
truncating the 0.5°C bit (bit 0) from the temperature data. The
|
||||
extended resolution temperature can then be calculated using the
|
||||
following equation:
|
||||
|
||||
COUNT_PER_C - COUNT_REMAIN
|
||||
TEMPERATURE = TEMP_READ - 0.25 + --------------------------
|
||||
COUNT_PER_C
|
||||
|
||||
Hagai Shatz simplified this to integer arithmetic for a 12 bits
|
||||
value for a DS18S20, and James Cameron added legacy DS1820 support.
|
||||
|
||||
See - http://myarduinotoy.blogspot.co.uk/2013/02/12bit-result-from-ds18s20.html
|
||||
*/
|
||||
|
||||
if (deviceAddress[0] == DS18S20MODEL){
|
||||
fpTemperature = ((fpTemperature & 0xfff0) << 3) - 16 +
|
||||
(
|
||||
((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7) /
|
||||
scratchPad[COUNT_PER_C]
|
||||
);
|
||||
}
|
||||
|
||||
return fpTemperature;
|
||||
}
|
||||
|
||||
|
||||
// returns temperature in 1/128 degrees C or DEVICE_DISCONNECTED_RAW if the
|
||||
// device's scratch pad cannot be read successfully.
|
||||
// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
|
||||
// DallasTemperature.h. It is a large negative number outside the
|
||||
// operating range of the device
|
||||
int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress){
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)) return calculateTemperature(deviceAddress, scratchPad);
|
||||
return DEVICE_DISCONNECTED_RAW;
|
||||
|
||||
}
|
||||
|
||||
// returns temperature in degrees C or DEVICE_DISCONNECTED_C if the
|
||||
// device's scratch pad cannot be read successfully.
|
||||
// the numeric value of DEVICE_DISCONNECTED_C is defined in
|
||||
// DallasTemperature.h. It is a large negative number outside the
|
||||
// operating range of the device
|
||||
float DallasTemperature::getTempC(const uint8_t* deviceAddress){
|
||||
return rawToCelsius(getTemp(deviceAddress));
|
||||
}
|
||||
|
||||
// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the
|
||||
// device's scratch pad cannot be read successfully.
|
||||
// the numeric value of DEVICE_DISCONNECTED_F is defined in
|
||||
// DallasTemperature.h. It is a large negative number outside the
|
||||
// operating range of the device
|
||||
float DallasTemperature::getTempF(const uint8_t* deviceAddress){
|
||||
return rawToFahrenheit(getTemp(deviceAddress));
|
||||
}
|
||||
|
||||
// returns true if the bus requires parasite power
|
||||
bool DallasTemperature::isParasitePowerMode(void){
|
||||
return parasite;
|
||||
}
|
||||
|
||||
|
||||
// IF alarm is not used one can store a 16 bit int of userdata in the alarm
|
||||
// registers. E.g. an ID of the sensor.
|
||||
// See github issue #29
|
||||
|
||||
// note if device is not connected it will fail writing the data.
|
||||
void DallasTemperature::setUserData(const uint8_t* deviceAddress, int16_t data)
|
||||
{
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
scratchPad[HIGH_ALARM_TEMP] = data >> 8;
|
||||
scratchPad[LOW_ALARM_TEMP] = data & 255;
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
}
|
||||
|
||||
int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress)
|
||||
{
|
||||
int16_t data = 0;
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad))
|
||||
{
|
||||
data = scratchPad[HIGH_ALARM_TEMP] << 8;
|
||||
data += scratchPad[LOW_ALARM_TEMP];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// note If address cannot be found no error will be reported.
|
||||
int16_t DallasTemperature::getUserDataByIndex(uint8_t deviceIndex)
|
||||
{
|
||||
DeviceAddress deviceAddress;
|
||||
getAddress(deviceAddress, deviceIndex);
|
||||
return getUserData((uint8_t*) deviceAddress);
|
||||
}
|
||||
|
||||
void DallasTemperature::setUserDataByIndex(uint8_t deviceIndex, int16_t data)
|
||||
{
|
||||
DeviceAddress deviceAddress;
|
||||
getAddress(deviceAddress, deviceIndex);
|
||||
setUserData((uint8_t*) deviceAddress, data);
|
||||
}
|
||||
|
||||
|
||||
// Convert float Celsius to Fahrenheit
|
||||
float DallasTemperature::toFahrenheit(float celsius){
|
||||
return (celsius * 1.8) + 32;
|
||||
}
|
||||
|
||||
// Convert float Fahrenheit to Celsius
|
||||
float DallasTemperature::toCelsius(float fahrenheit){
|
||||
return (fahrenheit - 32) * 0.555555556;
|
||||
}
|
||||
|
||||
// convert from raw to Celsius
|
||||
float DallasTemperature::rawToCelsius(int16_t raw){
|
||||
|
||||
if (raw <= DEVICE_DISCONNECTED_RAW)
|
||||
return DEVICE_DISCONNECTED_C;
|
||||
// C = RAW/128
|
||||
return (float)raw * 0.0078125;
|
||||
|
||||
}
|
||||
|
||||
// convert from raw to Fahrenheit
|
||||
float DallasTemperature::rawToFahrenheit(int16_t raw){
|
||||
|
||||
if (raw <= DEVICE_DISCONNECTED_RAW)
|
||||
return DEVICE_DISCONNECTED_F;
|
||||
// C = RAW/128
|
||||
// F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
|
||||
return ((float)raw * 0.0140625) + 32;
|
||||
|
||||
}
|
||||
|
||||
#if REQUIRESALARMS
|
||||
|
||||
/*
|
||||
|
||||
ALARMS:
|
||||
|
||||
TH and TL Register Format
|
||||
|
||||
BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
|
||||
S 2^6 2^5 2^4 2^3 2^2 2^1 2^0
|
||||
|
||||
Only bits 11 through 4 of the temperature register are used
|
||||
in the TH and TL comparison since TH and TL are 8-bit
|
||||
registers. If the measured temperature is lower than or equal
|
||||
to TL or higher than or equal to TH, an alarm condition exists
|
||||
and an alarm flag is set inside the DS18B20. This flag is
|
||||
updated after every temperature measurement; therefore, if the
|
||||
alarm condition goes away, the flag will be turned off after
|
||||
the next temperature conversion.
|
||||
|
||||
*/
|
||||
|
||||
// sets the high alarm temperature for a device in degrees Celsius
|
||||
// accepts a float, but the alarm resolution will ignore anything
|
||||
// after a decimal point. valid range is -55C - 125C
|
||||
void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress, char celsius){
|
||||
|
||||
// make sure the alarm temperature is within the device's range
|
||||
if (celsius > 125) celsius = 125;
|
||||
else if (celsius < -55) celsius = -55;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)){
|
||||
scratchPad[HIGH_ALARM_TEMP] = (uint8_t)celsius;
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// sets the low alarm temperature for a device in degrees Celsius
|
||||
// accepts a float, but the alarm resolution will ignore anything
|
||||
// after a decimal point. valid range is -55C - 125C
|
||||
void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress, char celsius){
|
||||
// make sure the alarm temperature is within the device's range
|
||||
if (celsius > 125) celsius = 125;
|
||||
else if (celsius < -55) celsius = -55;
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)){
|
||||
scratchPad[LOW_ALARM_TEMP] = (uint8_t)celsius;
|
||||
writeScratchPad(deviceAddress, scratchPad);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// returns a char with the current high alarm temperature or
|
||||
// DEVICE_DISCONNECTED for an address
|
||||
char DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress){
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[HIGH_ALARM_TEMP];
|
||||
return DEVICE_DISCONNECTED_C;
|
||||
|
||||
}
|
||||
|
||||
// returns a char with the current low alarm temperature or
|
||||
// DEVICE_DISCONNECTED for an address
|
||||
char DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress){
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[LOW_ALARM_TEMP];
|
||||
return DEVICE_DISCONNECTED_C;
|
||||
|
||||
}
|
||||
|
||||
// resets internal variables used for the alarm search
|
||||
void DallasTemperature::resetAlarmSearch(){
|
||||
|
||||
alarmSearchJunction = -1;
|
||||
alarmSearchExhausted = 0;
|
||||
for(uint8_t i = 0; i < 7; i++){
|
||||
alarmSearchAddress[i] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// This is a modified version of the OneWire::search method.
|
||||
//
|
||||
// Also added the OneWire search fix documented here:
|
||||
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
|
||||
//
|
||||
// Perform an alarm search. If this function returns a '1' then it has
|
||||
// enumerated the next device and you may retrieve the ROM from the
|
||||
// OneWire::address variable. If there are no devices, no further
|
||||
// devices, or something horrible happens in the middle of the
|
||||
// enumeration then a 0 is returned. If a new device is found then
|
||||
// its address is copied to newAddr. Use
|
||||
// DallasTemperature::resetAlarmSearch() to start over.
|
||||
bool DallasTemperature::alarmSearch(uint8_t* newAddr){
|
||||
|
||||
uint8_t i;
|
||||
char lastJunction = -1;
|
||||
uint8_t done = 1;
|
||||
|
||||
if (alarmSearchExhausted) return false;
|
||||
if (!_wire->reset()) return false;
|
||||
|
||||
// send the alarm search command
|
||||
_wire->write(0xEC, 0);
|
||||
|
||||
for(i = 0; i < 64; i++){
|
||||
|
||||
uint8_t a = _wire->read_bit( );
|
||||
uint8_t nota = _wire->read_bit( );
|
||||
uint8_t ibyte = i / 8;
|
||||
uint8_t ibit = 1 << (i & 7);
|
||||
|
||||
// I don't think this should happen, this means nothing responded, but maybe if
|
||||
// something vanishes during the search it will come up.
|
||||
if (a && nota) return false;
|
||||
|
||||
if (!a && !nota){
|
||||
if (i == alarmSearchJunction){
|
||||
// this is our time to decide differently, we went zero last time, go one.
|
||||
a = 1;
|
||||
alarmSearchJunction = lastJunction;
|
||||
}else if (i < alarmSearchJunction){
|
||||
|
||||
// take whatever we took last time, look in address
|
||||
if (alarmSearchAddress[ibyte] & ibit){
|
||||
a = 1;
|
||||
}else{
|
||||
// Only 0s count as pending junctions, we've already exhausted the 0 side of 1s
|
||||
a = 0;
|
||||
done = 0;
|
||||
lastJunction = i;
|
||||
}
|
||||
}else{
|
||||
// we are blazing new tree, take the 0
|
||||
a = 0;
|
||||
alarmSearchJunction = i;
|
||||
done = 0;
|
||||
}
|
||||
// OneWire search fix
|
||||
// See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
|
||||
}
|
||||
|
||||
if (a) alarmSearchAddress[ibyte] |= ibit;
|
||||
else alarmSearchAddress[ibyte] &= ~ibit;
|
||||
|
||||
_wire->write_bit(a);
|
||||
}
|
||||
|
||||
if (done) alarmSearchExhausted = 1;
|
||||
for (i = 0; i < 8; i++) newAddr[i] = alarmSearchAddress[i];
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
// returns true if device address might have an alarm condition
|
||||
// (only an alarm search can verify this)
|
||||
bool DallasTemperature::hasAlarm(const uint8_t* deviceAddress){
|
||||
|
||||
ScratchPad scratchPad;
|
||||
if (isConnected(deviceAddress, scratchPad)){
|
||||
|
||||
char temp = calculateTemperature(deviceAddress, scratchPad) >> 7;
|
||||
|
||||
// check low alarm
|
||||
if (temp <= (char)scratchPad[LOW_ALARM_TEMP]) return true;
|
||||
|
||||
// check high alarm
|
||||
if (temp >= (char)scratchPad[HIGH_ALARM_TEMP]) return true;
|
||||
}
|
||||
|
||||
// no alarm
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// returns true if any device is reporting an alarm condition on the bus
|
||||
bool DallasTemperature::hasAlarm(void){
|
||||
|
||||
DeviceAddress deviceAddress;
|
||||
resetAlarmSearch();
|
||||
return alarmSearch(deviceAddress);
|
||||
|
||||
}
|
||||
|
||||
// runs the alarm handler for all devices returned by alarmSearch()
|
||||
void DallasTemperature::processAlarms(void){
|
||||
|
||||
resetAlarmSearch();
|
||||
DeviceAddress alarmAddr;
|
||||
|
||||
while (alarmSearch(alarmAddr)){
|
||||
|
||||
if (validAddress(alarmAddr)){
|
||||
_AlarmHandler(alarmAddr);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// sets the alarm handler
|
||||
void DallasTemperature::setAlarmHandler(AlarmHandler *handler){
|
||||
_AlarmHandler = handler;
|
||||
}
|
||||
|
||||
// The default alarm handler
|
||||
void DallasTemperature::defaultAlarmHandler(const uint8_t* deviceAddress){}
|
||||
|
||||
#endif
|
||||
|
||||
#if REQUIRESNEW
|
||||
|
||||
// MnetCS - Allocates memory for DallasTemperature. Allows us to instance a new object
|
||||
void* DallasTemperature::operator new(unsigned int size){ // Implicit NSS obj size
|
||||
|
||||
void * p; // void pointer
|
||||
p = malloc(size); // Allocate memory
|
||||
memset((DallasTemperature*)p,0,size); // Initialise memory
|
||||
|
||||
//!!! CANT EXPLICITLY CALL CONSTRUCTOR - workaround by using an init() methodR - workaround by using an init() method
|
||||
return (DallasTemperature*) p; // Cast blank region to NSS pointer
|
||||
}
|
||||
|
||||
// MnetCS 2009 - Free the memory used by this instance
|
||||
void DallasTemperature::operator delete(void* p){
|
||||
|
||||
DallasTemperature* pNss = (DallasTemperature*) p; // Cast to NSS pointer
|
||||
pNss->~DallasTemperature(); // Destruct the object
|
||||
|
||||
free(p); // Free the memory
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,269 @@
|
|||
#ifndef DallasTemperature_h
|
||||
#define DallasTemperature_h
|
||||
|
||||
#define DALLASTEMPLIBVERSION "3.7.3"
|
||||
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
// set to true to include code for new and delete operators
|
||||
#ifndef REQUIRESNEW
|
||||
#define REQUIRESNEW false
|
||||
#endif
|
||||
|
||||
// set to true to include code implementing alarm search functions
|
||||
#ifndef REQUIRESALARMS
|
||||
#define REQUIRESALARMS true
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <OneWire.h>
|
||||
|
||||
// Model IDs
|
||||
#define DS18S20MODEL 0x10 // also DS1820
|
||||
#define DS18B20MODEL 0x28
|
||||
#define DS1822MODEL 0x22
|
||||
#define DS1825MODEL 0x3B
|
||||
|
||||
// OneWire commands
|
||||
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
|
||||
#define COPYSCRATCH 0x48 // Copy EEPROM
|
||||
#define READSCRATCH 0xBE // Read EEPROM
|
||||
#define WRITESCRATCH 0x4E // Write to EEPROM
|
||||
#define RECALLSCRATCH 0xB8 // Reload from last known
|
||||
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
|
||||
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
|
||||
|
||||
// Scratchpad locations
|
||||
#define TEMP_LSB 0
|
||||
#define TEMP_MSB 1
|
||||
#define HIGH_ALARM_TEMP 2
|
||||
#define LOW_ALARM_TEMP 3
|
||||
#define CONFIGURATION 4
|
||||
#define INTERNAL_BYTE 5
|
||||
#define COUNT_REMAIN 6
|
||||
#define COUNT_PER_C 7
|
||||
#define SCRATCHPAD_CRC 8
|
||||
|
||||
// Device resolution
|
||||
#define TEMP_9_BIT 0x1F // 9 bit
|
||||
#define TEMP_10_BIT 0x3F // 10 bit
|
||||
#define TEMP_11_BIT 0x5F // 11 bit
|
||||
#define TEMP_12_BIT 0x7F // 12 bit
|
||||
|
||||
// Error Codes
|
||||
#define DEVICE_DISCONNECTED_C -127
|
||||
#define DEVICE_DISCONNECTED_F -196.6
|
||||
#define DEVICE_DISCONNECTED_RAW -7040
|
||||
|
||||
typedef uint8_t DeviceAddress[8];
|
||||
|
||||
class DallasTemperature
|
||||
{
|
||||
public:
|
||||
|
||||
DallasTemperature();
|
||||
DallasTemperature(OneWire*);
|
||||
|
||||
void setOneWire(OneWire*);
|
||||
|
||||
// initialise bus
|
||||
void begin(void);
|
||||
|
||||
// returns the number of devices found on the bus
|
||||
uint8_t getDeviceCount(void);
|
||||
|
||||
// returns true if address is valid
|
||||
bool validAddress(const uint8_t*);
|
||||
|
||||
// returns true if address is of the family of sensors the lib supports.
|
||||
bool validFamily(const uint8_t* deviceAddress);
|
||||
|
||||
// finds an address at a given index on the bus
|
||||
bool getAddress(uint8_t*, uint8_t);
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
bool isConnected(const uint8_t*);
|
||||
|
||||
// attempt to determine if the device at the given address is connected to the bus
|
||||
// also allows for updating the read scratchpad
|
||||
bool isConnected(const uint8_t*, uint8_t*);
|
||||
|
||||
// read device's scratchpad
|
||||
bool readScratchPad(const uint8_t*, uint8_t*);
|
||||
|
||||
// write device's scratchpad
|
||||
void writeScratchPad(const uint8_t*, const uint8_t*);
|
||||
|
||||
// read device's power requirements
|
||||
bool readPowerSupply(const uint8_t*);
|
||||
|
||||
// get global resolution
|
||||
uint8_t getResolution();
|
||||
|
||||
// set global resolution to 9, 10, 11, or 12 bits
|
||||
void setResolution(uint8_t);
|
||||
|
||||
// returns the device resolution: 9, 10, 11, or 12 bits
|
||||
uint8_t getResolution(const uint8_t*);
|
||||
|
||||
// set resolution of a device to 9, 10, 11, or 12 bits
|
||||
bool setResolution(const uint8_t*, uint8_t);
|
||||
|
||||
// sets/gets the waitForConversion flag
|
||||
void setWaitForConversion(bool);
|
||||
bool getWaitForConversion(void);
|
||||
|
||||
// sets/gets the checkForConversion flag
|
||||
void setCheckForConversion(bool);
|
||||
bool getCheckForConversion(void);
|
||||
|
||||
// sends command for all devices on the bus to perform a temperature conversion
|
||||
void requestTemperatures(void);
|
||||
|
||||
// sends command for one device to perform a temperature conversion by address
|
||||
bool requestTemperaturesByAddress(const uint8_t*);
|
||||
|
||||
// sends command for one device to perform a temperature conversion by index
|
||||
bool requestTemperaturesByIndex(uint8_t);
|
||||
|
||||
// returns temperature raw value (12 bit integer of 1/128 degrees C)
|
||||
int16_t getTemp(const uint8_t*);
|
||||
|
||||
// returns temperature in degrees C
|
||||
float getTempC(const uint8_t*);
|
||||
|
||||
// returns temperature in degrees F
|
||||
float getTempF(const uint8_t*);
|
||||
|
||||
// Get temperature for device index (slow)
|
||||
float getTempCByIndex(uint8_t);
|
||||
|
||||
// Get temperature for device index (slow)
|
||||
float getTempFByIndex(uint8_t);
|
||||
|
||||
// returns true if the bus requires parasite power
|
||||
bool isParasitePowerMode(void);
|
||||
|
||||
bool isConversionAvailable(const uint8_t*);
|
||||
|
||||
#if REQUIRESALARMS
|
||||
|
||||
typedef void AlarmHandler(const uint8_t*);
|
||||
|
||||
// sets the high alarm temperature for a device
|
||||
// accepts a char. valid range is -55C - 125C
|
||||
void setHighAlarmTemp(const uint8_t*, char);
|
||||
|
||||
// sets the low alarm temperature for a device
|
||||
// accepts a char. valid range is -55C - 125C
|
||||
void setLowAlarmTemp(const uint8_t*, char);
|
||||
|
||||
// returns a signed char with the current high alarm temperature for a device
|
||||
// in the range -55C - 125C
|
||||
char getHighAlarmTemp(const uint8_t*);
|
||||
|
||||
// returns a signed char with the current low alarm temperature for a device
|
||||
// in the range -55C - 125C
|
||||
char getLowAlarmTemp(const uint8_t*);
|
||||
|
||||
// resets internal variables used for the alarm search
|
||||
void resetAlarmSearch(void);
|
||||
|
||||
// search the wire for devices with active alarms
|
||||
bool alarmSearch(uint8_t*);
|
||||
|
||||
// returns true if ia specific device has an alarm
|
||||
bool hasAlarm(const uint8_t*);
|
||||
|
||||
// returns true if any device is reporting an alarm on the bus
|
||||
bool hasAlarm(void);
|
||||
|
||||
// runs the alarm handler for all devices returned by alarmSearch()
|
||||
void processAlarms(void);
|
||||
|
||||
// sets the alarm handler
|
||||
void setAlarmHandler(const AlarmHandler *);
|
||||
|
||||
// The default alarm handler
|
||||
static void defaultAlarmHandler(const uint8_t*);
|
||||
|
||||
#endif
|
||||
|
||||
// if no alarm handler is used the two bytes can be used as user data
|
||||
// example of such usage is an ID.
|
||||
// note if device is not connected it will fail writing the data.
|
||||
// note if address cannot be found no error will be reported.
|
||||
// in short use carefully
|
||||
void setUserData(const uint8_t*, int16_t );
|
||||
void setUserDataByIndex(uint8_t, int16_t );
|
||||
int16_t getUserData(const uint8_t* );
|
||||
int16_t getUserDataByIndex(uint8_t );
|
||||
|
||||
// convert from Celsius to Fahrenheit
|
||||
static float toFahrenheit(float);
|
||||
|
||||
// convert from Fahrenheit to Celsius
|
||||
static float toCelsius(float);
|
||||
|
||||
// convert from raw to Celsius
|
||||
static float rawToCelsius(int16_t);
|
||||
|
||||
// convert from raw to Fahrenheit
|
||||
static float rawToFahrenheit(int16_t);
|
||||
|
||||
#if REQUIRESNEW
|
||||
|
||||
// initialize memory area
|
||||
void* operator new (unsigned int);
|
||||
|
||||
// delete memory reference
|
||||
void operator delete(void*);
|
||||
|
||||
#endif
|
||||
|
||||
private:
|
||||
typedef uint8_t ScratchPad[9];
|
||||
|
||||
// parasite power on or off
|
||||
bool parasite;
|
||||
|
||||
// used to determine the delay amount needed to allow for the
|
||||
// temperature conversion to take place
|
||||
uint8_t bitResolution;
|
||||
|
||||
// used to requestTemperature with or without delay
|
||||
bool waitForConversion;
|
||||
|
||||
// used to requestTemperature to dynamically check if a conversion is complete
|
||||
bool checkForConversion;
|
||||
|
||||
// count of devices on the bus
|
||||
uint8_t devices;
|
||||
|
||||
// Take a pointer to one wire instance
|
||||
OneWire* _wire;
|
||||
|
||||
// reads scratchpad and returns the raw temperature
|
||||
int16_t calculateTemperature(const uint8_t*, uint8_t*);
|
||||
|
||||
int16_t millisToWaitForConversion(uint8_t);
|
||||
|
||||
void blockTillConversionComplete(uint8_t, const uint8_t*);
|
||||
|
||||
#if REQUIRESALARMS
|
||||
|
||||
// required for alarmSearch
|
||||
uint8_t alarmSearchAddress[8];
|
||||
char alarmSearchJunction;
|
||||
uint8_t alarmSearchExhausted;
|
||||
|
||||
// the alarm handler function pointer
|
||||
AlarmHandler *_AlarmHandler;
|
||||
|
||||
#endif
|
||||
|
||||
};
|
||||
#endif
|
|
@ -0,0 +1,64 @@
|
|||
# Arduino Library for Maxim Temperature Integrated Circuits
|
||||
|
||||
## Usage
|
||||
|
||||
This library supports the following devices :
|
||||
|
||||
|
||||
* DS18B20
|
||||
* DS18S20 - Please note there appears to be an issue with this series.
|
||||
* DS1822
|
||||
* DS1820
|
||||
|
||||
|
||||
You will need a pull-up resistor of about 5 KOhm between the 1-Wire data line
|
||||
and your 5V power. If you are using the DS18B20, ground pins 1 and 3. The
|
||||
centre pin is the data line '1-wire'.
|
||||
|
||||
We have included a "REQUIRESNEW" and "REQUIRESALARMS" definition. If you
|
||||
want to slim down the code feel free to use either of these by including
|
||||
|
||||
|
||||
|
||||
#define REQUIRESNEW
|
||||
|
||||
or
|
||||
|
||||
#define REQUIRESALARMS
|
||||
|
||||
|