Ninety99
Published

Getting started with Agriculture IoT (ThingSpeak + Matlab)

The following project measures soil moisture, humidity, and temperature. It then sends the data to ThingSpeak via. ESP8266.

BeginnerFull instructions provided12,267
Getting started with Agriculture IoT (ThingSpeak + Matlab)

Things used in this project

Story

Read more

Schematics

project_bb_CkKjcAO14a.png

Code

Arduino CODE

Arduino
#include <math.h>
#include <dht.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //(Tx,Rx)
dht DHT;  
#define DHTxxPIN 5
int op_value;

int ack;
void setup()
{
  Serial.begin(9600);
  mySerial.begin(115200);
}
void loop()
{
  ack = 0;
  int chk = DHT.read11(DHTxxPIN);
  switch (chk)
  {
    case DHTLIB_ERROR_CONNECT:
      ack = 1;
      break;
  }
  if (ack == 0)
  {
    Serial.print("Temperature(*C) = ");
    Serial.println(DHT.temperature);
    Serial.print("Humidity(%) = ");
    Serial.println(DHT.humidity);
    op_value = analogRead(A0);
    op_value = map(op_value,550,0,0,100);
    Serial.print("Moisture(%): ");
    Serial.print(op_value);
    Serial.print("%");
    Serial.println("\n ------------------------- \n");
    //------Sending Data to ESP8266--------//
    mySerial.print('*'); // Starting char
    mySerial.print(round(DHT.temperature)); //2 digit data
    mySerial.print(round(DHT.humidity)); //2 digit data
    mySerial.print(op_value); //2 digit data
    mySerial.println('#'); // Ending char
    //------------------------------------//
    delay(2000);
  }
  if (ack == 1)
  {
    Serial.print("NO DATA");
    Serial.print("\n\n");
    delay(2000);
  }
}

ESP8266 Program

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

//------- WI-FI details ----------//
char ssid[] = " "; //SSID here
char pass[] = " "; // Passowrd here
//--------------------------------//

//----------- Channel details ----------------//
unsigned long Channel_ID =  ; // Your Channel ID
const char * myWriteAPIKey = " "; //Your write API key
//-------------------------------------------//

const int Field_Number_1 = 1;
const int Field_Number_2 = 2;
const int Field_Number_3 = 3;
String value = "";
int value_1 = 0, value_2 = 0, value_3 = 0;
int x, y, z;
WiFiClient  client;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  internet();
}

void loop()
{
  internet();
  if (Serial.available() > 0)
  {
    delay(100);
    while (Serial.available() > 0)
    {
      value = Serial.readString();
      if (value[0] == '*')
      {
        if (value[7] == '#')
        {
          value_1 = ((value[1] - 0x30) * 10 + (value[2] - 0x30));
          value_2 = ((value[3] - 0x30) * 10 + (value[4] - 0x30));
          value_3 = ((value[5] - 0x30) * 10 + (value[6] - 0x30));
        }
      }
    }
  }
  upload();
}

void internet()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
  }
}

void upload()
{
  ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey);
  delay(15000);
  ThingSpeak.writeField(Channel_ID, Field_Number_2, value_2, myWriteAPIKey);
  delay(15000);
  ThingSpeak.writeField(Channel_ID, Field_Number_3, value_3, myWriteAPIKey);
  delay(15000);
  value = "";
}

ThingSpeak-hum vs moist

MATLAB
readID =  ;
readkey = ' ';
hum =  thingSpeakRead(readID, 'Field', 2, 'NumPoints', 60, 'ReadKey', readkey);
moist= thingSpeakRead(readID, 'Field', 3, 'NumPoints', 60, 'ReadKey', readkey);
hum = hum(~isnan(hum));
moist = moist(~isnan(moist));
stem(moist(end-10:end),hum(end-10:end)')
xlabel('Moisture(%)')
ylabel('Humidity(%)')

ThingSpeak-temp vs moist

MATLAB
readID =  ;
readkey = ' ';
temp =  thingSpeakRead(readID, 'Field', 1, 'NumMinutes', 60, 'ReadKey', readkey);
moist= thingSpeakRead(readID, 'Field', 3, 'NumMinutes', 60, 'ReadKey', readkey);
temp = temp(~isnan(temp));
moist = moist(~isnan(moist));
stem(moist(end-10:end),temp(end-10:end)')
xlabel('Moisture (%)');
ylabel('Temperature (C)');

Credits

Ninety99

Ninety99

2 projects • 10 followers

Comments