diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 543a9f1f..ced3a36b 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -1025,4 +1025,8 @@ #include "../sensors/VL53L1XSensor.h" #endif +#if MAX6675_SUPPORT + #include "../sensors/MAX6675.h" +#endif + #endif // SENSOR_SUPPORT diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index bba1f959..eabab993 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -726,6 +726,16 @@ 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 + #if VEML6075_SUPPORT { VEML6075Sensor * sensor = new VEML6075Sensor(); 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 63263b43..ee2843a7 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 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 @@ -99,6 +99,7 @@ lib_deps = NewPing https://github.com/sparkfun/SparkFun_VEML6075_Arduino_Library#V_1.0.3 https://github.com/pololu/vl53l1x-arduino#1.0.1 + https://github.com/mcleng/MAX6675-Library#2.0.1 lib_ignore = # ------------------------------------------------------------------------------