NodeMCU-Based IoT Project: Connecting YL-69 & YL-38 Moisture (2024)

Things used in this project

Hardware components

NodeMCU-Based IoT Project: Connecting YL-69 & YL-38 Moisture (1)
NodeMCU ESP8266 Breakout Board
×1
yl-69, yl-38 soil moisture sensor
×1

Software apps and online services

NodeMCU-Based IoT Project: Connecting YL-69 & YL-38 Moisture (2)
Arduino IDE
thingsio.ai

Story

Sensor Description and Connection

You need to connect the 2 header pins (+ and -) to the YL-38 soil moisture sensor control board as shown by the 2 blue wires above. The board has 4 pins on the other side labeled VCC, GND, D0, A0.

VCC: Positive Voltage Power Supply GND: Ground D0: Digital Out /////here we are not connecting this pin...we only taking analog valueA0: Analog Out

In our case, we are going to be making the following connections: VCC on the YL-38 module to 3V3 on the ESP8266 Development board. (Red wire) GND on the YL-38 module to GND on the ESP8266 Development board. (Black wire) A0 on the YL-38 module to A0 on the ESP8266 Development board. (Yellow Wire)

Now onto the Arduino IDE

Here we will only see the coding for sensor only. Full coding we will see in coding section.

////Create a new sketch in the Arduino IDE and paste the following code into the body: int sensor_pin = A0;int output_value ;void setup() { Serial.begin(115200); Serial.println("Reading From the Sensor ...");  delay(2000); }void loop() { output_value= analogRead(sensor_pin); Serial.print("Moisture : ");  Serial.print(output_value);  Serial.print("\n"); delay(1000); }

Calibrating Sensor

Now that you have a functioning soil moisture sensor, how do you get some useful data out of it?

Take note of the number that flashes across the screen on your serial monitor inside of the Arduino IDE while your sensor is completely dry, it is usually 9XX if you have your soil moisture sensor plugged into 3V on your ESP8266 Development board. Next, completely submerge your soil moisture leads in a small cup of water. Be careful not to submerge the wires during this step as this could damage your electronic hardware.

As soon as you submerge your sensor in the water, keep an eye on the serial monitor. You will see the readings jump to a different value!!!!!

In mine (yours may be slightly different), a completely dry sensor gives a reading of about ~750, while a completely wet sensor reads consistently around ~180. Taking these numbers, we are going to add a few lines of code to our Arduino Sketch to map these values to a more familiar percentage (%).

First, we need to convert the values we are getting to a percentage, and we can do this using the map function.

output_value = map(output_value,750,180,0,100);

This simple function takes our 2 values and converts them from 0 - 100% based on their number between each other. The first number you put into the map function is the sensor value when it is completely dry, next comes the wet value. The following values are the map values, 0 being 0% and 100 being 100%.

Next, we an add a % symbol into the line being printed as shown.

Serial.print("%\n");

Your completely modified code should look like this before you push it to the development board:

int sensor_pin = A0;int output_value ;void setup() { Serial.begin(115200);  Serial.println("Reading From the Sensor ...");  delay(2000); }void loop() { output_value = analogRead(sensor_pin); output_value = map(output_value,750,180,0,100);  Serial.print("Moisture : ");  Serial.print(output_value); Serial.print("%\n");  delay(1000); }

Tutorial on Connecting NodeMCU to ThingsIO.AI Cloud

NodeMCU is one of the most popular IoT Development platform use by the thousands of developers worldwide. What is more attractive is that it can run on the even more popular Arduino IDE.

NodeMCU alone is able to connect and send the data to cloud that makes it more preferable(no need to have bunch of boards, only one is enough).

The goal of this tutorial is to enable you to send capture and send data to cloud from your NodeMCU device. Before starting this tutorial make sure you have your NodeMCU and lm35 sensor handy.

Part I: Installation and Settings of Arduino IDE

