NodeMCU MQTT Client With Arduino IDE - NodeMCU
NodeMCU MQTT Client With Arduino IDE - NodeMCU
NodeMCU MQTT Client With Arduino IDE - NodeMCU
Project Sign In
(/login#login)
(/) Platforms (/explore) Projects
(/projects) Contests (/contests)
Docs (/nodemcu/basics) Codes (/nodemcu/codes)
Platforms (/explore) Basics using Arduino IDE (/nodemcu/arduino-ide) NodeMCU MQTT Client with Arduino IDE
(/nodemcu/nodemcu-mqtt-client-with-arduino-ide)
For example, if the temperature sensor publishes the temperature data (message) on the topic “temperature” then interested
clients who have subscribed to the “temperature” topic get that published temperature data as shown in the above figure.
MQTT is widely used in IoT (Internet of Things) embedded applications, where every sensor is connected to a server and we
have access to control them over the internet.
NodeMCU is an open-source IoT platform. It is a firmware which runs on ESP8266 Wi-Fi SoC from Espressif Systems. It has
onboard wi-fi available through which IoT applications become easy to build.
The MQTT Client module of NodeMCU is according to version 3.1.1 of the MQTT protocol. Make sure that your broker supports
and is correctly configured for version 3.1.1. let’s see the functions used for MQTT on NodeMCU.
Here we are using the Adafruit server for MQTT Client demo purpose.
In the IOT platform, Adafruit IO Dashboard allows us to visualize and provides control over the connected devices to the
internet. Anyone can visualize and analyze live data from their sensor devices. To learn more and start with Adafruit IO
Dashboard refer link https://learn.adafruit.com/adafruit-io-basics-dashboards/creating-a-dashboard
(https://learn.adafruit.com/adafruit-io-basics-dashboards/creating-a-dashboard)
Just sign up and create a dashboard. After the successful creating of the dashboard, we will get the AIO key which is later
used to access feed data.
Once we created a dashboard on Adafruit we can add various blocks that can be used to control devices as well as monitor
the status of devices. To see more about blocks, refer link https://learn.adafruit.com/adafruit-io-basics-dashboards/adding-
blocks (https://learn.adafruit.com/adafruit-io-basics-dashboards/adding-blocks)
Here we are using Adafruit libraries for the above example. We will need to install the Adafruit IO, Adafruit MQTT, and
ArduinoHttpClient libraries using the Arduino Library Manager.
Open the Arduino IDE and navigate to Sketch -> Include Library -> Manage Libraries…
Add Project Sign In
(/login#login)
(/) Platforms (/explore) Projects
(/projects) Contests (/contests)
The library Manager window will pop up. Now enter Adafruit IO Arduino into the search box, and click Install on the Adafruit IO
Arduino library option to install version 2.6.0 or higher.
Now enter Adafruit MQTT into the search box, and click Install on the Adafruit MQTT library option to install version 0.17.0 or
higher.
Add Project Sign In
(/login#login)
(/) Platforms (/explore) Projects
(/projects) Contests (/contests)
Now enter Arduino Http Client into the search box, and click Install on the ArduinoHttpClient library option to install version
0.3.0 or higher.
Now open example of Adafruit mqtt io dashboard. To open it navigate to File -> Examples -> Adafruit MQTT Library ->
mqtt_esp8266
Add Project Sign In
(/login#login)
(/) Platforms (/explore) Projects
(/projects) Contests (/contests)
Now edit the wifi and Adafruit io credentials with correct information of example as shown in below image.
Add Project Sign In
(/login#login)
(/) Platforms (/explore) Projects
(/projects) Contests (/contests)
We have modified the mqtt_esp8266 example as per our above example as below
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
WiFiClient client;
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
void MQTT_connect();
uint16_t potAdcValue = 0;
uint16_t ledBrightValue = 0;
Add Project Sign In
void setup() {
(/login#login)
(/) Platforms
Serial.begin(9600);
(/explore) Projects
delay(10);
(/projects) Contests (/contests)
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(500);
Serial.print(".");
Serial.println();
Serial.println("WiFi connected");
mqtt.subscribe(&ledBrightness);
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
if (subscription == &ledBrightness) {
Serial.println(ledBrightValue);
analogWrite(ledPin, ledBrightValue);
potAdcValue = AdcValue;
Serial.print(potAdcValue);
Serial.print("...");
if (! potValue.publish(potAdcValue)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
/*
if(! mqtt.ping()) {
mqtt.disconnect();
Add Project Sign In
}
(/login#login)
(/)
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
uint8_t retries = 3;
Serial.println(mqtt.connectErrorString(ret));
mqtt.disconnect();
retries--;
if (retries == 0) {
while (1);
Serial.println("MQTT Connected!");
Video
Add Project Sign In
(https://www.mouser.i
Po (/login#login)
Platforms (/explore) Projects n?
(/) w
(/projects) Contests (/contests) utm_source=electronicswings&utm_
er
medium=display&utm_campaign=mo
ed
user-
B
componentslisting&utm_content=0x
Components Used y
0)
NodeMCU
(https://www.mouser.in/ProductDet
NodeMCUNodeMCU ail/Seeed-Studio/113990105?
qs=sGAEpiMZZMu3sxpa5v1qriyKvi
6JV%2FB8UHakxfOLUh4%3D&utm_
X 1 source=electronicswings&utm_medi
um=display&utm_campaign=mouse
r-
componentslisting&utm_content=0x
0)
ESP12F
Datasheet | (https://www.mouser
ESP12E .com/ProductDetail/
Adafruit/2491?
qs=N%2F3wi2MvZW
DVFv7HwruvGQ%3D
%3D&utm_source=el
X 1
ectronicswings&utm_
medium=display&ut
m_campaign=mouse
r-
componentslisting&u
tm_content=0x0)
Supporting Files
Source Code
NodeMCU MQTT Client Download 1777
Comments
embedprochd
(/users/embedprochd/profile)
2019-11-03 19:59:12
my led is not dimming but the pot value is shown on cell phone and comport. But by sliding bar on phone led does not glow .
Reply Like
muhammedimdaad16
(/users/muhammedimdaad16/profile)
2020-07-08 21:11:11
embedprochd
(/users/embedprochd/profile)
2019-11-03 19:59:34
Add Project Sign In
About Us (/about) Connect On:
(/login#login)
(/) Platforms
Business (/explore)
Offering Projects
(/business-services) Facebook
Host Platform (/launch-platform)
(/projects) Contests (/contests) (https://www.facebook.com/electronicwings)
Contact Us (/contactus)
LinkedIn
(https://www.linkedin.com/company/electronicwings/)
Youtube
(https://www.youtube.com/channel/UCNdqkukBtk4WhaBqc
Instagram
(https://www.instagram.com/electronicwings_com?
igshid=1cip10jijttko)