Example Code for Arduino-Smart Watering System (Based on Easy IoT)

This project implements a smart watering system using Arduino, a soil moisture sensor, and a WiFi IoT module connected to Easy IoT. The system monitors soil moisture levels and provides feedback when watering is needed. Remote monitoring and control enable users to manage plant care anytime, anywhere.

Hardware Preparation

  • Arduino UNO board x 1
  • IO Expansion board x1
  • WiFi IoT module x1
  • Soil Moisture Sensor x1
  • Digital Relay Module x1
  • Water Pump x1
  • Wires

Software Preparation

Wiring Diagram

Sample Code

 #include "DFRobot_WiFi_IoT_Module.h"
    #include <SoftwareSerial.h>
    int Relay = 3;
    //I2C mode
    DFRobot_WiFi_IoT_Module_I2C IoT;

    //I2C mode 
    DFRobot_WiFi_IoT_Module_I2C IoT;

    //You can change them based on your needs 
    const char *WIFI_SSID           = "hitest";         //WiFi account 
    const char *WIFI_PASSWORD       = "12345678";           //WiFi password 

    //Easy IOT English configuration 
    //const char *EASY_IOT_SERVER        = "iot.dfrobot.com";

    //EASY IoT Chinese configuration 
    const char *EASY_IOT_SERVER     = "iot.dfrobot.com.cn";    //IoT host address, no need to revise here 
    const char *EASY_IOT_PORT       = "1883";                  //IoT connect port, no need to revise here 
    const char *EASY_IOT_ID         = "smofVJDMR";             //Iot_id, the account recorded when creating device 
    const char *EASY_IOT_PWD        = "ymoBVJvMgz";            //Iot_pwd, the password recorded when creating device 
    const char *SUBSCRIBE_TOPIC     = "9qnYVJDMR";             //Subscribe a device, device number “9qnYVJDMR”
    const char *PUBLISH_TOPIC       = "9qnYVJDMR";             //Send data to the device, device number “9qnYVJDMR”
    const char *EASY_IOT_SEND_MESSAGE    = "Send_Message";

    /********************************************************************************************
    Function    : callback
    Description : print the subscribed IoT device number and message 
    Params      : topic: subscribed device number; message: subscribed device number message 
    Return      : 
    ********************************************************************************************/
    String data = "0";
    void callback(const char*topic,const char*message)
    {
     Serial.println("Receive [ " + (String)topic + "]," + "Message : " + (String)message);
     data = (String)message;
    }

    /********************************************************************************************
    Function    : getSoilHumidity
    Description : Get soil humidity value 
    Params      : 
    Return      : Current soil humidity 
    ********************************************************************************************/
    float getSoilHumidity()
    {
      uint16_t val;
      val = analogRead(A0);
      return val;
    }

    void setup(void){ 
      Serial.begin(115200);
      //Init communication port 
      while(IoT.begin() != 0){  
        Serial.println("init ERROR!!!!");
        delay(100);
      }
      Serial.println("init Success");
      //Connect to WiFi
      while(IoT.connectWifi(WIFI_SSID, WIFI_PASSWORD) != 0){  
        Serial.print(".");
        delay(100);
      }
      Serial.println("Wifi Connect SUccess");
      //Init MQTT and connect to IoT platform 
      while(IoT.MQTTBegin(EASY_IOT_SERVER, EASY_IOT_PORT, EASY_IOT_ID, EASY_IOT_PWD) != 0){
        Serial.print(".");
        delay(100);
      }
      Serial.println("MQTT Connect Success");
      //call callback function 
      IoT.setCallBack(callback); 
      //subscribe device SUBSCRIBE_TOPIC
      while(IoT.subscribe(SUBSCRIBE_TOPIC) != 0){
        Serial.print(".");
        delay(100);
      }
      Serial.println("Subscribe Topic Success");

      pinMode(13, OUTPUT);         //Set Pin13 as output
      digitalWrite(13, HIGH);     //Set Pin13 High
      pinMode(Relay, OUTPUT);     //Set Pin3 as output
    }

    static long timestamp = millis();
    void loop(void){

    IoT.loop();
      //Received command "1" from Easy IoT, set relay to high 
      if(strcmp(data.c_str(),"1") == 0){ 
        digitalWrite(Relay, HIGH);
        data = "0";

      }
    IoT.loop();
      //Received command "2" from Easy IoT, set relay to low
      if(strcmp(data.c_str(),"2") == 0){
         digitalWrite(Relay, LOW); 
        data = "0";
      }
      if(millis()-timestamp>5000){
        timestamp = millis();
        int Hum = getSoilHumidity();
      //When current soil humidity Hum<=100, send the current humidity to Easy IoT
        if(Hum <= 100){
          IoT.publish(PUBLISH_TOPIC, (String)Hum);
        }
      //When current soil humidity Hum>=300, send the current humidity to Easy IoT
        if(Hum >= 300){
          IoT.publish(PUBLISH_TOPIC, (String)Hum); 
        }
      } 
    }

Result

Upload the dectected soil moisture. The received message from Easy IoT is shown below:

4-1

Send command "1" from Easy IoT to water the plant(control the water pump by the relay module); command "2" for stopping watering.

Was this article helpful?

TOP