All code moved to new file (xsnx_47_max31865.ino)

Code streamlined. Unused code deleted.

New "Resistance" string added to i18n to report PTD reported resistance

USE_SPI enabled in my_user_config.h
This commit is contained in:
Alberto Lopez 2019-07-28 19:36:22 -03:00
parent c2ad3d8264
commit 475f208cdc
6 changed files with 182 additions and 150 deletions

View File

@ -12,12 +12,12 @@ src_dir = sonoff
build_dir = .pioenvs
; *** Uncomment one of the lines below to build/upload only one environment
;env_default = sonoff
env_default = sonoff
;env_default = sonoff-minimal
;env_default = sonoff-basic
;env_default = sonoff-classic
;env_default = sonoff-knx
env_default = sonoff-sensors
;env_default = sonoff-sensors
;env_default = sonoff-display
;env_default = sonoff-BG
;env_default = sonoff-BR

View File

@ -118,6 +118,7 @@
#define D_JSON_PROGRAMSIZE "ProgramSize"
#define D_JSON_REFERENCETEMPERATURE "ReferenceTemperature"
#define D_JSON_RESET "Reset"
#define D_JSON_RESISTANCE "Resistance"
#define D_JSON_RESOLUTION "Resolution"
#define D_JSON_RESTARTING "Restarting"
#define D_JSON_RESTARTREASON "RestartReason"

View File

@ -44,7 +44,7 @@
\*********************************************************************************************/
// -- Master parameter control --------------------
#define CFG_HOLDER 4618 // [Reset 1] Change this value (max 32000) to load SECTION1 configuration parameters to flash
#define CFG_HOLDER 4617 // [Reset 1] Change this value (max 32000) to load SECTION1 configuration parameters to flash
// -- Project -------------------------------------
#define PROJECT "sonoff" // PROJECT is used as the default topic delimiter
@ -425,12 +425,9 @@
// -- Low level interface devices -----------------
#define USE_DHT // Add support for DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321) and SI7021 Temperature and Humidity sensor (1k6 code)
#define USE_MAX31855 // Add support for MAX31855 K-Type thermocouple sensor using softSPI
#define USE_MAX318x5 // Add support for MAX31855/31865 K-Type thermocouple / RTD sensors using softSPI
//#define USE_MAX31855 // Add support for MAX31855 K-Type thermocouple sensor using softSPI
#ifdef USE_MAX318x5
#define USE_MAX31855 // Until we move code to its own sourcefile we use 31855 original
#endif
#define USE_MAX31865 // Add support for MAX31865 RTD sensors using softSPI
// -- IR Remote features --------------------------
#define USE_IR_REMOTE // Send IR remote commands using library IRremoteESP8266 and ArduinoJson (+4k3 code, 0k3 mem, 48 iram)

View File

