Tuesday 20 December 2016

Soil Moisture,Node-mcu and IoT

After a lot of browsing on the internet , I found nothing about how to interface a moisture sensor with Node-MCU. So, I decided to try it by myself.
Node-MCU is an open source IoT platform to make and build awesome stuff with data analysis and access throughout the world with the help of Thingspeak. It has built in ESP8266 with it, which makes it pretty easy to use.
This small project was a part of our college semester project. Our teacher asked everyone in the class to make a project based on wireless communication, that is, transferring data over a channel which doesn't have wires. In the past , I had worked with Arduino IDE and boards but never made something that employs wireless communication. I have worked with data storage , graphing using python, sensor interfacing but never with wireless communication. Everyone was busy making big circuits out of which some were working , some were not, but I appreciate their efforts. I wanted to make something useful and smart at the same time.
Some legend once said,"Why do hard work when you can opt for smartness". Node-MCU is a very smart micro-controller unit, I must say.
Lets go to the technical part now. The moisture sensor is based on a voltage produced due to variable resistance.The two probes of the sensor are to be dipped in water or soil to measure the moisture content.
These two probes are variable resistances which when dipped in water(Maximum moisture) produces a voltage at the output pin. For water the number of bits calculated by the analog channel is 1024 i.e above 3.3V(Node-MCU works with 3.3V). In short , make the following connections
 Node-MCU                 Sensor
VCC(3.3V/5V)  ---->   VCC
A0      ----->                OUT
GND  ---->                  GND

After you are done with connections. Open your Arduino IDE and write the code to sense and upload the moisture content to Thingspeak. The code is at the bottom of the page.
You might need to download the library for ESP8266 and Thingspeak. To install these libraries, follow the following path in Arduino IDE.
Go to  Sketch>Include library>Manage library.
In the search bar , search for both of the libraries.
Here is the screenshot to make it easy.

After the successful installation of libraries.Visit Thingspeak and create an account. After this, follow the following procedure to make a channel and get an API key and channel id.
Click on channels and then on My Channel.Click on New Channel.

Fill all the details you wish to fill including name and fields labels. Make it public if you want to.
When You will save the channel , you will get an API key by clicking on the channel keys in your channel section.
Now write the following code in the Arduino IDE.

#include<ESP8266WiFi.h>
#include<WiFiClient.h>
#include <ThingSpeak.h>

const char* ssid="Your ssid";
const char* password="Your paasword";
unsigned long myChannelNumber = "Your channel id"; 
const char * myWriteAPIKey = "Your API key"; 
WiFiClient client;
void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  ThingSpeak.begin(client);
}
void loop() {
moisture=analogRead(A0);
ThingSpeak.writeField(myChannelNumber, 1, moisture, myWriteAPIKey); //Write data to channel
delay(15000); ///delay required for thingspeak data to upload.



}
 
Here is the setup I used for testing.

Upload the code and open Your channel to see the output.

This project can be used to look after your lawn when out of station or away from your home  by controlling the water pump from the sensor values and it can be controlled from anywhere in the world.
If You have any questions, ask in comments.

1 comment: