add support for SHT31 sensor (untested), Add PWM frequency config in system settings

This commit is contained in:
DeltaLima 2024-12-08 23:45:47 +01:00
parent 5df2d0f4ab
commit 9585bec612
7 changed files with 121 additions and 21 deletions

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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) {

View file

@ -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();

View file

@ -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";

View file

@ -640,7 +640,7 @@ void WEBsystemSettings() {
// SoilmoistureLow byte
body += "Soilmoisture low: <input class='inputShort' type='number' name='SoilmoistureLow' min='0' value='";
body += SoilmoistureLow;
body += "' required> %<br>\n";
body += "' required> %<br>\n";
// SoilmoistureWet byte
body += "Soilmoisture wet: <input type='number' id='iSoilmoistureWet' name='SoilmoistureWet' min='0' value='";
@ -668,6 +668,16 @@ void WEBsystemSettings() {
}
body += "<option value='1'" + returnStrSelected(TemperatureSensor_Type, 1) + ">I2C BME280</option>\n";
body += "<option value='2'" + returnStrSelected(TemperatureSensor_Type, 2) + ">I2C Chirp</option>\n";
body += "<option value='2'" + returnStrSelected(TemperatureSensor_Type, 3) + ">I2C SHT31</option>\n";
body += "</select><br>\n";
// HumiditySensor_Type byte
body += "Humidity sensor: <select id='HumiditySensor_Type' name='HumiditySensor_Type' required>\n";
if(configured == false) {
body += "<option disabled value='' selected hidden>---</option>\n";
}
body += "<option value='1'" + returnStrSelected(HumiditySensor_Type, 1) + ">I2C BME280</option>\n";
body += "<option value='2'" + returnStrSelected(HumiditySensor_Type, 2) + ">I2C SHT31</option>\n";
body += "</select><br>\n";
// NtpOffset int
@ -679,6 +689,10 @@ void WEBsystemSettings() {
body += MaintenanceDuration;
body += "' required> Seconds<br>\n";
// PWMFrequency short
body += "PWM Frequency: <input type='number' name='PWMFrequency' min='0' max='20000' value='";
body += PWMFrequency;
body += "' required> Hz<br>\n";
body += "ESP32-Cam IP (optional): <input type='text' name='Esp32CamIP' maxlength='16' value='";
body += Esp32CamIP;
@ -864,6 +878,8 @@ void POSTsystemSettings() {
unsigned short UseLEDrelais_old = UseLEDrelais;
unsigned short UseFANrelais_old = UseFANrelais;
unsigned short PWMFrequency_old = PWMFrequency;
NtpOffset = webserver.arg("NtpOffset").toInt();
MoistureSensor_Type = webserver.arg("MoistureSensor_Type").toInt();
SoilmoistureLow = webserver.arg("SoilmoistureLow").toInt();
@ -879,8 +895,16 @@ void POSTsystemSettings() {
OutputInvert = webserver.arg("OutputInvert").toInt();
SoilmoistureWet = webserver.arg("SoilmoistureWet").toInt();
SoilmoistureDry = webserver.arg("SoilmoistureDry").toInt();
HumiditySensor_Type = webserver.arg("HumiditySensor_Type").toInt();
PWMFrequency = webserver.arg("PWMFrequency").toInt();
configured = true;
// when configured is false, set it to true and ensure outputs are set
if(configured == false) {
configured = true;
pinMode(PinLED, OUTPUT);
pinMode(PinPUMP, OUTPUT);
pinMode(PinFAN, OUTPUT);
}
// size is 1 byte
EEPROM.put(161, configured);
@ -907,6 +931,8 @@ void POSTsystemSettings() {
EEPROM.put(243, OutputInvert);
EEPROM.put(244, SoilmoistureWet);
EEPROM.put(246, SoilmoistureDry);
EEPROM.put(249, HumiditySensor_Type);
EEPROM.put(250, PWMFrequency);
// write data to EEPROM
EEPROM.commit();
@ -935,6 +961,11 @@ void POSTsystemSettings() {
Serial.println("UseFANrelais is 0, forcing PinFANPWM to max to prevent relais damage and ensure its turned on");
}
if(PWMFrequency != PWMFrequency_old) {
// if PWM freq changed, apply new settings
analogWriteFreq(PWMFrequency);
}
Serial.print("configured: ");
Serial.println(configured);
Serial.print("UseFan: ");
@ -1171,7 +1202,7 @@ void APIgetDebug() {
// BME280
objSensors["bme280"]["temperature"] = getTemperature(1);
objSensors["bme280"]["humidity"] = getHumidity();
objSensors["bme280"]["humidity"] = getHumidity(1);
objSensors["bme280"]["preassure"] = bme.readPressure() / 100.0F;
objSensors["bme280"]["appAltitude"] = bme.readAltitude(SEALEVELPRESSURE_HPA);

View file

@ -33,7 +33,7 @@ test -z $1 && help
case $1 in
s|setup)
ACLI_DIR="$(dirname $ACLI)"
declare -a LIBS=( "Adafruit SSD1306" "Adafruit BME280 Library" "ArduinoJson" "NTPClient" "Time" )
declare -a LIBS=( "Adafruit SSD1306" "Adafruit BME280 Library" "ArduinoJson" "NTPClient" "Time" "Adafruit SHT31 Library" )
echo ":: Setting up build environment for CanGrow Firmware."
echo " This will download the binary for arduino-cli and install"
echo " the packages for the arduino ide from the debian repository."