replaced HC-SR04 with SR04M-2

This commit is contained in:
calculator 2020-11-18 22:11:37 +01:00
parent cc277bdc93
commit 48756874e5
2 changed files with 31 additions and 14 deletions

View File

@ -89,10 +89,6 @@
#define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */ #define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */
#define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */ #define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */
#define HC_SR04 /**< Ultrasonic distance sensor to measure water level */
#define SENSOR_SR04_ECHO 17 /**< GPIO 17 - Echo */
#define SENSOR_SR04_TRIG 23 /**< GPIO 23 - Trigger */
#define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */ #define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */
#define PANIK_MODE_DEEPSLEEP (60 * 60 * 5U) /**< 5 hours in usecond */ #define PANIK_MODE_DEEPSLEEP (60 * 60 * 5U) /**< 5 hours in usecond */

View File

@ -112,6 +112,30 @@ long getLastMoisture(int plantId)
} }
} }
long getDistance(){
unsigned int distance;
byte startByte, h_data, l_data, sum = 0;
byte buf[3];
startByte = (byte)Serial.read();
if(startByte == 255){
Serial.readBytes(buf, 3);
h_data = buf[0];
l_data = buf[1];
sum = buf[2];
distance = (h_data<<8) + l_data;
if(((startByte + h_data + l_data)&0xFF) != sum){
return -1;
}
else{
return distance;
}
} else {
return -2;
}
}
void readSystemSensors() void readSystemSensors()
{ {
for (int i=0; i < 5; i++) { for (int i=0; i < 5; i++) {
@ -420,16 +444,13 @@ bool readSensors()
/* Use the Ultrasonic sensor to measure waterLevel */ /* Use the Ultrasonic sensor to measure waterLevel */
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
digitalWrite(SENSOR_SR04_TRIG, LOW); while(!Serial.available()){}
delayMicroseconds(2); unsigned int distance = getDistance();
digitalWrite(SENSOR_SR04_TRIG, HIGH); if(distance > 0){
delayMicroseconds(10); waterRawSensor.add(distance);
digitalWrite(SENSOR_SR04_TRIG, LOW); }
float duration = pulseIn(SENSOR_SR04_ECHO, HIGH);
waterRawSensor.add((duration * .343) / 2);
Serial << "Distance sensor " << duration << " ms : " << waterRawSensor.getAverage() << " cm" << endl;
delay(20);
} }
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
/* deactivate the sensors */ /* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW); digitalWrite(OUTPUT_SENSOR, LOW);
return leaveMode1; return leaveMode1;
@ -705,7 +726,7 @@ void mode2()
*/ */
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(9600);
Serial.setTimeout(1000); // Set timeout of 1 second Serial.setTimeout(1000); // Set timeout of 1 second
Serial << endl Serial << endl
<< endl; << endl;