element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Measuring Soil Moisture with the Wemos ESP 32
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: anpivey
  • Date Created: 3 Aug 2018 2:26 PM Date Created
  • Views 1993 views
  • Likes 5 likes
  • Comments 3 comments
  • esp32
  • tutorial
  • soil_moisture_sensor
  • iot
  • moisture_sensor
  • wemos
  • wia
Related
Recommended

Measuring Soil Moisture with the Wemos ESP 32

anpivey
anpivey
3 Aug 2018

Hi Everyone,

Today, we are going to see our plant needs to be watered with a Wemos ESP 32, a mh soil moisture sensor known as the flying fish and Wia.

If you haven’t already I suggest you go through the tutorial here about posting to Wia with your Wemos board. It details setting up the correct environment and libraries to complete this tutorial.

Connecting the hardware

  • Pin | Description
  • VCC - > +5 Volts Power Source
  • GND -> Ground or negative power source
  • DO -> Digital Output. (low when moisture exceeds threshold)</td>
  • A0 > Analog Output - 0 -4.2 volts. The lower the voltage, the greater the moisture

 

Connect the two pins on the probe to the plus and minus pins on the mh-senor.

image

Now we need to connect mh sensor to our Wemos in the following way

  • MH - > Wemos
  • VCC -> VCC
  • GND - > GND
  • A0 -> 35

Connect the Wemos board to the computer via USB and check lights on the sensor and the Wemos.

image

The Code

The code below is all you need to get the measurement of the voltage across the probe which can be translated into the percentage moisture of the soil.

#include <ArduinoJson.h>
#include <WiFi.h>
#include <ArduinoHttpClient.h>
#include <Arduino.h>




const char* ssid     = "your-wifi-ssid"; // Your WiFi ssid
const char* password = "your-wifi-password"; //Your Wifi password


// Get this sccret key from the wia dashboard. It should start with `d_sk`
const char* device_secret_key = "your-device-secret-key";


// Wia API parameters
char server[] = "api.wia.io";
char path[] = "/v1/events";
int port = 80;


WiFiClient client;
int status = WL_IDLE_STATUS;


StaticJsonBuffer<200> jsonBuffer;
HttpClient httpClient = HttpClient(client, server, port);
JsonObject& root = jsonBuffer.createObject();


const int INPUT_PIN = 23; 
const int ANALOG_PIN = 35;


void setup() {
  
  pinMode(INPUT_PIN, INPUT);
  
  // initialize serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  WiFi.begin(ssid, password);
  Serial.print("Attempting to connect to SSID: ");
  Serial.print(ssid);
  // attempt to connect to WiFi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP  network:
    // wait 5 seconds for connection:
    delay(5000);
  }


  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("connecting...");
}


// Thing function runs continiously
void loop() {
  
  root["name"] = "moisture";
  root["data"] =  analogRead (ANALOG_PIN);


  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {


    sendToWia(root);


  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
   delay(1000*15*60); // Wait for 15 minutes to post again
}


// Adds the correct headers for HTTP and sends Data to Wia
void sendToWia(JsonObject& data) {
  size_t len = data.measureLength();
  size_t size = len + 1;
  char json[size];
  httpClient.beginRequest();
  httpClient.post(path);
  httpClient.sendHeader("Content-Type", "application/json");
  httpClient.sendHeader("Content-Length", data.measureLength());
  httpClient.sendHeader("Authorization", "Bearer " + String(device_secret_key));
  httpClient.beginBody();
  data.printTo(httpClient);
  Serial.print("Posting ");
  data.printTo(Serial);
  Serial.println(" to Wia");
  httpClient.endRequest();


}

 

Replace the following values of the following variables names (Place the correct value between the quotation marks right of the variable names in the code):

  • ssid - with your WiFi network name.
  • password - with your WiFi network password.
  • device_secret_key with your device secret key from the Wia Dashboard (the one that begins with d_sk).

 

In the Arduino IDE:

  • Select the Wemos lollin32 board
  • Select the appropriate port
  • Click Sketch > Upload in the menu

 

Go to the Wia dashboard and view the events as the come in the Wia debugger.

image

 

Notifying you when your plant needs watering

Now we are going to set up a flow in the Wia flow studio that gives you a push notification when the moisture percent passes a certain threshold. The Wia app is required to send a push notification to your phone, it can be installed here for Android and here for ios.

Go to your Wia Dashboard and in the space you setup before, go to flow section in the left side bar and create a flow. You can name whatever you prefer.

image

In flow studio, drag an event node from the triggers section and:

  • Enter moisture as the the event name
  • Add the device that matches the one added to the code earlier

Drag a function node from the logic section and copy and paste the following code:

if (input.body) {

    if (input.body.data < 3000) {

        output.process = 0; 

    }

}

else {

    output.process = 0;

}

The Output from the soil moisture sensor is a number between 0 and 4200 with zero being completely wet and 4200 being completely dry.

All the code above does is sets to not process the output when there is no input or if the input is less than 3000 (i.e The doesn't plant need watering)

Finally, and a notification node and enter The plant is needs watering in the text field so that Wia can send a notification to you when your plant needs watering.

image

Thats it! If you are having any problems, drop us a line below and we'll help you out.

  • Sign in to reply

Top Comments

  • anpivey
    anpivey over 6 years ago in reply to dixonselvan +3
    Hi Dixon, I would be very interested to hear the difference's you notice between the two platforms. Be sure to share when you get a chance I love seeing people bring their projects to life! Cheers, Au…
  • dixonselvan
    dixonselvan over 6 years ago +2
    Hi anpivey , Thanks for sharing. Your post grabbed my attention, as I have recently worked for measuring temperature with Arduino MKR1000 and monitor from thinger.io IoT Platform [ Steps for connecting…
  • dixonselvan
    dixonselvan over 6 years ago in reply to anpivey +2
    Sure I will share when I get my hands on this. Well, the difference is thinger.io has simplified coding (library for Arduino IDE) and GUI for easy use. I am deducing this from the visual presentation you…
  • dixonselvan
    dixonselvan over 6 years ago in reply to anpivey

    Sure I will share when I get my hands on this.

     

    Well, the difference is thinger.io has simplified coding (library for Arduino IDE) and GUI for easy use. I am deducing this from the visual presentation you have provided here. Check this blog and let me know your thoughts also - Cue System for Anosmia and Smart WheelChair #4 - IoT Temperature Monitoring [DIY]

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • anpivey
    anpivey over 6 years ago in reply to dixonselvan

    Hi Dixon,

    I would be very interested to hear the difference's you notice between the two platforms. Be sure to share when you get a chance I love seeing people bring their projects to life!

     

    Cheers,

    Austin

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dixonselvan
    dixonselvan over 6 years ago

    Hi anpivey ,

     

    Thanks for sharing. Your post grabbed my attention, as I have recently worked for measuring temperature with Arduino MKR1000 and monitor from thinger.io IoT Platform [Steps for connecting Arduino MKR1000 with thinger.io for reading temperature data ]

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube