Real-time Uploading Of Data using nodeMCU and Soil Moisture Sensor

Sensor data is the output of a device that detects and responds to some type of input from the physical environment. The output from the sensor may be used to provide information or input to another system or to guide a process. Sensors can be used to detect just about any physical element. We have already covered about one such sensor in our previous blog that is soil moisture sensor.

As we have got the basic idea of how does a soil moisture sensor works, now we can proceed further to use this sensor data coming from soil moisture sensor to use it as a piece of information or to input it to another control system to work accordingly. In this blog, you will see how to upload the real-time data coming from soil moisture sensor on the thingspeak website to get all the information of all your devices at your finger tips and the technology that we will use here is the Internet of Things.

The Internet of things is the extension of Internet connectivity into physical devices and everyday objects. Embedded with electronics, Internet connectivity, and other forms of hardware, these devices can communicate and interact with others over the Internet, and they can be remotely monitored and controlled.

IoT or Internet of Things is a separate technology that is coming into a clear picture as the per the advancement in the technology and collaboration of electronics applications with IOT. So, to keep us up to date with the technology we will get started with our first IoT program with NodeMCU to upload the real-time sensor data on the thingspeak website.

 

Firstly, we will have to create a channel on thingspeak website where we will upload the data that we read from the soil moisture sensor. Secondly, we will interface soil moisture sensor with Nodemcu. Lastly, we will write a program to upload the real-time data on the thingspeak website.

Now let’s get started. Firstly, let’s see how to create a channel on thingspeak website. For creating a channel open your favourite browser and open www.thingspeak.com and now click on get started for free or click sign up to create your account on the website. Now enter the details that you are asked to enter and you will get a verification link on your email Id that you will enter, so just verify that and continue to set a password for your account. Once you are done with all this you have successfully created your account. Now it’s time to create a channel. For that go to channels – > My channels and click on New channel. To get the detailed info about creating a channel on thingspeak, check out the video below:

Now it will ask for the required field to be filled with some data and for each field a proper explanation is given on right side as shown in the image below. Once you are done with the required fields click save channel and it will create a channel for you. For more details also watch the video given in this blog.

Now we have to interface nodemcu with soil moisture sensor in the same way as we have done in our previous blog. Below is the interfacing circuit so make connections accordingly.

So as we have already interfaced the soil moisture sensor with the nodemcu in our previous blog and also we have gone through the basic idea of how to read the changes from the soil moisture sensor and to store it in a variable to use it further in the project. Also we have created a channel on thingspeak website to upload the real-time sensor data on the channel. So in our today’s blog we will read the sensor data and will store it in a variable and then we will create a link to upload the sensor data on a field in the channel that we have created on thinkspeak website. Also check out the video below to learn how to upload the real-time data from sensor to the server:

Now let’s move on and let’s use the soil moisture data to upload the real time data on thingspeak website. Here is a code below to upload the real-time data:

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

// Network Parameters
const char* ssid = “ssid name”;
const char* password = “password”;

// ThingSpeak information
char thingSpeakAddress[] = “api.thingspeak.com”;
unsigned long channelID = “channel ID”;

char* writeAPIKey = “write api from the channel”;

unsigned int dataFieldOne = 1; // Field to write temperature data

float aConst = 2.25E-02;

WiFiClient client;

void setup() {

Serial.begin(115200);
Serial.println(“Start”);
connectWiFi();
pinMode(2,INPUT);
Serial.println(aConst);
delay(1000);
}

void loop() {

 

float readValue = analogRead(2);
float logR = log( 10000 * ( 1024 / readValue – 1 )); // Separate the calculation for simplicity and debugging

Serial.println(“Moisture content = ” + String( readValue ) );
writeTSData( channelID , dataFieldOne , readValue ); // Write the temperature in F, C, and time since starting.
delay(20000);
}

int connectWiFi() {
WiFi.begin( ssid, password );
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}

Serial.println( “Connected” );
ThingSpeak.begin( client );
}

// Use this function if you want to write a single field
int writeTSData( long TSChannel, unsigned int TSField, float data ) {
int writeSuccess = ThingSpeak.writeField( TSChannel, TSField, data, writeAPIKey ); // Write the data to the channel
if ( writeSuccess ) {

Serial.println( String(data) + ” written to Thingspeak.” );
}

return writeSuccess;
}

 

Now, select the Board and port in Arduino IDE and click on upload to transfer the code to the NodeMCU and check the results on the thingspeak channel that you have created.

So this is it for this blog . I hope everything is clear to you, but still if you are facing any difficulties related to the topic then do leave a comment below in the comment section. Thanks for being here. 🙂

 

Leave a Reply

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