esp-idf-mqtt-broker
MQTT Broker for esp-idf.
This project use Mongoose networking library.
I forked from here.
However, with the release of mongoose ver7, I rewrote it.
Your fork is welcome.
Installation overview
-
In this project directory, create a components directory.
-
In the components directory, clone Mongoose:
git clone https://github.com/cesanta/mongoose.git
- In the new Mongoose directory, create a CMakeLists.txt file containing:
idf_component_register(SRCS "mongoose.c" INCLUDE_DIRS ".")
- Compile this project.
Installation for ESP32
git clone https://github.com/nopnop2002/esp-idf-mqtt-broker
cd esp-idf-mqtt-broker
mkdir -p components
cd components/
git clone https://github.com/cesanta/mongoose.git
cd mongoose/
echo "idf_component_register(SRCS \"mongoose.c\" INCLUDE_DIRS \".\")" > CMakeLists.txt
cd ../..
idf.py set-target esp32
idf.py menuconfig
idf.py flash monitor
Installation for ESP32-S2
git clone https://github.com/nopnop2002/esp-idf-mqtt-broker
cd esp-idf-mqtt-broker
mkdir -p components
cd components/
git clone https://github.com/cesanta/mongoose.git
cd mongoose/
echo "idf_component_register(SRCS \"mongoose.c\" INCLUDE_DIRS \".\")" > CMakeLists.txt
cd ../..
idf.py set-target esp32s2
idf.py menuconfig
idf.py flash monitor
Software requiment
- mongoose ver7.3 or later.
This is because mg_mqtt_next_unsub was officially added in mongoose ver 7.3.
Application Setting
Station Mode
SSID:SSID of your Wifi router
ESP32 get IP using DHCP.
Station Mode of Static Address
SSID:SSID of your Wifi router
ESP32 set your specific IP.
Access Point Mode
SSID:SSID of ESP32
ESP32 have 192.168.4.1.
Using MDNS hostname
You can use the MDNS hostname instead of the IP address.
- esp-idf V4.3 or earlier
You will need to manually change the mDNS strict mode according to this instruction. - esp-idf V4.4 or later
If you set CONFIG_MDNS_STRICT_MODE = y in sdkconfig.default, the firmware will be built with MDNS_STRICT_MODE = 1.
You can change MDNS hostname using menuconfig.
Start Built-In MQTT Subscriber
Start Built-In MQTT Publisher
Limitations
-
will topics
will qos and will retain are ignored. -
Unsupported MQTT request
Do not respond to these MQTT requests:
PUBREC
PUBREL
PUBCOMP
Subscribe using mosquitto-clients
$ sudo apt install mosquitto-clients moreutils
$ chmod 777 ./mqtt_sub.sh
$ ./mqtt_sub.sh
21/03/06 18:14:19 esp32 TickCount=110964
21/03/06 18:14:20 esp32 TickCount=111065
21/03/06 18:14:21 esp32 TickCount=111166
21/03/06 18:14:22 esp32 TickCount=111267
21/03/06 18:14:23 esp32 TickCount=111368
Publish using mosquitto-clients
$ sudo apt install mosquitto-clients moreutils
$ chmod 777 ./mqtt_sub.sh
$ ./mqtt_pub.sh
Notify topic of will using mosquitto-clients
- In Terminal #1, do the following:
$ mosquitto_sub -v -h esp32-broker.local -p 1883 -t "topic/#" --will-topic "topic/will" --will-payload "GOODBYE"
- Open new terminal. In Terminal #2, do the following:
$ mosquitto_sub -v -h esp32-broker.local -p 1883 -t "topic/#"
-
Press Control+C in Terminal #1:
-
The following is displayed in Terminal #2:
topic/will GOODBYE
Unsubscribe using python and paho
$ pip install paho
$ python unsub_example.py
Connected with result code 0
subscribe hoge
Received message 'b'test'' on topic 'hoge/1' with QoS 1
unsubscribe hoge, subscribe fuga
Received message 'b'test'' on topic 'fuga/1' with QoS 1
unsubscribe fuga, subscribe hoge
First, it subscribes to the topic [hoge/#].
When it receives the topic [hoge/1], it unsubscribes [hoge/#] and subscribes to [fuga/#].
When it receives the topic [fuga/1], it unsubscribes [fuga/#] and subscribes to [hoge/#] again.