Node-RED + MQTT + ESP8266: how to drive an articulated PTZ system in WiFi

We continue our series of articles on remote control of the actuator and more particularly the PTZ (Pan-Tilt) articulated systems. In the previous tutorials we saw how to drive a servo from a smartphone using the libraries for IoT Cayenne and Blynk. Today we will learn how to do it using Node-RED. To communicate with the ESP8266 in WiFi (or from the Internet), we will use the MQTT (Mosquitto broker) communication protocol.

How it works ?

Here is a diagram showing how the PTZ articulated system will be piloted. We will use the Node-RED Dashboard UI module to create 2 sliders (Pan, Tilt). At each change Node-RED publishes an MQTT message. For example, servo / pan, 92. The message is transmitted over the WiFi local area network to ESP8266. The PubSubClient library allows you to subscribe to MQTT (Broker) and receive messages. After decoding the message, the respective servomotor is moved to the required angular position.

What do you need ?

You will therefore need a computer with Node-RED installed. If you need to know how to do it, choose the tutorial that suits your environment:

You will also need a MQTT (broker) server. There are several. Here we will use Mosquitto.
Equipment used

esp8266 Wemos D1 mini Wemos D1 Mini
chargeur raspbery pi 3 5v 3000ma 5V/3A micro-usb power supply
mini kit fpv pan tilt sg90 Mini Kit FPV Pan Tilt with 2x SG90
jumper dupont Jumper Dupont
breadboard Breadboard


The circuit is identical to the previous tutorials.

node-red+mqtt+esp8266+ptz pan tilt kit

Arduino Code

To receive (or send) messages with the MQTT protocol, we will use the PubSubClient library (official documentation)

Installing libraries in the Arduino IDE

Before going any further, open the library manager and install the following libraries:


ide arduino install servo esp8266 library


 ide arduino install servo esp8266 library


 ide arduino install pubsubclient esp8266 library

How does the PubSubClient library work?

Here we will not go into the details of the connection to the WiFi network managed by the ESP8266WiFi library. We will only look at how to subscribe to several MQTT Topics and process incoming messages.

The PubSub library needs an object that contains the connection to the WiFi network. We start by creating an object using the WiFiClient class like this

Then, we create a client object

You will find 3 variables at the beginning of the program

  • const char * mqtt_server = “”: this is the IP address of your MQTT (Broker) server
  • const char * topic_pan = “servo/pan”: topic that contains the pan angle
  • const char * topic_tilt = “servo/tilt”: Topic that contains the angle of the servo Tilt

In the setup () function, the following operations are performed

  • Each servo is attached to the corresponding Pin. For example Pan on D5 and Tilt on D6

  • The setupWiFi()  function is used to connect the ESP8266 to the local WiFi network
  • We connect to the MQTT broker on port 1883 (the default port) client.setServer(mqtt_server, 1883)
  • Finally we define a callback function that will be called whenever an MQTT message is received client.setCallback (callback)

Receiving MQTT messages in the Arduino program

The callback function (documentation) allows you to retrieve incoming messages. It contains the topic, the message (payload) and the length of the message. Before you can process the message, you must convert it to string. There is no helper to decode messages that arrive in the form of a byte array. Fortunately, there are many examples on the internet. Here’s how to decode the incoming message.

The function servo.write(position) accepts as input in whole, one will have to convert the message (payload)

To determine whether to move the Pan or Tilt, we will use the string comparison function strcmp. It returns the number of different characters between the two strings. If there is no difference (return = 0), the two strings are identical. What will give us for example for the Topic Pan:

Complete Arduino Code

Here is the complete Arduino code. Create a new sketch and modify the parameters in the code before uploading:

  • SSID, Password
  • MQTT IP broker address
  • Servo spindle Pan and tilt

Flow Node-RED

For this flow Nod-RED, you will need to install several modules. Open the palette manager (full tutorial) and install the following modules

Import this Node-RED code directly onto a flow

node-red ptz servo control esp8266 wemos d1 mini flow

What does this flow do?

There are 2 sliders to control the position of the Pan servo and Tilt servo. A Reset button is used to reposition the 2 servos at 90 °. Finally, a repeat loop allows a scanning with a step of 10 ° in each direction (Pan and Tilt).

node-red ptz servo control esp8266 wemos d1 mini

Receiving MQTT messages

You can listen to messages that pass through the MQTT network using a client. For example, you can use MQTT.fx (available for Linux, MacOS and Windows, the download page). Start MQTT.fx and log in to the Broker. Subscribe to the topic servo / #. The # allows you to retrieve all messages from the tree. Here pan and tilt.

node-red mqtt mqttfx

Testing in operation

After uploading the program to ESP8266 and deploying flow Node-RED, open the dashboard from any web browser. You can also control the articulated system from the Internet browser of a tablet or a smartphone. Open the page at

Here is a short video demonstration to complete the article.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.