In this lesson, we will use NodeMCU to make IoT watering/sprinkler system which can be controlled remotely through web browser or mobile App.   We will use MQTT protocol to send and get data from NodeMCU chip which is connected to solenoid. Browser or Mobile APP will send control signal to open/close solenoid of the watering system and monitor solenoid  on/off status through Internet.

Circuit Graph

Physical Connection:


**  garden hose and spray are not included in our Arduino Garden watering kit, you need buy from your local shop home renovation shops such as Home Depot etc.


Software Setup:

Step 1) Install Arduino IDE and ESP8266 package.
If you have installed these two software, please skip this step.
Follow the instruction in , you can install the Arduino IDE and ESP8266 package properly.

Step 2) Install MQTT PubSubClient library.
If your Arduino IDE has already installed this library, please skip this step.
Follow the instruction in and install this library.

Step 3) Download and install sample sketch

Connect the NodeMCU to computer via USB cable,download sketch code from ,  then copy and paste the code into Arduino IDE(Version1.6.4+):

Edit the code to fit your own WiFi and MQTT settings as following operations:

1)Change local Wifi Configuration code in sketch line 23 and 24: 

const char* ssid = "your_wifi_hotspot_name";

const char* password = "your_wifi_passcode";

update above code line with your own ssid and password .

2)Change clientID and mqtt topic setting in sketch line 27 and 31: 

String clientId = "your_unique_clientid";

char* mqtt_topic="your_unique_topic";

Please make mqtt_topic and clientid as unique as possible, otherwise other people might control your IOT device.

If your MQTT broker require username and password authentication,you need to change LINE 97

if (client.connect(clientId.c_str()))


if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here

If not,just keep them as default.

3)MQTT  Server  Address Setting in line 36

const char* mqtt_server = "";

You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as  "",  "" etc.

Step 4) Select correct board type and port type as following, then load the sketch to NodeMCU

Sketch Running Result

Once the upload is done,if wifi hotspot ssid and password setting is correct and internet is working properly, LED in D4 pin will turn on. Once the MQTT server is connected , LED in D5 pin will turn on.

If you open the Serial Monitor window in Arduino IDE, you will see following result:

If LED in D4 is off , it means wifi connection has problem. You need double check your wifi hotspot name and password in your code.
If LED in D5 is off, it means MQTT connection has problem. You need check if your MQTT server host setting in line 36 is correct.

Remote MQTT Client Settings

In order to control remote solenoid, we should define 3 types of mqtt message  to control valve action as following:

command message to publish Result
1 When NodeMCU receives message "1", it will turn ON Solenoid
0 When NodeMCU receives message "0",it will shut Off Solenoid
q When NodeMCU receives message "q",it will publish Solenoid status as following:

  • If Solenoid is ON, publish "Hose is watering" to MQTT broker
  • If Solenoid is OFF, publish "Hose is shut off" to MQTT broker

You can use any MQTT client software to send above MQTT message to your mqtt server, in this lesson, we use "iot mqtt dashboard" android app as MQTT client. This is a nice app which can predefine our mqtt message into Graphic widgets.

Step 1) Install iot mqtt dashboard app
You can search "iot mqtt dashboard app" from google play store as following:

or directly Download the app from  IOT MQTT Dashboard Android APP

Step 2) Add a mqtt broker by   click "+" button as following:

Step 3)config clientid, mqtt server and port as following:

Client Id should be anything which is not blank,
Server must be same as mqtt_server variable value in Arduino code line 36
port must be 1883
After completes the required fields, click "CREATE" button

Step 4)click PUBLISH  TAB, then click "+" button to define some graphic widgets which can help us to publish messages to mqtt server,

Step 5)add "button" widget to send message "q"

Step 6)define "button" widget as per following graph:

Friendly Name field  use "check valve status"
Topic field must be the same value as mqtt_topic variable in Arduino code line 31
Button Text field uses "Check value status"
Value to Publish field uses "q"
After above fields have been filled properly, click "CREATE" button, you will see a button named "Check value status" in the app screen.

Step 7)Add a switch widget by click "+" button in the upper right corner, then select "Switch" as following:

Step 8)Define "0"/"1" message in the Switch widget as following:

Friendly Name field use "valve status" .
Topic field must be the same value as mqtt_topic variable in Arduino code line 31
Text (on) field fill with "Valve On"
Text (off) field fill with "Valve Off"
Publish value (on) field fill with "1"
Publish value (off) field fill with "0"
After above fields have been filled properly, click "CREATE" button, you will see final dashboard screen as following:

Test the garden watering kit
Step 1)Connect the whole circuit and solenoid to Garden Host and Garden Water Tap as per following graph, then open the Garden Water Tap :

Step 2) Put the APP valve switch to valve on position as following:

the solenoid will open and water hose will begin watering.

Step 3) click CHECK  VALVE STATUS button in the APP, you will see "hose is watering" message in the APP screen as following:

Step 4) Put the APP valve switch to valve off position as following:

the solenoid will shut off and water hose will stop watering.

Step 5)click CHECK  VALVE STATUS button in the APP, you will see "hose is shut off" message in the APP screen as following: