Back to Parent

//declaring integers
int val = 0;//variable to store soil value
int soil = A0;//Declare a variable for the soil moisture sensor
int soilPower = D7;//Variable for Soil moisture Power
//We wil use a digital pin to power this sensor, to prevent oxidation
//of the sensor as it sits in the corrosive soil.
//Setup function for the application
void setup()
{
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor

//connect to wifi and cloud
WiFi.on();
Particle.connect();

//Creates variables that are exposed through the cloud.
//You can request values from these using a variety of methods
Particle.variable("soil", &val, INT);
}

//loop for application (turn on motor when soil moisture is below 200)

void loop()
{
//display soil moisture to confirm is working
Serial.print("Soil Moisture = ");

//get soil moisture value from the readSoil function below and print it
Serial.println(readSoil());
delay(1000);//Take a reading every second

}

//This function is used to get the soil moisture content
int readSoil()
{
    digitalWrite(soilPower, HIGH);//turn D7 "On"
    delay(10);//wait 10 milliseconds
    val = analogRead(soil);
    digitalWrite(soilPower, LOW);//turn D7 "Off"
    return val;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0