@ -187,10 +187,6 @@ enum UserSelectablePins {
GPIO_BUZZER, // Buzzer
GPIO_BUZZER_INV, // Inverted buzzer
GPIO_OLED_RESET, // OLED Display Reset
GPIO_MAX318x5CS, // MAX318x5 SPI CS
GPIO_MAX318x5CLK, // MAX318x5 SPI CLK
GPIO_MAX318x5MISO, // MAX318x5 SPI MISO
GPIO_MAX318x5MOSI, // MAX318x5 SPI MOSI
GPIO_SENSOR_END };
// Programmer selectable GPIO functionality
@ -627,12 +623,6 @@ const uint8_t kGpioNiceList[] PROGMEM = {
GPIO_MAX31855CLK, // MAX31855 Serial interface
GPIO_MAX31855DO, // MAX31855 Serial interface
#endif
#ifdef USE_MAX318x5
GPIO_MAX318x5CS, // MAX318x5 SPI CS
GPIO_MAX318x5CLK, // MAX318x5 SPI CLK
GPIO_MAX318x5MISO, // MAX318x5 SPI MISO
GPIO_MAX318x5MOSI, // MAX318x5 SPI MOSI
#endif
#ifdef USE_LIGHT
GPIO_DI, // my92x1 PWM input
GPIO_DCKI, // my92x1 CLK input

View File

@ -1,8 +1,7 @@
/*
xsns_39_max318x5.ino - MAX318x5 thermocouple sensor support for Sonoff-Tasmota
xsns_39_max31855.ino - MAX31855 thermocouple sensor support for Sonoff-Tasmota
Copyright (C) 2019 Alberto Lopez
Based on original code written by Markus Past (2019)
Copyright (C) 2019 Markus Past
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -18,194 +17,129 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#if defined(USE_MAX31855) || defined(USE_MAX318x5)
#ifdef USE_MAX31855
#include "Adafruit_MAX31865.h"
#define XSNS_39 39
#define RTD0 104.0 // Temperature at 0 degrees Celcius
#define RREF 430.0
bool initialized = false;
bool begun = false;
bool toggle = false;
Adafruit_MAX31865 max31865;
/*
Adafruit_MAX31865 max31865 = Adafruit_MAX31865(
pin[GPIO_SSPI_CS],
pin[GPIO_SSPI_MOSI],
pin[GPIO_SSPI_MISO],
pin[GPIO_SSPI_SCLK]
);
*/
union MAX318x5_Result_U {
struct MAX31855_S {
struct MAX31855_ResultStruct{
uint8_t ErrorCode; // Error Codes: 0 = No Error / 1 = TC open circuit / 2 = TC short to GND / 4 = TC short to VCC
float ProbeTemperature; // Measured temperature of the 'hot' TC junction (probe temp)
float ReferenceTemperature; // Measured temperature of the 'cold' TC junction (reference temp)
} Max31855;
} MAX31855_Result;
struct MAX31865_S {
uint16_t PtdResistance;
float PtdTemp;
} Max31865;
} MAX318x5_Result;
void MAX318x5_Init(void){
void MAX31855_Init(void){
if(initialized)
return;
/************************************************************************
MAX31855 Original Code. Commented out for now
// Set GPIO modes for SW-SPI
pinMode(pin[GPIO_MAX318x5CS], OUTPUT);
pinMode(pin[GPIO_MAX318x5CLK], OUTPUT);
pinMode(pin[GPIO_MAX318x5MISO], INPUT);
pinMode(pin[GPIO_MAX31855CS], OUTPUT);
pinMode(pin[GPIO_MAX31855CLK], OUTPUT);
pinMode(pin[GPIO_MAX31855DO], INPUT);
// Chip not selected / Clock low
digitalWrite(pin[GPIO_MAX318x5CS], HIGH);
digitalWrite(pin[GPIO_MAX318x5CLK], LOW);
digitalWrite(pin[GPIO_MAX31855CS], HIGH);
digitalWrite(pin[GPIO_MAX31855CLK], LOW);
initialized = true;
*/
// Set GPIO modes for SW-SPI
/*
pinMode(pin[GPIO_SSPI_CS], OUTPUT);
pinMode(pin[GPIO_SSPI_SCLK], OUTPUT);
pinMode(pin[GPIO_SSPI_MISO], INPUT);
pinMode(pin[GPIO_SSPI_MOSI], OUTPUT);
// Chip not selected / Clock low
digitalWrite(pin[GPIO_SSPI_CS], HIGH);
digitalWrite(pin[GPIO_SSPI_SCLK], LOW);
digitalWrite(pin[GPIO_SSPI_MOSI], HIGH);
**************************************************************************/
max31865.setPins(
pin[GPIO_SSPI_CS],
pin[GPIO_SSPI_MOSI],
pin[GPIO_SSPI_MISO],
pin[GPIO_SSPI_SCLK]
);
begun = max31865.begin(MAX31865_2WIRE);
initialized = true;
}
/*
* MAX318x5_GetResult(void)
* Acquires the raw data via SPI, checks for MAX318x5 errors and fills result structure
* MAX31855_GetResult(void)
* Acquires the raw data via SPI, checks for MAX31855 errors and fills result structure
*/
void MAX318x5_GetResult(void){
/************************************************************************
MAX31855 Original Code. Commented out for now
int32_t RawData = MAX318x5_ShiftIn(32);
void MAX31855_GetResult(void){
int32_t RawData = MAX31855_ShiftIn(32);
uint8_t probeerror = RawData & 0x7;
MAX318x5_Result.ErrorCode = probeerror;
MAX318x5_Result.ReferenceTemperature = MAX318x5_GetReferenceTemperature(RawData);
MAX31855_Result.ErrorCode = probeerror;
MAX31855_Result.ReferenceTemperature = MAX31855_GetReferenceTemperature(RawData);
if(probeerror)
MAX318x5_Result.ProbeTemperature = NAN; // Return NaN if MAX318x5 reports an error
MAX31855_Result.ProbeTemperature = NAN; // Return NaN if MAX31855 reports an error
else
MAX318x5_Result.ProbeTemperature = MAX318x5_GetProbeTemperature(RawData);
**************************************************************************/
MAX318x5_Result.Max31865.PtdResistance = max31865.readRTD();
MAX318x5_Result.Max31865.PtdTemp = max31865.rtd_to_temperature(MAX318x5_Result.Max31865.PtdResistance, RTD0, RREF);
MAX31855_Result.ProbeTemperature = MAX31855_GetProbeTemperature(RawData);
}
/*
* MAX318x5_GetProbeTemperature(int32_t RawData)
* MAX31855_GetProbeTemperature(int32_t RawData)
* Decodes and returns the temperature of TCs 'hot' junction from RawData
*/
float MAX318x5_GetProbeTemperature(int32_t RawData){
float MAX31855_GetProbeTemperature(int32_t RawData){
if(RawData & 0x80000000)
RawData = (RawData >> 18) | 0xFFFFC000; // Negative value - Drop lower 18 bits and extend to negative number
else
RawData >>= 18; // Positiv value - Drop lower 18 bits
float result = (RawData * 0.25); // MAX318x5 LSB resolution is 0.25°C for probe temperature
float result = (RawData * 0.25); // MAX31855 LSB resolution is 0.25°C for probe temperature
return (Settings.flag.temperature_conversion) ? ConvertTemp(result) : result; // Check if we have to convert to Fahrenheit
}
/*
* MAX318x5_GetReferenceTemperature(int32_t RawData)
* MAX31855_GetReferenceTemperature(int32_t RawData)
* Decodes and returns the temperature of TCs 'cold' junction from RawData
*/
float MAX318x5_GetReferenceTemperature(int32_t RawData){
float MAX31855_GetReferenceTemperature(int32_t RawData){
if(RawData & 0x8000)
RawData = (RawData >> 4) | 0xFFFFF000; // Negative value - Drop lower 4 bits and extend to negative number
else
RawData = (RawData >> 4) & 0x00000FFF; // Positiv value - Drop lower 4 bits and mask out remaining bits (probe temp, error bit, etc.)
float result = (RawData * 0.0625); // MAX318x5 LSB resolution is 0.0625°C for reference temperature
float result = (RawData * 0.0625); // MAX31855 LSB resolution is 0.0625°C for reference temperature
return (Settings.flag.temperature_conversion) ? ConvertTemp(result) : result; // Check if we have to convert to Fahrenheit
}
/*
* MAX318x5_ShiftIn(uint8_t Length)
* Communicates with MAX318x5 via SW-SPI and returns the raw data read from the chip
* MAX31855_ShiftIn(uint8_t Length)
* Communicates with MAX31855 via SW-SPI and returns the raw data read from the chip
*/
int32_t MAX318x5_ShiftIn(uint8_t Length){
int32_t MAX31855_ShiftIn(uint8_t Length){
int32_t dataIn = 0;
digitalWrite(pin[GPIO_MAX318x5CS], LOW); // CS = LOW -> Start SPI communication
digitalWrite(pin[GPIO_MAX31855CS], LOW); // CS = LOW -> Start SPI communication
delayMicroseconds(1); // CS fall to output enable = max. 100ns
for (uint32_t i = 0; i < Length; i++)
{
digitalWrite(pin[GPIO_MAX318x5CLK], LOW);
digitalWrite(pin[GPIO_MAX31855CLK], LOW);
delayMicroseconds(1); // CLK pulse width low = min. 100ns / CLK fall to output valid = max. 40ns
dataIn <<= 1;
if(digitalRead(pin[GPIO_MAX318x5MISO]))
if(digitalRead(pin[GPIO_MAX31855DO]))
dataIn |= 1;
digitalWrite(pin[GPIO_MAX318x5CLK], HIGH);
digitalWrite(pin[GPIO_MAX31855CLK], HIGH);
delayMicroseconds(1); // CLK pulse width high = min. 100ns
}
digitalWrite(pin[GPIO_MAX318x5CS], HIGH); // CS = HIGH -> End SPI communication
digitalWrite(pin[GPIO_MAX318x5CLK], LOW);
digitalWrite(pin[GPIO_MAX31855CS], HIGH); // CS = HIGH -> End SPI communication
digitalWrite(pin[GPIO_MAX31855CLK], LOW);
return dataIn;
}
void MAX318x5_Show(bool Json){
char temperature[33];
char resistance[33];
sprintf(resistance, "%ld", MAX318x5_Result.Max31865.PtdResistance);
dtostrfd(MAX318x5_Result.Max31865.PtdTemp, Settings.flag2.temperature_resolution, temperature);
snprintf_P(log_data, sizeof(log_data), PSTR("MAX318x5_Show(%d), Resistance: %s, Temp: %s"),
Json, resistance, temperature);
AddLog(LOG_LEVEL_INFO);
void MAX31855_Show(bool Json){
char probetemp[33];
char referencetemp[33];
dtostrfd(MAX31855_Result.ProbeTemperature, Settings.flag2.temperature_resolution, probetemp);
dtostrfd(MAX31855_Result.ReferenceTemperature, Settings.flag2.temperature_resolution, referencetemp);
if(Json){
ResponseAppend_P(PSTR(",\"MAX31865\":{\"" D_JSON_PROBETEMPERATURE "\":%s,\"" D_JSON_REFERENCETEMPERATURE "\":%s,\"" D_JSON_ERROR "\":%d}"), \
temperature, resistance, 0);
ResponseAppend_P(PSTR(",\"MAX31855\":{\"" D_JSON_PROBETEMPERATURE "\":%s,\"" D_JSON_REFERENCETEMPERATURE "\":%s,\"" D_JSON_ERROR "\":%d}"), \
probetemp, referencetemp, MAX31855_Result.ErrorCode);
#ifdef USE_DOMOTICZ
if (0 == tele_period) {
DomoticzSensor(DZ_TEMP, temperature);
DomoticzSensor(DZ_TEMP, probetemp);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
if (0 == tele_period) {
KnxSensor(KNX_TEMPERATURE, MAX318x5_Result.Max31865.PtdTemp);
KnxSensor(KNX_TEMPERATURE, MAX31855_Result.ProbeTemperature);
}
#endif // USE_KNX
} else {
#ifdef USE_WEBSERVER
WSContentSend_PD(HTTP_SNS_TEMP, "MAX31865", temperature, TempUnit());
WSContentSend_PD(HTTP_SNS_TEMP, "MAX31855", probetemp, TempUnit());
#endif // USE_WEBSERVER
}
}
@ -217,38 +151,21 @@ void MAX318x5_Show(bool Json){
bool Xsns39(uint8_t function)
{
bool result = false;
if(1/*(pin[GPIO_MAX318x5CS] < 99) && (pin[GPIO_MAX318x5CLK] < 99) && (pin[GPIO_MAX318x5MISO] < 99)
#ifdef USE_MAX318x5
&& (pin[GPIO_MAX318x5MOSI] < 99)
#endif
*/){
if((pin[GPIO_MAX31855CS] < 99) && (pin[GPIO_MAX31855CLK] < 99) && (pin[GPIO_MAX31855DO] < 99)){
switch (function) {
case FUNC_INIT:
MAX318x5_Init();
snprintf_P(log_data, sizeof(log_data), PSTR("MAX318x5_Init(): %d, GPIO_SSPI_CS:%d, GPIO_SSPI_MOSI:%d, GPIO_SSPI_MISO:%d, GPIO_SSPI_SCLK:%d"),
begun, pin[GPIO_SSPI_CS], pin[GPIO_SSPI_MOSI], pin[GPIO_SSPI_MISO], pin[GPIO_SSPI_SCLK]);
AddLog(LOG_LEVEL_INFO);
MAX31855_Init();
break;
case FUNC_EVERY_SECOND:
MAX318x5_GetResult();
char temperature[33];
dtostrfd(MAX318x5_Result.Max31865.PtdTemp, Settings.flag2.temperature_resolution, temperature);
snprintf_P(log_data, sizeof(log_data), PSTR("FUNC_EVERY_SECOND. Temp: %s, Resistance: %ld"),
temperature, MAX318x5_Result.Max31865.PtdResistance);
AddLog(LOG_LEVEL_INFO);
MAX31855_GetResult();
break;
case FUNC_JSON_APPEND:
MAX318x5_Show(true);
MAX31855_Show(true);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
MAX318x5_Show(false);
MAX31855_Show(false);
break;
#endif // USE_WEBSERVER
}
@ -256,4 +173,4 @@ bool Xsns39(uint8_t function)
return result;
}
#endif // USE_MAX318x5
#endif // USE_MAX31855

127
sonoff/xsns_47_max31865.ino Normal file
View File

@ -0,0 +1,127 @@
/*
xsns_39_MAX31865.ino - MAX31865 thermocouple sensor support for Sonoff-Tasmota
Copyright (C) 2019 Alberto Lopez
Based on original code for MAx31855 written by Markus Past
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#if defined(USE_MAX31855) || defined(USE_MAX31865)
#ifdef USE_MAX31865
#include "Adafruit_MAX31865.h"
#define XSNS_47 47
#define RTD0 104.0 // Temperature at 0 degrees Celcius
#define RREF 430.0
int8_t init_status = 0;
Adafruit_MAX31865 max31865;
struct MAX31865_Result_Struct {
uint8_t ErrorCode;
uint16_t PtdResistance;
float PtdTemp;
} MAX31865_Result;
void MAX31865_Init(void){
if(init_status)
return;
max31865.setPins(
pin[GPIO_SSPI_CS],
pin[GPIO_SSPI_MOSI],
pin[GPIO_SSPI_MISO],
pin[GPIO_SSPI_SCLK]
);
if(max31865.begin(MAX31865_2WIRE))
init_status = 1;
else
init_status = -1;
}
/*
* MAX31865_GetResult(void)
* Acquires the raw data via SPI, checks for MAX31865 errors and fills result structure
*/
void MAX31865_GetResult(void){
MAX31865_Result.PtdResistance = max31865.readRTD();
MAX31865_Result.PtdTemp = max31865.rtd_to_temperature(MAX31865_Result.PtdResistance, RTD0, RREF);
}
void MAX31865_Show(bool Json){
char temperature[33];
char resistance[33];
sprintf(resistance, "%ld", MAX31865_Result.PtdResistance);
dtostrfd(MAX31865_Result.PtdTemp, Settings.flag2.temperature_resolution, temperature);
if(Json){
ResponseAppend_P(PSTR(",\"MAX31865\":{\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_RESISTANCE "\":%s,\"" D_JSON_ERROR "\":%d}"), \
temperature, resistance, MAX31865_Result.ErrorCode);
#ifdef USE_DOMOTICZ
if (0 == tele_period) {
DomoticzSensor(DZ_TEMP, temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
if (0 == tele_period) {
KnxSensor(KNX_TEMPERATURE, MAX31865_Result.Max31865.PtdTemp);
}
#endif // USE_KNX
} else {
#ifdef USE_WEBSERVER
WSContentSend_PD(HTTP_SNS_TEMP, "MAX31865", temperature, TempUnit());
#endif // USE_WEBSERVER
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xsns47(uint8_t function)
{
bool result = false;
if((pin[GPIO_SSPI_MISO] < 99) && (pin[GPIO_SSPI_MOSI] < 99) &&
(pin[GPIO_SSPI_SCLK] < 99) && (pin[GPIO_SSPI_CS] < 99)) {
switch (function) {
case FUNC_INIT:
MAX31865_Init();
break;
case FUNC_EVERY_SECOND:
MAX31865_GetResult();
break;
case FUNC_JSON_APPEND:
MAX31865_Show(true);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
MAX31865_Show(false);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_MAX31865