Setting up a ESP32 for MQTT connections using Mongoose OS
Setting up a ESP32 for MQTT connections using Mongoose OS
calendar_today 05/03/2020 | today 27 | visibility 15923Article content
1. Introduction
1.1 The ESP32
ESP32 is a series of low-cost, low-power system on a chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth.
In the projects relative to the series IoT MQTT, we will be mostly using this microcontroller due the simplicity of the hardware needs (which is none for setting up) and the versatility of the programming language (Javascript or C/C++).
The ESP32 has Wi-Fi integrated in the board so it won't need for any external harware to connect to the internet so, for most cases, the ESP32 will be the top solution for IoT in this series of tutorials.
In the projects relative to the series IoT MQTT, we will be mostly using this microcontroller due the simplicity of the hardware needs (which is none for setting up) and the versatility of the programming language (Javascript or C/C++).
The ESP32 has Wi-Fi integrated in the board so it won't need for any external harware to connect to the internet so, for most cases, the ESP32 will be the top solution for IoT in this series of tutorials.
1.2 Mongoose OS
Mongoose OS is an Internet of Things (IoT) Firmware Development Framework available under Apache License Version 2.0. It supports low power, connected microcontrollers such as: ESP32, ESP8266, TI CC3200, TI CC3220, STM32 (STM32L4, STM32F4, STM32F7 series). Its purpose is to be a complete environment for prototyping, development and managing connected devices.
It is designed to reduce the time and costs associated with IoT projects.
It is designed to reduce the time and costs associated with IoT projects.
2. Pre set up
Before starting writting code we need a IDE and know how to connect, build, flash and run a code, and for that, Mongoose-os has a perfect start up guide, take a look!
Summary, you must download and install a software, the MOS tool, and also its drivers. Both downloads are in their guide, the divers to download for using the ESP32 are "Silabs drivers for Espressif boards". Next, input the "clone <source> <name>" command to start a new project, build and flash.
After building and flashing (step 6) as the guide shows, you can now make several off-line applications but for the sake of IoT you need one more thing:
Summary, you must download and install a software, the MOS tool, and also its drivers. Both downloads are in their guide, the divers to download for using the ESP32 are "Silabs drivers for Espressif boards". Next, input the "clone <source> <name>" command to start a new project, build and flash.
After building and flashing (step 6) as the guide shows, you can now make several off-line applications but for the sake of IoT you need one more thing:
Libraries. To add a library to your program there's a file called "mos.yml" located on
C:\mos\testProject\mos.yml
When the mos tool is located on C:/mos and the project directory is C:/mos/testProject.
Aditionally, you must add a inclusion to that library inside the program such as:
load('api_adc.js');
Aditionally, you must add a inclusion to that library inside the program such as:
load('api_adc.js');
See the example at the end of this article.
3. Javascript environment
We will be using Javascript as the main programming language because..
1. Market overtake. More and more microcontrollers capable of running high-level languages will only continue to enter the market.
2. Web conectivity for IoT. It is the third leg of what is considered the three essential languages of the world wide web, the other two being HTML and CSS.
3. Languaje popularity. Is no new news that Javascript is everywhere and is one of the most known languages out there.
4. Performance. Low power consumption.
And here's a example code of a LED toggle written on Javascript for a ESP32 using mos
It's simple, isn't it?
Now let's move on to connectivity for IoT
1. Market overtake. More and more microcontrollers capable of running high-level languages will only continue to enter the market.
2. Web conectivity for IoT. It is the third leg of what is considered the three essential languages of the world wide web, the other two being HTML and CSS.
3. Languaje popularity. Is no new news that Javascript is everywhere and is one of the most known languages out there.
4. Performance. Low power consumption.
And here's a example code of a LED toggle written on Javascript for a ESP32 using mos
It's simple, isn't it?
Now let's move on to connectivity for IoT
4. Connecting to a MQTT broker
Open the mos tool (remember installing it in C:/mos/mos.exe for Windows).
Assuming you have already followed the guide in HERE up to step 7, you are now connected to internet but not connected to a MQTT broker, now input:
And that's it! you're now connected to test.mosquitto, a open MQTT broker for testing.
Optionally, try broker.hivemq.com:1883 if test.mosquitto is not online
To actually do something connected over MQTT see the example at the end of this article.
Assuming you have already followed the guide in HERE up to step 7, you are now connected to internet but not connected to a MQTT broker, now input:
And that's it! you're now connected to test.mosquitto, a open MQTT broker for testing.
Optionally, try broker.hivemq.com:1883 if test.mosquitto is not online
To actually do something connected over MQTT see the example at the end of this article.
5. Practical example
1. Pre set up
After downloading mos tool and the corresponding drivers as steps 1-3 HERE, open up the mos tool located in C:\mos\mos.exe and create a new project with
Connect the ESP32 to your computer and select ESP32 on the board selection dropdown menu.
IMPORTANT: Add these libraries to the mos.yml file since they all may not be by default
libs:
- origin: https://github.com/mongoose-os-libs/boards
- origin: https://github.com/mongoose-os-libs/js-demo-bundle
- origin: https://github.com/mongoose-os-libs/mqtt
- origin: https://github.com/mongoose-os-libs/wifi
- origin: https://github.com/mongoose-os-libs/rpc-common
IMPORTANT: Add these libraries to the mos.yml file since they all may not be by default
libs:
- origin: https://github.com/mongoose-os-libs/boards
- origin: https://github.com/mongoose-os-libs/js-demo-bundle
- origin: https://github.com/mongoose-os-libs/mqtt
- origin: https://github.com/mongoose-os-libs/wifi
- origin: https://github.com/mongoose-os-libs/rpc-common
2. Build and flash
Paste this code inside C:\mos\testProject\fs\init.js
Build the program to be loaded to the ESP32 (internet connection needed), most errors are from wrong board selection or syntax errors.
input and wait to finish:
input and wait to finish:
input and wait to finish:
mos build
Flash the ESP32 with the built programinput and wait to finish:
mos flash
3. Connect to test.mosquitto.org
Connect to internet, most errors are solved by fixing your internet connections
input and wait to finish
mos wifi WIFIname WIFIpassword
Connect to the broker, most errors are from the broker (test.mosquitto.org:1883 is not always online and not all of the topics are available to test on)
input and wait to finish
mos config-set mqtt.enable-true mqtt.server=test.mosquitto.org:1883
(Optional) Connect to another broker if the connection fails mos config-set mqtt.enable-true mqtt.server=broker.hivemq.com:1883
4. Turn on and off a LED from anywhere in the world
For testing, you can use this web client and follow the instructions below:
HiveMQ - public client
For the example in this article:
1. Broker must be:
2. For web clients such as HiveMQ - public client use:
Port (mosquitto broker):
Port (mosquitto broker):
3. Subscribe topic must be:
4. Publish topic must be:
5. Turn On and Off the LED:
Message to turn on the LED is:
Message to turn off the LED is:
6. Congratulations! you just made a IoT LED.
you can try another alternative such as: MQTT Client For Android or make a client yourself on a mobile app, or a webpage, etc..
7. Follow up the tutorials from the IoT MQTT series to see what more can you do with this new world of wireless connections.
HiveMQ - public client
For the example in this article:
1. Broker must be:
test.mosquitto.org
Optional broker: broker.hivemq.com
2. For web clients such as HiveMQ - public client use:
Port (mosquitto broker):
8080
Optional port (hiveMQ broker): 8000
For non web clients shuch as MQTT Client For Android use:Port (mosquitto broker):
1883
Optional port (hiveMQ broker): 1883
3. Subscribe topic must be:
testTopic123
4. Publish topic must be:
testTopic123
5. Turn On and Off the LED:
Message to turn on the LED is:
On
Message to turn off the LED is:
Off
6. Congratulations! you just made a IoT LED.
you can try another alternative such as: MQTT Client For Android or make a client yourself on a mobile app, or a webpage, etc..
7. Follow up the tutorials from the IoT MQTT series to see what more can you do with this new world of wireless connections.
5. Haven't tried yet? here is the result
6. References
MQTT - Wikipedia
JSDoc: Paho MQTT
Mongoose OS Documentation - Guide
Mongoose OS Documentation - MQTT
ESP32 Pinout Reference: GPIO
JSDoc: Paho MQTT
Mongoose OS Documentation - Guide
Mongoose OS Documentation - MQTT
ESP32 Pinout Reference: GPIO