From 36ef050fb618edeac8501bd8aa93b0361f82f61f Mon Sep 17 00:00:00 2001 From: Luciano Andrade Date: Tue, 27 Nov 2018 06:35:09 -0300 Subject: [PATCH] MAX6675 Support --- code/espurna/config/sensors.h | 4 + code/espurna/config/types.h | 1 + code/espurna/sensor.ino | 12 +++ code/espurna/sensors/MAX6675.h | 150 +++++++++++++++++++++++++++++++++ code/platformio.ini | 4 +- 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 code/espurna/sensors/MAX6675.h diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 68490789..b0b0872a 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -894,4 +894,8 @@ #include "../sensors/V9261FSensor.h" #endif +#if MAX6675_SUPPORT + #include "../sensors/MAX6675.h" +#endif + #endif // SENSOR_SUPPORT diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index a984bb7e..9bb4ebcc 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -279,6 +279,7 @@ #define SENSOR_SDS011_ID 0x27 #define SENSOR_MICS2710_ID 0x28 #define SENSOR_MICS5525_ID 0x29 +#define SENSOR_MAX6675_ID 0x30 //-------------------------------------------------------------------------------- // Magnitudes diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index f2983eda..ad05fa11 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -705,6 +705,18 @@ void _sensorLoad() { } #endif + #if MAX6675_SUPPORT + { + MAX6675Sensor * sensor = new MAX6675Sensor(); + sensor->setCS(MAX6675_CS_PIN); + sensor->setSO(MAX6675_SO_PIN); + sensor->setSCK(MAX6675_SCK_PIN); + _sensors.push_back(sensor); + } + #endif + + + } void _sensorCallback(unsigned char i, unsigned char type, double value) { diff --git a/code/espurna/sensors/MAX6675.h b/code/espurna/sensors/MAX6675.h new file mode 100644 index 00000000..39a58115 --- /dev/null +++ b/code/espurna/sensors/MAX6675.h @@ -0,0 +1,150 @@ +// ----------------------------------------------------------------------------- +// MAX6675 Sensor +// Uses MAX6675_Thermocouple library +// Copyright (C) 2017-2018 by Xose Pérez +// ----------------------------------------------------------------------------- + +#if SENSOR_SUPPORT && MAX6675_SUPPORT + +#pragma once + +#include "Arduino.h" +#include "BaseSensor.h" +#include +#include + +#define MAX6675_READ_INTERVAL 3000 + +class MAX6675Sensor : public BaseSensor { + + public: + + // --------------------------------------------------------------------- + // Public + // --------------------------------------------------------------------- + + MAX6675Sensor(): BaseSensor() { + _sensor_id = SENSOR_MAX6675_ID; + + } + + ~MAX6675Sensor() { + } + + // --------------------------------------------------------------------- + // --------------------------------------------------------------------- + + void setCS(unsigned char pin_cs) { + if (_pin_cs == pin_cs) return; + _pin_cs = pin_cs; + _dirty = true; + } + + void setSO(unsigned char pin_so) { + if (_pin_so == pin_so) return; + _pin_so = pin_so; + _dirty = true; + } + + void setSCK(unsigned char pin_sck) { + if (_pin_sck == pin_sck) return; + _pin_sck = pin_sck; + _dirty = true; + } + + // --------------------------------------------------------------------- + // Sensor API + // --------------------------------------------------------------------- + + // Initialization method, must be idempotent + void begin() { + + if (!_dirty) return; + + //// MAX6675 + int units = 1; // Units to readout temp (0 = raw, 1 = ˚C, 2 = ˚F) + if (_max) delete _max; + _max = new MAX6675(_pin_cs,_pin_so,_pin_sck,units); + + _count = 1; + + _ready = true; + _dirty = false; + + } + + // Loop-like method, call it in your main loop + void tick() { + static unsigned long last = 0; + if (millis() - last < MAX6675_READ_INTERVAL) return; + last = millis(); + + last_read = _max->read_temp(); + } + + + // Descriptive name of the sensor + String description() { + char buffer[20]; + //snprintf(buffer, sizeof(buffer), "MAX6675 @ CS %d", _gpio); + snprintf(buffer, sizeof(buffer), "MAX6675 "); + return String(buffer); + } + + String address(unsigned char index){ + return String("@ address"); + + } + + + // Address of the device + // Descriptive name of the slot # index + String slot(unsigned char index) { + if (index < _count) { + // char buffer[40]; + // uint8_t * address = _devices[index].address; + // snprintf(buffer, sizeof(buffer), "%s (%02X%02X%02X%02X%02X%02X%02X%02X) @ GPIO%d", + // chipAsString(index).c_str(), + // address[0], address[1], address[2], address[3], + // address[4], address[5], address[6], address[7], + // _gpio + // ); + return description(); + } + return String(); + } + + // Type for slot # index + unsigned char type(unsigned char index) { + if (index < _count) return MAGNITUDE_TEMPERATURE; + return MAGNITUDE_NONE; + } + + // Pre-read hook (usually to populate registers with up-to-date data) + void pre() { + _error = SENSOR_ERROR_OK; + } + + // Current value for slot # index + double value(unsigned char index) { + return last_read; + + } + + protected: + + // --------------------------------------------------------------------- + // Protected + // --------------------------------------------------------------------- + + unsigned int _pin_cs = MAX6675_CS_PIN; + unsigned int _pin_so = MAX6675_SO_PIN; + unsigned int _pin_sck = MAX6675_SCK_PIN; + bool _busy = false; + double last_read = 0; + MAX6675 * _max = NULL; + + +}; + +#endif // SENSOR_SUPPORT && MAX6675_SUPPORT diff --git a/code/platformio.ini b/code/platformio.ini index 265d4b4e..71eecf8d 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -41,7 +41,7 @@ debug_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP # -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY = v2 Lower Memory # -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH = v2 Higher Bandwidth # ------------------------------------------------------------------------------ -build_flags = -g -w -DMQTT_MAX_PACKET_SIZE=400 -DNO_GLOBAL_EEPROM ${sysenv.ESPURNA_FLAGS} -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH +build_flags = -g -w -DMQTT_MAX_PACKET_SIZE=400 -DNO_GLOBAL_EEPROM ${sysenv.ESPURNA_FLAGS} -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH -DMAX6675_SUPPORT -DSENSOR_SUPPORT -DUSE_CUSTOM_H build_flags_512k = ${common.build_flags} -Wl,-Teagle.flash.512k0m1s.ld build_flags_1m0m = ${common.build_flags} -Wl,-Teagle.flash.1m0m1s.ld build_flags_2m1m = ${common.build_flags} -Wl,-Teagle.flash.2m1m4s.ld @@ -97,6 +97,8 @@ lib_deps = https://github.com/LowPowerLab/RFM69#1.1.3 https://github.com/xoseperez/Time NewPing + https://github.com/mcleng/MAX6675-Library#2.0.1 + lib_ignore = # ------------------------------------------------------------------------------