From 950e24208896275c5066c4a697847807b2d0fe5f Mon Sep 17 00:00:00 2001 From: Manuel Fegerl Date: Sun, 20 Feb 2022 14:34:56 +0100 Subject: [PATCH] Added MCO9600 Support and fixed a bug with ADS1115 --- platformio.ini | 1 + src/SensatioFirmware.cpp | 3 +- src/controller/Bridge.cpp | 7 +- src/controller/Bridge.h | 1 + src/input/i2c/Ads1x15.cpp | 11 +- src/input/i2c/SensorMCP9600.cpp | 251 ++++++++++++++++++++++++++++++++ src/input/i2c/SensorMCP9600.h | 43 ++++++ 7 files changed, 312 insertions(+), 5 deletions(-) create mode 100644 src/input/i2c/SensorMCP9600.cpp create mode 100644 src/input/i2c/SensorMCP9600.h diff --git a/platformio.ini b/platformio.ini index 3046248..3e8268e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,6 +24,7 @@ lib_deps_external = adafruit/DHT sensor library@1.4.3 adafruit/Adafruit ST7735 and ST7789 Library@^1.7.5 paulstoffregen/OneWire @ 2.3.6 + sparkfun/SparkFun MCP9600 Thermocouple Library @ ^1.0.4 [env:NodeMCU-4M] platform = espressif8266@2.6.2 diff --git a/src/SensatioFirmware.cpp b/src/SensatioFirmware.cpp index a680c66..32837a9 100644 --- a/src/SensatioFirmware.cpp +++ b/src/SensatioFirmware.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensatio/firmware-platformIO.git @section HISTORY + v47 - Added Support for MCP9600 thermocouple sensors and fixed a bug that caused a crash if multiple ADS1115 were configured v46 - Merge of ESP8266 and ESP32 Firmware into PlatformIO project v45 - Fixed Pressure Measurement for BME280 Sensors v44 - More Memory Improvements @@ -35,7 +36,7 @@ VisualisationHelper *vHelper; Display *display = NULL; -int currentVersion = 46; +int currentVersion = 47; boolean printMemory = false; #ifdef ESP8266_Generic diff --git a/src/controller/Bridge.cpp b/src/controller/Bridge.cpp index 46bbe46..a2ca591 100644 --- a/src/controller/Bridge.cpp +++ b/src/controller/Bridge.cpp @@ -1,5 +1,5 @@ /**************************************************************************/ -/* +/*! @file Bridge.cpp @author M. Fegerl (Sensate Digital Solutions GmbH) @license GPL (see LICENSE file) @@ -973,6 +973,11 @@ void configureExpansionPort(int portNumber, JsonObject& portConfig) { { addSensor(new SensorSI1145(portConfig["id"], portConfig["c"], portConfig["sn"], portConfig["n"], portConfig["ec1"], portConfig["ec2"], refreshInterval, postDataInterval, portConfig["s"]["svt"], calc)); } + else if (portConfig["et"] == "MCP9600") + { + addSensor(new SensorMCP9600(portConfig["id"], portConfig["c"], portConfig["sn"], portConfig["n"], portConfig["ec1"], portConfig["ec2"], refreshInterval, postDataInterval, portConfig["s"]["svt"], calc)); + } + } diff --git a/src/controller/Bridge.h b/src/controller/Bridge.h index b7508a2..00815ac 100644 --- a/src/controller/Bridge.h +++ b/src/controller/Bridge.h @@ -56,6 +56,7 @@ #include "output/display/DisplayOLED128.h" #include "output/display/DisplayST7735.h" #include "output/VisualisationHelper.h" +#include "../input/i2c/SensorMCP9600.h" #ifdef ESP8266_BOARD void initSSL(); diff --git a/src/input/i2c/Ads1x15.cpp b/src/input/i2c/Ads1x15.cpp index 432c886..7158e68 100644 --- a/src/input/i2c/Ads1x15.cpp +++ b/src/input/i2c/Ads1x15.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensatio/firmware-platformIO.git @section HISTORY + v47 - Fixed a bug that caused a crash if multiple ADS1115 were configured v46 - Merge of ESP8266 and ESP32 Firmware into PlatformIO project v34 - First Public Release (Feature parity with ESP8266 Release v34) */ @@ -39,6 +40,7 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin if ((addressString == NULL) || (addressString == "") || (addressString == "0x48")) { if (!init48) { + Serial.println("I48"); init48 = true; if(type == "ADS1115") @@ -54,10 +56,11 @@ if ((addressString == NULL) || (addressString == "") || (addressString == "0x48" else if (addressString == "0x49") { if (!init49) { + Serial.println("I49"); init49 = true; if(type == "ADS1115") - ads1x15_48 = new Adafruit_ADS1115(0x49); + ads1x15_49 = new Adafruit_ADS1115(0x49); else ads1x15_49 = new Adafruit_ADS1015(0x49); @@ -70,10 +73,11 @@ if ((addressString == NULL) || (addressString == "") || (addressString == "0x48" else if (addressString == "0x4A") { if (!init4A) { + Serial.println("I4A"); init4A = true; if(type == "ADS1115") - ads1x15_48 = new Adafruit_ADS1115(0x4A); + ads1x15_4A = new Adafruit_ADS1115(0x4A); else ads1x15_4A = new Adafruit_ADS1015(0x4A); @@ -85,10 +89,11 @@ if ((addressString == NULL) || (addressString == "") || (addressString == "0x48" else if (addressString == "0x4B") { if (!init4B) { + Serial.println("I4B"); init4B = true; if(type == "ADS1115") - ads1x15_48 = new Adafruit_ADS1115(0x4B); + ads1x15_4B = new Adafruit_ADS1115(0x4B); else ads1x15_4B = new Adafruit_ADS1015(0x4B); diff --git a/src/input/i2c/SensorMCP9600.cpp b/src/input/i2c/SensorMCP9600.cpp new file mode 100644 index 0000000..02f7f35 --- /dev/null +++ b/src/input/i2c/SensorMCP9600.cpp @@ -0,0 +1,251 @@ +/**************************************************************************/ +/*! + @file SensorMCP9600.h + @author M. Fegerl (Sensate Digital Solutions GmbH) + @license GPL (see LICENSE file) + The Sensatio firmware is used to connect ESP8266 and ESP32 based hardware + with the Sensatio Cloud and the Sensatio apps. + + ----> https://www.sensatio.io + + SOURCE: https://github.com/sensatio/firmware-platformIO.git + + @section HISTORY + v47 - Added Support for MCP9600 thermocouple sensors +*/ +/**************************************************************************/ + +#include "SensorMCP9600.h" +#include "Wire.h" +#include "Adafruit_MCP9600.h" + +extern boolean isResetting; +extern int powerMode; + +MCP9600* SensorMCP9600::mcp9600_60; +MCP9600* SensorMCP9600::mcp9600_66; +MCP9600* SensorMCP9600::mcp9600_67; + +SensorMCP9600::SensorMCP9600 (long id, String category, String shortName, String name, String i2cAdress, String sensorType, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { + + int i=0; + failedInit = false; + + Wire.setClock(100000); + + if(i2cAdress=="0x67") + { + if(mcp9600_67 == NULL) + { + mcp9600_67 = new MCP9600(); + + mcp9600_67->begin(0x67); + + while(!mcp9600_67->isConnected()) + { + mcp9600_67->begin(0x67, Wire); + Serial.println("Trying to find MCP9600 sensor at address 0x67!"); + delay(500); + + if(i==5) + { + Serial.println("Could not find a valid MCP9600 sensor at address 0x67, check wiring!"); + failedInit=true; + return; + } + i++; + } + + if(sensorType=="K") { + mcp9600_67->setThermocoupleType(TYPE_K); + } + else if(sensorType=="J") { + mcp9600_67->setThermocoupleType(TYPE_J); + } + else if(sensorType=="T") { + mcp9600_67->setThermocoupleType(TYPE_T); + } + else if(sensorType=="N") { + mcp9600_67->setThermocoupleType(TYPE_N); + } + else if(sensorType=="S") { + mcp9600_67->setThermocoupleType(TYPE_S); + } + else if(sensorType=="E") { + mcp9600_67->setThermocoupleType(TYPE_E); + } + else if(sensorType=="B") { + mcp9600_67->setThermocoupleType(TYPE_B); + } + else if(sensorType=="R") { + mcp9600_67->setThermocoupleType(TYPE_R); + } + + mcp9600_67->setShutdownMode(NORMAL); + + } + mcp = mcp9600_67; + } + else if(i2cAdress=="0x66") + { + if(mcp9600_66 == NULL) + { + mcp9600_66 = new MCP9600(); + + mcp9600_66->begin(0x66); + + while(!mcp9600_66->isConnected()) + { + mcp9600_67->begin(0x66, Wire); + Serial.println("Trying to find MCP9600 sensor at address 0x66!"); + delay(500); + + if(i==5) + { + Serial.println("Could not find a valid MCP9600 sensor at address 0x66, check wiring!"); + failedInit=true; + return; + } + i++; + } + + if(sensorType=="K") { + mcp9600_66->setThermocoupleType(TYPE_K); + } + else if(sensorType=="J") { + mcp9600_66->setThermocoupleType(TYPE_J); + } + else if(sensorType=="T") { + mcp9600_66->setThermocoupleType(TYPE_T); + } + else if(sensorType=="N") { + mcp9600_66->setThermocoupleType(TYPE_N); + } + else if(sensorType=="S") { + mcp9600_66->setThermocoupleType(TYPE_S); + } + else if(sensorType=="E") { + mcp9600_66->setThermocoupleType(TYPE_E); + } + else if(sensorType=="B") { + mcp9600_66->setThermocoupleType(TYPE_B); + } + else if(sensorType=="R") { + mcp9600_66->setThermocoupleType(TYPE_R); + } + + mcp9600_66->setShutdownMode(NORMAL); + + } + mcp = mcp9600_66; + } + else + { + if(mcp9600_60 == NULL) + { + mcp9600_60 = new MCP9600(); + + mcp9600_60->begin(); + + while(!mcp9600_60->isConnected()) + { + mcp9600_60->begin(0x60, Wire); + Serial.println("Trying to find MCP9600 sensor at address 0x60!"); + delay(500); + + if(i==5) + { + Serial.println("Could not find a valid MCP9600 sensor at address 0x60, check wiring!"); + failedInit=true; + return; + } + i++; + } + + if(sensorType=="K") { + mcp9600_60->setThermocoupleType(TYPE_K); + } + else if(sensorType=="J") { + mcp9600_60->setThermocoupleType(TYPE_J); + } + else if(sensorType=="T") { + mcp9600_60->setThermocoupleType(TYPE_T); + } + else if(sensorType=="N") { + mcp9600_60->setThermocoupleType(TYPE_N); + } + else if(sensorType=="S") { + mcp9600_60->setThermocoupleType(TYPE_S); + } + else if(sensorType=="E") { + mcp9600_60->setThermocoupleType(TYPE_E); + } + else if(sensorType=="B") { + mcp9600_60->setThermocoupleType(TYPE_B); + } + else if(sensorType=="R") { + mcp9600_60->setThermocoupleType(TYPE_R); + } + + mcp9600_60->setShutdownMode(NORMAL); + } + mcp = mcp9600_60; + } + +} + +void SensorMCP9600::preCycle(int cycleId) +{ +} + +Data* SensorMCP9600::read(bool shouldPostData) +{ + if(!isResetting && !failedInit) + { + if(_calculation->getValueUnit()=="°C") + { + float tempHotJunction = mcp->getThermocoupleTemp(); + shouldPostData = smartSensorCheck(tempHotJunction, _smartValueThreshold, shouldPostData); + return _calculation->calculate(this, tempHotJunction, shouldPostData); + } + else if(_calculation->getValueUnit()=="K") + { + float delta = mcp->getTempDelta(); + shouldPostData = smartSensorCheck(delta, _smartValueThreshold, shouldPostData); + return _calculation->calculate(this, delta, shouldPostData); + } + else if(_calculation->getValueType()=="pressure") + { + float tempColdJunction = mcp->getAmbientTemp(); + shouldPostData = smartSensorCheck(tempColdJunction, _smartValueThreshold, shouldPostData); + return _calculation->calculate(this, tempColdJunction, shouldPostData); + } + + } + return NULL; +} + +boolean SensorMCP9600::smartSensorCheck(float currentRawValue, float threshhold, boolean shouldPostData) +{ + if(powerMode == 3) + { + if(!shouldPostData) + { + if(!isnan(lastPostedValue)) + { + if(lastPostedValue-currentRawValue>threshhold || lastPostedValue-currentRawValue<-threshhold) + { + shouldPostData = true; + } + } + } + + if(shouldPostData) + lastPostedValue = currentRawValue; + } + + return shouldPostData; + +} + + diff --git a/src/input/i2c/SensorMCP9600.h b/src/input/i2c/SensorMCP9600.h new file mode 100644 index 0000000..a4995d1 --- /dev/null +++ b/src/input/i2c/SensorMCP9600.h @@ -0,0 +1,43 @@ +/**************************************************************************/ +/*! + @file SensorMCP9600.h + @author M. Fegerl (Sensate Digital Solutions GmbH) + @license GPL (see LICENSE file) + The Sensatio firmware is used to connect ESP8266 and ESP32 based hardware + with the Sensatio Cloud and the Sensatio apps. + + ----> https://www.sensatio.io + + SOURCE: https://github.com/sensatio/firmware-platformIO.git + + @section HISTORY + v47 - Added Support for MCP9600 thermocouple sensors +*/ +/**************************************************************************/ + +#include + +#ifndef _SensorMCP9600_h_ +#define _SensorMCP9600_h_ + +#include "../Sensor.h" + +class SensorMCP9600 : public Sensor { + private: + static MCP9600 *mcp9600_60; + static MCP9600 *mcp9600_66; + static MCP9600 *mcp9600_67; + MCP9600* mcp; + boolean failedInit; + float lastPostedValue = NAN; + protected: + Data* read(bool); + void preCycle(int); + boolean smartSensorCheck(float, float, boolean); + public: + SensorMCP9600 (long, String, String, String, String, String, int, int, float, SensorCalculation*); +}; + + + +#endif /* _SensorMCP9600_h_ */