205 lines
4.5 KiB
C
205 lines
4.5 KiB
C
/*
|
|
* DHT Stuff
|
|
*
|
|
*/
|
|
// DHT support dropped to get a free pin for fan PWM
|
|
//#define DHTTYPE DHT11
|
|
//DHT dht(PINdht, DHTTYPE);
|
|
|
|
/*
|
|
* BME280 Stuff
|
|
*
|
|
*/
|
|
|
|
#define SEALEVELPRESSURE_HPA (1013.25)
|
|
Adafruit_BME280 bme;
|
|
|
|
|
|
/*
|
|
* Chirp functions
|
|
*/
|
|
void writeI2CRegister8bit(int addr, int value) {
|
|
Wire.beginTransmission(addr);
|
|
Wire.write(value);
|
|
Wire.endTransmission();
|
|
}
|
|
|
|
unsigned int readI2CRegister16bit(int addr, int reg) {
|
|
Wire.beginTransmission(addr);
|
|
Wire.write(reg);
|
|
Wire.endTransmission();
|
|
delay(20);
|
|
Wire.requestFrom(addr, 2);
|
|
unsigned int t = Wire.read() << 8;
|
|
t = t | Wire.read();
|
|
return t;
|
|
}
|
|
|
|
/*
|
|
*
|
|
* Sensor functions
|
|
*
|
|
*/
|
|
|
|
int getWaterlevel(bool returnRAW = false) {
|
|
|
|
/*
|
|
* waterlevelRAW
|
|
* ===========
|
|
* 0 - 199 : CRITICAL
|
|
* 200 - 399 : WARNING
|
|
* >400 : OK
|
|
*
|
|
* waterlevel
|
|
* ==========
|
|
* 2 : CRITICAL
|
|
* 1 : WARNING
|
|
* 0 : OK
|
|
*/
|
|
|
|
short waterlevelWARN = 200;
|
|
short waterlevelOK = 400;
|
|
short waterlevelRAW = 0;
|
|
byte waterlevel = 0;
|
|
|
|
// disable first PINsoilmoisture
|
|
digitalWrite(PINsoilmoisture, LOW);
|
|
// enable Vcc for water level sensor
|
|
digitalWrite(PINwaterlevel, HIGH);
|
|
// wait a bit to let the circuit stabilize
|
|
// TODO: replace delay() with millis()
|
|
delay(100);
|
|
// get the value
|
|
|
|
for(byte i = 0; i < 10 ; i++) {
|
|
waterlevelRAW = waterlevelRAW + analogRead(PINanalog);
|
|
}
|
|
waterlevelRAW = waterlevelRAW / 10;
|
|
|
|
// disable Vcc for the sensor to prevent electrolysis effect and release analog pin
|
|
digitalWrite(PINwaterlevel, LOW);
|
|
// and turn soilmoisture back on
|
|
digitalWrite(PINsoilmoisture, HIGH);
|
|
if( waterlevelRAW >= waterlevelOK) {
|
|
waterlevel = 0;
|
|
} else if( waterlevelRAW >= waterlevelWARN) {
|
|
waterlevel = 1;
|
|
} else {
|
|
waterlevel = 2;
|
|
}
|
|
|
|
return waterlevel;
|
|
}
|
|
|
|
float getTemperature(byte tempSensor) {
|
|
/*
|
|
* tempSensor
|
|
* ==========
|
|
* 1 : DHT11 temp sensor
|
|
* 2 : chirp I2C temp sensor
|
|
*/
|
|
|
|
float temperature = 0;
|
|
|
|
switch(tempSensor) {
|
|
case 1:
|
|
// read temperature from BME280
|
|
temperature = bme.readTemperature();
|
|
// read temperature from DHT11
|
|
// dht support dropped
|
|
// temperature = dht.readTemperature();
|
|
break;
|
|
case 2:
|
|
// read temperature from chrip I2C
|
|
temperature = readI2CRegister16bit(0x20, 5) * 0.10 ;
|
|
break;
|
|
default:
|
|
// if sensor type is not recognized, return 99
|
|
temperature = 99.99;
|
|
}
|
|
|
|
return temperature;
|
|
}
|
|
|
|
float getHumidity() {
|
|
// dht support dropped
|
|
// return dht.readHumidity();
|
|
return bme.readHumidity();
|
|
}
|
|
|
|
int getSoilmoisture(byte moistureSensor, bool returnRAW = false) {
|
|
/*
|
|
* moistureSensor
|
|
* ==============
|
|
* 1 : analog capacitive moisture sensor
|
|
* 2 : chirp I2C moisture sensor
|
|
*/
|
|
|
|
// value to return
|
|
int soilmoisture = 0;
|
|
// value for wet
|
|
//~ int wet;
|
|
// value for dry
|
|
//~ int dry;
|
|
|
|
switch(moistureSensor) {
|
|
case 1:
|
|
// read analog value from analog moisture sensor
|
|
//~ wet = 180;
|
|
// this value was measured in air, without contact to anything
|
|
//dry= 590;
|
|
|
|
// was measured in dry soil, not bone dry but really dry (6 days no watering)
|
|
//~ dry = 360;
|
|
|
|
digitalWrite(PINsoilmoisture, HIGH);
|
|
// wait a bit to let the circuit stabilize
|
|
delay(50);
|
|
|
|
// get analog input value
|
|
// get values 10 times and get the middle for more precise data
|
|
for(byte i = 0; i < 10 ; i++) {
|
|
soilmoisture = soilmoisture + analogRead(PINanalog);
|
|
}
|
|
soilmoisture = soilmoisture / 10;
|
|
|
|
// disable Vcc for the sensor to release analog pin
|
|
digitalWrite(PINsoilmoisture, LOW);
|
|
break;
|
|
case 2:
|
|
// read soil moisture from chrip I2C
|
|
// this value was measured in water
|
|
// wet = 560;
|
|
|
|
// measured in fresh watered soil
|
|
//~ wet = 485;
|
|
//~ dry= 250;
|
|
|
|
// get raw value from I2C chirp sensor
|
|
soilmoisture = readI2CRegister16bit(0x20, 0);
|
|
break;
|
|
default:
|
|
//~ wet = 0;
|
|
//~ dry = 1;
|
|
soilmoisture = -1;
|
|
}
|
|
|
|
if(returnRAW == true) {
|
|
return soilmoisture;
|
|
} else {
|
|
short soilmoistureP = map(soilmoisture, SoilmoistureWet, SoilmoistureDry, 100, 0);
|
|
// dont return negative percentage values
|
|
if(soilmoistureP < 0) {
|
|
return 0;
|
|
} else {
|
|
return soilmoistureP;
|
|
}
|
|
}
|
|
}
|
|
|
|
int getLightchirp() {
|
|
// get the "light value" from I2C chirp module
|
|
writeI2CRegister8bit(0x20, 3); //request light measurement
|
|
int lightchirp = readI2CRegister16bit(0x20, 4);
|
|
return lightchirp;
|
|
}
|