Hallo,
Ich baue gerade an einem Projekt mit einem Arduino UNO board. Es geht um eine Bewässerungsanlage für Topfpflanzen.
Ich nutze dazu ein RTC Modul um alle 24h Feuchtigkeitswerte mit Sensoren zu messen und dann ggf. eine Pumpe zu aktivieren.
Der Code soll wie folgt funktionieren:
Sind wieder 24h vorbei?
- Grüner Sensor an
- 2 Sekunden warten (mit millis)
- Grüner Sensor auslesen
- Wert an Pumpe übergeben
Boden trocken?
- Grüne Pumpe an
- Warte eine Sekunde
- Grüne Pumpe aus
Dann das gleiche mit blauem und weißem Sensor wiederholen.
Der Code sieht wie folgt aus:
// Arduino soil moisture sensor and real time clock
// This sketch works as an self watering flower pot
// https://iotspace.dev/arduino-bodenfeuchtesensoren---anleitung-und-sketch
//
#include "RTClib.h"
#include <Wire.h>
// Configuration, setup moisture threshold and checking time
// high number means dry soil
const int MOISTURE_THRESHOLD = 300;
// Intervall check
const long INTERVALL_PUMP_CHECK = 60;
// wait for the sensor before measuring
const long SENSOR_WAIT_TIME = 2000;
// pump time in ms, determines how much water will flow
const long PUMP_INTERVAL = 1000;
// Timer variable
long lastPumpCheckCycle = 0;
// Clock
RTC_DS3231 rtc;
// declare pins for power for sensor and pump
int GREEN_SENSOR = 6;
int GREEN_PUMP = 7;
int BLUE_SENSOR = 8;
int BLUE_PUMP = 9;
int WHITE_SENSOR = 10;
int WHITE_PUMP = 11;
// Analog pins
int GREEN_MOISTURE = A0;
int BLUE_MOISTURE = A1;
int WHITE_MOISTURE = A2;
void setup() {
// serial communication with 9600 baud rate
Serial.begin(1000000);
#ifndef ESP8266
while (!Serial)
; // wait for serial port to connect. Needed for native USB
#endif
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
Serial.println("Setting the time...");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// set up power pins for sensor and pump
pinMode(GREEN_SENSOR, OUTPUT);
digitalWrite(GREEN_SENSOR, HIGH);
pinMode(GREEN_PUMP, OUTPUT);
digitalWrite(GREEN_PUMP, HIGH);
pinMode(BLUE_SENSOR, OUTPUT);
digitalWrite(BLUE_SENSOR, HIGH);
pinMode(BLUE_PUMP, OUTPUT);
digitalWrite(BLUE_PUMP, HIGH);
pinMode(WHITE_SENSOR, OUTPUT);
digitalWrite(WHITE_SENSOR, HIGH);
pinMode(WHITE_PUMP, OUTPUT);
digitalWrite(WHITE_PUMP, HIGH);
// set up pin to measure moisture
pinMode(GREEN_MOISTURE, INPUT);
pinMode(BLUE_MOISTURE, INPUT);
pinMode(WHITE_MOISTURE, INPUT);
delay(500);
}
void loop() {
long nowTime = rtc.now().secondstime();
long difference = nowTime - lastPumpCheckCycle;
// Check if the current time is the predetermined check time
// If so, execute the checkMoisture function
if (lastPumpCheckCycle == 0 || difference > INTERVALL_PUMP_CHECK) {
Serial.println("Check moisture");
lastPumpCheckCycle = nowTime;
// Measure green sensor
int greenMoisture = checkMoisture(GREEN_SENSOR, GREEN_MOISTURE, "green");
startPump(GREEN_PUMP, greenMoisture, "green");
// Measure blue sensor
int blueMoisture = checkMoisture(BLUE_SENSOR, BLUE_MOISTURE, "blue");
startPump(BLUE_PUMP, blueMoisture, "blue");
// Measure white sensor
int whiteMoisture = checkMoisture(WHITE_SENSOR, WHITE_MOISTURE, "white");
startPump(WHITE_PUMP, whiteMoisture, "white");
}
}
int checkMoisture(int sensorPin, int moisturePin, String colour) {
// Turn on the sensor
digitalWrite(sensorPin, LOW);
unsigned long startMeasuring = millis();
while (millis() - startMeasuring < SENSOR_WAIT_TIME) {
// Do nothing, wait for SENSOR_WAIT_TIME
}
int moisture = analogRead(moisturePin);
Serial.println(colour + " moisture: " + moisture);
// Turn off the sensor
digitalWrite(sensorPin, HIGH);
return moisture;
}
void startPump(int pumpPin, int moistureLevel, String colour) {
// Turn on the pump for 1 second if moisture level is above the threshold
if (moistureLevel > MOISTURE_THRESHOLD) {
unsigned long startPumping = millis();
// Turn on the pump
Serial.println("Turning pump on at " + colour);
digitalWrite(pumpPin, LOW);
while (millis() - startPumping < PUMP_INTERVAL) {
// Do nothing, wait for PUMP_INTERVAL
}
digitalWrite(pumpPin, HIGH); // Turn off the pump after the time interval
} else {
Serial.println("No need for irrigation at " + colour);
}
}
Die Messung der Zeitintervalle nehme ich dabei mit millis() vor.
Wenn ich nur einen Sensor verwende funktioniert das noch. Wenn ich aber alle drei Sensoren auslesen will, wird alles durcheinander und die Schaltungen funktionieren nicht mehr richtig. Der Output sind da ca. so aus:
Check moisture
green moisture: 486
Turning pump on at green
on at blue
irrigation at white
���Setting the time...
Check moisture
Setting the time...
Check moisture
Setting the time...
Check moisture
green moisture: 489
Turning pump on at green
on at blue
Ich kann leider nicht nachvollziehen, warum der Code sich nicht so verhält wo oben beschrieben.
Ich hab auch schon verschiedene Baudrates (9600, 500000 und 1000000) verwendet, ohne Erfolg.
Gibt es vielleicht in diesem Fall bessere Methoden, um die Zeit zu messen um die Funktionen für eine bestimmte Dauer auszuführen?
VG
Gregor
