Use NodeMCU to control servo motor through MQTT IOT protocol


Use NodeMCU to control servo motor through MQTT IOT protocol

About NodeMCU

NodeMCU is a very popular Micro controller with wifi access. It is based on ESP8266 – a cheap but powerful chip and fully support Arduino IDE. If you familiar with Arduino IDE, you can program NodeMCU in no time.


In this project, we will connect a servo to a NodeMCU chip, then use a MQTT client(mobile app, browser, linux terminal program etc) to send rolling degree message to NodeMCU to control the motor moving. If the message value is between 0 to 180 degree, servo will roll as per the degree message, other it will not rexponse and move.

Parts and Devices:

NodeMCU board x 1 pc

Servo x 1 pc

Breadboard x 1 pc

5V power supply x1 pc

jumper wires

Circuit Connection Graph

In this project, we use external 6V battery box to supply energy to servo. Please connect the ground(negative) pin of the battery box to the GND pin NodeMCU.

Programming Prerequisite:

Before running the project, we need set up the Arduino IDE as per following procedures:

Library Installation:

In Arduino IDE, we need install MQTT client library to communicate with MQTT broker, please download the library from following link:

Unzip above file, move the unzipped folder to Arduino IDE library folder (you can also import these two zip files to Arduino library from IDE).

Download sample code from following link , copy the code into Arduino IDE. Before running the code, please do following changes to fit your wifi and MQTT setting:

1)Line 19 and 20:

const char* ssid = “your_hotspot_ssid”;

const char* password = “your_hotspot_password”;

You need change these 2 lines to match your wifi SSID and password

2)Line 21

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

If you want to install your own MQTT broker in Ubuntu Linux, please read this article

3)if your MQTT server require username and password authentication, you need change line 86

Running the code

After you running the code, please open the serial terminal window in upright corner of Arduino IDE.

If wifi hotspot name and password setting is ok and MQTT broker is connected, you will see following result:


After the NodeMCU connected to wifi and MQTT broker, it will subscribe to a MQTT broker with topic “OsoyooCommand” and waiting for motor control message. Let us publish a message from a MQTT client and test if NodeMCU can get this message.

You can select any MQTT client tool to send control message to MQTT broker(in this project, message must be under topic of “OsyooCommand”). simply use google to search MQTT client in internet, or search MQTT in Apple store or Google Play, you can find many free MQTT client software. In my case, I uses MQTTBox for Windows, you can learn MQTTBox configuration in following article:


After click Save button, enter into Topic to publish screen,do configuration as following picture:

After you PUBLISH an integer degree value between 0 to 180 from payload text field, the servo will rotate as per the degree value.

About the Author

amber administrator

You must be logged in to post a comment