Initial commit

Working fine with MAX3165 and a PT100 probe.

Using a modified version of Adafruit MAX31865 library. Added a default
constructor in order to allow lazy initialization once GPIO definitions
were loaded.

Tested on a Sonoff TH PCB Board 2.1 2018-06-15 using the following GPIO
mapping:

GPIO	Orig Func	SSPI	I/O	HEADER	HEADER2	S-JACK
 15	BOOT MODE 	CLK	   O	           1
 14	            MISO	 I		         2	   TIP
  4		          MOSI   O		         3	   RING1
VCC				                1 VCC	     4	   RING3
  2	DBG BOOT 	  ~CS	   O	5 EXP-LOG
GND				                4 GND		         RING2

HEADER2 is a 4-pin .1 header glued in the middle of the PCB to connect
to several GPIO locations on the Sonoff TH PCB.

TODO: Merge with MAX31855 code.
This commit is contained in:
Alberto Lopez 2019-07-27 19:58:17 -03:00
parent 250c991c97
commit c2ad3d8264
5 changed files with 154 additions and 55 deletions

@ -0,0 +1 @@
Subproject commit 83027ecdd5c8e059510fcca5f230451c55ad078a

View File

@ -17,7 +17,7 @@ build_dir = .pioenvs
;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
@ -207,7 +207,7 @@ upload_speed = 115200
upload_resetmethod = nodemcu
; *** Upload Serial reset method for Wemos and NodeMCU
upload_port = COM5
upload_port = /dev/cu.usbserial-A5XK3RJT
extra_scripts = pio/strip-floats.py
; *** Upload file to OTA server using SCP

View File

