Hello everyone!
I want do built an automatic watering system with moisture sensor and a 5v waterpump.
The measurement and the watering works okay but I'm only working with delay. I know with delay in the void loop is not the best way and working with Millies would be better but I'm pretty new to the coding with Arduino so I have no cloud how to do it. The main problem is that I want a message if the plant needs water, when it has enough water, when the pump will turn on and when the pump will turn off. I'm only getting the messages when the plant needs water and when it has enough but the messages for the pump aren't working. Somebody has an idea how to get it to work?
#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
https://create.arduino.cc/cloud/things/96bcc51b-9f5d-46b6-9d18-0ccbd11a1b3e
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String message;
int moistureLevel;
CloudPercentage moisturePercent;
CloudRelativeHumidity hum;
CloudTemperature temp;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHT_ENSOR_PIN 3
#define DHT_SENSOR_TYPE DHT22
#define PUMP_PIN 2
DHT dht_sensor(DHT_ENSOR_PIN, DHT_SENSOR_TYPE);
int soilMoistureLevel;
int soilMoisturePercent;
int airValue = 830;
int waterValue = 360;
void setup() {
pinMode(DHT_ENSOR_PIN, INPUT);
dht_sensor.begin();
// Initialize serial and wait for port to open:
Serial.begin(9600);
delay(1500);
pinMode(PUMP_PIN, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update(); // Update the Cloud's data
hum = dht_sensor.readHumidity();
temp = dht_sensor.readTemperature();
if(isnan(hum) || isnan(temp)){
Serial.println("Failed to read from DHT Sensor!");
}else{
Serial.print("Humidity: "); Serial.print(hum);Serial.print("%"); Serial.print(" | ");
Serial.print("Temperature: "); Serial.print(temp);Serial.println("°C"); //ALT 0176
if (hum > 75)
{
//Send message to Arduino Cloud messenger on dashboard
message = "Luftfeuchtigkeit zu hoch!";
}
}
//Read data from Analog Pin 1 on Nano 33 IoT and print to Serial Monitor for debugging
soilMoistureLevel = analogRead(1);
Serial.println(soilMoistureLevel);
//Convert soilMoistureLevel integer into percentage by using air and water values defined above and mapping with 0 - 100
soilMoisturePercent = map(soilMoistureLevel, airValue, waterValue, 0, 100);
Serial.println(soilMoisturePercent); //Print to Serial Monitor for debugging
//Update Arduino IoT Cloud values
moistureLevel = soilMoistureLevel;
moisturePercent = soilMoisturePercent;
//If the moisture percentage is less than 50%
if (moisturePercent < 50)
{
//Send message to Arduino Cloud messenger on dashboard
message = "Wasserlevel niedrig, Gießvorgang gestartet!";
digitalWrite(2, LOW);
Serial.println("Pumpe an");
message = "Pumpe an";
delay(5000);
digitalWrite(2, HIGH);
Serial.println("Pumpe aus");
message = "Pumpe aus";
Serial.print("Humidity: "); Serial.print(hum);Serial.print("%"); Serial.print(" | ");
Serial.print("Temperature: "); Serial.print(temp);Serial.println("°C"); //ALT 0176
delay(5000);
}
//If the moisture percentage is greater than 70%
if (moisturePercent > 70)
{
//Send message to Arduino Cloud messenger on dashboard
message = "Wasserlevel in Ornung, Gießvorgang beendet!";
digitalWrite(2, HIGH);
Serial.println("Pumpe aus");
message = "Pumpe aus";
delay(5000);
}
}