NodeMCU – Moisture Level Sensor How To

I had a chance to play around more with NodeMCU, this time testing out the moisture level sensor. There is a story behind this testing 🙂 In short, my wife and I somehow started to love house plants i.e. monstera, calathea and others. So both of us are quite busy and then some of the plants started to look like “not-a-plant” anymore, green became yellowish, then brownish , then into the trash. So we found out that the soil is too dry. So I decided to see if I can do a bit of IOT on the house plants. Search along then found out about the Moisture level sensor.

The sensor was only doing two things, check the moisture level and report the data to a webserver and in turn the webserver will send an alert to our phones.

Then I added a blinking led if a certain level is reached so just in case if my webserver fails to send the alerts. 🙂

I use the following:

  • NodeMCU v3 Lolin CH340G with ESP8266
  • Any Moisture Level Sensor. Mine has a label with Chinese Characters which I am not able to read 😀
  • USB Cable and USB Power Adaptor

Below in a way a simple flowchart on how the system flows:

Below is the code:

#include "ESP8266WiFi.h"
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>


#define LED D4
#define MoisturePin A0

const char* ssid = "ssidathome"; //configure to
const char* password = "xxxxxx";
String serverName = "http://192.168.5.20:8000/";  //may need to modify this according to your web app.
WiFiClient wifiClient;

int sensorValue;
int dryLimit = 400; //set for different level depending on the needs.
int sendCount = 0;
int initial = 1;
int delay_counter = 300; //how often do you want to send the data to the server

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(MoisturePin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
  }
  Serial.println("WiFi connected");
  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  sensorValue = analogRead(MoisturePin);
  Serial.print("Analog Value: ");
  Serial.println(sensorValue);
  if(initial == 1) {
    Serial.println("Sending initial data");
    sendHttp(sensorValue); //initial report
    initial = 2;
  }
  //Serial.println(sendCount);
  if(sensorValue > dryLimit) {
    digitalWrite(LED_BUILTIN,LOW);
    while(sensorValue > dryLimit) {
      digitalWrite(LED_BUILTIN,HIGH);
      delay(1000);
      digitalWrite(LED_BUILTIN,LOW);
      sensorValue = analogRead(MoisturePin);
      if(sendCount == delay_counter) {
        sendHttp(sensorValue);
        sendCount = 0;
      }
      sendCount++;
    }
  } else {
    digitalWrite(LED_BUILTIN,HIGH);
    sendCount++;
    if(sendCount == delay_counter) {
      sendHttp(sensorValue);
      sendCount = 0;
    }
    
  }
  delay(1000);
}

void sendHttp(int moistValue) {
  if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "monitor/2/insert"; //may need to modify this according to your web app.
      
      // Your webserver with URL path or IP address with path
      http.begin(wifiClient,serverPath.c_str());
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      String httpRequestData = "moist=" + String(moistValue);  //may need to modify this according to your web app.
      
      // Send HTTP GET request
      // int httpResponseCode = http.GET();
      int httpResponseCode = http.POST(httpRequestData);
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
        Serial.println(moistValue);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
}

Author:

I have been in love with computer since I was like seven years old and now doing the things that I love to do @ Wowrack. First in-love with how computer ran, and then starting to hack few games back then with MS-DOS, started to code (the first xDSL provisioning system in an ISP company), jump into the sea of network devices and now doing some automation as well as big data. Currently, serving employees and customers at https://www.wowrack.co.id We'll see what else will come up in the next few years.

Leave a Reply

Your email address will not be published. Required fields are marked *