@ -44,7 +44,7 @@
\*********************************************************************************************/
// -- Master parameter control --------------------
#define CFG_HOLDER 4617 // [Reset 1] Change this value (max 32000) to load SECTION1 configuration parameters to flash
#define CFG_HOLDER 4618 // [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
@ -384,7 +384,7 @@
#endif // USE_I2C
// -- SPI sensors ---------------------------------
//#define USE_SPI // Hardware SPI using GPIO12(MISO), GPIO13(MOSI) and GPIO14(CLK) in addition to two user selectable GPIOs(CS and DC)
#define USE_SPI // Hardware SPI using GPIO12(MISO), GPIO13(MOSI) and GPIO14(CLK) in addition to two user selectable GPIOs(CS and DC)
#ifdef USE_SPI
#ifndef USE_DISPLAY
@ -425,7 +425,12 @@
// -- 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_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
#ifdef USE_MAX318x5
#define USE_MAX31855 // Until we move code to its own sourcefile we use 31855 original
#endif
// -- 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,6 +187,10 @@ 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
@ -623,6 +627,12 @@ 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,7 +1,8 @@
/*
xsns_39_max31855.ino - MAX31855 thermocouple sensor support for Sonoff-Tasmota
xsns_39_max318x5.ino - MAX318x5 thermocouple sensor support for Sonoff-Tasmota
Copyright (C) 2019 Markus Past
Copyright (C) 2019 Alberto Lopez
Based on original code written by Markus Past (2019)
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
@ -17,129 +18,194 @@
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
bool initialized = false;
#define RTD0 104.0 // Temperature at 0 degrees Celcius
#define RREF 430.0
struct MAX31855_ResultStruct{
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 {
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_Result;
} Max31855;
void MAX31855_Init(void){
struct MAX31865_S {
uint16_t PtdResistance;
float PtdTemp;
} Max31865;
} MAX318x5_Result;
void MAX318x5_Init(void){
if(initialized)
return;
/************************************************************************
MAX31855 Original Code. Commented out for now
// Set GPIO modes for SW-SPI
pinMode(pin[GPIO_MAX31855CS], OUTPUT);
pinMode(pin[GPIO_MAX31855CLK], OUTPUT);
pinMode(pin[GPIO_MAX31855DO], INPUT);
pinMode(pin[GPIO_MAX318x5CS], OUTPUT);
pinMode(pin[GPIO_MAX318x5CLK], OUTPUT);
pinMode(pin[GPIO_MAX318x5MISO], INPUT);
// Chip not selected / Clock low
digitalWrite(pin[GPIO_MAX31855CS], HIGH);
digitalWrite(pin[GPIO_MAX31855CLK], LOW);
digitalWrite(pin[GPIO_MAX318x5CS], HIGH);
digitalWrite(pin[GPIO_MAX318x5CLK], 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;
}
/*
* MAX31855_GetResult(void)
* Acquires the raw data via SPI, checks for MAX31855 errors and fills result structure
* MAX318x5_GetResult(void)
* Acquires the raw data via SPI, checks for MAX318x5 errors and fills result structure
*/
void MAX31855_GetResult(void){
int32_t RawData = MAX31855_ShiftIn(32);
void MAX318x5_GetResult(void){
/************************************************************************
MAX31855 Original Code. Commented out for now
int32_t RawData = MAX318x5_ShiftIn(32);
uint8_t probeerror = RawData & 0x7;
MAX31855_Result.ErrorCode = probeerror;
MAX31855_Result.ReferenceTemperature = MAX31855_GetReferenceTemperature(RawData);
MAX318x5_Result.ErrorCode = probeerror;
MAX318x5_Result.ReferenceTemperature = MAX318x5_GetReferenceTemperature(RawData);
if(probeerror)
MAX31855_Result.ProbeTemperature = NAN; // Return NaN if MAX31855 reports an error
MAX318x5_Result.ProbeTemperature = NAN; // Return NaN if MAX318x5 reports an error
else
MAX31855_Result.ProbeTemperature = MAX31855_GetProbeTemperature(RawData);
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_GetProbeTemperature(int32_t RawData)
* MAX318x5_GetProbeTemperature(int32_t RawData)
* Decodes and returns the temperature of TCs 'hot' junction from RawData
*/
float MAX31855_GetProbeTemperature(int32_t RawData){
float MAX318x5_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); // MAX31855 LSB resolution is 0.25°C for probe temperature
float result = (RawData * 0.25); // MAX318x5 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
}
/*
* MAX31855_GetReferenceTemperature(int32_t RawData)
* MAX318x5_GetReferenceTemperature(int32_t RawData)
* Decodes and returns the temperature of TCs 'cold' junction from RawData
*/
float MAX31855_GetReferenceTemperature(int32_t RawData){
float MAX318x5_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); // MAX31855 LSB resolution is 0.0625°C for reference temperature
float result = (RawData * 0.0625); // MAX318x5 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
}
/*
* MAX31855_ShiftIn(uint8_t Length)
* Communicates with MAX31855 via SW-SPI and returns the raw data read from the chip
* MAX318x5_ShiftIn(uint8_t Length)
* Communicates with MAX318x5 via SW-SPI and returns the raw data read from the chip
*/
int32_t MAX31855_ShiftIn(uint8_t Length){
int32_t MAX318x5_ShiftIn(uint8_t Length){
int32_t dataIn = 0;
digitalWrite(pin[GPIO_MAX31855CS], LOW); // CS = LOW -> Start SPI communication
digitalWrite(pin[GPIO_MAX318x5CS], 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_MAX31855CLK], LOW);
digitalWrite(pin[GPIO_MAX318x5CLK], LOW);
delayMicroseconds(1); // CLK pulse width low = min. 100ns / CLK fall to output valid = max. 40ns
dataIn <<= 1;
if(digitalRead(pin[GPIO_MAX31855DO]))
if(digitalRead(pin[GPIO_MAX318x5MISO]))
dataIn |= 1;
digitalWrite(pin[GPIO_MAX31855CLK], HIGH);
digitalWrite(pin[GPIO_MAX318x5CLK], HIGH);
delayMicroseconds(1); // CLK pulse width high = min. 100ns
}
digitalWrite(pin[GPIO_MAX31855CS], HIGH); // CS = HIGH -> End SPI communication
digitalWrite(pin[GPIO_MAX31855CLK], LOW);
digitalWrite(pin[GPIO_MAX318x5CS], HIGH); // CS = HIGH -> End SPI communication
digitalWrite(pin[GPIO_MAX318x5CLK], LOW);
return dataIn;
}
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);
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);
if(Json){
ResponseAppend_P(PSTR(",\"MAX31855\":{\"" D_JSON_PROBETEMPERATURE "\":%s,\"" D_JSON_REFERENCETEMPERATURE "\":%s,\"" D_JSON_ERROR "\":%d}"), \
probetemp, referencetemp, MAX31855_Result.ErrorCode);
ResponseAppend_P(PSTR(",\"MAX31865\":{\"" D_JSON_PROBETEMPERATURE "\":%s,\"" D_JSON_REFERENCETEMPERATURE "\":%s,\"" D_JSON_ERROR "\":%d}"), \
temperature, resistance, 0);
#ifdef USE_DOMOTICZ
if (0 == tele_period) {
DomoticzSensor(DZ_TEMP, probetemp);
DomoticzSensor(DZ_TEMP, temperature);
}
#endif // USE_DOMOTICZ
#ifdef USE_KNX
if (0 == tele_period) {
KnxSensor(KNX_TEMPERATURE, MAX31855_Result.ProbeTemperature);
KnxSensor(KNX_TEMPERATURE, MAX318x5_Result.Max31865.PtdTemp);
}
#endif // USE_KNX
} else {
#ifdef USE_WEBSERVER
WSContentSend_PD(HTTP_SNS_TEMP, "MAX31855", probetemp, TempUnit());
WSContentSend_PD(HTTP_SNS_TEMP, "MAX31865", temperature, TempUnit());
#endif // USE_WEBSERVER
}
}
@ -151,21 +217,38 @@ void MAX31855_Show(bool Json){
bool Xsns39(uint8_t function)
{
bool result = false;
if((pin[GPIO_MAX31855CS] < 99) && (pin[GPIO_MAX31855CLK] < 99) && (pin[GPIO_MAX31855DO] < 99)){
if(1/*(pin[GPIO_MAX318x5CS] < 99) && (pin[GPIO_MAX318x5CLK] < 99) && (pin[GPIO_MAX318x5MISO] < 99)
#ifdef USE_MAX318x5
&& (pin[GPIO_MAX318x5MOSI] < 99)
#endif
*/){
switch (function) {
case FUNC_INIT:
MAX31855_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);
break;
case FUNC_EVERY_SECOND:
MAX31855_GetResult();
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);
break;
case FUNC_JSON_APPEND:
MAX31855_Show(true);
MAX318x5_Show(true);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
MAX31855_Show(false);
MAX318x5_Show(false);
break;
#endif // USE_WEBSERVER
}
@ -173,4 +256,4 @@ bool Xsns39(uint8_t function)
return result;
}
#endif // USE_MAX31855
#endif // USE_MAX318x5