Smart Irrigation using IoT

Hello,

For the past few days, I have been trying to complete my personal project on smart Irrigation using IoT. The project reads the readings from actuators, i.e, soil moisture sensor, distance sensor and helps us to automatically water the plants. I have been trying to show readings on LCD and send them to Thingspeak server using ESP8266 but have been failing. I am a beginner and would appreciate it very much if anyone could take a look at my code and design of the circuit and tell me what I have been doing wrong.

Thank you and regards.

Link to my Code

Link to my Tinkercad design

1 Like

Hi SAI

Can you tell us what error you get?

This is new territory for me so you may need to help me more than I can help you but I’ll see if I can spot a bug or two. I’m guessing you know that the connection to the access point has succeeded but could you confirm that please?

I noticed you configure it for multiple connections. Do you need to, or would a single connection suffice?

AT+CIPMUX=1

Are you able to run a local server that echoes your requests so that you can see what you’re sending to the website?

Hello Mr. Hofmann,

The code is from a similar project on YouTube. I have set up the components as shown and uploaded the code with wifi credentials and API to thingspeak. The AT commands simply don’t work. Everything I receive on serial monitor is Fail. I can’t quiet find out the fault. Please help me.

Have you seen my design on tinkecad cad sir, the serial monitor shows -1 for ESP connection and all the AT commands as fail.

No, the connection to access point isn’t successful. It is a single connection. And also the lcd isn’t showing any characters. It is showing squares on the first row and none on the second.

1 Like

Lest the code be lost, I’m posting it here for reference:

#include<LiquidCrystal.h>
#include <SoftwareSerial.h>
#define RX 9
#define TX 8
LiquidCrystal lcd(11, 10, 6, 5, 3, 2);
int ms_pin = A0;    // Moisture Sensor pin
int trig_pin=13;    // TRIG pin of Distance sensor
int echo_pin=12;    // ECHO pin of Distance Sensor
int BAUD_RATE=9600;
int output_value ;
String AP = "*********";       // AP NAME
String PASS = "***********"; // AP PASSWORD
String API = "**********";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
int T_Level;
boolean found = false;
int valSensor = 1;

SoftwareSerial esp8266(RX,TX);
void setup()
{
  lcd.begin(16,2);
  lcd.print("IoT based smart");
  lcd.setCursor(0,1);
  lcd.print("water Irrigation");
  delay(2000);
  lcd.clear();
  lcd.print(" Project Team");
  lcd.setCursor(0,1);
  lcd.print("Welcomes You");
  delay(2000);
 // gsmInit();
  lcd.clear();
  lcd.print("System Ready");
pinMode(4,OUTPUT);  // Used to trigger the relay
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
Serial.begin(BAUD_RATE);
Serial.println("\n\nReadings from the Sensors:");

delay(2000);
  lcd.clear();

  Serial.begin(9600);
  esp8266.begin(115200);
  esp8266.println("AT");
  Serial.println(esp8266.read());
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop()
{
  lcd.setCursor(0,0);
    lcd.print("Readings ");
  lcd.setCursor(0,1);
  lcd.print("from sensors");
    delay(3000);
    lcd.clear();
output_value= analogRead(ms_pin);
output_value = map(output_value,550,10,0,100);
Serial.print("\n\nMositure : ");
Serial.print(output_value);
Serial.println("%");
  lcd.print("Mositure : ");
  lcd.print(output_value);
  lcd.println("%");
delay(2000);
  lcd.clear();

digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
long duration= pulseIn(echo_pin, HIGH);
int distance= duration/29/2;
  lcd.print("Tank level : ");
  T_Level=30-distance;
  lcd.print(30-distance);
  //lcd.println("%");
delay(2000);
  lcd.clear();
 if(duration == 0)
   {
   Serial.println("\nWarning: no pulse from sensor");
   }
 else
   {
    Serial.print("\nWater level in cm:");
    Serial.println( 30 - distance); // Assuming 30 cm is height of tank
   }
 if(distance > 8)
   {
    Serial.print("\nTime to Refill the tank");
   lcd.print("Time to refill");
   lcd.setCursor(0,1);
  lcd.print("the tank");
  //lcd.println("%");
delay(2000);
  lcd.clear();

   }

 if(output_value < 0)
   {
    digitalWrite(4,HIGH);
   }
 else
   {
    digitalWrite(4,LOW);
   }
delay(1000);


String getData = "GET /update?api_key="+ API +"&field1="+output_value+"&field2="+T_Level;
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}


void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }

    countTimeCommand++;
  }

  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }

  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }

  found = false;
 }

The tinkercad link doesn’t load for me. Chrome says

This site can’t be reached
https ’s server IP address could not be found.
DNS_PROBE_FINISHED_NXDOMAIN

tinkercad

Tinkercad wants me to log in or create an account. I’m not bothered to do that.

In the mean time, you could set up a local web server that logs everything you send it if you get to the point you’re connected and want to verify that it’s sending the correct messages. Here’s an answer on StackOverflow on how to do that: https://stackoverflow.com/a/44013423/39722

But let’s address the immediate problems:

  1. LCD shows blocks rather than the characters written to it
  2. ESP doesn’t connect to the AP

LCD shows blocks rather than the characters written to it

I assume you’re confident that there’s no damage to the LCD. In which case I would be inclined to check the wiring, and which pins are in use, then check which address it’s using in the software. Typically 2x16 type LCDs use 0x3F or 0x27.

ESP doesn’t connect to the AP

I assume your version of the code has the AP, PASS, and API defined (i.e. not “*****”). If that’s all there, check that you have the uppercase and lowercase letters correct. Is the AP the name of your router that you can find in the WiFi settings of your phone or PC when connected.

Hope that helps, or we’ll have to dig deeper.

Hello,
Thanks for the response.The code has appropriate AP, PASS and API, the problem, I think, is somewhere in the code.
The LCD is 16x2 and the code is appropriate. The design on tinkecad helps you to understand the problem better. Id be delighted if you look at the design. It takes less than a minute to create a Tinkecad account and it’s absolutely free. I’m sure you would like to use it once you look at it’s simulation uses.