ESP8266 ESP-01 and IoT of LED — Part 4

Mudassar Tamboli
4 min readJul 2, 2018

--

Series : Part 1, Part 2 , Part 3, Part 4

Introduction

In the earlier parts i explained how ESP8266 can be used as an HTTP client and HTTP server to control and monitor the status of an LED. In the world of Internet of Things there are so many connected sensors, controllers, gadgets etc that are generating and reporting data. The Client/Server model is not the best method to handle the huge number of messages and classify, speedily distribute and act upon according to their name, level, type, quality, emergency, priority etc. Publisher/Subscriber is the best suited model to cater to the requirements of connected Internet of Things. We use MQTT to implement Pub/Sub method to control and monitor the ESP8266 GPIO2 LED

MQTT is a very light weight PubSub messaging protocol extensively used in low powered devices

ESP8266 as MQTT Pub/Sub

In the above picture ESP8266, Smart Phone and the Laptop PC are publisher/subscriber of topics related to the status and action on LED.

LED Action

  1. ESP8266 subscribes to topic “esp8266/gpio2/action” .
  2. Smart phone/ Laptop PC publishes to topic “esp8266/gpio2/action”

The above pub/sub method makes sense because the user will use smart phone/laptop to turn the GPIO2 Led on/off. The user will notify about the action by publishing the topic. In order ESP8266 receive the action command, it needs to susbcribe to that topic i.e “esp8266/gpio2/action”.

LED Status

  1. Smart phone/ Laptop PC subscribes to topic “esp8266/gpio2/status”
  2. ESP8266 publishes to topic “esp8266/gpio2/status” .

In this case the user will use smart phone/laptop to get the status of the GPIO2 Led. The ESP8266 will notify about the status by publishing the topic. In order user receive the status, it needs to susbcribe to that topic i.e “esp8266/gpio2/status”.

Arduino Pub/Sub Client Library

PubSubClient Library

There are many Pub/Sub client library for MQTT messaging. I used PubSubClient library for this demo. Go to Menu->Sketch->Include Library ->Manage Libraries… and install PubSubClient.

MQTT Broker Setup

CloudMQTT is an MQTT broker server. You can subscribe for a free plan and test your solutions. I experimented another mqtt broker iot.eclipse.org but struggled connecting to it at times. You can also install MQTT broker on a private cloud instance.

We will use these credetials to test ESP8266 mqtt client. By the time i publish this article, the credential would be reset :). So please change the username/password accordingly.

ESP8266 client connected to Cloud MQTT

Arduino Code

Code Details

  1. Include PubSubClient.h. Include PubSubClient library before building the firmware
  2. ESP8266 is set to Wifi Station mode.
  3. mqttClient connects to m14.cloudmqtt.com on port 14205. Change it as per your MQTT broker configuration
  4. PubSubCallback function is called on subscribed topics.
  5. ESP8266 subscribe to topic “esp8266/gpio2/action” in order to get commands from user to turn LED ON and OFF.
  6. The status of the LED is published to topic “esp8266/gpio2/status” which is subscribed by the client of ESP8266.
  7. If ESP8266 receives ON on topic “esp8266/gpio2/action” it turns LED ON
  8. If ESP8266 receives OFF on topic “esp8266/gpio2/action” it turns LED OFF

Pub/Sub message clients

Cloud MQTT Web client
MQTT.fx client and Arduino serial prints

Sources

Project: ESP8266_ESP-01-MQTT

Conclusion

The article demonstrated how to use ESP8266 as MQTT client. We used MQTT.fx as client to publish and subscribe messages, CloudMQTT is used as MQTT broker. ESP8266 subscribe to MQTT topic to turn LED ON and OFF.

Series : Part 1, Part 2 , Part 3, Part 4

--

--

Mudassar Tamboli

AWS Certified Solutions Architect, IoT enthusiast, Entrepreneurial Thinker, Polyglot programmer