From 9585bec612bf13a52abeae7e8f7ab5b1b265b1a4 Mon Sep 17 00:00:00 2001 From: DeltaLima Date: Sun, 8 Dec 2024 23:45:47 +0100 Subject: [PATCH] add support for SHT31 sensor (untested), Add PWM frequency config in system settings --- Arduino/CanGrow/CanGrow.ino | 36 +++++++++++++++++----- Arduino/CanGrow/CanGrow_Init.h | 9 ++++-- Arduino/CanGrow/CanGrow_Sensors.h | 41 +++++++++++++++++++++++--- Arduino/CanGrow/CanGrow_SysFunctions.h | 15 ++++++++-- Arduino/CanGrow/CanGrow_Version.h | 2 +- Arduino/CanGrow/CanGrow_WebFunctions.h | 37 +++++++++++++++++++++-- cangrow.sh | 2 +- 7 files changed, 121 insertions(+), 21 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 4f969e6..0597d8f 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -38,6 +38,11 @@ // DHT support dropped // https://github.com/adafruit/DHT-sensor-library // #include "DHT.h" +// SHT30/31 +// https://github.com/adafruit/Adafruit_SHT31/ +#include "Adafruit_SHT31.h" + + /* * CanGrow header files @@ -80,9 +85,6 @@ void setup() { // set FAN2 to off with digitalWrite LOW analogWrite(PinFAN2, PinFAN2PWM); - // set PWM frequency to 13.37KHz - analogWriteFreq(13370); - // Start EEPROM EEPROM.begin(512); @@ -132,6 +134,22 @@ void setup() { Serial.println("!! Cannot find BME280 on I2C bus. Please check connection or ID"); } + // initialise SHT31 + Serial.println(":: initialise SHT31 sensor ::"); + if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr + Serial.println("Couldn't find SHT31"); + } + + Serial.print("SHT31 Heater Enabled State: "); + if (sht31.isHeaterEnabled()) + Serial.println("ENABLED"); + else + Serial.println("DISABLED"); + + + + + Serial.println("To wipe the EEPROM saved data, set D4 (PinWIPE) to LOW - NOW! (2 seconds left)"); // wait a few seconds to let the user pull D4 down to wipe EEPROM // and we can enjoy the boot screen meanwhile :p @@ -198,12 +216,16 @@ void setup() { display.display(); } - // at the end of setup, set the outputs + // at the end of setup, set the outputs, when configured true // we do this here because otherwise on inverted // boards like CanGrow PCB v0.6 it would be turned on - pinMode(PinLED, OUTPUT); - pinMode(PinPUMP, OUTPUT); - pinMode(PinFAN, OUTPUT); + if(configured == true) { + pinMode(PinLED, OUTPUT); + pinMode(PinPUMP, OUTPUT); + pinMode(PinFAN, OUTPUT); + // set PWM frequency + analogWriteFreq(PWMFrequency); + } } diff --git a/Arduino/CanGrow/CanGrow_Init.h b/Arduino/CanGrow/CanGrow_Init.h index 4632880..41a377a 100644 --- a/Arduino/CanGrow/CanGrow_Init.h +++ b/Arduino/CanGrow/CanGrow_Init.h @@ -105,6 +105,8 @@ bool UseLEDrelais; bool UseFANrelais; // Which temperature sensor to use? byte TemperatureSensor_Type; +// which humidity sensor to use +byte HumiditySensor_Type; unsigned short MaintenanceDuration = 300; char Esp32CamIP[16]; // PumpLastOn (long) timestamp @@ -112,6 +114,7 @@ unsigned long PumpLastOn; bool OutputInvert; unsigned short SoilmoistureWet; unsigned short SoilmoistureDry; +unsigned short PWMFrequency = 13370; @@ -141,9 +144,9 @@ byte SunriseHour = 7; byte SunriseMinute = 0; // PinLEDPWM - contains the PWM value for dimming the grow light // default is 255 to ensure it is just on for the case UseLEDrelais is true -byte PinLEDPWM = 0; -byte PinFANPWM = 0; -byte PinFAN2PWM = 0; +byte PinLEDPWM = 255; +byte PinFANPWM = 255; +byte PinFAN2PWM = 255; // fade in and out sunrise and sunset? bool SunFade; diff --git a/Arduino/CanGrow/CanGrow_Sensors.h b/Arduino/CanGrow/CanGrow_Sensors.h index c41f101..05d3034 100644 --- a/Arduino/CanGrow/CanGrow_Sensors.h +++ b/Arduino/CanGrow/CanGrow_Sensors.h @@ -14,7 +14,15 @@ #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; - +/* + * SHT30/31 Stuff + * + */ + + bool enableHeater = false; + Adafruit_SHT31 sht31 = Adafruit_SHT31(); + + /* * Chirp functions */ @@ -97,6 +105,7 @@ float getTemperature(byte tempSensor) { * ========== * 1 : DHT11 temp sensor * 2 : chirp I2C temp sensor + * 3 : SHT31 */ float temperature = 0; @@ -113,6 +122,10 @@ float getTemperature(byte tempSensor) { // read temperature from chrip I2C temperature = readI2CRegister16bit(0x20, 5) * 0.10 ; break; + case 3: + // read temp from SHT31 + temperature = sht31.readTemperature(); + break; default: // if sensor type is not recognized, return 99 temperature = 99.99; @@ -121,10 +134,30 @@ float getTemperature(byte tempSensor) { return temperature; } -float getHumidity() { - // dht support dropped +float getHumidity(byte HumSensor) { + + /* + * sensors: + * 1: BME280 + * 2: SHT31 + * + */ + float humidity; + + switch(HumSensor) { + case 1: + humidity = bme.readHumidity(); + break; + case 2: + humidity = sht31.readHumidity(); + break; + default: + humidity = 0.0; + break; + } + // return dht.readHumidity(); - return bme.readHumidity(); + return humidity; } int getSoilmoisture(byte moistureSensor, bool returnRAW = false) { diff --git a/Arduino/CanGrow/CanGrow_SysFunctions.h b/Arduino/CanGrow/CanGrow_SysFunctions.h index 9af08f6..f2531c9 100644 --- a/Arduino/CanGrow/CanGrow_SysFunctions.h +++ b/Arduino/CanGrow/CanGrow_SysFunctions.h @@ -111,7 +111,9 @@ bool loadEEPROM() { * 244 SoilmoistureWet (2 byte) * 246 SoilmoistureDry (2 byte) * 248 PinFAN2PWM (1 byte) - * 249 ... + * 249 HumiditySensor_Type (1 byte) + * 250 PWMFrequency (2 byte) + * 252 ... * */ @@ -183,6 +185,8 @@ bool loadEEPROM() { EEPROM.get(244, SoilmoistureWet); // size is 1 byte EEPROM.get(246, SoilmoistureDry); + // size is 1 byte + EEPROM.get(249, HumiditySensor_Type); } // TODO auth does not work atm // EEPROM.get(160, WebUiUsername); @@ -223,6 +227,8 @@ bool loadEEPROM() { EEPROM.get(242, PumpIntervalBloom); // size is 1 byte EEPROM.get(248, PinFAN2PWM); + // size is 2 byte + EEPROM.get(250, PWMFrequency); } @@ -269,6 +275,11 @@ bool loadEEPROM() { Serial.println(SoilmoistureWet); Serial.print("SoilmoistureDry: "); Serial.println(SoilmoistureDry); + Serial.print("HumiditySensor_Type: "); + Serial.println(HumiditySensor_Type); + Serial.print("PWMFrequency: "); + Serial.println(PWMFrequency); + Serial.println("---- Grow values ----"); Serial.print("GrowName: "); @@ -547,7 +558,7 @@ void refreshSensors() { byte soilmoistureAvgSampleCount = 5; valSoilmoisture = getSoilmoisture(MoistureSensor_Type); - valHumidity = getHumidity(); + valHumidity = getHumidity(HumiditySensor_Type); valTemperature = getTemperature(TemperatureSensor_Type); valWaterlevel = getWaterlevel(); diff --git a/Arduino/CanGrow/CanGrow_Version.h b/Arduino/CanGrow/CanGrow_Version.h index b152786..07ba194 100644 --- a/Arduino/CanGrow/CanGrow_Version.h +++ b/Arduino/CanGrow/CanGrow_Version.h @@ -1,5 +1,5 @@ /* CanGrow_Version.h gets generated from cangrow.sh */ const char* CanGrowVer = "0.1-dev"; -const char* CanGrowBuild = "61c7af5-20241208140924"; +const char* CanGrowBuild = "5df2d0f-20241208233808"; diff --git a/Arduino/CanGrow/CanGrow_WebFunctions.h b/Arduino/CanGrow/CanGrow_WebFunctions.h index 07e2c4a..8ec05bd 100644 --- a/Arduino/CanGrow/CanGrow_WebFunctions.h +++ b/Arduino/CanGrow/CanGrow_WebFunctions.h @@ -640,7 +640,7 @@ void WEBsystemSettings() { // SoilmoistureLow byte body += "Soilmoisture low: %
\n"; + body += "' required> %
\n"; // SoilmoistureWet byte body += "Soilmoisture wet: I2C BME280\n"; body += "\n"; + body += "\n"; + body += "
\n"; + + // HumiditySensor_Type byte + body += "Humidity sensor:
\n"; // NtpOffset int @@ -679,6 +689,10 @@ void WEBsystemSettings() { body += MaintenanceDuration; body += "' required> Seconds
\n"; + // PWMFrequency short + body += "PWM Frequency: Hz
\n"; body += "ESP32-Cam IP (optional):