From 62124cb22d04960cfc89ede79cfb97b22ecad745 Mon Sep 17 00:00:00 2001 From: andreas Date: Thu, 26 Oct 2023 21:44:47 +0200 Subject: [PATCH] 1st step for M5 ENV3 iic module support --- lib/iictask/GwIicTask.cpp | 73 ++++++++++++++++++++++++++++++++++++- lib/iictask/SHT3X.cpp | 45 +++++++++++++++++++++++ lib/iictask/SHT3X.h | 26 ++++++++++++++ lib/iictask/config.json | 74 ++++++++++++++++++++++++++++++++++++++ lib/iictask/platformio.ini | 12 +++++++ 5 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 lib/iictask/SHT3X.cpp create mode 100644 lib/iictask/SHT3X.h create mode 100644 lib/iictask/config.json create mode 100644 lib/iictask/platformio.ini diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index 5d7e7d0..9801e1e 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -2,6 +2,33 @@ #include "GwIicTask.h" #include "GwHardware.h" #include +#include +#include "GwTimer.h" +#include "N2kMessages.h" +#define GWSHT3X -1 + +class SHT3XConfig{ + public: + String tempTransducer; + String humidTransducer; + int iid; + bool tempActive; + bool humidActive; + long interval; + tN2kHumiditySource humiditySource; + tN2kTempSource tempSource; + SHT3XConfig(GwConfigHandler *config){ + tempTransducer=config->getString(GwConfigDefinitions::SHT3XTempName); + humidTransducer=config->getString(GwConfigDefinitions::SHT3XHumidName); + iid=config->getInt(GwConfigDefinitions::SHT3Xiid,99); + tempActive=config->getBool(GwConfigDefinitions::iicSHT3XTemp); + humidActive=config->getBool(GwConfigDefinitions::iicSHT3XHumid); + interval=config->getInt(GwConfigDefinitions::SHT3Xinterval); + interval*=1000; + humiditySource=N2khs_InsideHumidity; + tempSource=N2kts_InsideTemperature; + } +}; void runIicTask(GwApi *api){ GwLog *logger=api->getLogger(); #ifndef _GWIIC @@ -22,6 +49,50 @@ void runIicTask(GwApi *api){ vTaskDelete(NULL); return; } - vTaskDelete(NULL); + GwConfigHandler *config=api->getConfig(); + SHT3X *sht3x=nullptr; + bool runLoop=false; + GwIntervalRunner timers; + #ifdef GWSHT3X + int8_t addr=GWSHT3X; + if (addr < 0) addr=0x44; //default + SHT3XConfig sht3xConfig(config); + if (sht3xConfig.humidActive || sht3xConfig.tempActive){ + sht3x=new SHT3X(); + sht3x->init(addr,&Wire); + LOG_DEBUG(GwLog::LOG,"initialized SHT3X at address %d",(int)addr); + runLoop=true; + timers.addAction(sht3xConfig.interval,[logger,api,sht3x,sht3xConfig](){ + int rt=0; + if ((rt=sht3x->get())==0){ + double temp=sht3x->cTemp; + temp=CToKelvin(temp); + double humid=sht3x->humidity; + LOG_DEBUG(GwLog::DEBUG,"SHT3X measure temp=%2.1f, humid=%2.0f",(float)temp,(float)humid); + tN2kMsg msg; + if (sht3xConfig.humidActive){ + SetN2kHumidity(msg,1,sht3xConfig.iid,sht3xConfig.humiditySource,humid); + api->sendN2kMessage(msg); + } + if (sht3xConfig.tempActive){ + SetN2kTemperature(msg,1,sht3xConfig.iid,sht3xConfig.tempSource,temp); + api->sendN2kMessage(msg); + } + } + else{ + LOG_DEBUG(GwLog::DEBUG,"unable to query SHT3X: %d",rt); + } + }); + } + #endif + if (! runLoop){ + LOG_DEBUG(GwLog::LOG,"nothing to do for IIC task, finish"); + vTaskDelete(NULL); + return; + } + while(true){ + delay(100); + timers.loop(); + } } //#endif diff --git a/lib/iictask/SHT3X.cpp b/lib/iictask/SHT3X.cpp new file mode 100644 index 0000000..194d7fa --- /dev/null +++ b/lib/iictask/SHT3X.cpp @@ -0,0 +1,45 @@ +#include "SHT3X.h" + +bool SHT3X::init(uint8_t slave_addr_in, TwoWire* wire_in) +{ + _wire = wire_in; + _address=slave_addr_in; + return true; +} + +byte SHT3X::get() +{ + unsigned int data[6]; + + // Start I2C Transmission + _wire->beginTransmission(_address); + // Send measurement command + _wire->write(0x2C); + _wire->write(0x06); + // Stop I2C transmission + if (_wire->endTransmission()!=0) + return 1; + + delay(200); + + // Request 6 bytes of data + _wire->requestFrom(_address, (uint8_t) 6); + + // Read 6 bytes of data + // cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc + for (int i=0;i<6;i++) { + data[i]=_wire->read(); + }; + + delay(50); + + if (_wire->available()!=0) + return 2; + + // Convert the data + cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45; + fTemp = (cTemp * 1.8) + 32; + humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0); + + return 0; +} diff --git a/lib/iictask/SHT3X.h b/lib/iictask/SHT3X.h new file mode 100644 index 0000000..bee5812 --- /dev/null +++ b/lib/iictask/SHT3X.h @@ -0,0 +1,26 @@ +#ifndef __SHT3X_H +#define __HT3X_H + + +#if ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "Wire.h" + +class SHT3X{ +public: + bool init(uint8_t slave_addr_in=0x44, TwoWire* wire_in = &Wire); + byte get(void); + float cTemp=0; + float fTemp=0; + float humidity=0; + +private: + uint8_t _address; + TwoWire* _wire; +}; + +#endif diff --git a/lib/iictask/config.json b/lib/iictask/config.json new file mode 100644 index 0000000..b1d3037 --- /dev/null +++ b/lib/iictask/config.json @@ -0,0 +1,74 @@ +[ + { + "name": "iicSHT3XTemp", + "label": "SHT3X Temperature", + "type": "boolen", + "default": true, + "description": "Enable the I2C SHT3x temp sensor", + "category": "sensors", + "capabilities": { + "SHT3X":"true" + } + }, + { + "name": "iicSHT3XHumid", + "label": "SHT3X Humidity", + "type": "boolean", + "default": true, + "description": "Enable the I2C SHT3x humidity sensor", + "category": "sensors", + "capabilities": { + "SHT3X":"true" + } + }, + { + "name": "SHT3XTempName", + "label": "SHT3X Temperature XDR", + "type": "String", + "default": "Temp", + "description": "set the XDR transducer name for the SHT3X Temperature, leave empty to disable ", + "category": "sensors", + "capabilities": { + "SHT3X":"true" + } + }, + { + "name": "SHT3XHumidName", + "label": "SHT3X Humidity XDR", + "type": "String", + "default": "Humidity", + "description": "set the XDR transducer name for the SHT3X Humidity, leave empty to disable ", + "category": "sensors", + "capabilities": { + "SHT3X":"true" + } + }, + { + "name": "SHT3Xiid", + "label": "SHT3X N2K instance id", + "type": "number", + "default": 99, + "description": "the N2K instance id the SHT3X Temperature and Humidity ", + "category": "sensors", + "min": 0, + "max": 255, + "check": "checkMinMax", + "capabilities": { + "SHT3X":"true" + } + }, + { + "name": "SHT3Xinterval", + "label": "SHT3X Interval", + "type": "number", + "default": 2, + "description": "Interval(s) to query SHT3X Temperature and Humidity (1...300)", + "category": "sensors", + "min": 1, + "max": 300, + "check": "checkMinMax", + "capabilities": { + "SHT3X":"true" + } + } +] diff --git a/lib/iictask/platformio.ini b/lib/iictask/platformio.ini new file mode 100644 index 0000000..e4ebff3 --- /dev/null +++ b/lib/iictask/platformio.ini @@ -0,0 +1,12 @@ +[platformio] + +[env:testenv3] +board = m5stack-atom +lib_deps = + ${env.lib_deps} +build_flags= + -D M5_ENV3 + -D M5_CAN_KIT + ${env.build_flags} +upload_port = /dev/esp32 +upload_protocol = esptool \ No newline at end of file