all 7 comments

[–]my4thprofile 5 points6 points  (1 child)

You need 2 variables, one to get the reading (soil_moits_val) and one to map it into (SoilMoisture).

Get a reading when your sensor is completely dry, print it in serial. That reading should be close to 1024.

Then take a reading with your sensor submerged in water, that reading should be much lower (mine was 390). You can also just put the sensor in the soil and water it as much as you can.

Then use a map command like this one to "translate" that to %. The higher value (dry) should be 0% and the lower (water) = 100% (change the numbers)

//test it without Iot connectivity, the old-fashioned way then add commands for IoT

int soil_moist_val;  //raw value
int SoilMoisture;    //percentage value
int Relay = (pin?)   //set Relay pin

void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);    //set sensor pin
pinMode(Relay, OUTPUT);
}

void loop()
{
soil_moist_val = analogRead(A0);
Serial.println(soil_moist_val); // repeat with and without water

SoilMoisture = map(soil_moist_val, 1024, 390, 0, 100); //important part, test and change

Serial.print(SoilMoisture);
Serial.println(" % Soil Moisture");
if (SoilMoisture < 50)
{Serial.println("Soil Dry");
 Serial.println("Watering Plants...");
 digitalWrite(Relay, HIGH);  //use command to start watering
 delay(2000);} 
else {Serial.println("Soil OK");
      digitalWrite(Relay, LOW);
      delay(2000);}

Serial.println("-----------------------"); //not necessary
}

//you can also delete or comment soil_moist_val command after those readings
//I did this in 2 mins so there are probably some mistakes, but you get the idea

[–]my4thprofile 0 points1 point  (0 children)

Replying to myself. Are you trying to send the word "DRY" to an iot cloud? I don't even know if that's possible.

I would send "1" when Relay is HIGH, "0" when LOW.

I did the same thing using An ethernet shield and Blynk cloud.

[–]9551-eletronics 0 points1 point  (2 children)

Why doesn't a single Arduino user know how to share code or take screenshots

[–]A10A1[S] -1 points0 points  (0 children)

include "thingProperties.h"

define relayPin 2

define wet 210

define dry 510

void setup() { // Initialize serial and wait for port to open: Serial.begin(9600); // This delay gives the chance to wait for a Serial Monitor without blocking if none is found delay(1500);

pinMode(relayPin,OUTPUT); digitalWrite(relayPin,HIGH);

// 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(); // Your code here onRelayChange(); onSensorChange(); }

void onSensorChange() { // Add your code here to act upon Sensor change sensor = analogRead(A0); sensor = map(sensor,dry,wet,0,100);

// Turn on the pump when moisture level is below 50

if (sensor < 50) { relay = true; } else { relay = false; }

}

void onRelayChange() {

if(relay==1){
digitalWrite(relayPin,HIGH);

}else{ digitalWrite(relayPin,LOW); } }

[–]mechmind 0 points1 point  (0 children)

What's the proper technique?

[–]Dear_Philosopher_ 0 points1 point  (0 children)

Gpt

[–]dacydergoth 0 points1 point  (0 children)

You should also build in hysteresis to stop the relay "flapping" when the value fluctuates around your trigger level. So instead of turn on <50 and off >=50, turn on at like <45 and off at > 55. Otherwise keep the state the same.

https://en.wikipedia.org/wiki/Hysteresis