Getting Started with MQTT on Arduino Using ESP8266

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign to program an esp8266 microcontroller for reading and Publishing sensor data using mqtt to a high fmq Cloud mqtt broker further I'll show you how to receive mqtt messages as commands from the cloud to activate the microcontroller output for switching on and off a light emitting diode to begin let us briefly look at the hardware components we will use for this demo the note MCU microcontroller unit and the dht-11 temperature and humidity sensor the note MCU is a small fast and Wi-Fi capable microcontroller board that can be easily programmed in the popular Arduino integrated development environment the board is based on the esp8266 chip and it has many input output ports and various serial interfaces such as SPI I squared C or the ris232 the dst11 is a sensor capable of measuring the relative humidity and temperature of the surrounding environment it consists of three pins 2 4 power and one is a signal line for transmitting the sensor data to the microcontroller next let us look at how to wire up the hardware components for this demonstration as shown in this diagram the VCC of the dht11 goes to plus 3 volts of the note MCU the signal pin of the dht-11 goes into the digital Pim gpio2 of the note MCU and the ground pin of the dht-11 goes into the ground pin of the note MCU further the LED is connected to the node MCU digital Pim gpio5 through a resistor since I'll be publishing mqtt messages from the north MCU board to an mqtt broker I need first to create the mqtt broker on high fmq cloud and then get the broker details that I can use in my Arduino code I'll go to the High fmq website and select Cloud on the menu when I do that I'll get the high fmq Cloud page and I'll click on the try out for free button this provides a sign up page to create a free account that allows me to connect up to 100 devices if you are accessing the high fmq Cloud portal for the first time you'll need to provide an email address and password and follow the simple steps to confirm your email and create your account when your account is successfully created an mqtt broker cluster will also be automatically created for you you will need to begin by setting up credentials to allow devices to connect to your mqtt broker so I'll Define my username and password and then I'll click on ADD when I click on clusters I'll get a list of my mqtt broker clusters and since I just created this account I only have one so my mqtt broker setup is ready and now I just need to get the cluster URL port number and access credentials for use in my Arduino code and if at any point I wish to update my connection credentials I can always log into my portal click on manage cluster and select access management next I'll start building my software on the Arduino IDE to program my esp8266 node MCU board I need to use the Arduino IDE as previously mentioned you can download Arduino IDE by going to the software downloads page of their website I will download Arduino for Windows and then I'll install it after installing the Arduino IDE you can program various Arduino boards however to program a node MCU board instead of an Arduino board you will need to add a package to the IDE support management system to do this I'll go to the file menu and select preferences then under the additional board manager field I'll enter this URL and then I'll click ok next I'll go to the tools menu and select port by ports manager foreign and then click on the install button to install the esp8266 package now I'm ready to start programming my note MCU and other esp8266 modules using the Arduino IDE next I need to specify the esp8266 board I want you to program in the Arduino IDE so I'll need to connect my note MCU board to my PC using a micro USB cable and while I do that I need to have my Windows device manager open as soon as I plug in my board a new com Port appears on the device manager looking up Windows device manager like this is unnecessary but it can help you troubleshoot if you have problems with your USB drivers you can usually access the com Port by going to the port menu item under Tools in the Arduino IDE to select my board for programming I'll go to tools followed by board and then under esp8266 select this note MCU esp12 module before moving directly to coding I need to install the required libraries to read sensor data you must install the DHT sensor Library available from the Arduino Library manager so I'll navigate to sketch include Library manage libraries and then I'll search for DHT and then choose to install one from a number of TST libraries next to be able to publish mqtt messages to my hive mq Cloud mqtt broker I also need to add an mqtt client Library in this demo I'll use a library called Pub sub client again there are many to choose from I'll add this one from Nick eulerim now we can start developing our code let us go through the complete code for sending and receiving mqtt messages using node MCU on the Arduino platform first I import the required libraries into my Arduino code next I declare gpio2 as the pin on which My DST sensor is connected and I also declared gpio5 as the pin on which the LED is connected after that I declare variables for holding my wifi connection and Hive mq Cloud mqtt broker details then I make sure to initialize a secure Wi-Fi client connection create an mqtt client using Pub subclient library and attach it to the secure Wi-Fi connectivity and also here I declare variables for buffering mqtt messages and then after that I've got a variable for holding a root certificate that allows my client to connect securely to the high fmq Cloud mqtt broker then I declare a function that I'm going to use to connect to my Wi-Fi network after that I have a function for connecting to the high fmq Cloud mqtt broker next I have a callback method for receiving mqtt messages and switching the LED on and off after that I have a method for publishing mqtt messages to my high fmq Cloud mqtt broker and then I have the default Arduino setup method where I set up my GST 11 sensor and led and enable my certificate for secure connection and set up Wi-Fi and also create instances of my mqtt server and callback methods finally I have my main function for executing the application logic here I first connect my mqtt client to the high fmq Cloud mqtt broker if not already connected then I read temperature and humidity values from the dht11 sensor and serialize it including device ID and site ID properties into a Json object after serialization I published the Json string to my high fmq Cloud mqtt broker under the topic esp8266 underscore data and then I repeat the process every 5 Seconds next I'll deploy this code onto my microcontroller board now that my code is running and I'm publishing data to the ifmq cloud mqtt broker I can use an MTT client application such as mqtt.fx to subscribe to the broker and view the data when I open my mqtt.fx application which is already connected to the ifmq cloud mqtt broker and subscribe to the same topic I can see the data coming through this means I'm successfully publishing data from the note MCU to the high fmq clock MP3 broker using mqtt now I will publish an mqtta command to switch on the LED light from my mqtt.fx application by publishing the value of one under the topic LED underscore State when I do that you can see the LED light switch on I can also switch the LED light off by publishing a value of zero so I have successfully demonstrated how to send and receive mqtt messages from the Arduino platform to High fmq Cloud mqtt broker using node MCU based on the esp8266 chip thank you for watching and for more video like this please check out the ifmq YouTube channel foreign
Info
Channel: HiveMQ
Views: 16,179
Rating: undefined out of 5
Keywords: mqtt broker, cloud broker, Arduino, esp8266, industry 4.0, mqtt tutorial
Id: VdGUzGXamlQ
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Tue Jan 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.