Once you download and install the arduino ide, to get start with ide it is good to check this first (http://www.circuitbasics.com/arduino-basics-installing-software).

Part II: Setting Up Cloud Account on ThingsIO.AI

ThingsIO.AI is a IoT platform for developers. As a developer we understood the pain of not having a seamless place to connect, analyze and process device data. Many pure analytics platforms does not offer device management, which is core for the iot. That is the reason for ThingsIO.AI.

  • Register the account in http://thingsio.ai/#/register.
  • You will get the notification for sign up.
  • Go in your email address and verify your account.
  • After that, sign in your account with your email address and password
  • Now, you will be on the project dashboard. Click on the new project option.
  • Click on the add a new device option. You will get device ID.
  • Enter the device name and click on the create and configure device.
  • You will be on your device dashboard.

Some basic points (1-5) for analysing data on ThingsIO.AI

1. Track your device’s special parameters here. This can be set in device configuration(default to null).

2. You can see your real time and special parameter’s graph.

3. You can see your all data points.

4. You can see here your last 5 data points.

5. You can see here all the list of created graphs.

  • Go in sample device code options and click on the NodeMCU.
  • You will get the sample code from there copy and paste into your Arduino IDE.

Click on the “Send trial data” to send a trial data to the server:

Part III: Setting Up NodeMCU

we have already made hardware connection so just upload this code on nodemcu by using an usb. and open your serial monitor where you see the values of temperature in different units.

- one important thing while uploading the code, check the device id.

Part IV: Charting and Visualizations on ThingsIO.AI

Go to your ThingsIO.AI account and go to your device ->

Click on the configuration device option:

  • You can set the special parameters (this is tracked on the device dashboard) & transform it accordingly and add new parameters (Parameters are updated automatically as you send them from your device).
  • Click on the update device option:
  • To create a graph for your device click on the “create graph option."

Part V: Graph

You will see graph of data points something like this.....

Read more

Schematics

NodeMcu connection diagram with soil moisture sensor

NodeMCU-Based IoT Project: Connecting YL-69 & YL-38 Moisture (3)

Code

  • NodeMcu connection diagram with soil moisture sensor

NodeMcu connection diagram with soil moisture sensor

Arduino

#include <ESP8266WiFi.h>const char* ssid = "****"; // ******* is your wifi SSID nameconst char* pass = "*******";//********* is your wifi password const char* host = "api.thingsio.ai";const char* post_url = "/devices/deviceData"; const char* time_server = "baas.thethingscloud.com"; const int httpPort = 80;char timestamp[10];int count=0,i,m,j,k;int humdity;int outputpin= A0; WiFiClient client;int GiveMeTimestamp(){ unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 50000) { client.stop(); return 0; } }while (client.available()) { String line = client.readStringUntil('\r'); //indexOf() is a funtion to search for smthng , it returns -1 if not found int pos = line.indexOf("\"timestamp\""); //search for "\"timestamp\"" from beginning of response got and copy all data after that , it'll be your timestamp if (pos >= 0)  { int j = 0; for(j=0;j<10;j++) { timestamp[j] = line[pos + 12 + j]; } } }}  void setup() {  Serial.begin(115200); WiFi.disconnect(); delay(10); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);  WiFi.begin(ssid,pass );  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("NodeMcu connected to wifi..."); Serial.println(ssid); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); Serial.println();} void loop() {  int analogValue = analogRead(outputpin); {  /////////////////////////////////////// SEND THE QUERY AND RECEIVE THE RESPONSE///////////////////////  humidity = analogValue; humidity = map(humidity,750,180,0,100); Serial.print("humidity (analog): "); Serial.print(humidity); Serial.print("%\n");   Serial.print("connecting to "); Serial.println(host); //defined upside :- host = devapi2.thethingscloud.com or 139.59.26.117///////////////////////////////////// TIMESTAMP CODE SNIPPET /////////////////////////Serial.println("inside get timestamp\n"); if (!client.connect(time_server, httpPort))  { return; //*-*-*-*-*-*-*-*-*-* } client.println("GET /api/timestamp HTTP/1.1"); //Whats this part doing, i didnt get client.println("Host: baas.thethingscloud.com"); client.println("Cache-Control: no-cache"); client.println("Postman-Token: ea3c18c6-09ba-d049-ccf3-369a22a284b8"); client.println();GiveMeTimestamp(); //it'll call the function which will get the timestamp response from the serverSerial.println("timestamp receieved");Serial.println(timestamp);///////////////////////////////////////////////////////////////////////////////// Use WiFiClient class to create TCP connections if (!client.connect(host, httpPort)) //host = devapi2.thethingscloud.com , port = 80 { Serial.println("connection failed"); // *-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*- return; }  client.print("Content-Length: "); client.println();  unsigned long timeout = millis(); while (client.available() == 0)  { if (millis() - timeout > 50000)  { Serial.println(">>> Client Timeout !"); client.stop(); return; } }  Serial.println("***********************************************"); Serial.println(); Serial.println("closing connection"); Serial.println("inside ThingsCloudPost");  String PostValue = "{\"device_id\": 6111998 , \"slave_id\": 2"; PostValue = PostValue + ",\"dts\":" +timestamp; PostValue = PostValue +",\"data\":{\"moisture%\":" + h1 +"}"+"}";   Serial.println(PostValue);//////////////////////////////// try to connect to server again and POST the data on the cloud ////////////////////////// if (!client.connect(host, httpPort))  { return; } client.print("POST "); client.print(post_url); /// POST api/device-datas HTTP/1.1 Host: baas.thethingscloud.com Content-Type: application/json Cache-Control: no-cache client.println(" HTTP/1.1"); //Authorization: Content-Length:  client.print("Host: "); client.println(host); client.println("Content-Type: application/json"); client.println("Cache-Control: no-cache"); client.print("Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.IjViZTk1NzRiNDAyYjlkNGI5MDdiZjgwNyI.Ao71P5YERN9E66J7KJHRR5FczsJIHhmv44X7w9QoKCI"); client.println(); client.print("Content-Length: "); client.println(PostValue.length()); client.println(); client.println(PostValue); //////////////////////////////////POSTING the data on to the cloud is done and now get the response form cloud server////////////////// while (client.available() == 0)  { if (millis() - timeout > 50000)  { Serial.println(">>> Client Timeout !"); client.stop(); return; } }  Serial.println("Response From Server"); while(client.available()) {  String line2 = client.readStringUntil('\r');  Serial.print(line2);  } Serial.println("////////////////////// THE END /////////////////////");delay(3000);}}

Credits

HARGOVIND

8 projects • 16 followers

Comments

NodeMCU-Based IoT Project: Connecting YL-69 & YL-38 Moisture (2024)

References

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6246

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.