From 43c91c0662642c6ee78ffd719b835223caf44ae9 Mon Sep 17 00:00:00 2001 From: c3ma Date: Fri, 16 Oct 2020 15:04:21 +0200 Subject: [PATCH] Temperature sensor --- .../Esp32DeepSleepTest/include/DS18B20.h | 52 +++++++++ esp32test/Esp32DeepSleepTest/src/DS18B20.cpp | 105 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 esp32test/Esp32DeepSleepTest/include/DS18B20.h create mode 100644 esp32test/Esp32DeepSleepTest/src/DS18B20.cpp diff --git a/esp32test/Esp32DeepSleepTest/include/DS18B20.h b/esp32test/Esp32DeepSleepTest/include/DS18B20.h new file mode 100644 index 0000000..7ab8e66 --- /dev/null +++ b/esp32test/Esp32DeepSleepTest/include/DS18B20.h @@ -0,0 +1,52 @@ +/** + * @file DS18B20.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2020-06-09 + * + * @copyright Copyright (c) 2020 + * Based on the LUA code from the ESP8266 + * -------------------------------------------------------------------------------- + * -- DS18B20 one wire module for NODEMCU + * -- NODEMCU TEAM + * -- LICENCE: http://opensource.org/licenses/MIT + * -- Vowstar + * -- 2015/02/14 sza2 Fix for negative values + * -------------------------------------------------------------------------------- + */ + +#ifndef DS18B20_H +#define DS18B20_H + +#include + +class Ds18B20 { + private: + OneWire* mDs; + int foundDevices; + public: + Ds18B20(int pin) { + this->mDs = new OneWire(pin); + } + + ~Ds18B20() { + delete this->mDs; + } + /** + * @brief read amount sensots + * check for available of DS18B20 sensors + * @return amount of sensors + */ + int readDevices(void); + + /** + * @brief Read all temperatures in celsius + * + * @param pTemperatures array of float valuies + * @param maxTemperatures size of the given array + * @return int amount of read temperature values + */ + int readAllTemperatures(float* pTemperatures, int maxTemperatures); +}; +#endif diff --git a/esp32test/Esp32DeepSleepTest/src/DS18B20.cpp b/esp32test/Esp32DeepSleepTest/src/DS18B20.cpp new file mode 100644 index 0000000..0bc738e --- /dev/null +++ b/esp32test/Esp32DeepSleepTest/src/DS18B20.cpp @@ -0,0 +1,105 @@ +/** + * @file DS18B20.cpp + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2020-06-09 + * + * @copyright Copyright (c) 2020 + * + */ + +#include "DS18B20.h" + +#define STARTCONV 0x44 +#define READSCRATCH 0xBE // Read EEPROM +#define TEMP_LSB 0 +#define TEMP_MSB 1 +#define SCRATCHPADSIZE 9 +#define OFFSET_CRC8 8 /**< 9th byte has the CRC of the complete data */ + +//Printf debugging +//#define DS_DEBUG + +int Ds18B20::readDevices() { + byte addr[8]; + + int amount = -1; + while (this->mDs->search(addr)) { + amount++; + } + this->mDs->reset_search(); + return amount; +} + +int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) { + byte addr[8]; + uint8_t scratchPad[SCRATCHPADSIZE]; + int currentTemp = 0; + + while (this->mDs->search(addr)) { +#ifdef DS_DEBUG + Serial.print(" ROM ="); + for (i = 0; i < 8; i++) { + Serial.write(' '); + Serial.print(addr[i], HEX); + } +#endif + this->mDs->reset(); + this->mDs->select(addr); + this->mDs->write(STARTCONV); + this->mDs->reset(); + this->mDs->select(addr); + this->mDs->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 +#ifdef DS_DEBUG + Serial.write("\r\nDATA:"); + for (uint8_t i = 0; i < 9; i++) { + Serial.print(scratchPad[i], HEX); + } +#else + delay(50); +#endif + for (uint8_t i = 0; i < 9; i++) { + scratchPad[i] = this->mDs->read(); + } + uint8_t crc8 = this->mDs->crc8(scratchPad, 8); + + /* Only work an valid data */ + if (crc8 == scratchPad[OFFSET_CRC8]) { + int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11) + | (((int16_t) scratchPad[TEMP_LSB]) << 3); + float celsius = (float) fpTemperature * 0.0078125; +#ifdef DS_DEBUG + Serial.printf("\r\nTemp%d %f °C (Raw: %d, %x =? %x)\r\n", (currentTemp+1), celsius, fpTemperature, crc8, scratchPad[8]); +#endif + /* check, if the buffer as some space for our data */ + if (currentTemp < maxTemperatures) { + pTemperatures[currentTemp] = celsius; + } else { + return -1; + } + } + currentTemp++; + } + this->mDs->reset(); +#ifdef DS_DEBUG + Serial.println(" No more addresses."); + Serial.println(); +#endif + + return currentTemp; +} \ No newline at end of file