Categories
Tutorial

Soil moisture sensor

We are creating a device that measures soil moisture, based on a WEMOS D1 mini and a Capacitive Soil Moisture sensor.

With cloud integration the measurement of the sensor sent to the IoT Guru Cloud, where we get fancy graphs and we can set a warning that we need to watering the plant.

Supplies:

  • WEMOS D1 mini
  • Capacitive Soil Moisture sensor

Step 1

The Capacivite Soil Moisture sensor has three pin:

  1. Ground
  2. Vcc
  3. Analog output

You can connect this pins to the WEMOS D1 mini in same order:

  1. D5
  2. D0
  3. A0

Step 2

The source code is contains an IoT Guru Cloud integration, you can check out our Tutorials about the integration. You will need:

  • userShortId
  • deviceShortId
  • deviceKey
  • nodeShortId
  • fieldName

Step 3

The code will send an analogue measurement in every one minute. Compile it and upload the firmware to your WEMOS D1 mini.

#include <IoTGuru.h>

#include <ESP8266WiFi.h>

const char* ssid      = "ssid";
const char* password  = "password";

WiFiClient client;

String userShortId    = "uuuuuuuuuuuuuuuuuuuuuu";
String deviceShortId  = "dddddddddddddddddddddd";
String deviceKey      = "kkkkkkkkkkkkkkkkkkkkkk";
IoTGuru iotGuru = IoTGuru(userShortId, deviceShortId, deviceKey);

String nodeShortId    = "nnnnnnnnnnnnnnnnnnnnnn";
String fieldName      = "analog";

void setup() {
    Serial.begin(115200);
    delay(10);

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(50);
        Serial.print(".");
    }
    Serial.println("");

    /**
     * Set the callback function.
     */
    iotGuru.setCallback(&callback);
    /**
     * Set the debug printer (optional).
     */
    iotGuru.setDebugPrinter(&Serial);
    /**
     * Set the network client.
     */
    iotGuru.setNetworkClient(&client);

    pinMode(14, OUTPUT);
    pinMode(16, OUTPUT);
    digitalWrite(14, LOW);
    digitalWrite(16, HIGH);
}

volatile unsigned long nextSendUptime = 0;

void loop() {
    iotGuru.loop();

    if (nextSendUptime < millis()) {
        nextSendUptime = millis() + 60000;
        int analogValue = analogRead(A0);
        float analog = analogValue * (3.2 / 1023.0f) * 100.0f;
        Serial.println("Analog value: " + String(analog));
        iotGuru.sendMqttValue(nodeShortId, fieldName, analog);
    }
}

void callback(const char* nodeShortId, const char* fieldName, const char* message) {
    Serial.print(nodeShortId);Serial.print(" - ");Serial.print(fieldName);Serial.print(": ");Serial.println(message);
}

Step 4

Check the chart of the measurements:

One reply on “Soil moisture sensor”

I like this capacitive sensors. Thé resistive ones are useless because of electrolysis.

Leave a Reply

Your email address will not be published.


The reCAPTCHA verification period has expired. Please reload the page.